【二維樹狀陣列】poj 2155 Matrix

CN_swords發表於2017-09-06

Link:http://poj.org/problem?id=2155

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;

#define lowbit(i) (i&(-i))
const int N = 1010;
int tree[N][N];
int n;
void add(int x,int y,int val){
    for(int xx = x; xx; xx -= lowbit(xx))
        for(int yy = y; yy; yy -= lowbit(yy))
            tree[xx][yy] += val;
}
int query(int x,int y){
    int ans = 0;
    for(int xx = x; xx<=n ; xx+=lowbit(xx))
        for(int yy = y; yy<=n ; yy+=lowbit(yy))
            ans += tree[xx][yy];
    return ans;
}
int main()
{
    int T,q;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&q);
        memset(tree,0,sizeof(tree));
        while(q--){
            getchar();
            char c;
            int x1,x2,y1,y2;
            scanf("%c",&c);
            if(c == 'C'){
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                add(x2,y2,1);
                add(x1-1,y2,-1);
                add(x2,y1-1,-1);
                add(x1-1,y1-1,1);
            }
            else{
                scanf("%d%d",&x1,&y1);
                printf("%d\n",query(x1,y1)%2);
            }
        }
        if(T!=0)
            puts("");
    }
    return 0;
}


相關文章