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.
InputThe 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.
OutputFor 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
好迷啊,之前用数组存就tle,改成vector就AC,不明白为什么……
思路与正常扫描线求面积基本相同,将大的矩形拆分即可
1 #include <iostream> 2 //#include<bits/stdc++.h> 3 //#include <stack> 4 //#include <queue> 5 //#include <map> 6 //#include <set> 7 #include <cstdio> 8 #include <cstring> 9 #include <algorithm> 10 #include <math.h> 11 #define ll long long 12 using namespace std; 13 const int MAX=5e4+5; 14 int sum[MAX<<2],mark[MAX<<2]; 15 struct seg 16 { 17 int l,r,h; 18 int d; 19 seg(){} 20 seg(int l1,int r1,int h1,int d1):l(l1),r(r1),h(h1),d(d1){} 21 bool operator <(const seg &a)const 22 { 23 // if(h==a.h) 24 // return d>a.d; 25 return h<a.h; 26 } 27 }; 28 vector <seg> s; 29 void pushup(int l,int r,int k) 30 { 31 if(mark[k]>0) 32 { 33 sum[k]=r-l+1; 34 return; 35 } 36 else if(l==r) 37 sum[k]=0; 38 else 39 { 40 sum[k]=sum[2*k]+sum[2*k+1]; 41 return; 42 } 43 } 44 void update(int ul,int ur,int l,int r,int k,int val) 45 { 46 if(l>=ul&&r<=ur) 47 { 48 mark[k]+=val; 49 pushup(l,r,k); 50 return; 51 } 52 int mid=(l+r)>>1; 53 if(ul<=mid) 54 update(ul,ur,l,mid,2*k,val); 55 if(ur>mid) 56 update(ul,ur,mid+1,r,2*k+1,val); 57 pushup(l,r,k); 58 } 59 int N; 60 int main() 61 { 62 int maxh; 63 while(~scanf("%d",&N)) 64 { 65 if(N==0) 66 break; 67 s.clear(); 68 maxh=-1; 69 memset(sum,0,sizeof(sum)); 70 memset(mark,0,sizeof(mark)); 71 int i; 72 ll an=0; 73 int ge=0; 74 int x1,y1,x2,y2,x3,y3,x4,y4; 75 while(N--) 76 { 77 scanf("%d %d %d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4); 78 // if(x1<x3&&y1<y4) 79 // { 80 maxh=max(maxh,max(x2,x4)); 81 s.push_back(seg(x1,x3,y1,1)); 82 s.push_back(seg(x1,x3,y4,-1)); 83 // } 84 // if(x3<x2&&y1<y3) 85 // { 86 s.push_back(seg(x3,x2,y1,1)); 87 s.push_back(seg(x3,x2,y3,-1)); 88 // } 89 // if(x4<x2&&y3<y2) 90 // { 91 s.push_back(seg(x4,x2,y3,1)); 92 s.push_back(seg(x4,x2,y2,-1)); 93 // } 94 // if(x1<x4&&y4<y2) 95 // { 96 s.push_back(seg(x1,x4,y4,1)); 97 s.push_back(seg(x1,x4,y2,-1)); 98 // } 99 } 100 sort(s.begin(),s.end()); 101 ge=s.size(); 102 for(i=0;i<ge-1;i++) 103 { 104 if(s[i].l<s[i].r) 105 update(s[i].l,s[i].r-1,0,50005,1,s[i].d); 106 an+=(long long )sum[1]*(s[i+1].h-s[i].h); 107 } 108 cout<<an<<" "; 109 } 110 return 0; 111 }