Description
Given a binary number, we are about to do some operations on the number. Two types of operations can be here.
'I i j' which means invert the bit from i to j (inclusive)
'Q i' answer whether the ith bit is 0 or 1
The MSB (most significant bit) is the first bit (i.e. i=1). The binary number can contain leading zeroes.
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case starts with a line containing a binary integer having length n(1 ≤ n ≤ 105). The next line will contain an integer q (1 ≤ q ≤ 50000)denoting the number of queries. Each query will be either in the form 'I i j' where i, j are integers and 1 ≤ i ≤ j ≤ n. Or the query will be in the form'Q i' where i is an integer and 1 ≤ i ≤ n.
Output
For each case, print the case number in a single line. Then for each query 'Q i' you have to print 1 or 0 depending on the ith bit.
Sample Input
2
0011001100
6
I 1 10
I 2 7
Q 2
Q 1
Q 7
Q 5
1011110111
6
I 1 10
I 2 7
Q 2
Q 1
Q 7
Q 5
Sample Output
Case 1:
0
1
1
0
Case 2:
0
0
0
1
#include<stdio.h>
#include<string.h>
#define N 100010
#define rever(a) (a+1)%2
struct node{
int l,r,rev;
};
node tree[N*4];
char a[N];
int n,ans;
void Build(int id,int l,int r)
{
tree[id].l=l;tree[id].r=r;
if(r==l)
return ;
Build(id*2,l,(l+r)/2);
Build(id*2+1,(l+r)/2+1,r);
}
void Update(int id,int l,int r)
{
if(tree[id].l==l && tree[id].r==r)
{
tree[id].rev = rever(tree[id].rev);
return ;
}
if(r<=(tree[id].l+tree[id].r)/2)
Update(id*2,l,r);
else if(l>=(tree[id].l+tree[id].r)/2+1)
Update(id*2+1,l,r);
else
{
Update(id*2,l,(tree[id].l+tree[id].r)/2);
Update(id*2+1,(tree[id].l+tree[id].r)/2+1,r);
}
}
void Query(int id,int v)
{
if(tree[id].l==v && tree[id].r==v)
{
ans=tree[id].rev;
return ;
}
tree[id*2].rev=(tree[id].rev+tree[id*2].rev)%2;
tree[id*2+1].rev=(tree[id].rev+tree[id*2+1].rev)%2;
if(tree[id].rev)
tree[id].rev=0;
if(v<=(tree[id].l+tree[id].r)/2)
Query(id*2,v);
else
Query(id*2+1,v);
}
int main()
{
int T,i,q;
int l,r;
char s[5];
int cas=1;
scanf("%d",&T);
while(T--)
{
getchar();
gets(a+1);
n=strlen(a+1);
memset(tree,0,sizeof(tree));
Build(1,1,n);
scanf("%d",&q);
printf("Case %d:\n",cas++);
for(i=1;i<=q;i++)
{
scanf("%s",s);
if(s[0]=='I')
{
scanf("%d%d",&l,&r);
Update(1,l,r);
}
else
{
ans=0;
scanf("%d",&l);
Query(1,l);
if(ans)
{
if(a[l]=='0')
printf("1\n");
else
printf("0\n");
}
else
printf("%c\n",a[l]);
}
}
}
return 0;
}