poj2155 Matrix 二维树状数组

/**
题目:poj2155 Matrix
链接:http://poj.org/problem?id=2155
题意:c x1 y1 x2 y2表示把左上角为(x1,y1)右下角为(x2,y2)这个矩形内的所有元素进行取非操作(x=!x)。
q x y表示查询a[x][y]这个元素值。
思路:二维树状数组

*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<cstring>
#include<cmath>
using namespace std;
typedef pair<int,int> P;
typedef long long LL;
const int mod = 998244353;
const int INF = 0x3f3f3f3f;
const int N = 2005;
const int maxn = 1004;
int n, m;
int c[maxn][maxn];
///二维树状数组
int query(int i,int j)
{
    int s = 0;
    for(int x = i; x > 0; x-=(x&(-x))){
        for(int y = j; y > 0; y-=(y&(-y))){
            s = s+c[x][y];
        }
    }
    return s;
}
void update(int i,int j,int d)
{
    for(int x = i; x <= n; x+=(x&(-x))){
        for(int y = j; y <= n; y+=(y&(-y))){
            c[x][y] = c[x][y]+d;
        }
    }
}
char q[2];
int main()
{
    int T;
    cin>>T;
    for(int i = 0; i<T; i++)
    {
        scanf("%d%d",&n,&m);
        memset(c, 0, sizeof c);
        int x1, y1, x2, y2;
        for(int i = 1; i <= m; i++){
            scanf("%s",q);
            if(q[0]=='C'){
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                update(x1,y1,1);
                update(x2+1,y2+1,1);
                update(x2+1,y1,-1);
                update(x1,y2+1,-1);
            }else
            {
                scanf("%d%d",&x1,&y1);
                printf("%d
",query(x1,y1)%2);
            }
        }
        if(i!=T-1) cout<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/xiaochaoqun/p/7296006.html