Posters

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5230    Accepted Submission(s): 1220


Problem Description
Ted has a new house with a huge window. In this big summer, Ted decides to decorate the window with some posters to prevent the glare outside. All things that Ted can find are rectangle posters. 

However, Ted is such a picky guy that in every poster he finds something ugly. So before he pastes a poster on the window, he cuts a rectangular hole on that poster to remove the ugly part. Ted is also a careless guy so that some of the pasted posters may overlap when he pastes them on the window. 

Ted wants to know the total area of the window covered by posters. Now it is your job to figure it out.

To make your job easier, we assume that the window is a rectangle located in a rectangular coordinate system. The window’s bottom-left corner is at position (0, 0) and top-right corner is at position (50000, 50000). The edges of the window, the edges of the posters and the edges of the holes on the posters are all parallel with the coordinate axes. 
 

 

Input
The input contains several test cases. For each test case, the first line contains a single integer N (0<N<=50000), representing the total number of posters. Each of the following N lines contains 8 integers x1, y1, x2, y2, x3, y3, x4, y4, showing details about one poster. (x1, y1) is the coordinates of the poster’s bottom-left corner, and (x2, y2) is the coordinates of the poster’s top-right corner. (x3, y3) is the coordinates of the hole’s bottom-left corner, while (x4, y4) is the coordinates of the hole’s top-right corner. It is guaranteed that 0<=xi, yi<=50000(i=1…4) and x1<=x3<x4<=x2, y1<=y3<y4<=y2.

The input ends with a line of single zero.
 

 

Output
For each test case, output a single line with the total area of window covered by posters.
 

 

Sample Input
2
0 0 10 10 1 1 9 9
2 2 8 8 3 3 7 7 0
 

 

Sample Output
56
 

 

Source
 
 
题目意思:
有n张回字形海报,然后求总面积(覆盖面积仅算一次)
 
思路:
矩形面积并的变形。
 
 
以如图所示建边插入线段树,就和普通的扫描线一样了
 
代码:
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <vector>
  6 #include <queue>
  7 #include <cmath>
  8 #include <set>
  9 using namespace std;
 10 
 11 #define N 50005
 12 #define ll root<<1
 13 #define rr root<<1|1
 14 #define mid (a[root].l+a[root].r)/2
 15 
 16 int max(int x,int y){return x>y?x:y;}
 17 int min(int x,int y){return x<y?x:y;}
 18 int abs(int x,int y){return x<0?-x:x;}
 19 
 20 struct node{
 21     int l, r;
 22     int val;
 23     __int64 sum;
 24 }a[N*16];
 25 
 26 struct Line{
 27     int x1, x2, y;
 28     int val;
 29     Line(){}
 30     Line(int a,int b,int c,int d){
 31         x1=a;
 32         x2=b;
 33         y=c;
 34         val=d;
 35     }
 36 }line[N*8];
 37 
 38 bool cmp(Line a,Line b){
 39     return a.y<b.y;
 40 }
 41 
 42 int xx[N*4];
 43 int n, m;
 44 
 45 int b_s(int key){
 46     int l=1, r=m;
 47     while(l<=r){
 48         int mm=(l+r)/2;
 49         if(xx[mm]==key) return mm;
 50         else if(xx[mm]<key) l=mm+1;
 51         else if(xx[mm]>key) r=mm-1;
 52     }
 53 }
 54 
 55 void build(int l,int r,int root){
 56     a[root].l=l;
 57     a[root].r=r;
 58     a[root].sum=a[root].val=0;
 59     if(l==r) return;
 60     build(l,mid,ll);
 61     build(mid+1,r,rr);
 62 }
 63 
 64 void up(int root){
 65     if(a[root].val) a[root].sum=xx[a[root].r+1]-xx[a[root].l];
 66     else if(a[root].l==a[root].r) a[root].sum=0;
 67     else a[root].sum=a[ll].sum+a[rr].sum;
 68 }
 69 
 70 void update(int l,int r,int val,int root){
 71     if(l>r) return;
 72     if(a[root].l==l&&a[root].r==r){
 73         a[root].val+=val;
 74         up(root);
 75         return;
 76     }
 77     if(r<=a[ll].r) update(l,r,val,ll);
 78     else if(l>=a[rr].l) update(l,r,val,rr);
 79     else{
 80         update(l,mid,val,ll);
 81         update(mid+1,r,val,rr);
 82     }
 83     up(root);
 84 }
 85 main()
 86 {
 87     int x1, y1, x2, y2, x3, y3, x4, y4;
 88     int i, j, k;
 89     while(scanf("%d",&n)&&n){
 90         k=0;
 91         m=1;
 92         for(i=0;i<n;i++){
 93             scanf("%d %d %d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
 94             line[k++]=Line(x1,x3,y1,1);
 95             line[k++]=Line(x1,x3,y2,-1);
 96             line[k++]=Line(x3,x4,y1,1);
 97             line[k++]=Line(x3,x4,y3,-1);
 98             line[k++]=Line(x3,x4,y4,1);
 99             line[k++]=Line(x3,x4,y2,-1);
100             line[k++]=Line(x4,x2,y1,1);
101             line[k++]=Line(x4,x2,y2,-1);
102             xx[m++]=x1;
103             xx[m++]=x2;
104             xx[m++]=x3;
105             xx[m++]=x4;
106         }
107         sort(xx+1,xx+m);
108         m=unique(xx+1,xx+m)-xx-1;
109         sort(line,line+k,cmp);
110         build(1,m,1);
111         __int64 ans=0;
112         for(i=0;i<k-1;i++){
113             update(b_s(line[i].x1),b_s(line[i].x2)-1,line[i].val,1);
114             ans+=a[1].sum*(__int64)(line[i+1].y-line[i].y);
115         }
116         printf("%I64d\n",ans);
117     }
118 }

 

 
posted on 2015-07-05 16:26  MC幽夜  阅读(210)  评论(0编辑  收藏  举报