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 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.
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.
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
Case 1:
0
1
1
0
Case 2:
0
0
0
1
大意:给出一行字符串,只包含 0 和 1,给出操作选项;I: 为把一个区间内字符反向 0 变为 1,1 变为 0;Q:查询第 i 个字符。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 100005
using namespace std;
int sum[N<<2],lazy[N<<2];//sum数组维护翻转次数
void pushdown(int rf,int m)
{
lazy[rf*2]+=lazy[rf];
lazy[rf*2+1]+=lazy[rf];
sum[rf*2]+=lazy[rf];
sum[rf*2+1]+=lazy[rf];
lazy[rf]=0;
}
void update(int rf,int l,int r,int a,int b)
{
if(a<=l&&b>=r)
{
sum[rf]++;
lazy[rf]++;
return;
}
if(lazy[rf]!=0)
pushdown(rf,r-l+1);
int mid=(l+r)>>1;
if(a<=mid)
update(rf*2,l,mid,a,b);
if(b>mid)
update(rf*2+1,mid+1,r,a,b);
}
int query(int rf,int l,int r,int a)
{
if(l==r)
return sum[rf];
if(lazy[rf]!=0)
pushdown(rf,r-l+1);
int mid=(l+r)>>1;
if(a<=mid)
return query(rf*2,l,mid,a);
else
return query(rf*2+1,mid+1,r,a);
}
int main()
{
int n,t,cat=0;
char s[N];
cin>>t;
while(t--)
{
memset(sum,0,sizeof(sum));
memset(lazy,0,sizeof(lazy));
scanf("%s",s);
n=strlen(s);
int p,a,b;
char c;
scanf("%d",&p);
printf("Case %d:\n",++cat);
while(p--)
{
scanf(" %c",&c);
if(c=='I')
{
scanf("%d%d",&a,&b);
update(1,1,n,a,b);
}
else
{
scanf("%d",&a);
if(query(1,1,n,a)&1==1)//翻转次数为奇数,逆原来
{
if(s[a-1]=='1')
printf("0\n");
else
printf("1\n");
}
else
printf("%c\n",s[a-1]);
}
}
}
return 0;
}