POJ2155 Matrix(树状数组)

根据武森的国家集训队论文我们知道,只需要在四个点插入值即可,之后用二维数组求和,因为矩阵上都是0和1,并且初始为0,我们只需计算他更改次数的奇偶次

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<vector>
#include<string>
#include<cstring>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int N=1e5+10;
int tr[1100][1100];
int n;
int lowbit(int x){
    return x&-x;
}
void add(int x,int y,int c){
    int i,j;
    for(i=x;i<=n;i+=lowbit(i)){
        for(j=y;j<=n;j+=lowbit(j))
        tr[i][j]+=c;
    }
}
int sum(int x,int y){
    int i,j;
    int res=0;
    for(i=x;i;i-=lowbit(i)){
        for(j=y;j;j-=lowbit(j)){
            res+=tr[i][j];
        }
    }
    return res;
}
int main(){
    int q;
    int m;
    cin>>q;
    while(q--){
        cin>>n>>m;
        int i;
        memset(tr,0,sizeof tr);
        while(m--){
            char s[N];
            scanf("%s",s);
            if(*s=='C'){
            int x1,x2,y1,y2;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            add(x1,y1,1);
            add(x2+1,y1,1);
            add(x2+1,y2+1,1);
            add(x1,y2+1,1);
            }
            else{
                int x,y;
                scanf("%d%d",&x,&y);
                if(sum(x,y)%2==0)
                printf("0\n");
                else 
                printf("1\n");
            }
            
        }printf("\n"); 
    }
} 
View Code

猜你喜欢

转载自www.cnblogs.com/ctyakwf/p/12320446.html