Monitor 二维差分,前缀和

题目链接

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

Monitor 二维差分,前缀和_第1张图片

题解

把能看到的地方标记成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
#include
#include
#include
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;
}

 

你可能感兴趣的:(前缀和,差分)