树状数组
题意:给定一个矩阵,有两种操作,一种是对于其子矩阵进行取反操作,另一种是查询某一个位置的值。
思路:这一题是二位树状数组的模板题
可能是不熟练的原因吧,做题的时候花费了很长时间
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1100;
typedef long long ll;
int a[maxn][maxn];
int n,t;
int lowbit(int i)
{
return i&(-i);
}
void update(int i,int j,int d)
{
for(int x=i;x<=n;x+=lowbit(x))
{
for(int y=j;y<=n;y+=lowbit(y))
a[x][y]+=d;
}
}
ll getsum(int i,int j)
{
ll ans=0;
for(int x=i;x>0;x-=lowbit(x))
{
for(int y=j;y>0;y-=lowbit(y))
ans+=a[x][y];
}
return ans;
}
int main()
{
scanf("%d",&t);
while(t--)
{
memset(a,0,sizeof a);
int m;
scanf("%d%d",&n,&m);getchar();
while(m--)
{
char p;
scanf("%c",&p);
if(p=='C')
{
int x1,y2,x2,y1;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
update(x1,y1,1);
update(x2+1,y2+1,1);
update(x1,y2+1,-1);
update(x2+1,y1,-1);
}
else if(p=='Q')
{
int x,y;
scanf("%d%d",&x,&y);
printf("%lld\n",getsum(x,y)%2);
}
getchar();
}
if(t)
printf("\n");
}
return 0;
}