HDU - 6514 Monitor (二維差分)

Xiaoteng has a large area of land for growing crops, and the land can be seen as a rectangle of n×mn×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 pp monitors installed by Xiaoteng, and the rectangle monitored by each monitor is known.

Xiao Teng guess that the thieves would also steal qq 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)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)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)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)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)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 qq 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個詢問區域,判斷詢問區域內有無未被監視區域

 

思路:

看了題解才知道這是個二維差分orz

先來學習一下二維差分:https://www.cnblogs.com/LMCC1108/p/10753451.html

對於二維數組a[n][m],其二維前綴和爲sum[i][j] = a[i][j] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];

則(x1, y1), (x2, y2)區域內的值爲:sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1];

 

本題開二維數組會爆內存,所以使用變長數組vector

在判斷是否全覆蓋時,求出(x1, y1)(x2, y2)區域的值,判斷是否等於給定區域面積

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 1e7 + 20;

int main()
{
    int n, m, p, q, x1, y1, x2, y2;
    while(~scanf("%d%d", &n, &m))
    {
        vector< vector<int> >a(n + 2, vector<int>(m + 2, 0));
        scanf("%d", &p);
        while(p--)
        {
            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
            a[x1][y1]++;
            a[x2 + 1][y2 + 1]++;
            a[x1][y2 + 1]--;
            a[x2 + 1][y1]--;
        }
        for(int i = 1; i <= n; ++i)
        {
            for(int j = 1; j <= m; ++j)
            {
                a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
            }
        }
        for(int i = 1; i <= n; ++i)
        {
            for(int j = 1; j <= m; ++j)
            {
                if(a[i][j])
                    a[i][j] = 1;
                a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
            }
        }
        scanf("%d", &q);
        while(q--)
        {
            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
            ll s;
            s = (x2 - x1 + 1) * (y2 - y1 + 1);
            if(s == a[x2][y2] - a[x1 - 1][y2] - a[x2][y1 - 1] + a[x1 - 1][y1 - 1])
                cout<<"YES"<<'\n';
            else
                cout<<"NO"<<'\n';
        }
    }
    return 0;
}

 

 

發佈了170 篇原創文章 · 獲贊 12 · 訪問量 9022
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章