Binary Simulation 解题报告--线段树

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值