您的位置:首页 > 编程语言 > Go语言

POJ2155--Matrix(二维树状数组)

2014-01-21 14:15 246 查看
对于翻转,我们可以直接+1,取其结果%2即可。

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <queue>
#include <stack>
#include <vector>
#include <ctype.h>
#define INF 999999999
#define M 1005
#define LL long long
#define Min(a,b) a<b?a:b
#define Max(a,b) a>b?a:b
using namespace std;

int c[M][M];
int n;

int Lowbit(int x)
{
return x&(-x);
}

void Update(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))
c[i][j]+=d;
}
}

int Getsum(int x,int y)
{
int sum=0;
int i,j;
for(i=x;i>0;i-=Lowbit(i))
{
for(j=y;j>0;j-=Lowbit(j))
{
sum+=c[i][j];
}
}
return sum;
}
int main()
{
int t,m,count;
scanf("%d",&t);
for(count=1;count<=t;count++)
{
if(count!=1)
printf("\n");
scanf("%d%d",&n,&m);
memset(c,0,sizeof(c));
while(m--)
{
char ch;
getchar();
scanf("%c",&ch);
if(ch=='C')
{
int x1,x2,y1,y2;
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
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",Getsum(a,b)&1);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  algorithm