Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 22369 | Accepted: 8389 |
Description
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.
Input
The input consists of several test cases. Each test case starts with a line containing a single integer n (1 <= n <= 100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0 <= x1 < x2 <= 100000;0 <= y1 < y2 <= 100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.
The input file is terminated by a line containing a single 0. Don't process it.
The input file is terminated by a line containing a single 0. Don't process it.
Output
For each test case, your program should output one section. The first line of each section must be "Test case #k", where k is the number of the test case (starting with 1). The second one must be "Total explored area: a", where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.
Output a blank line after each test case.
Output a blank line after each test case.
Sample Input
2 10 10 20 20 15 15 25 25.5 0
Sample Output
Test case #1 Total explored area: 180.00
Source
几何 扫描线 线段树
传说中的矩形面积并,为什么这个坑放了这么久……
想象一条垂直于x轴的直线,从左往右扫描整个区间,如果遇到一个矩形的左边线,它的有效长度就并上矩形的边线;如果遇到一个矩形的右边线,它的有效长度就减去矩形的边线(但要注意,如果减去的这部分当前被包含在多个矩形里,只需cnt--而不能减去长度)
1 /*by SilverN*/ 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 const int mxn=200010; 9 int read(){ 10 int x=0,f=1;char ch=getchar(); 11 while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();} 12 while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();} 13 return x*f; 14 } 15 struct node{ 16 int w; 17 double len,ri,le; 18 }t[mxn<<1]; 19 struct line{ 20 double x,u,d; 21 bool tp; 22 }a[mxn]; 23 int lct=0; 24 int cmp(line a,line b){return a.x<b.x;} 25 double y[mxn]; 26 void Build(int l,int r,int rt){ 27 t[rt].le=y[l]; t[rt].ri=y[r]; 28 t[rt].len=0; 29 t[rt].w=0; 30 if(l+1==r)return; 31 int mid=(l+r)>>1; 32 Build(l,mid,rt<<1); 33 Build(mid,r,rt<<1|1); 34 return; 35 } 36 void pushup(int l,int r,int rt){ 37 if(t[rt].w){ 38 t[rt].len=t[rt].ri-t[rt].le; 39 return; 40 } 41 t[rt].len=0; 42 if(l+1<r){// 43 t[rt].len=t[rt<<1].len+t[rt<<1|1].len; 44 } 45 return; 46 } 47 void update(int l,int r,int rt,int pos){ 48 if(t[rt].le>=a[pos].d && a[pos].u>=t[rt].ri){ 49 if(a[pos].tp) t[rt].w++;else t[rt].w--; 50 pushup(l,r,rt); 51 return; 52 } 53 if(l+1==r)return; 54 int mid=(l+r)>>1; 55 if(a[pos].d<t[rt<<1].ri)update(l,mid,rt<<1,pos); 56 if(a[pos].u>t[rt<<1|1].le)update(mid,r,rt<<1|1,pos); 57 pushup(l,r,rt); 58 return; 59 } 60 double solve(int n){ 61 Build(1,n,1); 62 update(1,n,1,1); 63 double ans=0; 64 for(int i=2;i<=lct;i++){ 65 ans+=(a[i].x-a[i-1].x)*t[1].len; 66 update(1,n,1,i); 67 } 68 return ans; 69 } 70 int n; 71 int main(){ 72 int i,j; 73 int cas=0; 74 while(scanf("%d",&n) && n){ 75 if(cas)printf(" "); 76 ++cas; 77 lct=0; 78 double X1,Y1,X2,Y2; 79 for(i=1;i<=n;i++){ 80 scanf("%lf%lf%lf%lf",&X1,&Y1,&X2,&Y2); 81 a[++lct].x=X1; 82 a[lct].tp=1; 83 a[lct].d=Y1;a[lct].u=Y2; 84 y[lct]=Y1; 85 a[++lct].x=X2; 86 a[lct].tp=0; 87 a[lct].d=Y1;a[lct].u=Y2; 88 y[lct]=Y2; 89 } 90 sort(y+1,y+lct+1); 91 sort(a+1,a+lct+1,cmp); 92 double ans=solve(lct); 93 printf("Test case #%d ",cas); 94 printf("Total explored area: %.2f ",ans); 95 } 96 return 0; 97 }