题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6514
题目:
Monitor
Xiaoteng has a large area of land for growing crops, and the land can be seen as a rectangle of n×mn×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 pp monitors installed by Xiaoteng, and the rectangle monitored by each monitor is known.
Xiao Teng guess that the thieves would also steal qq 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)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)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)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)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)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 qq 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
下图是题目给的提示图,红色区域是监控范围,蓝色区域是小偷出现的区域。
题意解析:在一片矩形区域上,有一部分区域是监控可以覆盖到的,这部分区域由一个或多个包含于该矩形区域的小矩形构成;现在给你另一个包含在该矩形区域的小矩形A,问你这个小矩形能否被监控完全覆盖。
具体思路看大佬题解:https://blog.csdn.net/D5__J9/article/details/89428614
我只是在代码上标注一些自己对该题的理解。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
int n,m,p,q;
const int INF=1e7+5;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
vector<vector<int> >v(n + 2, vector<int>(m + 2)), record(n + 2, vector<int>(m + 2));
scanf("%d",&p);
int i,j;
int x1,y1,x2,y2;
while(p--){//构建差分数组
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
record[x1][y1]++;
record[x2+1][y2+1]++;
record[x2+1][y1]--;
record[x1][y2+1]--;
}
for(i=1;i<=n;i++)//差分数组前缀求和
{
for(j=1;j<=m;j++){
record[i][j]+=record[i-1][j]+record[i][j-1]-record[i-1][j-1];
}
}
for(i=1;i<=n;i++)//利用差分数组的前缀和标记监控区域
{
for(j=1;j<=m;j++)
{
v[i][j]=v[i-1][j]+v[i][j-1]-v[i-1][j-1]+(record[i][j]>0?1:0);
}
}
scanf("%d",&q);
while(q--)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
int ans=v[x2][y2]-v[x2][y1-1]-v[x1-1][y2]+v[x1-1][y1-1];
int val=(x2-x1+1)*(y2-y1+1);
if(ans==val){
printf("YES\n");
}
else printf("NO\n");
}
}
return 0;
}