#include <iostream> #include<string> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; const int mmax = 210; const double eps = 1e-8; int sgn(double x) { if(fabs(x)<eps) return 0; return x<0?-1:1; } struct Rect { double x1,x2; double y1,y2; void read() { scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2); if(x1>x2) swap(x1,x2); if(y1>y2) swap(y1,y2); } }R[110]; struct node { int l,r; int cov; double len; int mid() { return (l+r)>>1; } }T[4*mmax]; struct Num { double x; int id; bool operator < (const Num &a) const { return x<a.x; } }X[mmax]; struct Line { int l,r; double y; int fg; bool operator < (const Line &a) const { return y<a.y; } }L[mmax]; void build(int id,int l,int r) { T[id].l=l,T[id].r=r; T[id].cov=0,T[id].len=0; if(l==r) return ; int mid=T[id].mid(); build(id<<1,l,mid); build(id<<1|1,mid+1,r); } double pos[mmax]; void updata(int id,int l,int r,int fg) { if(l<=T[id].l && T[id].r<=r) { T[id].cov+=fg; if(T[id].cov) T[id].len=pos[T[id].r+1]-pos[T[id].l]; else { if(T[id].l==T[id].r) T[id].len=0; else T[id].len=T[id<<1].len+T[id<<1|1].len; } return ; } int mid=T[id].mid(); if(mid>=l) updata(id<<1,l,r,fg); if(mid<r) updata(id<<1|1,l,r,fg); if(T[id].cov==0) { T[id].len=T[id<<1].len+T[id<<1|1].len; } } int id[mmax]; int main() { int n,ca=0; while(~scanf("%d",&n)&&n) { int cnt=0; for(int i=0;i<n;i++) { R[i].read(); X[cnt].x=R[i].x1; X[cnt].id=cnt; cnt++; X[cnt].x=R[i].x2; X[cnt].id=cnt; cnt++; } sort(X,X+cnt); id[0]=1; pos[1]=X[0].x; for(int i=1;i<cnt;i++) { if(sgn(X[i].x-X[i-1].x)==0) id[X[i].id]=id[X[i-1].id]; else id[X[i].id]=id[X[i-1].id]+1; pos[id[X[i].id]]=X[i].x; } for(int i=0;i<n;i++) { L[i].l=id[2*i]; L[i].r=id[2*i+1]; L[i].y=R[i].y1; L[i].fg=1; L[i+n].l=id[2*i]; L[i+n].r=id[2*i+1]; L[i+n].y=R[i].y2; L[i+n].fg=-1; } sort(L,L+2*n); build(1,1,id[X[cnt-1].id]-1); updata(1,L[0].l,L[0].r-1,L[0].fg); double ans=0.0; for(int i=1;i<2*n;i++) { ans+=(L[i].y-L[i-1].y)*T[1].len; updata(1,L[i].l,L[i].r-1,L[i].fg); } printf("Test case #%d\nTotal explored area: %.2lf\n\n",++ca,ans); } return 0; }
原文地址:http://blog.csdn.net/u012127882/article/details/46367835