Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 20273 | Accepted: 7656 |
最小面积并
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
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <set> using namespace std; const int N=1010; struct node { int l,r; int cover; double x,y; double cnt; }; struct Line { double x,y1,y2; int flag; bool operator<(const Line& J)const { return x<J.x; } }; node ST[N<<2]; Line Line[N]; int n; double A[N]; void build(int l,int r,int rt) { ST[rt].l=l;ST[rt].r=r; ST[rt].cover=ST[rt].cnt=0; ST[rt].x=A[l];ST[rt].y=A[r]; if(r-l>1) { int mid=(l+r)>>1; build(l,mid,rt<<1); build(mid,r,rt<<1|1); } } void cal(int rt) { if(ST[rt].cover>0) ST[rt].cnt=ST[rt].y-ST[rt].x; else if(ST[rt].r-ST[rt].l>1) { ST[rt].cnt=ST[rt<<1].cnt+ST[rt<<1|1].cnt; } else { ST[rt].cnt=0; } } void update(struct Line e,int rt) { if(e.y1==ST[rt].x&&e.y2==ST[rt].y) { ST[rt].cover+=e.flag; cal(rt); return; } if(e.y2<=ST[rt<<1].y) { update(e,rt<<1); } else if(e.y1>=ST[rt<<1|1].x) update(e,rt<<1|1); else { struct Line tmp=e; tmp.y2=ST[rt<<1].y; update(tmp,rt<<1); tmp=e; tmp.y1=ST[rt<<1|1].x; update(tmp,rt<<1|1); } cal(rt); } int main() { int k=0; while(scanf("%d",&n),n) { int cnt=0; double x1,x2,y1,y2; for(int i=0;i<n;i++) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); Line[i<<1].x=x1; Line[i<<1|1].x=x2; Line[i<<1].y1=Line[i<<1|1].y1=y1; Line[i<<1].y2=Line[i<<1|1].y2=y2; Line[i<<1].flag=1; Line[i<<1|1].flag=-1; A[i<<1]=y1,A[i<<1|1]=y2; } n<<=1; sort(A,A+n); sort(Line,Line+n); for(int i=1;i<n;i++) { if(A[i]!=A[i-1]) A[cnt++]=A[i-1]; } A[cnt++]=A[n-1]; build(0,cnt-1,1); double ans=0; for(int i=0;i<n-1;i++) { update(Line[i],1); ans+=ST[1].cnt*(Line[i+1].x-Line[i].x); } printf("Test case #%d\n",++k); printf("Total explored area: %.2f\n\n",ans); } return 0; }