Monitor
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 163840/163840 K (Java/Others)
Total Submission(s): 192 Accepted Submission(s): 47
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
题意:
给你n和m,表示有一个n*m的矩阵(n*m<=1e7),初始全0,接下来一个数p(p<=1e6),接下来p个矩阵的左下角和右上角的坐标。
把这位于这些矩阵内的格子置为1,再接下来一个数q(1<=1e6),接下来q个矩阵的左下角和右上角的坐标,对于每个矩阵,如果它包含的每个格子都是1,则输出YES,否则输出NO。
思路:
经典的二维前缀和。。。
用前缀和的方法计算出每个格子是否被覆盖(p个矩阵都进行二维前缀和的加1更新操作,然后统计一遍前缀和即可得出每个格子是否被覆盖,大于等于1即为被覆盖)
然后把被覆盖的格子都置为1,重新统计一遍前缀和(这时候前缀和就表示矩形(1,1,x,y)包含的被覆盖的格子数了)。方便我们接下来的判断。
然后对于q个矩阵,直接查询(x,y,xx,yy)的被覆盖的面积是否等于(xx-x+1)*(yy-y+1)即可。。。
代码:
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
using namespace std;
const int maxn=10000010;
int n,m,k,p,q;
int a[maxn];
int getid(int i,int j)
{
if(i==0||j==0||i>n||j>m) return 0;
return (i-1)*m+j;
}
void add(int i,int j,int v)
{
if(getid(i,j)==0) return;
a[getid(i,j)]+=v;
}
int query(int i,int j)
{
return a[getid(i,j)];
}
int main()
{
int T,cas=1;
while(scanf("%d%d",&n,&m)!=EOF){
rep(i,1,n)
rep(j,1,m) a[getid(i,j)]=0;
scanf("%d",&p);
while(p--)
{
int x,y,xx,yy;
scanf("%d%d%d%d",&x,&y,&xx,&yy);
add(x,y,1);
add(xx+1,yy+1,1);
add(xx+1,y,-1);
add(x,yy+1,-1);
}
rep(i,1,n)
rep(j,1,m)
{
a[getid(i,j)]+=query(i-1,j)+query(i,j-1)-query(i-1,j-1);
//cout<<a[getid(i,j)]<<" ";
//if(j==m) cout<<endl;
}
rep(i,1,n)
rep(j,1,m)
a[getid(i,j)]=(a[getid(i,j)]>0);
rep(i,1,n)
rep(j,1,m)
a[getid(i,j)]+=query(i-1,j)+query(i,j-1)-query(i-1,j-1);
scanf("%d",&q);
while(q--)
{
int x,y,xx,yy;
scanf("%d%d%d%d",&x,&y,&xx,&yy);
int ans=query(xx,yy)+query(x-1,y-1)-query(x-1,yy)-query(xx,y-1);
//cout<<ans<<" "<<(xx-x+1)*(yy-y+1)<<endl;
if(ans==(xx-x+1)*(yy-y+1)) puts("YES");
else puts("NO");
}
//printf("%d\n",ans);
}
return 0;
}