【二维差分】Monitor

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.

【二维差分】Monitor

 

题目大意:

有多组输入,对于每一组输入,先输入两个个整数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;
}

 

上一篇:拓扑排序


下一篇:洛谷 P2747 [USACO5.4]周游加拿大Canada Tour