hdu1255 覆盖的面积(线段树+扫描线+离散化,求矩形面积并)
2017-08-24 15:14
706 查看
给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积。和周长并套路差不多。。甚至简单了些我觉得。重点是维护重复了两次以上的线段长度。分类讨论即可。
以coverlen[2]为例:
如果cover==0 则coverlen[2]利用子树的coverlen[2]来更新
如果cover==1 则coverlen[2]利用子树的coverlen[1]来更新
如果cover==2 则coverlen[2]利用子树的coverlen[0]来更新
其中coverlen[0]就是每个节点的总长度。
注意一下叶子节点特殊处理,不能再往下找儿子了。
tips:MLE N次,以为是写的太丑内存爆了?结果是数组开小了???无奈.jpg
以coverlen[2]为例:
如果cover==0 则coverlen[2]利用子树的coverlen[2]来更新
如果cover==1 则coverlen[2]利用子树的coverlen[1]来更新
如果cover==2 则coverlen[2]利用子树的coverlen[0]来更新
其中coverlen[0]就是每个节点的总长度。
注意一下叶子节点特殊处理,不能再往下找儿子了。
tips:MLE N次,以为是写的太丑内存爆了?结果是数组开小了???无奈.jpg
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define N 2010 struct Scanline{ double x,y1,y2;bool f; }line ; inline bool cmp(Scanline x,Scanline y){ return x.x==y.x?x.f>y.f:x.x<y.x; } struct node{ int cover;double coverlen[3];//cover[i]--覆盖了i次及以上的长度 }tree[N<<2]; int tst,n; double a ; inline void build(int p,int l,int r){ memset(tree[p].coverlen,0,sizeof(tree[p].coverlen)); tree[p].cover=0; if(l==r){tree[p].coverlen[0]=a[l+1]-a[l];return;} int mid=l+r>>1; build(p<<1,l,mid);build(p<<1|1,mid+1,r); tree[p].coverlen[0]=tree[p<<1].coverlen[0]+tree[p<<1|1].coverlen[0]; } inline void pushup(int p){ if(tree[p].cover>=2){ tree[p].coverlen[2]=tree[p].coverlen[0]; tree[p].coverlen[1]=tree[p].coverlen[0];return; } else if(tree[p].cover==1){ tree[p].coverlen[1]=tree[p].coverlen[0]; tree[p].coverlen[2]=tree[p<<1].coverlen[1]+tree[p<<1|1].coverlen[1]; return; } else{ tree[p].coverlen[2]=tree[p<<1].coverlen[2]+tree[p<<1|1].coverlen[2]; tree[p].coverlen[1]=tree[p<<1].coverlen[1]+tree[p<<1|1].coverlen[1]; } } inline void update(int p,int l,int r,int x,int y,int val){ if(x<=l&&r<=y){ tree[p].cover+=val; if(l==r){ tree[p].coverlen[1]= tree[p].cover>=1?tree[p].coverlen[0]:0; tree[p].coverlen[2]= tree[p].cover>=2?tree[p].coverlen[0]:0; } else pushup(p);return; } int mid=l+r>>1; if(x<=mid) update(p<<1,l,mid,x,y,val); if(y>mid) update(p<<1|1,mid+1,r,x,y,val); pushup(p); } int main(){ // freopen("a.in","r",stdin); scanf("%d",&tst); while(tst--){ int num=0,m=0;double ans=0; scanf("%d",&n); while(n--){ double x1,y1,x2,y2; scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); line[++num].x=x1;line[num].y1=y1;line[num].y2=y2;line[num].f=1; line[++num].x=x2;line[num].y1=y1;line[num].y2=y2;line[num].f=0; a[++m]=y1;a[++m]=y2; } sort(a+1,a+m+1);m=unique(a+1,a+m+1)-a-1; build(1,1,m-1); sort(line+1,line+num+1,cmp); line[0]=line[1]; for(int i=1;i<=num;++i){ ans+=tree[1].coverlen[2]*(line[i].x-line[i-1].x); update(1,1,m-1,lower_bound(a+1,a+m+1,line[i].y1)-a,lower_bound(a+1,a+m+1,line[i].y2)-a-1,line[i].f?1:-1); } printf("%.2lf\n",ans); } return 0; }
相关文章推荐
- HDU1255覆盖的面积(线段树+离散化+扫描线)
- HDU - 1255 覆盖的面积(线段树求矩形面积交 扫描线+离散化)
- hdu1255--覆盖的面积(线段树+离散化+扫描线)
- HDU1255 覆盖的面积(线段树,离散化,扫描线,矩形面积交)
- 【HDU1255】覆盖的面积【线段树】【矩形面积交】
- HDU 1255 覆盖的面积(线段树+扫描线+离散化)
- hdu1255 覆盖的面积(矩形面积交+扫描线)
- CDOJ 1335-求恰好k次覆盖矩形面积并- (线段树 扫描线 离散化)
- hdu 1255 覆盖的面积 (线段树,离散化+扫描线)
- HDU-1255 覆盖的面积(线段树扫描线+离散化+改进后超快的算法)
- hdu 1255 覆盖的面积(线段树+离散化+扫描线)
- 覆盖的面积(线段树+扫描线+离散化)
- poj1151 Atlantis(线段树+扫描线+离散化,求矩形面积并)
- hdu1255 覆盖的面积 线段树+里离散化求矩形面积的交
- HDU 1255 覆盖的面积 (线段树+扫描线+离散化)
- HDU 1255 覆盖的面积(线段树+离散化+扫描线)
- HDU1255-覆盖的面积(线段树+扫描线——面积交)
- Codevs 题目3044 矩形面积求并(线段树+扫描线+离散化)
- Poj1151/HDU1542(求并矩形覆盖的面积)分别用离散化处理,矩形切割,线段树三种方法实现.
- HDU 1255 覆盖的面积 (扫描线 线段树 离散化 矩形面积并)