Monday, 10 August 2015

Queries on the String 
Solved

Problem code: QSET

All submissions for this problem are available.

Read problems statements in Mandarin Chinese and Russian.

You have a string of N decimal digits, denoted by numbers A1,A2, ..., AN.
Now you are given M queries, each of whom is of following two types.
  • Type 1: 1 X Y: Replace AX by Y.
  • Type 2: 2 C D: Print the number of sub-strings divisible by 3 of the string denoted by AC,AC+1 ...
    AD
    .


    Formally, you have to print the number of pairs (i,j) such that the string Ai,Ai+1...Aj,
    (C ≤ i ≤ j ≤ D), when considered as a decimal number, is divisible by 3.

Input

  • There is only one test case.
  • First line of input contains two space separated integers N, M.
  • Next line contains a string of N digits, denoting string A.
  • For next M lines, each line contains a query.
  • Each query is given by three space separated integers.
  • The first integer denotes type of the query. For each query of type 1, next two integers denote Xi, Yi.
    For each query of type 2, next two integers denote Ci, Di.

Output

For each query of type 2, print the required answer in a single line.

Constraints

  • 0 ≤ Ai ≤ 9
  • 1 ≤ Xi ≤ N
  • 0 ≤ Yi ≤ 9
  • 1 ≤ Ci ≤ Di ≤ N

Subtasks

  • Subtask #1 (10 points): 1 ≤ N, M ≤ 103 and only type 2 queries.
  • Subtask #2 (15 points): 1 ≤ N, M ≤ 103
  • Subtask #3 (20 points): 1 ≤ N, M ≤ 105 and only type 2 queries
  • Subtask #4 (55 points): 1 ≤ N, M ≤ 105

Example

Input:
5 3
01245
2 1 3
1 4 5
2 3 5

Output:
3
1

Explanation

For first query, the sub-strings S[1,1]="0", S[1,3]="012" and S[2,3]="12" are divisible by 3.
After second query, the string A becomes "01255".
For third query, only sub-string S[3,5]="255" is divisible by 3.


A very firm understanding of this question is the key to solve it .
For each node we require 4 things.    the number of substrings divisible by 3 on the segment , the total sum of the segment and the prefix and the suffix array .
where prefix array stands for the number of elements starting with the prefix and whos sum%3 gives 0,1 ,2. It is the same for prefix array. Now while ,merging two nodes , we first need to create the prefix and suffix arrays for the node

No comments:

Post a Comment