Lightoj1080硬币翻转次数(区段更新)

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

大意:给出一行字符串,只包含 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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值