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. Makefile的问题

    注意包含路径的变量,可能会带有空格而使得运行失败 直接定义:DEBUG  = false后面引用需要使用 $(DEBUG) 或者set  DEBUG  $(DEBUG)之后就可以直接引用了

  2. python课程:python3的数字与字符串

    一下是基于python2的教程的 python中有 多个数据类型,和,两种字符串类型 他们都是不可变的.

  3. liunx基本操作常用命令

    liunx通常用作服务器,运行服务器软件,服务器要等待,类似超市学关键命令操作 内核,外壳 shell命令跟内核打交道用的是发行版本,不是内核,Radhat公司的CentOS,阿里巴巴也用这个 liu ...

  4. 【Codeforces Round #443 (Div. 2) A】Borya's Diagnosis

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟 [代码] #include <bits/stdc++.h> using namespace std; const ...

  5. 【Struts2三】拦截器

    拦截器:就是在訪问action之前.对其进行拦截!能够在拦截器中做一些逻辑的处理! 比方权限验证.没有权限就不给予訪问! 拦截器等效于servlet中的过滤器! 使用拦截器步骤: 1.定义自己的拦截器 ...

  6. php课程 8-32 如何使用gd库进行图片裁剪和缩放

    php课程 8-32 如何使用gd库进行图片裁剪和缩放 一.总结 一句话总结:图片缩放到图片裁剪就是改变原图截取的位置以及截取的宽高. 1.电商网站那么多的图片,如果全部加载卡得慢的很,所以他们是怎么 ...

  7. RTC时钟和BKP的配置stm32

    摘自:https://blog.csdn.net/gtkknd/article/details/52233605 RTC和后备寄存器通过一个开关供电,在VDD有效的时候选择VDD供电,否则选择VBAT ...

  8. linux中关闭程序或进程

  9. UIScrollView(滚动试图)

    UIScrollView(滚动试图) 1.简介 为什么有UISCrollView: 在iOS开发中,由于移动设备的屏幕大小有限,所以不能像PC一样显示很多内容,因此当手机屏幕需要展示的内容较多超出一个 ...

  10. [WASM] Call a JavaScript Function from WebAssembly

    Using WASM Fiddle, we show how to write a simple number logger function that calls a consoleLog func ...