题目链接:
http://poj.org/problem?id=2155
题意:
给出N*N的矩阵,初始为0。
给出矩阵左上角和右下角坐标,这个面积范围里的元素 1变0 ,0 变1,然后给出询问,问某个点是多少。
题解:
http://blog.csdn.net/zxy_snow/article/details/6264135
我们将1变0,0变1转变为将这一块面积翻转几次,奇数次就是1,偶数次就是0。
二维数组维护面积,加加减减。。
add(x2,y2);
add(x1-1,y2);
add(x2,y1-1);
add(x1-1,y1-1);
看图:虽然非常丑,对于翻转一块面积,先翻转整体,减掉多加的,加上多减的,图上的数字代表这块面积翻转了几次,偶数次代表没有改变嘛。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
#define MS(a) memset(a,0,sizeof(a))
#define MP make_pair
#define PB push_back
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
inline ll read(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
//////////////////////////////////////////////////////////////////////////
const int maxn = 1e3+10;
int bit[maxn][maxn];
void add(int x,int y){
for(int i=x; i<maxn; i+=i&-i)
for(int j=y; j<maxn; j+=j&-j)
bit[i][j]++;
}
int sum(int x,int y){
int res = 0;
for(int i=x; i>0; i-=i&-i)
for(int j=y; j>0; j-=j&-j)
res += bit[i][j];
return res;
}
int main(){
int T = read();
while(T--){
MS(bit);
int n,k;
cin >> n >> k;
for(int i=0; i<k; i++){
char op; scanf(" %c",&op);
if(op == 'C'){
int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x1++,y1++,x2++,y2++;
add(x2,y2);
add(x1-1,y1-1);
add(x1-1,y2);
add(x2,y1-1);
}else {
int x,y; scanf("%d%d",&x,&y);
cout << sum(x,y)%2 << endl;
}
}
puts("");
}
return 0;
}