lightoj 1080 - Binary Simulation(区间更新,单点查询)

来源:互联网 发布:列式数据库 olap 编辑:程序博客网 时间:2024/06/05 19:03


1080 - Binary Simulation
   PDF (English)StatisticsForum
Time Limit: 2 second(s)Memory Limit: 64 MB

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

Output for 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

Case 1:

0

1

1

0

Case 2:

0

0

0

1

Note

Dataset is huge, use faster i/o methods.


题意:给你一串二进制,两个操作,一个i到j反转一下,1变0,0变1,一个问某个点的值是多少

思路:区间更新单点查询模板题,直接记录每个点反转过几次,最后对他%2,如果==1等价于转过一次,如果==0,等价于没有转,但是这个区间更新单点查询只能查询改变次数,也就是c数组一开始都是0,只能记录他改变过几次,所以不可以先把给的二进制更新进c数组,然后区间更新单点查询,最后结果取模这样是错的。。。这个区间更新单点查询只能记录+-的数量。。

如果不明白可以看看这个博客:http://blog.csdn.net/qq_34374664/article/details/52787481


#include <cstdio>#include <cstring>#include <iostream>using namespace std;const int maxn = 1e5 + 5;int c[maxn];int lowbit(int k){    return k & (-k);}void update(int n, int v){    while(n <= maxn)    {        c[n] += v;        n += lowbit(n);    }}int sum(int x){    int sum = 0;    while(x >= 1)    {        sum += c[x];        x -= lowbit(x);    }    return sum;}char str[maxn];char cmd;int main(){    int t, q, x, y, Case = 0;    scanf("%d", &t);    while(t--)    {        printf("Case %d:\n", ++Case);        scanf("%s", str);        scanf("%d", &q);        memset(c, 0, sizeof(c));        while(q--)        {            scanf(" %c", &cmd);            if(cmd == 'I')            {                scanf("%d%d", &x, &y);                update(x, 1);                update(y+1, -1);            }            if(cmd == 'Q')            {                scanf("%d", &x);                if(sum(x) % 2) printf("%d\n", !(str[x-1]-'0'));                else printf("%d\n", str[x-1]-'0');            }        }    }    return 0;}



1 0
原创粉丝点击