【HDU - 6514】Monitor(二维差分,前缀和)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/89415094

题干:

Monitor

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 163840/163840 K (Java/Others)
Total Submission(s): 872    Accepted Submission(s): 145

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

扫描二维码关注公众号,回复: 5985270 查看本文章

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次询问(每次一个蓝色区域),每次问你能否蓝色区域被红色区域完全覆盖。

解题报告:

   考虑暴力的做法,对于每一个红色区域,每一行都做标记差分,这样预处理的复杂度是O(pn),显然会T,考虑优化掉这个n,那就是进行第二次差分,最后就可以得到真值(每一个点被覆盖的次数)。然后xjb搞一下答案就可以了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
int n,m,p,q;
int main()
{
	while(~scanf("%d%d",&n,&m)) { 
		vector<vector<ll> > vv(m+14,vector<ll>(n+14,0));
		vector<vector<ll> > sum(m+14,vector<ll>(n+14,0));
		scanf("%d",&p);
		for(int x1,x2,y1,y2,i = 1; i<=p; i++) {
			scanf("%d%d%d%d",&y1,&x1,&y2,&x2);
			vv[x1][y1]++;
			vv[x1][y2+1]--;
			vv[x2+1][y1]--;
			vv[x2+1][y2+1]++;
		}
		for(int i = 1; i<=n; i++) {
			for(int j = 1; j<=m; j++) {
				vv[j][i] += vv[j-1][i];
			}
		}
		for(int i = 1; i<=m; i++) {
			for(int j = 1; j<=n; j++) {
				vv[i][j] += vv[i][j-1];
			}
		}
		for(int i = 1; i<=m; i++) {
			for(int j = 1; j<=n; j++) {
				if(vv[i][j] >= 1) vv[i][j] = 1;
				else vv[i][j] = 0;
			}
		}
		for(int i = 1; i<=m; i++) {
			for(int j = 1; j<=n; j++) {
				sum[i][j] = vv[i][j] + sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1];
			} 
		}
		scanf("%d",&q);
		while(q--) {
			int x1,x2,y1,y2;
			scanf("%d%d%d%d",&y1,&x1,&y2,&x2);
			ll tmp = sum[x2][y2] - sum[x1-1][y2] - sum[x2][y1-1] + sum[x1-1][y1-1];
			if(tmp == 1LL*(x2-x1+1)*(y2-y1+1)) puts("YES");
			else puts("NO");
		}
	} 


	return 0 ;
}

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/89415094
今日推荐