【二维差分】Monitor

这篇博客讲述了小腾如何利用二维差分法监控农田,防止农作物被盗。小腾安装了多个矩形监控区域,需要判断这些监控是否能覆盖到预计被盗的农田区域。题目给出了多组测试数据,通过计算监控覆盖的前缀和来确定是否所有盗窃都能被监测到。
摘要由CSDN通过智能技术生成

Monitor

题目:http://acm.hdu.edu.cn/showproblem.php?pid=6514

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


 

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,其下一行是一个整数p,下面p行每行输入四个整数x1,y1,x2,y2,代表着从(x1,y1)到(x2,y2)这一矩形区域被全部标记,然后输入一个整数q,其下q行,每行输入四个整数x1,y1,x2,y2,如果矩形区域(x1,y1)到(x2,y2)这整个矩形均被标记,则输出YES,否则输出NO.

解题思路:

我们可以在图中将所有被标记过的矩形区域里面的数全部置为1,然后求一下面积的前缀和,在查询过程中,只需根据面积前缀和求出这一区域的面积,若求出的面积与该矩形的实际面积相同,则证明这一区域被全部覆盖,因此,在每一次输入覆盖区域时,我们可以通过类似差分的思想,将mapp[x1][y1],mapp[x2+1][y2+1]都加上一,将mapp[x1][y2+1],mapp[x2+1][y1]均减去一,然后求整个区域的和,对于那些mapp[i][j]>1,将其置为1,方便后面求面积的前缀和,最后求一下面积的前缀和,查询即可。注:此题数组范围不确定,只能根据n,m的值来开空间,否则会内存超限。

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
/*vector<int> mapp[10000010];
*/int main() 
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    //freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m;
    while(scanf("%d %d",&n,&m)!=EOF) {
    	/*rep(i,0,n+1) {
    		mapp[i].clear();
    		rep(j,0,m+1) {
    			mapp[i].push_back(0);
    		}
    	}*/
    	int **mapp;
        mapp=(int**)malloc(sizeof(int*)*(n+10));
        rep(i,0,n+1) mapp[i]=(int*)malloc(sizeof(int)*(m+10));
        rep(i,0,n+1) rep(j,0,m+1) mapp[i][j]=0;
    	int p;
    	int x1,x2,y1,y2;
    	scanf("%d",&p);
    	rep(i,1,p) {
    		scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
    		mapp[x1][y1]++;mapp[x2+1][y2+1]++;
    		mapp[x1][y2+1]--;mapp[x2+1][y1]--;
    	}
    	rep(i,1,n) {
    		rep(j,1,m) {
    			mapp[i][j]=mapp[i][j]+mapp[i-1][j]+mapp[i][j-1]-mapp[i-1][j-1];
    		}
    	}
    	rep(i,1,n) {
    		rep(j,1,m) {
    			if(mapp[i][j]) mapp[i][j]=1;
    		}
    	}
    	rep(i,1,n) {
    		rep(j,1,m) {
    			mapp[i][j]=mapp[i][j]+mapp[i-1][j]+mapp[i][j-1]-mapp[i-1][j-1];
    		}
    	}
    	int q;
    	scanf("%d",&q);
    	rep(i,1,q) {
    		scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
    		int ans=mapp[x2][y2]-mapp[x1-1][y2]-mapp[x2][y1-1]+mapp[x1-1][y1-1];
    		if(ans==(x2-x1+1)*(y2-y1+1)) printf("YES\n");
    		else printf("NO\n");
    	}
    }


    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值