Codeforces Round #439 (Div. 2) E. The Untended Antiquity(二维BIT)
来源:互联网 发布:金字塔原理 知乎 编辑:程序博客网 时间:2024/06/11 07:31
题意:在 n×m 的二维图上,有三种操作:
1 r1 c1 r2 c2 表示沿着 (r1, c1, r2, c2) 所表示的矩形的外边框建围墙。(其中 (r1, c1) 为矩形左上角,(r2, c2) 表示矩形右下角)。
2 r1 c1 r2 c2 表示取消 (r1, c1, r2, c2) 所示矩形的围墙。(保证最初图不存在围墙,删除的围墙一定是之前通过操作 1 建立的)。
1 r1 c1 r2 c2 表示沿着 (r1, c1, r2, c2) 所表示的矩形的外边框建围墙。(其中 (r1, c1) 为矩形左上角,(r2, c2) 表示矩形右下角)。
2 r1 c1 r2 c2 表示取消 (r1, c1, r2, c2) 所示矩形的围墙。(保证最初图不存在围墙,删除的围墙一定是之前通过操作 1 建立的)。
3 r1 c1 r2 c2 表示询问 (r1, c1) 到 (r2, c2) 是否可以不翻越围墙到达。对于每个操作 3,可以输出 Yes ,否则输出 No 。
(n, m <= 2500, q <= 1e5, 保证围墙没有相交)
思路:因为围墙之间不相交,所以我们可以把每个围墙内部都设置成一个值,询问的时候只要看两个点的值是否相同即可。那么怎么
将一个围墙内设为一个值呢,我们可以利用二维BIT,利用前缀和,进行区间更新,单点查询,跟一维的时候区间更新单点更新一
样。那么删除的时候怎么快速得到这个围墙原来设置的是什么值呢,我们可以用哈希,将四个坐标哈希成一个值,具体见代码。
代码:
#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 2505;const int seed = 2333;ll tree[maxn][maxn], n, m, q;int lowbit(int x){ return x&(-x);}void update(int x, int y, ll val){ for(int i = x; i <= n; i += lowbit(i)) for(int j = y; j <= m; j += lowbit(j)) tree[i][j] += val;}void update(int x1, int y1, int x2, int y2, ll val){ update(x1, y1, val); update(x1, y2+1, -val); update(x2+1, y1, -val); update(x2+1, y2+1, val);}ll query(int x, int y){ ll res = 0; for(int i = x; i; i -= lowbit(i)) for(int j = y; j; j -= lowbit(j)) res += tree[i][j]; return res;}int main(void){ while(~scanf("%I64d%I64d%I64d", &n, &m, &q)) { memset(tree, 0, sizeof(tree)); int cmd, x1, y1, x2, y2; while(q--) { scanf("%d%d%d%d%d", &cmd, &x1, &y1, &x2, &y2); ll val = x1; val = val*seed+y1; val = val*seed+x2; val = val*seed+y2; if(cmd == 1) update(x1, y1, x2, y2, val); else if(cmd == 2) update(x1, y1, x2, y2, -val); else { ll tmp1 = query(x1, y1); ll tmp2 = query(x2, y2); puts(tmp1 == tmp2 ? "Yes" : "No"); } } } return 0;}
阅读全文
1 0
- Codeforces Round #439 (Div. 2) E. The Untended Antiquity(二维BIT)
- Codeforces Round #439 (Div. 2) E. The Untended Antiquity 二维线段树||二维树状数组
- Codeforces Round #439 (Div. 2) E. The Untended Antiquity
- Codeforces Round #439 (Div. 2) E. The Untended Antiquity
- Codeforces Round #439 (Div. 2) E:The Untended Antiquity(Hash+二维树状数组)
- Codeforces Round #439 (Div. 2) E. The Untended Antiquity 二维树状数组 随机化
- Codeforces Round #439 (Div. 2) E. The Untended Antiquity hash+二维树状数组或二维线段树
- codeforces 869E The Untended Antiquity 二维BIT + hash
- Codeforces Round #439 (Div. 2) E. The Untended Antiquity (hash+数状数组)
- Codeforces Round #439 (Div. 2) E. The Untended Antiquity(Hash)
- Codeforces Round #439 E. The Untended Antiquity (树状数组+随机化)
- Codeforces 869 E. The Untended Antiquity 二维树状数组
- CF #439 E The Untended Antiquity
- CF869 E. The Untended Antiquity
- CF869 E. The Untended Antiquity
- [哈希+差分] Codeforces 869E. The Untended Antiquity
- codeforces—— 869E —— The Untended Antiquity
- Codeforces869E The Untended Antiquity
- LintCode 字符串查找(暴力法+KMP)
- 百度2017春招笔试真题[编程题] 度度熊回家
- leetcode-402. Remove K Digits
- 网络复习-运输层
- 面向对象:多态 Polymorphism、重载、重写、继承
- Codeforces Round #439 (Div. 2) E. The Untended Antiquity(二维BIT)
- 洛谷 P1845 影像之结构化特征_NOI导刊2011提高(12)
- STL常用算法(转)
- LYK快跑!(run)
- 浅谈getElementsByClassName
- 页面传参
- 19、C#里面的冒泡排序操作
- Pig数据分析引擎 二 使用PigLatin语句分析数据
- run