hdoj 6514 Monitor
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
Solution
二维区间修改,二维区间查询
先二维差分,再做一遍前缀和求出每个位置是否为1(大于0即为1),再求一次前缀和,然后区间查询判断格子里1的个数是否和格子面积一样即可。
技巧:N*M<=10000000,无法判断N 和 M的大小,二维数组存不下。
故二维转一维 (x,y) --> (x - 1) * m + y 即可存储。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int INF = 1e9 + 7;
const int SZ = 10000000 + 20;
int num[SZ];
int n,m,p,q;
int x1,x2,y1,y2;
inline int query(int x,int y)
{
if(x <= 0 || y <= 0) return 0;
else return num[(x - 1) * m + y];
}
int main()
{
while(scanf("%d%d",&n,&m) == 2)
{
memset(num,0,sizeof(num));
scanf("%d",&p);
while(p -- )
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
num[(x1 - 1) * m + y1] += 1;
if(x2 + 1 <= n && y2 + 1 <= m) num[x2 * m + y2 + 1] += 1;
if(y2 + 1 <= m ) num[(x1 - 1)* m + y2 + 1] -= 1;
if(x2 + 1 <= n) num[x2 * m + y1] -= 1;
}
scanf("%d",&q);
for(int i = 1;i <= n;i ++ )
for(int j = 1;j <= m;j ++ )
{
num[(i - 1) * m + j] += query(i - 1,j) + query(i,j - 1) - query(i - 1,j - 1);
}
for(int i = 1 ;i <= n * m;i ++ )
if(num[i]) num[i] = 1;
for(int i = 1;i <= n;i ++ )
for(int j = 1;j <= m;j ++ )
{
num[(i - 1) * m + j] += query(i - 1,j) + query(i,j - 1) - query(i - 1,j - 1);
}
while(q -- )
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(query(x2,y2)+query(x1-1,y1-1)-query(x1-1,y2)-query(x2,y1-1) == ((x2 - x1 + 1) * (y2 - y1 + 1)))
printf("YES\n");
else printf("NO\n");
}
}
return 0;
}
2020.4.2