标签:des style blog http color strong
题目在这:http://poj.org/problem?id=1151
Time Limit: 1000MS | Memory Limit: 10000K |
Description
Input
Output
Sample Input
2 10 10 20 20 15 15 25 25.5 0
Sample Output
Test case #1 Total explored area: 180.00
Source
1 #include<set> 2 #include<queue> 3 #include<vector> 4 #include<cstdio> 5 #include<cstdlib> 6 #include<cstring> 7 #include<iostream> 8 #include<algorithm> 9 using namespace std; 10 const int N = 110; 11 #define Ch1 (i<<1) 12 #define Ch2 (Ch1|1) 13 #define mid(i) T[i].mid 14 #define len(i) T[i].r - T[i].l 15 #define rlen(i) T[i].rr - T[i].rl 16 #define For(i,n) for(int i=1;i<=n;i++) 17 #define Rep(i,l,r) for(int i=l;i<=r;i++) 18 19 struct tnode{ 20 int l,r,mid,cover; 21 double rl,rr,len; 22 }T[N<<8]; 23 24 struct lines{ 25 double l,r,h; 26 int kind;//1表示上底边 27 }L[N*2]; 28 int n,Lim,tot,kth; 29 double x1,y1,x2,y2,cor[N*2],ans; 30 31 bool cmp(lines A,lines B){ 32 return A.h<B.h; 33 } 34 35 void init(){ 36 tot = 0;ans = 0; 37 For(i,n){ 38 scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); 39 L[++tot].l = x1; L[tot].r = x2; L[tot].h = y1; L[tot].kind = 0; cor[tot] = x1; 40 L[++tot].l = x1; L[tot].r = x2; L[tot].h = y2; L[tot].kind = 1; cor[tot] = x2; 41 } 42 sort(L+1,L+tot+1,cmp); 43 sort(cor+1,cor+tot+1); 44 Lim = unique(cor+1,cor+tot+1) - cor - 1; 45 } 46 47 void Build(int l,int r,int i){ 48 T[i].l = l; T[i].r = r; T[i].mid = (l+r)>>1; 49 T[i].rl = cor[l]; T[i].rr = cor[r]; 50 if(l==r-1) return; 51 Build(l,mid(i),Ch1); Build(mid(i),r,Ch2); 52 } 53 54 void Modify(int i,double l,double r,int delta){ 55 if(l==T[i].rl&&T[i].rr==r){ 56 T[i].cover+=delta; 57 if(T[i].cover>0) T[i].len = rlen(i);else 58 if(T[i].l+1==T[i].r) T[i].len = 0; else 59 T[i].len = T[Ch1].len + T[Ch2].len; 60 return; 61 } 62 double Mid = cor[mid(i)]; 63 if(r<=Mid) Modify(Ch1,l,r,delta);else 64 if(l>=Mid) Modify(Ch2,l,r,delta);else 65 Modify(Ch1,l,Mid,delta),Modify(Ch2,Mid,r,delta); 66 if(T[i].cover>0) T[i].len = rlen(i); 67 else T[i].len = T[Ch1].len + T[Ch2].len; 68 } 69 70 int main(){ 71 scanf("%d",&n); 72 while(n){ 73 init();kth++; 74 Build(1,Lim,1);L[0].h = L[1].h; 75 For(i,tot){ 76 if(!L[i].kind){ 77 ans+=T[1].len * (L[i].h - L[i-1].h); 78 Modify(1,L[i].l,L[i].r,1); 79 }else{ 80 ans+=T[1].len * (L[i].h - L[i-1].h); 81 Modify(1,L[i].l,L[i].r,-1); 82 } 83 } 84 printf("Test case #%d\n",kth); 85 printf("Total explored area: %.2f\n",ans); 86 printf("\n"); 87 scanf("%d",&n); 88 if(!n) return 0; 89 } 90 return 0; 91 }
POJ·1151 Atlantis·线段树求矩形面积并,布布扣,bubuko.com
标签:des style blog http color strong
原文地址:http://www.cnblogs.com/kjerome/p/3815481.html