POJ 2155 树状数组

来源:互联网 发布:华为云计算部门北京 编辑:程序博客网 时间:2024/05/01 07:15

传送门: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
原创粉丝点击