hdu 1828 线段树+扫描线

矩形周长和~

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define lson pos<<1
#define rson pos<<1|1
using namespace std;
const int MAXN=30000;
struct line
{
    int l,r,h,f;
    line(int a=0,int b=0,int c=0,int d=0):l(a),r(b),h(c),f(d){}
};
line a[MAXN];
bool cmp(const line & lhs, const line &rhs)
{
    return lhs.h<rhs.h;
}
struct node
{
    int l,r;
    int cover;
    int lbd,rbd;
    int numline;
    int sum;
    int mid()
    {
        return (l+r)>>1;
    }
};
node tree[MAXN*4];
void build(int l,int r,int pos)
{
    tree[pos].l=l;
    tree[pos].r=r;
    if(l==r)
    {
        tree[pos].cover=0;
        tree[pos].sum=0;
        tree[pos].lbd=tree[pos].rbd=0;
        tree[pos].numline=0;
        return ;
    }
    int mid=tree[pos].mid();
    build(l,mid,lson);
    build(mid+1,r,rson);
    tree[pos].sum=0;
    tree[pos].cover=0;
    tree[pos].lbd=tree[pos].rbd=0;
    tree[pos].numline=0;
}
int abs(int a)
{
    return a>0?a:-a;
}
void pushup(int pos)
{
    if(tree[pos].cover)
    {
        tree[pos].lbd=tree[pos].rbd=1;
        tree[pos].numline=2;
        tree[pos].sum=tree[pos].r-tree[pos].l+1;
    }
    else if(tree[pos].l==tree[pos].r)
        tree[pos].lbd=tree[pos].rbd=tree[pos].numline=tree[pos].sum=0;
    else
    {
        tree[pos].lbd=tree[lson].lbd;
        tree[pos].rbd=tree[rson].rbd;
        tree[pos].numline=tree[lson].numline+tree[rson].numline;
        if(tree[lson].rbd&&tree[rson].lbd)
            tree[pos].numline-=2;

        tree[pos].sum=tree[lson].sum+tree[rson].sum;
    }
}
void update(int l,int r,int f,int pos)
{
    if(l==tree[pos].l&&r==tree[pos].r)
    {
        tree[pos].cover+=f;
        pushup(pos);
        return ;
    }
    int mid=tree[pos].mid();
    if(r<=mid)
        update(l,r,f,lson);
    else if(l>mid)
        update(l,r,f,rson);
    else
    {
        update(l,mid,f,lson);
        update(mid+1,r,f,rson);
    }
    pushup(pos);
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int i;
        int k=0;
        memset(a,0,sizeof(a));
        for(i=0;i<n;i++)
        {
            int x1,y1,x2,y2;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            a[k++]=line(x1,x2,y1,1);
            a[k++]=line(x1,x2,y2,-1);
        }
        sort(a,a+k,cmp);
        int res=0;
        int last=0;
        build(-10000,10000,1);
        for(i=0;i<k;i++)
        {
            int l=a[i].l;
            int r=a[i].r-1;
            update(l,r,a[i].f,1);
            res+=(a[i+1].h-a[i].h)*tree[1].numline;
            res+=abs(tree[1].sum-last);
            last=tree[1].sum;

        }
        printf("%d\n",res);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值