2155

/*
二维的树状数组

这树状解法出了是二维的以外,还有个巧妙的应用。常规的BIT是用+lowbit计算更新,-lowbit计算和
不过这只是一种现象,+lowbit和-lowbit还可以做别的事情,只要发散思维。 
这个题目的特殊性是,更新是一个区间,查找是一个点的值
每个点可以被操作多次,那么最后要找出这个点的值是多少。该怎么做呢?

如果更新用-lowbit实现,会有什么效果? 效果是比该点小的所有的Bit值都增加了1,这就相当于对区间进行更新了。
查找用+lowbit实现,会有什么效果?效果是比该店大的所有的Bit值都是对该点所做的操作,把他们全部加起来,就是总共操作的次数,然后对2取模,就是最后要的结果了。

是不是很巧妙啊? 仔细想想
*/

// include file
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <ctime>

#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>

#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <functional>

using namespace std;

// typedef
typedef long long LL;
typedef unsigned long long ULL;

// 
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define FORi(a,b,c) for(int i=(a);i<(b);i+=c)
#define FORj(a,b,c) for(int j=(a);j<(b);j+=c)
#define FORk(a,b,c) for(int k=(a);k<(b);k+=c)
#define FORp(a,b,c) for(int p=(a);p<(b);p+=c)
#define FORii(a,b,c) for(int ii=(a);ii<(b);ii+=c)
#define FORjj(a,b,c) for(int jj=(a);jj<(b);jj+=c)
#define FORkk(a,b,c) for(int kk=(a);kk<(b);kk+=c)

#define FF(i,a)    for(int i=0;i<(a);i++)
#define FFD(i,a)   for(int i=(a)-1;i>=0;i--)

#define Z(a) (a<<1)
#define Y(a) (a>>1)

const double eps = 1e-6;
const double INFf = 1e100;
const int INFi = 1000000000;
const LL INFll = (LL)1<<62;
const double Pi = acos(-1.0);

template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T TMAX(T x,T y)
{
	if(x>y) return x;
	return y;
}
template<class T> inline T TMIN(T x,T y)
{
	if(x<y) return x;
	return y;
}
template<class T> inline void SWAP(T &x,T &y)
{
	T t = x;
	x = y;
	y = t;
}
template<class T> inline T MMAX(T x,T y,T z)
{
	return TMAX(TMAX(x,y),z);
}
template<class T> inline T MMIN(T x,T y,T z)
{
	return TMIN(TMIN(x,y),z);
}


// code begin
bool Bit[1010][1010];
int X,N,T;
int xx1,yy1,xx2,yy2;
char cmd[3];
int Nx,Ny;

inline int lowBit(int x)
{
	return x&(-x);
}

void getSum(int x,int y,int c) //此处getSum用于更新
{
	for(int i=x;i>0;i-=lowBit(i))
	{
		for(int j=y;j>0;j-=lowBit(j))
		{
			Bit[i][j] = (Bit[i][j] + c + 2)&1;
		}
	}
}

int update(int x,int y) //更新用于查找,返回的结果是更新了几次的意思
{
	int ans = 0;
	for(int i=x;i<Nx;i+=lowBit(i))
	{
		for(int j=y;j<Ny;j+=lowBit(j))
		{
			ans = (ans + Bit[i][j] + 2)&1;
		}
	}
	return ans;
}

int main()
{
	read;
	write;
	scanf("%d",&X);
	while(X--)
	{
		scanf("%d %d",&N,&T);
		memset(Bit,0,sizeof(Bit));
		Nx = Ny = 1001;
		while(T--)
		{
			scanf("%s",cmd);
			switch(cmd[0])
			{
			case 'C':
				scanf("%d %d %d %d",&xx1,&yy1,&xx2,&yy2);
				getSum(xx2,yy2,1);
				getSum(xx2,yy1-1,-1);
				getSum(xx1-1,yy2,-1);
				getSum(xx1-1,yy1-1,1);
				break;
			case 'Q':
				scanf("%d %d",&xx1,&yy1);
				printf("%d\n",update(xx1,yy1) );
				break;
			}
		}
		printf("\n");
	}
	return 0;
}

posted @ 2011-04-13 15:46  AC2012  阅读(511)  评论(0编辑  收藏  举报