Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1542
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 200000 5 using namespace std; 6 7 int n; 8 double Y[maxn],X[maxn]; 9 struct point 10 { 11 double x; 12 double y1; 13 double y2; 14 int lr; 15 bool operator <(const point &a)const 16 { 17 return x<a.x; 18 } 19 } p[maxn]; 20 21 struct node 22 { 23 int l,r; 24 double x; 25 double y1; 26 double y2; 27 int cover; 28 double len; 29 } tree[maxn*4]; 30 31 void build(int i,int l,int r) 32 { 33 tree[i].l=l; 34 tree[i].r=r; 35 tree[i].x=-1; 36 tree[i].y1=Y[l]; 37 tree[i].y2=Y[r]; 38 tree[i].cover=0; 39 tree[i].len=0; 40 if(l==r-1) return ; 41 int mid=(l+r)>>1; 42 build(i<<1,l,mid); 43 build(i<<1|1,mid,r); 44 } 45 46 void update(int i,double l,double r,int lr) 47 { 48 if(Y[tree[i].l]==l&&Y[tree[i].r]==r) 49 { 50 tree[i].cover+=lr; 51 if(tree[i].cover) 52 { 53 tree[i].len=Y[tree[i].r]-Y[tree[i].l]; 54 } 55 else if(tree[i].l==tree[i].r-1) tree[i].len=0; 56 else 57 tree[i].len=tree[i<<1].len+tree[i<<1|1].len; 58 return; 59 } 60 int mid=(tree[i].l+tree[i].r)>>1; 61 if(r<=Y[mid]) 62 { 63 update(i<<1,l,r,lr); 64 } 65 else if(l>=Y[mid]) 66 { 67 update(i<<1|1,l,r,lr); 68 } 69 else 70 { 71 update(i<<1,l,Y[mid],lr); 72 update(i<<1|1,Y[mid],r,lr); 73 } 74 if(tree[i].cover) 75 { 76 tree[i].len=Y[tree[i].r]-Y[tree[i].l]; 77 } 78 else if(tree[i].l==tree[i].r-1) tree[i].len=0; 79 else 80 tree[i].len=tree[i<<1].len+tree[i<<1|1].len; 81 } 82 83 int main() 84 { 85 int cas=1; 86 while(scanf("%d",&n)!=EOF) 87 { 88 if(n==0) break; 89 int t1=0; 90 for(int i=0; i<n; i++) 91 { 92 double x1,y1,x2,y2; 93 scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); 94 p[t1].x=x1; 95 p[t1].y1=y1; 96 p[t1].y2=y2; 97 p[t1].lr=1; 98 Y[t1++]=y1; 99 p[t1].x=x2;100 p[t1].y1=y1;101 p[t1].y2=y2;102 p[t1].lr=-1;103 Y[t1++]=y2;104 }105 sort(Y,Y+t1);106 sort(p,p+t1);107 int cnt=unique(Y,Y+t1)-Y;108 build(1,0,cnt-1);109 double x=0;110 double s=0;111 for(int i=0; i<t1; i++)112 {113 s+=(p[i].x-x)*tree[1].len;114 update(1,p[i].y1,p[i].y2,p[i].lr);115 x=p[i].x;116 }117 printf("Test case #%d\n",cas);118 cas++;119 printf("Total explored area: %.2lf\n",s);120 printf("\n");121 }122 return 0;123 }
View code