Monitor
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 163840/163840 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
Problem Description
Xiaoteng has a large area of land for growing crops, and the land can be seen as a rectangle of n×m.
But recently Xiaoteng found that his crops were often stolen by a group of people, so he decided to install some monitors to find all the people and then negotiate with them.
However, Xiao Teng bought bad monitors, each monitor can only monitor the crops inside a rectangle. There are p monitors installed by Xiaoteng, and the rectangle monitored by each monitor is known.
Xiao Teng guess that the thieves would also steal q times of crops. he also guessed the range they were going to steal, which was also a rectangle. Xiao Teng wants to know if his monitors can see all the thieves at a time.
Input
There are mutiple test cases.
Each case starts with a line containing two integers n,m(1≤n,1≤m,n×m≤107) which represent the area of the land.
And the secend line contain a integer p(1≤p≤106) which represent the number of the monitor Xiaoteng has installed. This is followed by p lines each describing a rectangle. Each of these lines contains four intergers x1,y1,x2 and y2(1≤x1≤x2≤n,1≤y1≤y2≤m) ,meaning the lower left corner and upper right corner of the rectangle.
Next line contain a integer q(1≤q≤106) which represent the number of times that thieves will steal the crops.This is followed by q lines each describing a rectangle. Each of these lines contains four intergers x1,y1,x2 and y2(1≤x1≤x2≤n,1≤y1≤y2≤m),meaning the lower left corner and upper right corner of the rectangle.
Output
For each case you should print q lines.
Each line containing YES or NO mean the all thieves whether can be seen.
Sample Input
6 6 3 2 2 4 4 3 3 5 6 5 1 6 2 2 3 2 5 4 1 5 6 5
Sample Output
YES NO
Hint
In the picture,the red solid rectangles mean the monitor Xiaoteng installed, and the blue dotted rectangles mean the area will be stolen.
题意: 有p个红色框框, q个蓝色框框, 对于每个蓝色框框, 是不是全部被红色框框覆盖.
思路:
二维差分处理p个红色框框, 一次二维前缀和得到每个范围内被覆盖点的个数, 判断点的个数是不是与范围内的面积相等即可.
一维差分:
给你一个数组a[1] ~ a[n],有Q个操作,每一操作给定 l , r , x 表示 [l, r] 区间所有的数都加上 x, 让你求最后a[1] ~ a[n] 最后各自是多少,要求用 O(n) 复杂度 完成。
设定一个差分数组C[] , 对于每一次操作 c[l] += x , c[r + 1] -= x. 最后对C 做一遍前缀和。C[i] 最后就得到 a[i] 这个数变化了多少。
a[1] ~ a[n] 最后各自就是 a[i] + C[i].
二维差分:
和一维差分差不多, 每一次给定 左上角 (x1,y1),右下角 (x2,y1),x
每次操作 C[x1][y1] += x , C[x2 + 1][y2 + 1] += x , C[x1][y2 + 1] -= x , C[x2 + 1][y1] -= x;
然后做一遍二维前缀和。
然后在二维差分的数组上进行一次二维前缀和:
二维前缀和:
求二维前缀和: a[i][j]=a[i][j]+a[i][j-1]+a[i-1][j]-a[i-1][j-1]
用二维前缀和: x1,y1,x2,y2 a[x2][y2]-a[x1-1][y2]-a[x2][y1-1]+a[x1-1][y1-1]
代码:
#include<bits/stdc++.h>
#define fuck(x) std::cout<<"["<<#x<<"->"<<x<<"]"<<endl;
using namespace std;
typedef long long ll;
const int M=1e6+5;
const int inf=1e9+5;
int n,m;
struct node {
int x1,x2,y1,y2;
} blue[M];
int main() {
while(~scanf("%d%d",&n,&m)) {
int flag=0;
if(n<m) {
swap(n,m);
flag=1;
}
//开空间大法
int (* a)[3500] = new int[n+5][3500];///差分数组
memset(a,0,sizeof(a));
int p,q;
scanf("%d",&p);
if(flag==0) {
for(int i=0; i<p; i++) {
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
++ a[x1][y1];
++ a[x2+1][y2+1];
-- a[x1][y2+1];
-- a[x2+1][y1];
}
scanf("%d",&q);
for(int i=0; i<q; i++) {
scanf("%d%d%d%d",&blue[i].x1,&blue[i].y1,&blue[i].x2,&blue[i].y2);
}
} else {
for(int i=0; i<p; i++) {
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
swap(x1,y1);
swap(x2,y2);
++ a[x1][y1];
++ a[x2+1][y2+1];
-- a[x1][y2+1];
-- a[x2+1][y1];
}
scanf("%d",&q);
for(int i=0; i<q; i++) {
scanf("%d%d%d%d",&blue[i].x1,&blue[i].y1,&blue[i].x2,&blue[i].y2);
swap(blue[i].x1,blue[i].y1);
swap(blue[i].x2,blue[i].y2);
}
}
//二维差分's 前缀和
for(int i = 1; i <= n + 1; ++ i)
for(int j = 1; j <= m + 1; ++ j) {
a[i][j] = a[i][j] + a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
}
//覆盖过的格子赋成1
for(int i = 1; i <= n; ++ i)
for(int j = 1; j <= m; ++ j) {
if(a[i][j] >= 1)
a[i][j] = 1;
}
//前缀和得出蓝框内被覆盖格子的个数
for(int i = 1; i <= n; ++ i)
for(int j = 1; j <= m; ++ j) {
a[i][j] = a[i][j] + a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
}
for(int i = 0; i < q; ++ i) {
int tmp = a[blue[i].x2][blue[i].y2] + a[blue[i].x1 - 1][blue[i].y1 - 1] - a[blue[i].x1 - 1][blue[i].y2] - a[blue[i].x2][blue[i].y1 - 1];
if(tmp == (blue[i].x2 - blue[i].x1 + 1) * (blue[i].y2 - blue[i].y1 + 1))
puts("YES");
else
puts("NO");
}
}
return 0;
}