B - Atlantis


#include <stdio.h> #include <iostream> #include <algorithm> using namespace std; struct node { int l,r; int c; double cnt,lf,rf; }seg[603]; double y[201]; struct Line { double x,y1,y2; int f; }line[201]; bool cmp(Line a,Line b) { return a.x<b.x; } void Build(int t,int l,int r) { seg[t].l = l; seg[t].r = r; seg[t].cnt = seg[t].c = 0; seg[t].lf = y[l]; seg[t].rf = y[r]; if(l + 1 == r) return ; int mid = (l + r)/2; Build(2*t,l,mid); Build(2*t+1,mid,r); } void calen(int t) { if(seg[t].c>0) { seg[t].cnt = seg[t].rf-seg[t].lf; return ; } if(seg[t].l+1 == seg[t].r)seg[t].cnt = 0; else seg[t].cnt = seg[2 *t].cnt+seg[2 *t+1].cnt; } void update(int t,Line e) { if(e.y1 == seg[t].lf && e.y2 == seg[t].rf) { seg[t].c+=e.f; calen(t); return ; } if(e.y2<=seg[2 *t].rf)update(2 *t,e); else if(e.y1>=seg[2 * t+1].lf)update(2 *t+1,e); else { Line tmp = e; tmp.y2 = seg[2 *t].rf; update(2 * t,tmp); tmp = e; tmp.y1 = seg[t<<1|1].lf; update(2 * t + 1,tmp); } calen(t); } int main() { int i,n,t,iCase = 0; double x1,y1,x2,y2; while(scanf("%d",&n),n) { iCase++; t = 1; for(i = 1;i<=n;i++) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); line[t].x = x1; line[t].y1 = y1; line[t].y2 = y2; line[t].f = 1; y[t] = y1; t++; line[t].x = x2; line[t].y1 = y1; line[t].y2 = y2; line[t].f = -1; y[t] = y2; t++; } sort(line+1,line+t,cmp); sort(y+1,y+t); Build(1,1,t-1); update(1,line[1]); double res = 0; for(i = 2;i<t;i++) { res+=seg[1].cnt*(line[i].x - line[i-1].x); update(1,line[i]); } printf("Test case #%d\nTotal explored area: %.2f\n\n",iCase,res); } return 0; }

扫描线

posted @ 2014-08-12 08:55  HuberyQian  阅读(107)  评论(0编辑  收藏  举报