Monitor
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 163840/163840 K (Java/Others)
Total Submission(s): 852 Accepted Submission(s): 266
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
分析:题目大意为有一个n*m的矩阵,给出p次区间修改,q次查询,询问每次区间是否能够被前面的p次修改后的覆盖区间完全覆盖,如果能输出YES,否则为NO。
由于所给出的修改次数和查询次数庞大,所以要实现几乎线性的修改和查询,所以可以利用维护差分数组来得到修改后的结果,然后对结果进行处理,得到原数组,再由原数组的区间和来进行完全覆盖判断。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX = 1e7 + 4000;
const ll mod = 1e9+7;
int a[MAX],pa[MAX],psum[MAX]; //由于题目所给的只有n*m的范围,而没有具体的n和m的范围
int n,m; //所以直接定义二维数组无法定义,故而用一维数组来进行模拟
int fixid(int x,int y) //得到该点在数组中的实际位置
{
if(x<1||x>n||y<1||y>m)return 0;
else{
return (x-1)*m+y;
}
}
int main()
{
int p,q,x1,y1,x2,y2;
int x,y;
while(~scanf("%d %d",&n,&m)){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
a[fixid(i,j)]=0;
pa[fixid(i,j)]=0;
psum[fixid(i,j)]=0;
}
}
scanf("%d",&p);
for(int i=1;i<=p;i++){
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
if(x1>x2)swap(x1,x2);
if(y1>y2)swap(y1,y2);
a[fixid(x1,y1)]++;
a[fixid(x2+1,y2+1)]++;
a[fixid(x1,y2+1)]--;
a[fixid(x2+1,y1)]--;
}
a[0]=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
pa[fixid(i,j)]=pa[fixid(i-1,j)]+pa[fixid(i,j-1)]-pa[fixid(i-1,j-1)]+a[fixid(i,j)];
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(pa[fixid(i,j)]>0)pa[fixid(i,j)]=1;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
psum[fixid(i,j)]=psum[fixid(i-1,j)]+psum[fixid(i,j-1)]-psum[fixid(i-1,j-1)]+pa[fixid(i,j)];
}
}
scanf("%d",&q);
for(int i=1;i<=q;i++){
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
if(x1>x2)swap(x1,x2);
if(y1>y2)swap(y1,y2);
if(psum[fixid(x2,y2)]-psum[fixid(x2,y1-1)]-psum[fixid(x1-1,y2)]+psum[fixid(x1-1,y1-1)]==(x2-x1+1)*(y2-y1+1)){
printf("YES\n");
}else{
printf("NO\n");
}
}
}
return 0;
}