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.
题意:n*m<=1e7 的矩阵 给p个红框范围(左上角+右下角)表示监控器覆盖的范围,给q个蓝框范围(左上角+右下角)表示询问(蓝框内是否全被监控器覆盖)
思路:二维前缀和+二维差分
先求出监控器覆盖范围的差分数组,对差分数组求前缀和,处理重叠部分,加上原来的矩阵(这里原来矩阵为全零所以不用加),在求一次前缀和就行了
参考:https://www.cnblogs.com/hua-dong/p/10738963.html
https://blog.csdn.net/K_R_forever/article/details/81775899
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 1e7 + 7; //数组最大
//n*m <= 1e7 二维转一维
vector<int> v;
int n, m;
void add(int i, int j, int k) {
if (i > n || j > m) return;
v[(i-1)*m + j] += k;
}
int query(int i, int j) {
if (i < 1 || j < 1) return 0;
return v[(i-1)*m+j];
}
int main() {
while (~scanf ("%d %d", &n, &m)) {
v.resize(n*m+7); //!!!
//v.assign(n*m+6, 0);
v.clear();
int p, x1, x2, y1, y2;
scanf ("%d", &p);
for (int i = 1; i <= p; ++i) {
scanf ("%d %d %d %d", &x1, &y1, &x2, &y2);
//差分数组
add(x1, y1, 1);
add(x1, y2 + 1, -1);
add(x2 + 1, y1, -1);
add(x2 + 1, y2 + 1, 1);
}
for (int i = 1; i <= n; ++i) { //对差分数组求前缀和
for (int j = 1; j <= m; ++j) {
v[(i-1)*m + j] += (query(i-1, j) + query(i, j-1) - query(i-1, j-1));
}
}
//因为a数组初始值全为零所以a的后缀数组就是差分数组
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (v[(i-1)*m + j] != 0) v[(i-1)*m + j] = 1; //处理重叠部分
}
}
for (int i = 1; i <= n; ++i) { //对前缀数组求前缀和
for (int j = 1; j <= m; ++j) {
v[(i-1)*m + j] += (query(i-1, j) + query(i, j-1) - query(i-1, j-1));
}
}
int q;
scanf ("%d", &q);
while (q--) {
scanf ("%d %d %d %d", &x1, &y1, &x2, &y2);
int sum = query(x2, y2) - query(x1-1, y2) - query(x2, y1-1) + query(x1-1, y1-1);
if (sum == (x2 - x1 + 1) * (y2 - y1 + 1)) puts("YES");
else puts("NO");
}
}
return 0;
}