light oj 1080 区间更新

来源:互联网 发布:淘宝信用还款方式 编辑:程序博客网 时间:2024/05/29 00:33

Binary Simulation
Time Limit: 2000MSMemory Limit: 65536KB64bit IO Format: %lld & %llu

Submit Status uDebug

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

Hint

Dataset is huge, use faster i/o methods.


Problem Setter: Jane Alam Jan
Developed and Maintained by 
JANE ALAM JAN
Copyright © 2012 
LightOJ, Jane Alam Jan

#include <iostream>

#include <cstdio>
#include <cstring>
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
const int N = 100010;
char str[N];
int flag[N<<2], lazy[N<<2];
void update(int L,int R,int l,int r,int rt);
void pushup(int rt);
int query(int p,int l,int r,int rt);


int main()
{
    int t, ncase=1;
    scanf("%d", &t);
    while(t--)
    {
        memset(flag,0,sizeof(flag));
        memset(lazy,0,sizeof(lazy));
        scanf("%s",str);
        int n=strlen(str);
        int q, x, y;
        char  ss[10];
        scanf("%d", &q);
        printf("Case %d:\n",ncase++);
        while(q--)
        {
            scanf("%s",ss);
            if(ss[0]=='I')
            {
                scanf("%d %d", &x, &y);
                update(x,y,1,n,1);
            }
            else
            {
                scanf("%d", &x);
                int num=query(x,1,n,1);
                if(num&1)
                {
                    printf("%c\n",str[x-1]=='0'?'1':'0');
                }
                else
                {
                    printf("%c\n",str[x-1]=='0'?'0':'1');
                }
            }
        }
    }
    return 0;
}


void update(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        flag[rt]++;
        lazy[rt]++;
        return ;
    }
    pushup(rt);
    int mid=(l+r)/2;
    if(L<=mid)
    {
        update(L,R,lson);
    }
    if(R>mid)
    {
        update(L,R,rson);
    }
    return ;
}


void pushup(int rt)
{
    if(lazy[rt])
    {
        flag[rt<<1]+=lazy[rt];
        flag[rt<<1|1]+=lazy[rt];
        lazy[rt<<1]+=lazy[rt];
        lazy[rt<<1|1]+=lazy[rt];
        lazy[rt]=0;
    }
    return ;
}




int query(int p,int l,int r,int rt)
{
    if(l==r)
    {
        return flag[rt];
    }
    pushup(rt);
    int mid=(l+r)/2;
    if(p<=mid)
    {
        return query(p,lson);
    }
    else
    {
        return query(p,rson);
    }
}
0 0
原创粉丝点击