POJ2155【二维树状数组模板】

来源:互联网 发布:广州网络推广公司 编辑:程序博客网 时间:2024/05/17 07:36

sum(i,j)%2 = 0/1的具体证明;

推荐一篇论文  浅谈信息学竞赛中的“0”和“1”.pdf(百度的到吧)

//#include<bits/stdc++.h>//using namespace std;//typedef long long LL;#include <stdio.h>#include <string.h>#include <iostream> #include <algorithm>using namespace std;typedef long long LL;const int N=1e3+10;int mat[N][N];int lowbit(int x){    return x&(-x);}void add(int x, int y, int d){    int i, j;    for(i = x; i < N; i += lowbit(i))        for(j = y; j < N; j += lowbit(j))            mat[i][j] += d;}int sum(int x, int y){    int res = 0;    int i, j;    for(i = x; i > 0; i -= lowbit(i))        for(j = y; j > 0; j -= lowbit(j))            res += mat[i][j];    return res;}int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    int T;    scanf("%d",&T);    while(T--){        int n,q;        char op[5];        int x1,x2,y1,y2;        memset(mat,0,sizeof(mat));        scanf("%d%d",&n,&q);        while(q--){            scanf("%s",op);            if(op[0]=='C'){                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);                add(x1,y1,1);                add(x2+1,y1,1);                add(x1,y2+1,1);                add(x2+1,y2+1,1);            }            else{                scanf("%d%d",&x1,&y1);                if(sum(x1,y1)%2 == 0) puts("0");                else puts("1");            }        }        if(T>0) puts("");    }    return 0;}


0 0
原创粉丝点击