Light OJ--1080-----Binary Simulation树状数组

Binary Simulation

Time Limit: 2 second(s)Memory Limit: 64 MB

Given a binary number, we are about to do some operations onthe number. Two types of operations can be here.

'I i j'    which means invert the bit from itoj (inclusive)

'Q i'    answer whether the ith bitis 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 integerhaving length n (1 ≤ n ≤ 105). The nextline will contain an integer q (1 ≤ q ≤ 50000) denoting thenumber 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 querywill 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. Thenfor each query 'Q i' you have to print 1 or 0 depending on the ithbit.

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


区间更新,本来想用数组作标记来做,发现做不了,看过题解才知道有专门解决区间更新的算法--树状数组,线段树

给出任意多个区间i,j,i到j之间的数字翻转,最后问第n个数字是什么,只有0和1即计算这个数字翻转了几次

奇数次就是翻转后的数,偶数次就是本身不变。

这题树状数组简单一些

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<map>
#include<queue>
#include<algorithm>
#include<cmath>
#include<vector>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define DL(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long LL;
const int maxn = 1e5+10;
int put[maxn];
char str[maxn];
int lowbit(int x){
    return x&(-x);
}
void change(int s, int val, int n){
    while(s <= n){
        put[s] += val;
        s += lowbit(s);
    }
}
int get_bit(int s){
    int t = 0;
    while(s){
        t += put[s];
        s -= lowbit(s);
    }
    return t;
}
int main(){
    int t, n, m, c, kcase = 1;
    char ch;
    scanf("%d", &t);
    while(t--){
        DL(put, 0);
        scanf("%s", str+1);
        scanf("%d", &n);
        m = strlen(str+1);
        printf("Case %d:\n", kcase++);
        for(int i = 0; i < n; i++){
            scanf(" %c", &ch);
            if(ch == 'I'){
                int st, en;
                scanf("%d %d", &st, &en);
                change(st, 1, m);//区间起点+1,终点后一位-1,根据树状数组的特性,就相当于st到en区间都+1
                change(en+1, -1, m);
                continue;
            }
            scanf("%d", &c);
            printf("%d\n", get_bit(c)&1 ? !(str[c]-48) : str[c]-48);//最后单点查询该点翻转过几次
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值