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 r1 c1 r2 c2” 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 r1 c1 r2 c2” 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 r1 c1 r2 c2” 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 n, m 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 t, r1, 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.
Example
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.
题意:
题意:在 n×m 的二维图上,有三种操作:
1 r1 c1 r2 c2 表示沿着 (r1, c1, r2, c2) 所表示的矩形的外边框建围墙。(其中 (r1, c1) 为矩形左上角,(r2, c2) 表示矩形右下角)。
2 r1 c1 r2 c2 表示取消 (r1, c1, r2, c2) 所示矩形的围墙。(保证最初图不存在围墙,删除的围墙一定是之前通过操作 1 建立的)。
3 r1 c1 r2 c2 表示询问 (r1, c1) 到 (r2, c2) 是否可以不翻越围墙到达。对于每个操作 3,可以输出 Yes ,否则输出 No 。
(n, m <= 2500, q <= 1e5, 保证围墙没有相交)
分析: 我们将每个矩形区域都加上自身独特的值,这样的话,对于两个点,如果值不相同,则不是一个区域。(就相当于矩形区域覆盖,当覆盖了之后 当前的值为 自身特殊值+其下方被覆盖的区域的值,但是同一个区域中的值都会加那么多,值还是会一样。可以画个图很清新)。
如果确保每个矩形区域都有唯一的值,这个时候我们可以将4个坐标hash为一个值(这样就会导致,每个区域加的值就与确定这个区域的坐标 一 一对应),同时这样删除的时候,也可以o(1)得到矩形区域应该给删除多少。
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)
const int MAXN = 2500+10;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const int seed = 2333 ;
#define lowbit(x) x&(-x)
LL hash(int a,int b,int c,int d) { return (LL)a*seed*seed*seed+b*seed*seed+c*seed+d ; }
struct BIT{
int nx,ny;
LL c[MAXN][MAXN];
void add(int x,int y,LL val){
for(int i=x;i<=nx;i+=lowbit(i))
for(int j=y;j<=ny;j+=lowbit(j))
c[i][j]+=val;
}
LL sum(int x,int y){
LL ans=0;
for(int i=x;i>0;i-=lowbit(i))
for(int j=y;j>0;j-=lowbit(j))
ans+=c[i][j];
return ans;
}
} bit;
int main(){
CLOSE();
// fread();
// fwrite();
memset(bit.c,0,sizeof(bit.c));
int q;scanf("%d%d%d",&bit.nx,&bit.ny,&q);
int op;int x1,x2,y1,y2; LL val;
while(q--){
scanf("%d%d%d%d%d",&op,&x1,&y1,&x2,&y2);
val=hash(x1,y1,x2,y2);
if(op==3){
LL a=bit.sum(x1,y1);
LL b=bit.sum(x2,y2);
puts(a==b?"Yes":"No");
continue;
}
if(op==2) val=-val;
y2++;x2++;
bit.add(x1,y1,val);
bit.add(x1,y2,-val);
bit.add(x2,y1,-val);
bit.add(x2,y2,val);
}
return 0;
}