Time Limit: 2000/1000 MS
(Java/Others) Memory Limit: 65536/32768 K
(Java/Others)
Total Submission(s): 5932 Accepted
Submission(s): 2599
1 #include <iostream> 2 #include <algorithm> 3 #include <stdio.h> 4 using namespace std; 5 typedef struct node 6 { 7 double x,y1,y2; 8 int flag; 9 } node; 10 typedef struct tree 11 { 12 double y1,y2,x; 13 double cover; 14 bool flag; 15 } treee; 16 node n[400]; 17 treee tree[10000000]; 18 double q[400]; 19 bool cmp(node a,node b) 20 { 21 return a.x<b.x; 22 } 23 void build(int l,int r,int t) 24 { 25 tree[t].cover=0; 26 tree[t].flag=false; 27 tree[t].x=-1; 28 tree[t].y1=q[l]; 29 tree[t].y2=q[r]; 30 if(l+1==r) 31 { 32 tree[t].flag=true; 33 return ; 34 } 35 int m=(l+r)>>1; 36 build(l,m,t<<1); 37 build(m,r,t<<1|1); 38 } 39 double insert(double x,double y1,double y2,int t,int cover) 40 { 41 if(tree[t].y1>=y2||tree[t].y2<=y1) 42 { 43 return 0; 44 } 45 if(tree[t].flag) 46 { 47 if(tree[t].cover>0) 48 { 49 double sum=0,x1=tree[t].x; 50 sum=(x-x1)*(tree[t].y2-tree[t].y1); 51 tree[t].cover+=cover; 52 tree[t].x=x; 53 return sum; 54 } 55 else 56 { 57 tree[t].cover+=cover; 58 tree[t].x=x; 59 return 0; 60 } 61 } 62 return insert(x,y1,y2,t<<1,cover)+insert(x,y1,y2,t<<1|1,cover); 63 } 64 int main() 65 { 66 double x,y,xx,yy; 67 int m,t,i,ww=1; 68 while(~scanf("%d",&m)&&m) 69 { 70 t=1; 71 for(i=0; i<m; i++) 72 { 73 scanf("%lf%lf%lf%lf",&x,&y,&xx,&yy); 74 q[t]=y; 75 n[t].flag=1; 76 n[t].x=x; 77 n[t].y1=y; 78 n[t++].y2=yy; 79 q[t]=yy; 80 n[t].flag=-1; 81 n[t].x=xx; 82 n[t].y1=y; 83 n[t++].y2=yy; 84 } 85 sort(n+1,n+t,cmp); 86 sort(q+1,q+t); 87 build(1,t-1,1); 88 double sum=0; 89 for(i=1; i<t; i++) 90 { 91 sum+=insert(n[i].x,n[i].y1,n[i].y2,1,n[i].flag); 92 } 93 cout<<"Test case #"<<ww++<<endl; 94 cout<<"Total explored area: "; 95 printf("%.2lf\n\n",sum); 96 } 97 }
hdu 1542 线段树 求矩形并,布布扣,bubuko.com
原文:http://www.cnblogs.com/ERKE/p/3571318.html