poj2155

求出每个点改变的次数%2就行了

参考http://blog.youkuaiyun.com/zxy_snow/article/details/6264135

可以利用二维树桩数组记录,每次更新(x1,y1),(x2+1,y2+1)(x1,y2+1)(x2+1,y1)四个点,可以发现

对应区域内的sum(x,y)的奇偶性改变,而其他区域的奇偶性不变。

如一个4*4的例子

x1=2,y1=2,x2=3,y2=3

更新(x2+1,y2+1)后

0 0 0 0 
0 0 0 0 
0 0 0 0 
0 0 0 1

更新(x1,y1)

0 0 0 0 
0 1 1 1 
0 1 1 1 
0 1 1 2 

更新(x1,y2+1)

0 0 0 0 
0 1 1 2 
0 1 1 2 
0 1 1 3 

更新(x2+1,y1)

0 0 0 0 
0 1 1 2 
0 1 1 2 
0 2 2 4 

可以看出除了覆盖区域外其他区域的奇数偶性并未改变

#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAX = 1010;
int c[MAX][MAX];
int n;
int Lowbit(int x)
{
	return x & (-x);
}
void Updata(int x,int y)
{
	int i,k;
	for(i=x; i<=n; i+=Lowbit(i))
		for(k=y; k<=n; k+=Lowbit(k))
			c[i][k]++;
}
int Get(int x,int y)
{
	int i,k,sum = 0;
	for(i=x; i>0; i-=Lowbit(i))
		for(k=y; k>0; k-=Lowbit(k))
			sum += c[i][k];
	return sum;
}
int main()
{
	int ncases,m;
	int x1,y1,x2,y2;
	char ch[2];
	scanf("%d",&ncases);
	while( ncases-- )
	{
		memset(c,0,sizeof(c));
		scanf("%d%d",&n,&m);
		while( m-- )
		{
			scanf("%s",ch);
			if( ch[0] == 'C' )
			{
				scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
				Updata(x2+1,y2+1);
				Updata(x1,y1);
				Updata(x1,y2+1);
				Updata(x2+1,y1);
			}
			else
			{
				scanf("%d%d",&x1,&y1);
				printf("%d\n",Get(x1,y1)%2);
			}
		}
		printf("\n");
	}
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值