您的位置:首页 > 其它

POJ2155 Matrix(二维树状数组)

2015-09-05 11:08 381 查看
题意:给出矩阵左上角和右下角坐标,矩阵里的元素 1变0 ,0 变1,然后给出询问,问某个点是多少。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#define MAXN 1010
int c[MAXN][MAXN],n;
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int y)
{
for(int i=x; i<=n; i+=lowbit(i))
for(int j=y; j<=n; j+=lowbit(j))
c[i][j]++;
}
int getSum(int x,int y)
{
int s=0;
for(int i=x; i>0; i-=lowbit(i))
for(int j=y; j>0; j-=lowbit(j))
s+=c[i][j];
return s;
}
int main()
{
int t,m,x1,x2,y1,y2;
char ch[2];
scanf("%d",&t);
while(t--)
{
memset(c,0,sizeof(c));
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%s",ch);
if(ch[0]=='C')
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x1++;
y1++;
x2++;
y2++;
update(x2,y2);
update(x1-1,y1-1);
update(x1-1,y2);
update(x2,y1-1);
}
else
{
scanf("%d%d",&x1,&y1);
printf("%d\n",getSum(x1,y1)%2);
}
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: