标签:des style http color os io strong for
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 17252 | Accepted: 6567 |
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
//208K 16MS #include<stdio.h> #include<algorithm> #define M 207 using namespace std; double y[M]; struct Tree { int l,r,mid,cover; double len; }tree[M<<4]; struct Line { double x,y1,y2; int dir; }l[M<<4]; int cmp(Line a,Line b) { return a.x<b.x; } void cal(int i) { if(tree[i].cover>0)tree[i].len=y[tree[i].r]-y[tree[i].l];//如果被覆盖那么就是整段长度 else if(tree[i].l+1==tree[i].r)tree[i].len=0;//未被覆盖且为叶子节点,则为0 else tree[i].len=tree[i*2].len+tree[i*2+1].len;//非叶子节点即为下面子区间的覆盖总和 } void build(int left,int right,int i) { tree[i].l=left;tree[i].r=right;tree[i].mid=(left+right)>>1; tree[i].cover=0;tree[i].len=0; if(left+1!=right) { build(left,tree[i].mid,i*2); build(tree[i].mid,right,i*2+1); } } void insert(Line a,int i) { Line t; if(y[tree[i].l]==a.y1&&y[tree[i].r]==a.y2)tree[i].cover+=a.dir; else { if(y[tree[i*2].r]>=a.y2)insert(a,i*2); else if(y[tree[i*2+1].l]<=a.y1)insert(a,i*2+1); else { t=a; t.y2=y[tree[i*2].r]; insert(t,i*2); t=a; t.y1=y[tree[i*2+1].l]; insert(t,i*2+1); } } cal(i); } int main() { int n,cas=1; while(scanf("%d",&n),n) { double x1,y1,x2,y2,ans=0; int k=0; for(int i=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); l[k].x=x1;l[k].y1=y1;l[k].y2=y2;l[k].dir=1; y[k++]=y1; l[k].x=x2;l[k].y1=y1;l[k].y2=y2;l[k].dir=-1; y[k++]=y2; } sort(l,l+k,cmp); sort(y,y+k); build(0,k-1,1); insert(l[0],1); for(int i=1;i<k;i++) { ans+=tree[1].len*(l[i].x-l[i-1].x); insert(l[i],1); } printf("Test case #%d\n",cas++); printf("Total explored area: %.2lf\n\n",ans); } return 0; }
POJ 1151 Atlantis 扫描线+线段树,布布扣,bubuko.com
标签:des style http color os io strong for
原文地址:http://blog.csdn.net/crescent__moon/article/details/38473787