Monitor (矩阵是否完全被覆盖,二维转一维,求前缀和)

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个子矩阵覆盖这些矩阵,q次查询,每次查询给出一个子矩阵,问每次查询的子矩阵是否全部被覆盖。

思路:刚开始想到了用树状数组,但数据量太大了,自己不会处理就放弃了,然后尝试用线段树,结果自己还是不会处理QAQ。

正文:其实我们可以把二维的矩阵转成一维的,然后用前缀和维护就好了,要注意的是,p组数据中有些点被覆盖了多次,所以要重新跑一次,把覆盖至少一次的点的权值都变成1.这时我们再跑一遍前缀和。最后,只需要判断这个子矩阵的覆盖个数和大小是否一致就可以了。

代码如下:

#include<bits/stdc++.h>
#define N 10000010
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
int n,m,a[N];
void add(int i,int j,int c)
{
    if(i<1||j<1||i>n||j>m)
        return;
    a[(i-1)*m+j]+=c;
}
int query(int i,int j)
{
    if(!i||!j)return 0;
    return a[(i-1)*m+j];
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(a,0,sizeof(a));
        int p,q;
        scanf("%d",&p);
        while(p--)
        {
            int x1,y1,x2,y2;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);  //一维替换二维矩阵
            add(x1,y1,1);
            add(x1,y2+1,-1);
            add(x2+1,y1,-1);
            add(x2+1,y2+1,1);
        }
        for(int i=1;i<=n;i++)  //对(假二维)矩阵求前缀和
            for(int j=1;j<=m;j++)
                a[(i-1)*m+j]+=query(i,j-1)+query(i-1,j)-query(i-1,j-1);
        for(int i=1;i<=n;i++)  //当(假二维)矩阵的前缀和不为0时,说明这一点已经被覆盖了至少一次
            for(int j=1;j<=m;j++) //我们规定它只被覆盖了一次
                if(a[(i-1)*m+j])a[(i-1)*m+j]=1;
        for(int i=1;i<=n;i++)  //再求(假二维)矩阵的前缀和
            for(int j=1;j<=m;j++)
                a[(i-1)*m+j]+=query(i,j-1)+query(i-1,j)-query(i-1,j-1);
        scanf("%d",&q);
        while(q--)
        {
            int x1,y1,x2,y2;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            int ans=query(x2,y2)-query(x2,y1-1)-query(x1-1,y2)+query(x1-1,y1-1); //(假二维)矩阵 中这个子矩阵的和
            int cnt=(x2-x1+1)*(y2-y1+1);  //子矩阵的大小
            if(ans==cnt)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、付费专栏及课程。

余额充值