#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;  // 改數組大小!!!
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章