#HDU 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

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的矩阵, 其中有K个监视器,输入K个监视器的监视范围,Q次询问, 每次询问某个范围内是否全部被监视器包含,如果是输出 YES , 否则 输出 NO

思路 :

二维前缀和基础题,先利用差分预处理所有的范围,然后在处理每个方格是否被包含的过程中, 将权值与1取最小值,这样保证每个方格最大权值为1,询问的时候直接判断矩阵的权值是否等于该矩阵面积就好

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

int n, m, t, q;

int main()
{
	while (~sc("%d %d", &n, &m)) {
		vector <vector <int>> e;
		e.resize(n + 3);
		for (int i = 0; i <= n + 1; i++) e[i].resize(m + 3);
		vector <vector <int>> c;
		c.resize(n + 3);
		for (int i = 0; i <= n + 1; i++) c[i].resize(m + 3);
		sc("%d", &t);
		while (t--) {
			int x1, y1, x2, y2;
			sc("%d %d %d %d", &x1, &y1, &x2, &y2);
			e[x1][y1]++, e[x2 + 1][y2 + 1]++;
			e[x2 + 1][y1]--, e[x1][y2 + 1]--;  // 差分
		}
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				e[i][j] += e[i - 1][j] + e[i][j - 1] - e[i - 1][j - 1]; // 每个方格赋值
			}
		}
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				Min(e[i][j], 1);               // 保证权值最大为1
				c[i][j] += c[i - 1][j] + c[i][j - 1] - c[i - 1][j - 1] + e[i][j];
			}
		}
		sc("%d", &q); 
		while (q--) {
			int x1, y1, x2, y2;
			sc("%d %d %d %d", &x1, &y1, &x2, &y2);
			int ans = c[x2][y2] + c[x1 - 1][y1 - 1] - c[x1 - 1][y2] - c[x2][y1 - 1];
			if (ans == (x2 - x1 + 1) * (y2 - y1 + 1)) printf("YES\n");
			else printf("NO\n");
		}
	}
	return 0;  // 改数组大小!!!
}
发布了213 篇原创文章 · 获赞 264 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43851525/article/details/103498890
今日推荐