CF439E:The Untended Antiquity - 哈希 + 二维树状数组
题目大意
有一个n*m的网格,支持三中操作:
1.在x1,y1,x2,y2为顶点的矩形周围围上栅栏
2.将x1,y1,x2,y2为顶点的矩形周围的栅栏拆掉
3.询问x1,y1,x2,y2两点是否联通
保证栅栏矩形不相交
题目分析
因为栅栏的矩形互不相交,所以两点不连通时一定在不同的地域里。
因此可以将栅栏附上一个hash值,用二维树状数组维护前缀和,如果点查到的值相同则代表在同一个地域里。
code
#include<bits/stdc++.h>
using namespace std;
const int N = 2505;
int n, m, q;
typedef unsigned long long uint;
typedef pair<int, int> P;
map<P, uint> Map;
struct IO{
inline int rint(){
int i = 0, f = 1; char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0' && ch <= '9'; ch = getchar())
i = (i << 3) + (i << 1) + (ch - '0');
return i * f;
}
inline void wstr(string s){
int len = s.length();
for(int i = 0; i < len; i++) putchar(s[i]);
putchar('\n');
}
}io;
struct BIT{
uint tree[N][N];
inline void add(int x, int y, uint val){
for(int i = x; i <= n; i += (i & -i))
for(int j = y; j <= m; j += (j & -j))
tree[i][j] += val;
}
inline uint query(int x, int y){
uint ret = 0;
for(int i = x; i; i -= (i & -i))
for(int j = y; j; j -= (j & -j))
ret += tree[i][j];
return ret;
}
}bit;
inline uint RandHash(){
static uint RAND_VAL = 1388593021;
return RAND_VAL += RAND_VAL << 2 | 1;
}
int main(){
n = io.rint(), m = io.rint(), q = io.rint();
for(int i = 1; i <= q; i++){
int op = io.rint();
int x1 = io.rint(), y1 = io.rint();
int x2 = io.rint(), y2 = io.rint();
switch(op){
case 1:{
uint hashVal = RandHash();
bit.add(x1, y1, hashVal), bit.add(x1, y2 + 1, -hashVal), bit.add(x2 + 1, y1, -hashVal), bit.add(x2 + 1, y2 + 1, hashVal);
Map[P((x1 - 1) * n + y1, (x2 - 1) * n + y2)] = hashVal;
break;
}
case 2:{
uint hashVal = Map[P((x1 - 1) * n + y1, (x2 - 1) * n + y2)];
bit.add(x1, y1, -hashVal), bit.add(x1, y2 + 1, hashVal), bit.add(x2 + 1, y1, hashVal), bit.add(x2 + 1, y2 + 1, -hashVal);
break;
}
case 3:{
if(bit.query(x1, y1) == bit.query(x2, y2)) io.wstr("Yes");
else io.wstr("No");
break;
}
}
}
system("pause");
}
CF439E:The Untended Antiquity - 哈希 + 二维树状数组的更多相关文章
- 2018牛客网暑期ACM多校训练营(第二场) J - farm - [随机数哈希+二维树状数组]
题目链接:https://www.nowcoder.com/acm/contest/140/J 时间限制:C/C++ 4秒,其他语言8秒 空间限制:C/C++ 262144K,其他语言524288K ...
- 二维树状数组 BZOJ 1452 [JSOI2009]Count
题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...
- HDU1559 最大子矩阵 (二维树状数组)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others) ...
- POJMatrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22058 Accepted: 8219 Descripti ...
- poj 1195:Mobile phones(二维树状数组,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14489 Accepted: 6735 De ...
- Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*
D. Iahub and Xors Iahub does not like background stories, so he'll tell you exactly what this prob ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- POJ 2155 Matrix (二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17224 Accepted: 6460 Descripti ...
随机推荐
- 【例题 6-21 UVA - 506】System Dependencies
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 记录每个物品它的依赖有哪些,以及它被哪些东西依赖就可以了. 显式安装的东西不能被隐式删除删掉(就是remove item,然后删除i ...
- 【CS Round #46 (Div. 1.5) C】Set Subtraction
[链接]h在这里写链接 [题意] 一开始有n个数字,然后有一个数字X,把每个数字都减去X,又生成N个新的数字. 然后把这2*N个数字混在一起. 告诉你这2*N个数字是什么.让你复原出原来的N个数字,以 ...
- COGS——C 908. 校园网 || 洛谷——P 2746 [USACO5.3]校园网Network of Schools
http://www.cogs.pro/cogs/problem/problem.php?pid=908 || https://www.luogu.org/problem/show?pid=27 ...
- HDU 5071 模拟
考察英语的题 - -# 按条件模拟,一遍即可了,每一个聊天对象有其价值U.数组模拟队列过程即可,若存在Top标记,则和Top标记的人聊天,否则和队列的第一个人聊天 mark记录队尾,top记录Top操 ...
- Spring-data-redis:特性与实例--转载
原文地址:http://shift-alt-ctrl.iteye.com/blog/1886831 Spring-data-redis为spring-data模块中对redis的支持部分,简称为“SD ...
- 【Codeforces Round #443 (Div. 2) B】Table Tennis
[链接] 我是链接,点我呀:) [题意] n个人站在一排. 每次第一个人和第二个人打架. 输的人跑到队列的尾巴去. 然后赢的人继续在队首.和第三个人打. 谁会先赢K次. [题解] 会发现,一轮之后就一 ...
- php ignore_user_abort()实现计划(定时执行)任务功能
? 1 2 3 4 5 6 7 8 9 10 11 12 <?php ignore_user_abort(TRUE); //关掉浏览器,PHP脚本也可以继续执行. set_ti ...
- 【例题5-8 UVA - 400】Unix ls
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 设n个字符串中出现的最长的为len; 最后一列能容纳len个字符,然后前面的列能容纳len+2个字符. 每行最多60个字符. 按照这 ...
- 【BZOJ 2119】股市的预测
[链接]h在这里写链接 [题意] 给你一个长度为n的数组a[] 设b[i] = a[i+1]-a[i]; 然后让你在b[i]里面找ABA的形式. 这里B的长度要求为m; ...
- html中radio、checkbox选中状态研究(静下心来看,静下心来总结)
html中radio.checkbox选中状态研究(静下心来看,静下心来总结) 一.总结 1.单选框的如果有多个checked 会以最后一个为准 2.js动态添加checked属性:不行:通过 $(& ...