The fight for the best number in the globe is going to finally come to an end.The top two contenders for the best number are number 2 and number 3.It's the final the entire world was waiting for. Expectorates from all across the globe came to witness the breath taking finals.
The finals began in an astonishing way.A common problem was set for both of them which included both these number.The problem goes like this.
Given a binary string (that is a string consisting of only 0 and 1). They were supposed to perform two types of query on the string.
Type 0: Given two indices l and r.Print the value of the binary string from l to r modulo 3.
Type 1: Given an index l flip the value of that index if and only if the value at that index is 0.
The problem proved to be a really tough one for both of them.Hours passed by but neither of them could solve the problem.So both of them wants you to solve this problem and then you get the right to choose the best number in the globe.
Input:
The first line contains N denoting the length of the binary string .
The second line contains the N length binary string.Third line contains the integer Q indicating the number of queries to perform.
This is followed up by Q lines where each line contains a query.
Output:
For each query of Type 0 print the value modulo 3.
Constraints:
1<= N <=10^5
1<= Q <= 10^5
0 <= l <= r < N
Sample Input
5
10010
6
0 2 4
0 2 3
1 1
0 0 4
1 1
0 0 3
Sample Output
2
1
2
1
public class Main {
static int[] power = new int[100006], arr = new int[100006], tree = new int[300000];
public static void calculatePowers() {
power[0] = 1;
for (int i = 1; i < 100006; i++)
power[i] = (power[i - 1] * 2) % 3;
}
public static void build(int index, int start, int end) {
if (start == end)
tree[index] = arr[start];
else {
int mid = start + (end - start) / 2;
build(2 * index, start, mid);
build(2 * index + 1, mid + 1, end);
tree[index] = (tree[2 * index] * power[end - mid] + tree[2 * index + 1]) % 3;
}
}
public static int query(int index, int start, int end, int L, int R) {
if (R < start || L > end)
return 0;
if (L <= start && end <= R)
return (tree[index] * power[R - end]) % 3;
int mid = (start + end) / 2;
int p1 = query(2 * index, start, mid, L, R);
int p2 = query(2 * index + 1, mid + 1, end, L, R);
return (p1 + p2) % 3;
}
public static void update(int index, int start, int end, int idx) {
if (start == end) {
tree[index] = 1;
arr[idx] = 1;
} else {
int mid = (start + end) / 2;
if (start <= idx && mid >= idx)
update(2 * index, start, mid, idx);
else
update(2 * index + 1, mid + 1, end, idx);
tree[index] = (tree[2 * index] * power[end - mid] % 3) + tree[2 * index + 1] % 3;
}
}
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
char[] str = in.nextLine().toCharArray();
calculatePowers();
for (int i = 0; i < n; i++)
arr[i + 1] = str[i] - '0';
build(1, 1, n);
int q = in.nextInt();
while (q-- > 0) {
int t = in.nextInt();
if (t == 0) {
int l = in.nextInt(), r = in.nextInt();
System.out.println(query(1, 1, n, l + 1, r + 1));
} else {
int x = in.nextInt();
if (str[x] == '0') {
str[x] = '1';
arr[x + 1] = 1;
update(1, 1, n, x + 1);
}
}
}
}
}