http://acm.hdu.edu.cn/showproblem.php?pid=6514
Problem Description
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
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
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.
(X1, y1) is the lower left corner of the rectangle, (x2, y2) for the lower right corner of the rectangle, the positive x direction is vertically upward, the horizontal rightward direction is the positive y
Meaning of the questions:
In a rectangular area of no more than n * m, p-A rectangular, rectangular before q B after A is asked whether all covered.
Ideas:
Since n * m, p, q range is too large, then consider O (n * m + p + q) approach, i.e., the prefix and a two-dimensional differential +.
For Class A rectangle (x1, y1, x2, y2), we need, (x2 + 1, y2 + 1) only at (x1, y1) +1, the (x1, y2 + 1), (x2 + 1 , y1) at -1
After seeking a prefix and the entire area is greater than 0 is the point where the covered class A rectangle.
Where the value is greater than 0 becomes 1, and once again seeking a prefix, the number of points to be covered in a rectangle can be calculated in O (1) time after the handle.
(For details see Notes)
Highlights:
Two-dimensional array into a one-dimensional
Two prefix and seek re-assignment
code show as below:
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <string> 5 #include <math.h> 6 #include <algorithm> 7 #include <vector> 8 #include <stack> 9 #include <queue> 10 #include <set> 11 #include <map> 12 #include <math.h> 13 const int INF=0x3f3f3f3f; 14 typedef long long LL; 15 const int MOD = + 1E9 . 7 ; 16 // const Double the PI = ACOS (-1); . 17 const int MAXN 1E7 + = 10 ; 18 is the using namespace STD; . 19 20 is int A [MAXN]; // two-dimensionally into one-dimensional 21 int n-, m; 22 is 23 is void the Add ( int X, int Y, int Val) // add tags 24 { 25 IF (X> n-|| Y> m) 26 is return ; 27 A [(X- . 1 ) * m + Y] + = Val; 28 } 29 30 int Query ( int X, int Y) // get the value there is mainly used a processing margin 0 31 is { 32 IF (X = = 0 || Y == 0 ) 33 is return 0 ; 34 is return A [(X- . 1 ) * m + Y]; 35 } 36 37 [ void the Sum () // find a prefix, and 38 is { 39 for ( int I = . 1 ; i <= n; i ++) 40 { 41 for(int j=1;j<=m;j++) 42 { 43 a[(i-1)*m+j]+=Query(i,j-1)+Query(i-1,j)-Query(i-1,j-1); 44 } 45 } 46 } 47 48 int main() 49 { 50 while(~scanf("%d %d",&n,&m)) 51 { 52 memset(a,0,sizeof(a)); 53 int x1,x2,y1,y2; 54 int p,q; 55 scanf("%d",&p); 56 while(p--) 57 { 58 scanf("%d %d %d %d",&x1,&y1,&x2,&y2); 59 Add(x1,y1,1); 60 Add(x2+1,y2+1,1); 61 Add(x1,y2+1,-1); 62 is the Add (+ X2 . 1 , Y1, - . 1 ); 63 is } 64 the Sum (); // first prefix and dimensional requirements, a [i]> 0 where it is described to cover over 65 for ( int I = 1 ; I <= n-; I ++) // to point covered reassign 1, facilitating determination of 66 { 67 for ( int J = 1 ; J <= m; J ++ ) 68 { 69 IF (a [(I- . 1 ) * m + J]) 70 A [(I- . 1 ) * m + J] = . 1 ; 71 is } 72 } 73 is the Sum (); // results and the second prefix request, shall be obtained within the rectangular area stained 74 Scanf ( " % D " , & Q); 75 the while (q - ) 76 { 77 Scanf ( " % D%% D D D% " , & X1, Y1 &, & X2, & Y2); 78 int ANS = Query (X2, Y2) -Query (X1- . 1 , Y2) -Query (X2, Y1- . 1 ) + Query (X1- . 1 , Y1- . 1 ); // use the prefix and the dyed derived rectangular area 79 IF (ANS == (X2-X1 + . 1) * (Y2-Y1 + . 1 )) // see stained area is equal to the rectangle total area 80 the printf ( " YES \ n- " ); 81 the else 82 the printf ( " NO \ n- " ); 83 } 84 } 85 return 0 ; 86 }