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 ...
随机推荐
- 洛谷——P1035 级数求和
https://www.luogu.org/problem/show?pid=1035 题目描述 已知:Sn= 1+1/2+1/3+…+1/n.显然对于任意一个整数K,当n足够大的时候,Sn大于K. ...
- 【算法导论-36】并查集(Disjoint Set)具体解释
WiKi Disjoint是"不相交"的意思.Disjoint Set高效地支持集合的合并(Union)和集合内元素的查找(Find)两种操作,所以Disjoint Set中文翻译 ...
- css中hack是什么
css中hack是什么 一.总结 1.CSS hack:由于不同厂商的浏览器,比如Internet Explorer,Safari,Mozilla Firefox,Chrome等,或者是同一厂商的浏览 ...
- 从大整数乘法的实现到 Karatsuba 快速算法
Karatsuba 快速乘积算法是具有独特合并过程(combine/merge)的分治算法(Karatsuba 是俄罗斯人).此算法主要是对两个整数进行相乘,并不适用于低位数(如 int 的 32 位 ...
- 关于python的拷贝
https://blog.csdn.net/koukehui0292/article/details/82993958 Python的 深度拷贝: import copy d=copy.deepcop ...
- python3 turtle 画围棋棋盘
python3 环境 利用turtle模块画出 围棋棋盘 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan impor ...
- 6.2、Android硬件访问服务编写系统代码
1.实现接口文件给App使用,接口文件是应用程序查询获得服务时获得 使用AIDL(Android接口定义语言)来实现ILedService.java接口 定义ILedService.aidl inte ...
- Java反射学习总结四(动态代理使用实例和内部原理解析)
通过上一篇文章介绍的静态代理Java反射学习总结三(静态代理)中,大家可以发现在静态代理中每一个代理类只能为一个接口服务,这样一来必然会产生过多的代理,而且对于每个实例,如果需要添加不同代理就要去添加 ...
- [array] leetCode-1-Two Sum-Easy
leetCode-1-Two Sum-Easy descrition Given an array of integers, return indices of the two numbers suc ...
- opencv播放不了AVI视频的问题
有些avi视频的编码可能不是Cinepak Codec by Radius编码格式的,需要转换成这种格式. 我用的是swf转avi视频,在转变换时----->设置---->AVI视频设置- ...