hdu1255 覆盖的面积(线段树+扫描线+离散化,求矩形面积并)

123 篇文章 0 订阅
14 篇文章 0 订阅

给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积。和周长并套路差不多。。甚至简单了些我觉得。重点是维护重复了两次以上的线段长度。分类讨论即可。
以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[N];
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[N];
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值