HDU - 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
Hint
In the picture,the red solid rectangles mean the monitor Xiaoteng installed, and the blue dotted rectangles mean the area will be stolen.
题意:
给定p个已监视区域,q个询问区域,判断询问区域内有无未被监视区域
思路:
看了题解才知道这是个二维差分orz
先来学习一下二维差分:https://www.cnblogs.com/LMCC1108/p/10753451.html
对于二维数组a[n][m],其二维前缀和为sum[i][j] = a[i][j] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
则(x1, y1), (x2, y2)区域内的值为:sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1];
本题开二维数组会爆内存,所以使用变长数组vector
在判断是否全覆盖时,求出(x1, y1)(x2, y2)区域的值,判断是否等于给定区域面积
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 1e7 + 20;
int main()
{
int n, m, p, q, x1, y1, x2, y2;
while(~scanf("%d%d", &n, &m))
{
vector< vector<int> >a(n + 2, vector<int>(m + 2, 0));
scanf("%d", &p);
while(p--)
{
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
a[x1][y1]++;
a[x2 + 1][y2 + 1]++;
a[x1][y2 + 1]--;
a[x2 + 1][y1]--;
}
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= m; ++j)
{
a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
}
}
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= m; ++j)
{
if(a[i][j])
a[i][j] = 1;
a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
}
}
scanf("%d", &q);
while(q--)
{
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
ll s;
s = (x2 - x1 + 1) * (y2 - y1 + 1);
if(s == a[x2][y2] - a[x1 - 1][y2] - a[x2][y1 - 1] + a[x1 - 1][y1 - 1])
cout<<"YES"<<'\n';
else
cout<<"NO"<<'\n';
}
}
return 0;
}