【题解】hdu1264(2018-07-29校赛 线段树扫描线 B)线段树+扫描线+离散化

48 篇文章 0 订阅
24 篇文章 0 订阅

题目

题目链接

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
#define lc o<<1
#define rc o<<1|1
const int N=1e5+10;
int hx[N<<1],num;
ll ans,len[N<<2];
int dat[N<<2];
struct Edge{
    int l,r,h,f;
    Edge(){}
    Edge(int _l,int _r,int _h,int _f):l(_l),r(_r),h(_h),f(_f){}
    bool operator <(const Edge&rhs)const{
    return h<rhs.h;}
}edge[N<<1];
void push_up(int o,int l,int r)
{
    if(dat[o])len[o]=hx[r+1]-hx[l];
    else if(l==r)len[o]=0;
    else len[o]=len[lc]+len[rc];
}
void update(int o,int l,int r,int nl,int nr,int val)
{
    if(nl<=l&&r<=nr)
    {
        dat[o]+=val;
        push_up(o,l,r);
        return;
    }
    int mid=l+r>>1;
    if(nl<=mid)update(lc,l,mid,nl,nr,val);
    if(nr>mid)update(rc,mid+1,r,nl,nr,val);
    push_up(o,l,r);
}
int main()
{
    //freopen("in.txt","r",stdin);
    int x1=0,x2=0,y1=0,y2=0;
    while(x1!=-2)
    {
        num=0,ans=0;
        while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)&&x1>=0)
        {
            if(x1>x2)swap(x1,x2);
            if(y1>y2)swap(y1,y2);//漏了 
            hx[num]=x1;
            edge[num++]=Edge(x1,x2,y1,1);
            hx[num]=x2;
            edge[num++]=Edge(x1,x2,y2,-1);
        }
        sort(hx,hx+num);sort(edge,edge+num);
        int m=unique(hx,hx+num)-hx;
        for(int i=0;i<num;i++)
        {
            int l=lower_bound(hx,hx+m,edge[i].l)-hx;
            int r=lower_bound(hx,hx+m,edge[i].r)-hx-1;
            update(1,0,m,l,r,edge[i].f);
            ans+=len[1]*(edge[i+1].h-edge[i].h);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

总结

矩形面积并模板题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值