题目地址:POJ 2155
二维数组裸题。
对于更新操作,分别对(x1,y1)和(x2+1,y2+1)加1,对(x2+1,y1)和(x1,y2+1)减1.然后对于询问操作,求(0,0)至(x2,y2)的和。
代码如下:
#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
#include <string>
#include <time.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
//#pragma comment(linker, "/STACK:1024000000")
//const int mod=9901;
const int INF=0x3f3f3f3f;
const double eqs=1e-9;
const int MAXN=1000+10;
int c[MAXN][MAXN], n;
int lowbit(int x)
{
return x&(-x);
}
void add(int x, int y, int val)
{
int i=x, j;
while(i<=n){
j=y;
while(j<=n){
c[i][j]+=val;
j+=lowbit(j);
}
i+=lowbit(i);
}
}
int Query(int x, int y)
{
int ans=0;
int i=x, j;
while(i>0){
j=y;
while(j>0){
ans+=c[i][j];
j-=lowbit(j);
}
i-=lowbit(i);
}
return ans;
}
int main()
{
int t, q, i, j, x1, y1, x2, y2;
scanf("%d",&t);
char s[3];
while(t--){
scanf("%d%d",&n,&q);
memset(c,0,sizeof(c));
while(q--){
scanf("%s",s);
if(s[0]=='C'){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
add(x1,y1,1);
add(x2+1,y1,-1);
add(x1,y2+1,-1);
add(x2+1,y2+1,1);
}
else{
scanf("%d%d",&x1,&y1);
printf("%d\n",Query(x1,y1)%2);
}
}
if(t) puts("");
}
return 0;
}