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. js进阶 14-1 jquery的ajax系列中的load方法的作用是什么

    js进阶 14-1 jquery的ajax系列中的load方法的作用是什么 一.总结 一句话总结:jQuery load()方法作用是从服务器加载数据,是一个简单但强大的AJAX方法. 1.load函 ...

  2. Notepad++和MinGW的安装和配置

    http://blog.csdn.net/cclovepl/article/details/70568313 http://blog.csdn.net/cclovepl/article/details ...

  3. 解题报告 之 HDU5305 Friends

    解题报告 之 HDU5305 Friends Description There are  people and  pairs of friends. For every pair of friend ...

  4. [array] leetCode-11. Container With Most Water-Medium

    leetCode-11. Container With Most Water-Medium descrition Given n non-negative integers a1, a2, ..., ...

  5. ocx 中使用CImage和CComPtr

    #include <atlimage.h> using namespace ATL;

  6. 【BZOJ 1597】 [Usaco2008 Mar]土地购买

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把这n个土地按照x为第一关键字.y为第二关键字.都升序排. 然后考虑一个土地xi,yi 若有一个土地的x<=xi且y<= ...

  7. ios开发之核心动画四:核心动画-Core Animation--CABasicAnimation基础核心动画

    #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutl ...

  8. Loader之一:基本原理 分类: H1_ANDROID 2013-11-16 10:29 1923人阅读 评论(0) 收藏

    参考APIDEMO及http://developer.android.com/guide/components/loaders.html#app 1.Introduced in Android 3.0 ...

  9. 三大主流ETL工具选型 分类: H2_ORACLE 2013-08-23 11:17 426人阅读 评论(0) 收藏

    ETL(extract, transform and load)产品乍看起来似乎并不起眼,单就此项技术本身而言,几乎也没什么特别深奥之处,但是在实际项目中,却常常在这个环节耗费太多的人力,而在后续的维 ...

  10. 设置UIButton的文字显示位置、字体的大小、字体的颜色

    btn.frame = CGRectMake(x, y, width, height); [btn setTitle: @"search" forState: UIControlS ...