Magic Door

题目大意

有一个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 - 哈希 + 二维树状数组的更多相关文章

  1. 2018牛客网暑期ACM多校训练营(第二场) J - farm - [随机数哈希+二维树状数组]

    题目链接:https://www.nowcoder.com/acm/contest/140/J 时间限制:C/C++ 4秒,其他语言8秒 空间限制:C/C++ 262144K,其他语言524288K ...

  2. 二维树状数组 BZOJ 1452 [JSOI2009]Count

    题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...

  3. HDU1559 最大子矩阵 (二维树状数组)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others)  ...

  4. POJMatrix(二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 22058   Accepted: 8219 Descripti ...

  5. poj 1195:Mobile phones(二维树状数组,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14489   Accepted: 6735 De ...

  6. 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 ...

  7. POJ 2155 Matrix(二维树状数组+区间更新单点求和)

    题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...

  8. [poj2155]Matrix(二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 25004   Accepted: 9261 Descripti ...

  9. POJ 2155 Matrix (二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17224   Accepted: 6460 Descripti ...

随机推荐

  1. ospp.vbs是什么文件?激活过程cscript ospp.vbs命令详解

    ospp.vbs是什么文件?激活过程cscript ospp.vbs命令详解 在Office 2013激活过程中我们经常会用到cscript ospp.vbs这个命令.那么,很有必要来了解一下,osp ...

  2. 【例题 7-2 UVA - 11059】Maximum Product

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] C语言for循环练习题 [代码] /* 1.Shoud it use long long ? 2.Have you ever tes ...

  3. [React] Animate your user interface in React with styled-components and "keyframes"

    In this lesson, we learn how to handle CSS keyframe animations in styled-components, via the 'keyfra ...

  4. Keepalived + Mysql 双主

    VIP 192.168.1.41 Master 192.168.1.42 Slave 192.168.1.43 .配置 yum -y install mysql-server chkconfig -- ...

  5. 毕业两年做到测试经理的经历总结- 各个端的自动化,性能测试结合项目具体场景实战,分析客户反馈的Bug

    前言 最近看到行业的前辈都分享一些过往的经历来指导我们这些测试人员,我很尊敬我们的行业前辈,没有他们在前面铺路,如今我们这帮年轻的测试人估计还在碰壁或摸着石头过河,结合前辈们的经验,作为年轻的测试人也 ...

  6. uva 10710 - Chinese Shuffle(完美洗牌)

    option=com_onlinejudge&Itemid=8&category=474&page=show_problem&problem=1651"> ...

  7. 通过量产解决U盘写保护,无法格式化问题

    1.首先下载ChipGenius.地址:http://pan.baidu.com/s/1eQvf1zc 2.解压,双击ChipGenius_v4_00_0027_pre2. 3.能够检測到U盘的主控厂 ...

  8. linux安装anaconda

    打开网址:https://repo.continuum.io/archive/ 下载对应版本: 然后把下载的文件放到linux系统上 在终端执行: bash Anaconda3-5.1.0-Linux ...

  9. REGEXP_LIKE,REGEXP_INSTR,REGEXP_SUBSTR,REGEXP_REPLACE

    参考: http://www.cnblogs.com/scottckt/archive/2012/10/11/2719562.html http://www.jb51.net/article/3842 ...

  10. hcharts实现堆叠柱形图

    <!DOCTYPE > <html> <head> <meta charset="utf-8"><link rel=" ...