Codeforces Round #439 (Div. 2) Problem E (Codeforces 869E) - 暴力 - 随机化 - 二维树状数组 - 差分

时间:2022-09-03 12:42:05

Adieu l'ami.

Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around the abandoned Eikou Cram School building, Oshino's makeshift residence.

The space is represented by a rectangular grid of n × m cells, arranged into n rows and m columns. The c-th cell in the r-th row is denoted by (r, c).

Oshino places and removes barriers around rectangular areas of cells. Specifically, an action denoted by "1 rcrc2" means Oshino's placing barriers around a rectangle with two corners being (r1, c1) and (r2, c2) and sides parallel to squares sides. Similarly, "2 rcrc2" means Oshino's removing barriers around the rectangle. Oshino ensures that no barriers staying on the ground share any common points, nor do they intersect with boundaries of the n × m area.

Sometimes Koyomi tries to walk from one cell to another carefully without striding over barriers, in order to avoid damaging various items on the ground. "3 rcrc2" means that Koyomi tries to walk from (r1, c1) to (r2, c2) without crossing barriers.

And you're here to tell Koyomi the feasibility of each of his attempts.

Input

The first line of input contains three space-separated integers nm and q (1 ≤ n, m ≤ 2 500, 1 ≤ q ≤ 100 000) — the number of rows and columns in the grid, and the total number of Oshino and Koyomi's actions, respectively.

The following q lines each describes an action, containing five space-separated integers tr1, c1, r2, c2 (1 ≤ t ≤ 3, 1 ≤ r1, r2 ≤ n, 1 ≤ c1, c2 ≤ m) — the type and two coordinates of an action. Additionally, the following holds depending on the value of t:

  • If t = 1: 2 ≤ r1 ≤ r2 ≤ n - 1, 2 ≤ c1 ≤ c2 ≤ m - 1;
  • If t = 2: 2 ≤ r1 ≤ r2 ≤ n - 1, 2 ≤ c1 ≤ c2 ≤ m - 1, the specified group of barriers exist on the ground before the removal.
  • If t = 3: no extra restrictions.
Output

For each of Koyomi's attempts (actions with t = 3), output one line — containing "Yes" (without quotes) if it's feasible, and "No" (without quotes) otherwise.

Examples
input
5 6 5
1 2 2 4 5
1 3 3 3 3
3 4 4 1 1
2 2 2 4 5
3 1 1 4 4
output
No
Yes
input
2500 2500 8
1 549 1279 1263 2189
1 303 795 1888 2432
1 2227 622 2418 1161
3 771 2492 1335 1433
1 2017 2100 2408 2160
3 48 60 798 729
1 347 708 1868 792
3 1940 2080 377 1546
output
No
Yes
No
Note

For the first example, the situations of Koyomi's actions are illustrated below.

Codeforces Round #439 (Div. 2) Problem E (Codeforces 869E) - 暴力 - 随机化 - 二维树状数组 - 差分


  题目大意 在一个m×n的方格板上,操作1将一个矩形区域的边界上加上一圈障碍,操作2将一个矩形区域的边界上的障碍移除,操作3询问两点是否能不越过障碍互相到达。题目保证任意两圈矩形障碍不会有公共点(边界上)。

  题目可以看成每次从平面中分割出一个小平面或者将一个平面复原,然后询问两个点是否在同一平面内。

  于是常用套路打标记。

Solution 1 暴力差分

  每次修改操作暴力每行进行差分。

  当然这个差分得稍微改一下,前者用一个独一无二的正数,后者用一个负数。

  查询的时候进行由那一行首往后扫描,如果遇到正数就压入栈,遇到负数就弹处栈顶。

  最终栈顶就是这个点所在平面的标号。由于开始是在最外面的平面,所以首先在栈中加入一个元素。

  然后判断一下两个点是否在同一平面就行了。

  如果代码比较丑,请卡常数。

  (最坏的时间复杂度是O(nq),虽然觉得不可思议,但它就是过了。。)

Code

 /**
* Codeforces
* Problem#869E
* Accepted
* Time: 1934ms
* Memory: 24600k
*/
#include <bits/stdc++.h>
using namespace std; int n, m, q;
int top = ;
int sta[];
int mp[][]; inline void init() {
scanf("%d%d%d", &n, &m, &q);
} int getSign(int x, int y) {
top = ;
sta[] = ;
for(int i = ; i <= y; i++) {
if(mp[x][i] > )
sta[++top] = mp[x][i];
else if(mp[x][i] < )
top--;
}
return sta[top];
} inline void solve() {
int opt, x1, y1, x2, y2;
while(q--) {
scanf("%d%d%d%d%d", &opt, &x1, &y1, &x2, &y2);
switch(opt) {
case :
for(int i = x1; i <= x2; i++)
mp[i][y1] = q, mp[i][y2 + ] = -;
break;
case :
for(int i = x1; i <= x2; i++)
mp[i][y1] = , mp[i][y2 + ] = ;
break;
case :
top = ;
puts((getSign(x1, y1) == getSign(x2, y2)) ? ("Yes") : ("No"));
break;
}
}
} int main() {
init();
solve();
return ;
}

The Untended Antiquity(Brute force)

Solution 2 随机化标号

  思想仍然是差分,这次思考能不能想普通的差分一样做,可以扔进某个数据结构维护前缀和。

  是不是可以前面 + 标号,右边的后一个 - 标号?很遗憾这么做是错误的。

  比如一处+1 +2,而另一处是+3,显然它们不在一个平面内,但是这个算法会认为它们在一个平面内。

  那怎么办呢?rand啊!没人知道它的标号是什么,可以通过两次rand得到一个30位的数,这样几乎不可能被卡掉。

Code

 /**
* Codeforces
* Problem#869E
* Accepted
* Time: 109ms
* Memory: 49300k
*/
#include <bits/stdc++.h>
using namespace std;
//#define lowbit(_x) (_x & (-_x))
#define ll long long
#define pii pair<int, int> int n, m, q;
int top = ;
ll f[][];
map< pair< pii, pii >, int> g; inline int lowbit(int x) {
return x & -x;
} inline void init() {
scanf("%d%d%d", &n, &m, &q);
} inline int rd() {
return rand() << | rand();
} inline void add(int x, int y, int val) {
for(; x <= n + ; x += lowbit(x))
for(int j = y; j <= m + ; j += lowbit(j))
f[x][j] += val;
} inline ll getSum(int x, int y) {
ll rt = ;
for(; x; x -= lowbit(x))
for(int j = y; j; j -= lowbit(j))
rt += f[x][j];
return rt;
} template<typename V, typename K>
pair<V, K> makepair(V a, K b) {
return pair<V, K>(a, b);
} inline void solve() {
srand((unsigned) time (NULL));
for(int i = ; i <= ; i++)
srand(rd());
int opt, x1, y1, x2, y2, v;
while(q--) {
scanf("%d%d%d%d%d", &opt, &x1, &y1, &x2, &y2);
switch(opt) {
case :
v = rd();
g[makepair(pii(x1, y1), pii(x2, y2))] = v;
add(x2 + , y2 + , v), add(x1, y1, v);
add(x1, y2 + , -v), add(x2 + , y1, -v);
break;
case :
v = g[makepair(pii(x1, y1), pii(x2, y2))];
add(x2 + , y2 + , -v), add(x1, y1, -v);
add(x1, y2 + , v), add(x2 + , y1, v);
break;
case :
// cerr << getSum(x1, y1) << " " << getSum(x2, y2) << endl;
puts((getSum(x1, y1) == getSum(x2, y2)) ? ("Yes") : ("No"));
break;
}
}
} int main() {
init();
solve();
return ;
}

Codeforces Round #439 (Div. 2) Problem E (Codeforces 869E) - 暴力 - 随机化 - 二维树状数组 - 差分的更多相关文章

  1. Codeforces Round &num;439 &lpar;Div&period; 2&rpar; Problem C &lpar;Codeforces 869C&rpar; - 组合数学

    — This is not playing but duty as allies of justice, Nii-chan! — Not allies but justice itself, Onii ...

  2. Codeforces Round &num;439 &lpar;Div&period; 2&rpar; Problem B &lpar;Codeforces 869B&rpar;

    Even if the world is full of counterfeits, I still regard it as wonderful. Pile up herbs and incense ...

  3. Codeforces Round &num;439 &lpar;Div&period; 2&rpar; Problem A &lpar;Codeforces 869A&rpar; - 暴力

    Rock... Paper! After Karen have found the deterministic winning (losing?) strategy for rock-paper-sc ...

  4. Codeforces Round &num;198 &lpar;Div&period; 1&rpar; D&period; Iahub and Xors 二维树状数组&ast;

    D. Iahub and Xors   Iahub does not like background stories, so he'll tell you exactly what this prob ...

  5. Codeforces 707 E&period; Garlands &lpar;二维树状数组&rpar;

    题目链接:http://codeforces.com/problemset/problem/707/E 给你nxm的网格,有k条链,每条链上有len个节点,每个节点有一个值. 有q个操作,操作ask问 ...

  6. Codeforces Round &num;368 &lpar;Div&period; 2&rpar; E&period; Garlands 二维树状数组 暴力

    E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...

  7. Codeforces Round &num;369 &lpar;Div&period; 2&rpar; A&period; Bus to Udayland【字符串&sol;二维字符数组求连起来的座位并改为其他字符】

    A. Bus to Udayland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. HDU5465&sol;BestCoder Round &num;56 &lpar;div&period;2&rpar; 二维树状数组

    Clarke and puzzle 问题描述 克拉克是一名人格分裂患者.某一天,有两个克拉克(aa和bb)在玩一个方格游戏. 这个方格是一个n*mn∗m的矩阵,每个格子里有一个数c_{i, j}c​i ...

  9. Codeforces &num;590 D 二维树状数组

    题意 给一个10^5之内的字符串(小写字母)时限2s 输入n,有n个操作  (n<10^5) 当操作是1的时候,输入位置x和改变的字母 当操作是2的时候,输入区间l和r,有多少不同的字母 思路 ...

随机推荐

  1. HTML中id、name、class 区别

    参考:http://www.cnblogs.com/polk6/archive////.html http://blog.csdn.net/ithomer/article/details/ HTML ...

  2. &lpar;转&rpar;解决Android SDK Manager无法更新或下载太慢问题

    原帖地址:http://blog.csdn.net/exlsunshine/article/details/22208857 天朝的网络...哎~真是无语...还好最近装了谷歌的chrome浏览器+红 ...

  3. linux中shell变量&dollar;&num;&comma;&dollar;&commat;&comma;&dollar;0&comma;&dollar;1&comma;&dollar;2的含义解释

    linux中shell变量$#,$@,$0,$1,$2的含义解释 linux中shell变量$#,$@,$0,$1,$2的含义解释:  变量说明:  $$  Shell本身的PID(ProcessID ...

  4. Android应用开发基础篇(6)-----Service

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/20/2360336.html 一.概述 我们知道,Service是Android的四大组件之一. ...

  5. 抽象类 abstract 和 接口 interface 类的区别

    在看一些框架的优秀改良时,都会设计一层base层,并且 base里面一般都是 abstract 类,然后 就找了为什么做的原因.发现: PHP5支持抽象类和抽象方法.抽象类不能直接被实例化,你必须先继 ...

  6. Selenium高级篇Web自动化测试框架

    现在常用的是对象模型PO(Page Object), 从过去要知道具体的定位,返回使用现在只需要知道所在页面的名称即可访问页面对象即可看到该页面的元素 PageObject实现了对页面对象及方法的抽离 ...

  7. CListCtrl颜色设置

    动态改变listctrl 单元格背景及文字颜色 m_listshow.InsertColumn( 0, "ID", LVCFMT_LEFT, 40 );//插入列 m_listsh ...

  8. js&comma;JQuery实现&comma;带筛选&comma;搜索的select

    代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title& ...

  9. NGUI和UGUI图片字 艺术字&lpar;Bitmap图片转文字&rpar;制作方法

    用图片字而不是图片 美术和程序的配合,需要程序能够很快抓住问题重点并提出解决方案.美术出的图片字比我们使用的字体更好好看,那么是否要一个个图片去拼成数字呢? NGUI创建图片字 准备材料 美术提供的数 ...

  10. PowerBI开发 第十一篇:报表设计技巧(更新)

    PowerBI版本在持续的更新,这使得报表设计能够实现更多新的功能,您可以访问 PowerBI Blog查看PowerBI的最新更新信息,本文总结了PowerBI新版本的重要更新和设计技巧. 我的Po ...