标签:
Description
Input
Output
Sample Input
Sample Output
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 struct p1 6 { 7 double x,y1,y2; 8 int v; 9 };p1 str[2010]; 10 bool com(p1 a,p1 b) 11 { 12 return a.x<b.x; 13 } 14 double y[2010]; 15 struct p2 16 { 17 int l,r,c; 18 double s,ll,rr; 19 };p2 tree[4100]; 20 void build(int l,int r,int p) 21 { 22 tree[p].l=l;tree[p].r=r; 23 tree[p].s=0; 24 tree[p].c=0; 25 tree[p].ll=y[l]; 26 tree[p].rr=y[r]; 27 if (l+1==r) return ; 28 int m=(l+r)/2; 29 build(l,m,p*2); 30 build(m,r,p*2+1); 31 } 32 void pushup(int p) 33 { 34 if (tree[p].c>0) 35 { 36 tree[p].s=tree[p].rr-tree[p].ll; 37 return ; 38 } 39 if (tree[p].l+1==tree[p].r) tree[p].s=0; 40 else tree[p].s=tree[p*2].s+tree[p*2+1].s; 41 } 42 void un(int p,p1 e) 43 { 44 if (e.y1==tree[p].ll&&e.y2==tree[p].rr) 45 { 46 tree[p].c+=e.v; 47 pushup(p); 48 return ; 49 } 50 if (e.y2<=tree[p*2].rr) un(p*2,e); 51 else if (e.y1>=tree[p*2+1].ll) un(p*2+1,e); 52 else 53 { 54 p1 ee=e; 55 ee.y2=tree[p*2].rr; 56 un(p*2,ee); 57 ee=e; 58 ee.y1=tree[p*2+1].ll; 59 un(p*2+1,ee); 60 } 61 pushup(p); 62 } 63 int main() 64 { 65 int n,i,j,k=1,t; 66 double x1,x2,y1,y2,ans; 67 while (~scanf("%d",&n)) 68 { 69 t=1; 70 if (n==0) break; 71 for (i=1;i<=n;i++) 72 { 73 scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); 74 str[t].x=x1; 75 str[t].y1=y1; 76 str[t].y2=y2; 77 str[t].v=1; 78 y[t]=y1; 79 t++; 80 str[t].x=x2; 81 str[t].y1=y1; 82 str[t].y2=y2; 83 str[t].v=-1; 84 y[t]=y2; 85 t++; 86 } 87 sort(str+1,str+t,com); 88 sort(y+1,y+t); 89 build(1,t-1,1); 90 ans=0.0; 91 un(1,str[1]); 92 for (i=2;i<t;i++) 93 { 94 ans+=tree[1].s*(str[i].x-str[i-1].x); 95 un(1,str[i]); 96 } 97 printf("Test case #%d\n",k); 98 printf("Total explored area: %.2lf\n\n",ans); 99 k++; 100 } 101 }
标签:
原文地址:http://www.cnblogs.com/pblr/p/4731999.html