话说这题很多人直接离散化过的,但我想这题的考察点应该是一个经典的问题(矩阵面积并),由于矩阵面积并的求法和上一篇blog矩阵周长并的求发极为相似,故在此不多加说明,以下是我的代码,希望对大家有帮助: #include<iostream> #include<stdio.h> #include<algorithm> #include<iomanip> using namespace std; struct lineTree{ int down,up; double len; int num,numCover; int numD,numU; }T[500]; double px[500],py[500]; struct node{ double x,y1,y2; bool f; }Node[500]; int numPy; struct pt_cmp { bool operator () ( const node &a, const node &b ) const { return a.x < b.x; } }; int B_search(double num,int l,int r,double a[]) { int low=l,high=r; while(low<=high) { int mid=(low+high)/2; if(a[mid]==num) return mid; if(num<a[mid]) high=mid-1; else low=mid+1; } return 0; } void buildTree(int d,int u,int step) { T[step].numCover=0;T[step].len=0.0; T[step].numD=false;T[step].numU=false; T[step].num=0; T[step].down=d;T[step].up=u; if(u-d>1) { buildTree(d,(d+u)/2,2*step); buildTree((d+u)/2,u,2*step+1); } } void update(int step) { if(T[step].numCover>0) { T[step].len=py[B_search(py[T[step].up],1,numPy,py)]-py[B_search(py[T[step].down],1,numPy,py)]; T[step].num=1; } else { if(T[step].up-T[step].down>1) { T[step].len=T[2*step].len+T[2*step+1].len; T[step].num=T[2*step].num+T[2*step+1].num; if(T[step].num&&T[2*step].numU&&T[2*step+1].numD) T[step].num--; } else { T[step].len=0.0; T[step].num=0; } } } void Insert(int d,int u,int step) { if(T[step].down==d) T[step].numD++; if(T[step].up==u) T[step].numU++; if(d==T[step].down&&T[step].up==u) T[step].numCover++; else { int mid=(T[step].down+T[step].up)/2; if(u<=mid) Insert(d,u,2*step); else if(d>=mid) Insert(d,u,2*step+1); else { Insert(d,mid,2*step); Insert(mid,u,2*step+1); } } update(step); } void Del(int d,int u,int step) { if(T[step].down==d) T[step].numD--; if(T[step].up==u) T[step].numU--; if(d==T[step].down&&T[step].up==u) T[step].numCover--; else { int mid=(T[step].down+T[step].up)/2; if(u<=mid) Del(d,u,2*step); else if(d>=mid) Del(d,u,2*step+1); else { Del(d,mid,2*step); Del(mid,u,2*step+1); } } update(step); } int main() { int n; int Case=1; while(cin>>n&&n) { int m=0,m1=0; double x1,x2,y1,y2; for(int i=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); py[++m1]=y1; px[m1]=x1; py[++m1]=y2; px[m1]=x2; Node[++m].x=x1; Node[m].y1=y1; Node[m].y2=y2; Node[m].f=true; Node[++m].x=x2; Node[m].y1=y1; Node[m].y2=y2; Node[m].f=false; } sort(py+1,py+1+m1); numPy=1; for(int i=2;i<=m1;i++) if(py[i]!=py[i-1]) py[++numPy]=py[i]; sort(Node+1,Node+1+m,pt_cmp()); buildTree(1,numPy,1); double sum=0; double lastlen=0; for(int i=1;i<=m;i++) { if(Node[i].f) Insert(B_search(Node[i].y1,1,numPy,py),B_search(Node[i].y2,1,numPy,py),1); else Del(B_search(Node[i].y1,1,numPy,py),B_search(Node[i].y2,1,numPy,py),1); if(i!=1) sum+=lastlen*(Node[i].x-Node[i-1].x); lastlen=T[1].len; } printf("Test case #%d/n",Case++); printf("Total explored area: %.2f/n/n",sum); } return 0; }