Monitor
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 163840/163840 K (Java/Others)
Total Submission(s): 1076 Accepted Submission(s): 336
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.
Source
解题思路
先利用差分维护区间,然后前缀和求出每个点覆盖的次数,再遍历将覆盖次数大于1的点的次数改为1后,求一次前缀和(此时变为前缀的覆盖点数)。对于每个询问,只需求询问区间的覆盖点数,比较覆盖点数是否等于这个区间的总点数,若等于则完全覆盖。
代码如下
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> vec;
vector<int> v;
int main()
{
int n, m;
std::ios::sync_with_stdio(false);
while(cin >> n >> m){
for(int j = 0; j <= m + 1; j ++)
v.push_back(0);
for(int i = 0; i <= n + 1; i ++)
vec.push_back(v);
int p;
cin >> p;
for(int i = 1; i <= p; i ++){
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
vec[x1][y1] ++;
vec[x2 + 1][y2 + 1] ++;
vec[x2 + 1][y1] --;
vec[x1][y2 + 1] --;
}
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= m; j ++)
vec[i][j] = vec[i - 1][j] + vec[i][j - 1] - vec[i - 1][j - 1] + vec[i][j];
}
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= m; j ++){
if(vec[i][j] > 1)
vec[i][j] = 1;
}
}
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= m; j ++)
vec[i][j] = vec[i - 1][j] + vec[i][j - 1] - vec[i - 1][j - 1] + vec[i][j];
}
int q;
cin >> q;
for(int i = 1; i <= q; i ++){
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
if((x2 - x1 + 1) * (y2 - y1 + 1) == vec[x2][y2] - vec[x1 - 1][y2] - vec[x2][y1 - 1] + vec[x1 - 1][y1 - 1]){
cout << "YES\n";
}
else
cout << "NO\n";
}
for(int i = 0; i <= n; i ++)
vec.clear();
v.clear();
}
return 0;
}