pku 2155 Matrix(二维树状数组)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXN 1005
int N,c[MAXN][MAXN];
inline int lowbit(int x)
{
return x&(-x);
}
void update(int x,int y,int delta)
{
while(x<=N)
{
int t=y;
while(t<=N)
{
c[x][t]+=delta;
t+=lowbit(t);
}
x+=lowbit(x);
}
}
int getSum(int x,int y)
{
int s=0;
while(x>0)
{
int t=y;
while(t>0)
{
s+=c[x][t];
t-=lowbit(t);
}
x-=lowbit(x);
}
return s;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("tdata.txt","r",stdin);
#endif
int cas,T,x1,y1,x2,y2;
char op[2];
scanf("%d",&cas);
bool first=true;
while(cas--)
{
memset(c,0,sizeof(c));
scanf("%d %d",&N,&T);
if(!first) printf("\n");
first=false;
while(T--)
{
scanf("%s",op);
if(op[0]=='C')
{
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
update(x1,y1,1);
update(x1,y2+1,-1);
update(x2+1,y1,-1);
update(x2+1,y2+1,1);
}
else
{
scanf("%d %d",&x1,&y2);
printf("%d\n",getSum(x1,y2)%2);
}
}
}
return 0;
}
浙公网安备 33010602011771号