Monitor HDU - 6514 (二维差分)

Monitor HDU - 6514 (二维差分)

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.
在这里插入图片描述

题目大意:

有一个小偷想在个商店里偷东西。这个店里有p个监控,每个监控都有一个监视范围,是一个矩形范围。然后小偷要在q个范围内偷东西,每个范围也是一个矩形。给定的监控能否监控所有的小偷的偷盗。

解题思路:

用二维前缀和二维插分的思想就很容易解出这个题。对于每一个监控,我们都给这个范围的区间每一个小格变成1 ,对于每一次查询,只要查询这个矩形的范围的和,如果等于矩形的面积,就说明这个范围全在监控之中,就输出yes 反之输出no

有个问题就是这个题的数据范围,n x m<=1e7 ,如果你开二维数组应该开多少呢???[1e7][1e7]这显然是不行的,根本开不了这么大的数组,所以我们用一维数组代替二维数组.
这里附一个二维前缀和二维插分的博客
https://blog.csdn.net/why932346109/article/details/89468663#commentBox

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=2e7+10;
int a[maxn];
int sum[maxn];
int n,m;//n表示列 m表示行 
void init(int x1,int y1,int x2,int y2,int val){
	a[(x1)*n+y1]+=val;
	a[(x2+1)*n+y2+1]+=val;
	a[(x1)*n+y2+1]-=val;
	a[(x2+1)*n+y1]-=val;
}
void updata(){
	for(int i=1;i<=m;i++){
		for(int j=1;j<=n;j++){
			a[(i)*n+j]+=a[(i)*n+j-1]+a[(i-1)*n+j]-a[(i-1)*n+j-1];
		}
	}
	for(int i=1;i<=m;i++){
		for(int j=1;j<=n;j++){
			if(a[i*n+j]>0)			
				sum[(i)*n+j]=sum[(i)*n+j-1]+sum[(i-1)*n+j]-sum[(i-1)*n+j-1]+1;
		}
	}
}
int get_sum(int x1,int y1,int x2,int y2){
	//int ans=sum[x2][y2]-sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1];
	int ans=sum[(x2)*n+y2]-sum[(x1-1)*n+y2]-sum[(x2)*n+y1-1]+sum[(x1-1)*n+y1-1];
	return ans;
}
int main()
{
	while(~scanf("%d%d",&m,&n)){
		memset(a,0,sizeof(a));
		memset(sum,0,sizeof(sum));
		int p;
		scanf("%d",&p);
		while(p--){
			int x1,x2,y1,y2;
			scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
			init(x1,y1,x2,y2,1);
		}
		updata();
		int q;
		scanf("%d",&q);

		while(q--){
			int x1,x2,y1,y2;
			scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
			int tmp=(x2-x1+1)*(y2-y1+1);
		//	cout<<"面积:"<<tmp<<"    "<<get_sum(x1,y1,x2,y2)<<endl;
			if(tmp==get_sum(x1,y1,x2,y2)) printf("YES\n");
			else printf("NO\n"); 
			
		}

	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值