POJ 2155 树状数组

传送门:POJ 2155

题意

给定一个初始全为0的n * n矩阵, 可以分块修改各项值, 查询某点当前值
修改是0->1 || 1->0


题解

二维区间染色, 每次更新染色区间, 更新各点染色
然后值是染色次数奇偶性
区间染色


AC code:

#include<iostream>
#include<cstring>
using namespace std;

#define debug 0
#define lowbit(x) (x & (-x))
#define M(a, b) memset(a, b, sizeof(a))

const int maxn(1005);


int c[maxn][maxn], n, q;

void add(int x, int y, int v){//单点更新

    for(int i = x; i <= n; i += lowbit(i))
        for(int j = y; j <= n; j += lowbit(j)){
            c[i][j] += v;
        }

}

int getSum(int x, int y){//查询
    int res = 0;

    for(int i = x; i; i -= lowbit(i))
        for(int j = y; j; j -= lowbit(j)){
           res += c[i][j];

        }

    return res;
}

int main(){
#if debug
    freopen("in.txt", "r", stdin);
#endif //debug

    int t, x1, y1, x2, y2;
    char s[5];

    cin >> t;
    while(t--){

        M(c, 0);
        cin >> n >> q;

        while(q--){

            cin >> s;
            if(s[0] == 'C'){
                cin >> x1 >> y1 >> x2 >> y2;
                add(x1, y1, 1);//染色更新
                add(x1, y2 + 1, -1);
                add(x2 + 1, y1, -1);
                add(x2 + 1, y2 + 1, 1);
            }else{
                cin >> x1 >> y1;
                int ans = getSum(x1, y1);//查询染色次数
                ans = ans & 1;
                cout << ans << endl;
            }
        }

         if(t) cout << endl;//输出格式
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值