您的位置:首页 > 其它

POJ - 2155 Matrix

2015-11-29 11:19 260 查看
BIT更新一个区间的前缀和是很容易的,modify操作修改的是[x,N]的所有前缀和(也就是修改的是一个后缀)。

二维的也是容斥一下就好了。对于查询的(x,y),回答前缀和(x,y)就好了。

/*********************************************************
*            ------------------                          *
*   author AbyssalFish                                   *
**********************************************************/
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<numeric>
using namespace std;

const int MAX_N = 1e3+1;
bool C[MAX_N][MAX_N];

int N;

bool prefix(int x,int y)
{
int r = 0;
for(;x > 0; x &= x-1){
for(int j = y; j > 0; j &= j-1){
if(C[x][j]) r++;
}
}
return r&1;
}

void modify(int x,int y)
{
for(;x <= N; x += x&-x){
for(int j = y; j <= N; j += j&-j){
C[x][j] = !C[x][j];
}
}
}

//#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
int T; scanf("%d",&T);
while(T--){
int Q; scanf("%d%d",&N,&Q);
char ch[2];
while(Q--){
int x,y;
scanf("%s%d%d",ch,&x,&y);
if(*ch == 'Q'){
puts(prefix(x,y)?"1":"0");
//printf("%s%s",prefix(x,y)?"1":"0",Q?"\n":"");
}
else {
int x1,y1; scanf("%d%d",&x1,&y1);
modify(x,y);
modify(x,++y1);
modify(++x1,y);
modify(x1,y1);
}
}
puts("");//between?
if(T){
for(int i = 1; i <= N; i++) memset(C[i]+1,0,sizeof(bool)*N);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: