Range MOD Query

3.8

4 votes
Advanced Data Structures, Segment Trees, Modular arithmetic, Data Structures, Modular exponentiation, Math
Problem

You are given a binary string, s, and you have to answer q queries in order. 

A binary string is a string consisting of only 0's and 1's

There are two types of queries: 

  • 1i - Flip the character at position i i.e 0 becomes 1, and 1 becomes 0. Each modification affects the subsequent queries i.e the queries are dependent. 
  • 2lr - Print the decimal value of the binary substring formed by indices in some range [l,r]. As the value could be very large, compute it modulo 109+7.

The decimal value of some range [l,r] in s is Sl2rl+Sl+12rl1+...+Si2ri+...+Sr20, where Si represents the integer value of the character at the ith position in s

Input format

  • The first line of the input contains two integers, n and q - denoting the number of characters in the string s and the number of queries. 
  • The second line of the input contains the string s - the string contains only 0's or 1's
  • The next q lines of the input contain two types of queries: 
    • 1i - denoting a type 1 query and the index to be flipped.
    • 2lr - denoting a type 2 query and the indices to find the decimal value modulo 109+7.

Output format

For each of the type 2 queries, output an integer denoting the decimal value of the binary representation of the queried range modulo 109+7.

Constraints

1n,q1051in1lrn

 

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

In the sample test case, we have 3 queries for s=10101

In the first query, we get the substring from position 1 to 5 in s, which is 10101 and it has a decimal value of 21.

21=24+22+20.

In the second query, we flip the bit at the second index, so s becomes 11101

In the last query, we get the substring from position 1 to 3, which is now 111, and it has a decimal value of 7

7=22+21+20.

Editor Image

?