poj-2155 matrix :二维树状数组

前情提要:原题是英文题面,我直接在某谷上找翻译,由于过于信任某谷 ,拿着错误题面干瞪眼好久…
题面:

  • 有一个矩阵,里面所有数字都是0,有两种操作
  • c操作,把一个矩形里的数字都取非
  • q操作,展示某个点的值。

思路:

  1. 取非/异或等操作都有一个特性–只有奇数次操作才有用
  2. 所以只需存有几次操作就行
  3. 用树状数组存次数
  4. 最后只要注意统计答案时的query就行(容斥原理)
#include<cstdio>  
#include<cstring>  
#include<cstdlib>  
#include<algorithm>  
#include<functional>  
#include<iostream>  
#include<cmath>  
#include<cctype>  
#include<ctime> 
#define ll long long
#define il inline
#define re register
//有一个矩阵,里面所有数字都是0,有两种操作
//c操作,把一个矩形里的数字都取非
//q操作,展示某个点的值。
using namespace std;
int T;
int n, q; 
int c[1010][1010];
il int lowbit(int x) {
	return x & (-x);
}
il void update(int x, int y, int v) {
	//二维更新
	for(int i = x; i <= n; i += lowbit(i)) {
		for(int j = y; j <= n; j += lowbit(j)) {
			c[i][j] += v;//这个区间的点取非次数++ 
		}
	} 
}
il int getsum(int x, int y) {
	int res = 0;
	for(int i = x; i > 0; i-= lowbit(i)) {
		for(int j = y; j > 0; j -= lowbit(j)) {
			res += c[i][j];
		}
	}
	return res;//整个矩形的点的取非次数 
}
int main() {
	scanf("%d", &T);
	while(T--) {
		memset(c, 0, sizeof(c));
		char ch[3];
		scanf("%d%d", &n, &q);
		while(q--) {
			scanf("%s", &ch);
			if(ch[0] == 'C') {
				int x1, x2, y1, y2;
				scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
				//更新嘛
				update(x1, y1, 1);
				update(x2+1, y2+1, 1);
				update(x1, y2+1, -1);//右上 
				update(x2+1, y1, -1);//左下 
			}else {
				int x, y;
				scanf("%d%d", &x, &y);
				printf("%d\n", getsum(x, y)%2);//偶数次就当不存在	
			}
		}
		printf("\n");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值