您的位置:首页 > 其它

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

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: