POJ
来源:互联网 发布:仙界网络直播间无弹窗 编辑:程序博客网 时间:2024/06/15 00:44
传送门:POJ2155
题意:给一个n*n的01矩阵,然后有两种操作:
1.C x1 y1 x2 y2是把这个小矩形内所有数字取反
2.Q x y 是询问当前这个点的值是多少?
思路:很容易想到要用树状数组或线段树。
树状数组的话显然是不能暴力更新的,这时候就要用到一个类似于imos法的思想了,关于imos法:点击打开链接(只看图理解思想就好了,感觉里面的代码写的有问题)
首先要注意的是我们维护的是每个点被操作的次数,而不是他真正的01值,然后运用了上面的imos思想,我们只需要在(x1,y1) + 1,(x1,y2+1) - 1 ,(x2+1,y1) - 1 ,(x2+1 , y2+1) + 1,然后询问某点值的时候输出sum(x,y) & 1就好了。
注意这种方法只适用于区间更新,点查询。
还不是很理解的话推荐看一下:
《浅谈信息学竞赛中的“0”和“1”》
代码:
#include<stdio.h>#include<iostream>#include<string.h>#define ll long long#define pb push_back#define fi first#define se second#define pi acos(-1)#define inf 0x3f3f3f3f#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1#define rep(i,x,n) for(int i=x;i<n;i++)#define per(i,n,x) for(int i=n;i>=x;i--)using namespace std;typedef pair<int,int>P;const int MAXN=1010;int gcd(int a,int b){return b?gcd(b,a%b):a;}int bit[MAXN][MAXN];int sum(int x, int y){int res = 0;for(int i = x; i; i -= -i & i)for(int j = y; j; j -= -j & j)res += bit[i][j];return res;}void add(int x, int y, int delta){for(int i = x; i < MAXN; i += -i & i)for(int j = y; j < MAXN; j += -j & j)bit[i][j] += delta;}int main(){int T;cin >> T;while(T--){memset(bit, 0, sizeof(bit));int n, q, x1, x2, y1, y2;char tp;cin >> n >> q;while(q--){scanf(" %c", &tp);if(tp == '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", sum(x1, y1) & 1);}}puts("");} return 0;}
二维线段树想想就很蛋疼。
阅读全文
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- nginx php 配置
- apache2.2添加ssl
- 牛客题库(LAN、WAN.../ARP/双绞线../HTTP状态码)
- Day3-9.Using other components
- GDB调试core文件详解
- POJ
- Git远程协作_Anroid群英传之神兵利器笔记(三)
- Python 基础 —— @classmethod 与 @staticmethod 的意义及用途
- HashMap和ConCurrentHashMap各自的相关特性和区别(浅谈)
- Android 帧动画 trying to use a recycled bitmap引起的崩溃问题
- 数据库面试题
- 二叉树非递归遍历
- 文科生细谈学习Linux系统的重要性
- 中位数