覆盖的面积
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2494 Accepted Submission(s): 1237
Problem Description
给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.
![](https://i-blog.csdnimg.cn/blog_migrate/d1adc6253c233d9968f03fe1d872f86d.jpeg)
Input
输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1<=N<=1000),代表矩形的数量,然后是N行数据,每一行包含四个浮点数,代表平面上的一个矩形的左上角坐标和右下角坐标,矩形的上下边和X轴平行,左右边和Y轴平行.坐标的范围从0到100000.
注意:本题的输入数据较多,推荐使用scanf读入数据.
注意:本题的输入数据较多,推荐使用scanf读入数据.
Output
对于每组测试数据,请计算出被这些矩形覆盖过至少两次的区域的面积.结果保留两位小数.
Sample Input
2 5 1 1 4 2 1 3 3 7 2 1.5 5 4.5 3.5 1.25 7.5 4 6 3 10 7 3 0 0 1 1 1 0 2 1 2 0 3 1
Sample Output
7.63 0.00这个题目以前做过,当时刚开始学习线段树,所以在网上看了一个代码,效率不高,但是过了,后来发现看那个代码会忽略线段树的精髓,今天做HDU4419的时候受到启发,于是重写改写了一下,效率提高很多,我觉得这个算法应该就是排行上前几名的算法了,453MS,虽然不是第一名的265MS,但这个也只是是否保留标记的问题。这个算法的核心就在于setlen()函数,这里我做了三个长度,分别是len[0],len[1],len[2]分别表示没有覆盖,覆盖一次,以及覆盖2次和两次以上的线段长度。然后通过转移方程,就可以方便计算了。而且不用每次都计算到叶子结点。下面上代码#include <iostream> #include <algorithm> #include <map> #include <cstdio> using namespace std; const int maxn=1001; struct Line { double x1,x2,y; int flag; }line[2*maxn]; struct node { int l,r,cover; double len[3]; int getmid() { return (l+r)/2; } }tree[8*maxn]; double X[2*maxn]; int k; int n; int cmp(Line a,Line b) { return a.y<b.y; } void build(int l,int r,int u) { tree[u].l=l; tree[u].r=r; tree[u].len[1]=tree[u].len[2]=tree[u].cover=0; tree[u].len[0]=X[r]-X[l]; if (r-l==1) return ; int mid=tree[u].getmid(); build(l,mid,2*u); build(mid,r,2*u+1); } void setlen(int u) { int l=tree[u].l,r=tree[u].r; if (r-l==1) { if (tree[u].cover>1) { tree[u].len[0]=0; tree[u].len[1]=0; tree[u].len[2]=X[r]-X[l]; } else if (tree[u].cover==1) { tree[u].len[2]=tree[u].len[0]=0; tree[u].len[1]=X[r]-X[l]; } else { tree[u].len[1]=tree[u].len[2]=0; tree[u].len[0]=X[r]-X[l]; } } else { if (tree[u].cover>1) { tree[u].len[0]=0; tree[u].len[1]=0; tree[u].len[2]=X[r]-X[l]; } else if (tree[u].cover==1) { tree[u].len[0]=0; tree[u].len[1]=tree[2*u].len[0]+tree[2*u+1].len[0]; tree[u].len[2]=tree[2*u].len[1]+tree[2*u+1].len[1]+tree[2*u].len[2]+tree[2*u+1].len[2]; } else { tree[u].len[0]=tree[2*u].len[0]+tree[2*u+1].len[0]; tree[u].len[1]=tree[2*u].len[1]+tree[2*u+1].len[1]; tree[u].len[2]=tree[2*u].len[2]+tree[2*u+1].len[2]; } } } void updata(int l,int r,int u,int flag) { if (tree[u].r==r&&tree[u].l==l) { tree[u].cover+=flag; setlen(u); return ; } int mid=tree[u].getmid(); if (r<=mid) { updata(l,r,2*u,flag); } else if (l>=mid) { updata(l,r,2*u+1,flag); } else { updata(l,mid,2*u,flag); updata(mid,r,2*u+1,flag); } setlen(u); } int main() { int count,i,j; int t; double x1,x2,y1,y2; scanf("%d",&t); while(t--) { scanf("%d",&n); count=0; for (i=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); count++; X[count]=x1; line[count].x1=x1; line[count].x2=x2; line[count].y=y1; line[count].flag=1; count++; X[count]=x2; line[count].x1=x1; line[count].x2=x2; line[count].y=y2; line[count].flag=-1; } sort(X+1,X+1+count); sort(line+1,line+1+count,cmp); k=unique(X+1,X+1+count)-X-1; map<double,int> m; for (i=1;i<=k;i++) m.insert(map<double,int>::value_type(X[i],i)); double ans=0; build(1,k,1); int i=1; while(i<=count) { j=i; while(j<=count&&line[j].y==line[i].y) { int l=m[line[j].x1]; int r=m[line[j].x2]; updata(l,r,1,line[j].flag); j++; } if (j<=count) ans+=tree[1].len[2]*(line[j].y-line[i].y); i=j; } printf("%.2f\n",ans); } return 0; }