Monitor 二维差分,前缀和

小腾有一块矩形农田,为了防止作物被偷,他安装了若干个监控,每个监控覆盖一个矩形区域。小腾想知道他的监控是否能覆盖到所有可能的盗窃区域。题目要求通过二维差分和前缀和来判断监控是否全面覆盖。对于每一个可能的盗窃区域,如果其完全被监控覆盖,则输出"YES",否则输出"NO"。
摘要由CSDN通过智能技术生成

题目链接

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

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≤10^7) which represent the area of the land.

And the secend line contain a integer p(1≤p≤10^6) 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≤10^6) 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

题解

把能看到的地方标记成1,不能看到的地方标记成0,求一下前缀和(即区域内1的个数)

每次查询判断实际面积是不是等于这个面积内1的个数。相等为YES,否则为NO

范围为n*m<10^7,那么可以动态开辟一个二维数组,代码如下:

   int **v=new int*[n+2];
    for(int i=0;i<=n+1;i++){
        v[i]=new int[m+2];
     }

把一个区域内所有位置都加一可以用差分来做

差分完之后求一次前缀和即 v[i][j]这个地方重叠的次数,大于等于表示能看到,把大于1的数都变成1

然后再求前缀和,v[i][j]是区域(1,1)到(i,j)内1的个数。

代码

 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        //动态开辟内存
        int **v=new int*[n+2];
        for(int i=0;i<=n+1;i++){
            v[i]=new int[m+2];
        }
        for(int i=0;i<=n+1;i++){
            for(int j=0;j<=m+1;j++){
                v[i][j]=0;
            }
        }
        int t;
        cin>>t;
        while(t--){
            int x,y,xx,yy;
            scanf("%d%d%d%d",&x,&y,&xx,&yy);
            //差分
            v[x][y]++;v[xx+1][yy+1]++;
            v[x][yy+1]--; v[xx+1][y]--;
        }
        //前缀和,求出位置(i,j)这个点的值
        for(int i=1;i<=n+1;i++){
            for(int j=1;j<=m+1;j++){
                v[i][j]+=v[i-1][j]-v[i-1][j-1]+v[i][j-1];
            }
        }
        for(int i=1;i<=n+1;i++){
            for(int j=1;j<=m+1;j++){
                if(v[i][j]) v[i][j]=1;
            }
        }
        //前缀和,求出区域(1,1)到(i,j)内1的个数
        for(int i=1;i<=n+1;i++){
            for(int j=1;j<=m+1;j++){
                v[i][j]+=v[i-1][j]+v[i][j-1]-v[i-1][j-1];
            }
        }
        int q;
        cin>>q;
        while(q--){
            int x,y,xx,yy;
            scanf("%d%d%d%d",&x,&y,&xx,&yy);
            int sum=v[xx][yy]-v[x-1][yy]-v[xx][y-1]+v[x-1][y-1];
            int area=(xx-x+1)*(yy-y+1);
            if(sum==area) printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值