UVA 1232 -SKYLINE(线段树)





#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
/*
题意是要在[L,R]区间盖一个高为H的大楼。
如果盖楼之前在[L,R]区间上有楼房的高度<H,则要重新盖。
最终求要重新盖的楼层的高度。
*/
int t,n,l,r,h;
const int maxn=100000+5;
int mine[100005<<2];
int pot[1000005<<2];
int result;

void build(int l,int r,int pos)
{
    if(l==r){
        pot[pos]=-1;
        mine[pos]=0;
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,pos<<1);
    build(mid+1,r,pos<<1|1);
    mine[pos]=min(mine[pos<<1],mine[pos<<1|1]);
}
void pushdown(int pos)
{
        if(pot[pos]>=0){
            pot[pos<<1]=pot[pos];
            mine[pos<<1]=pot[pos];

            pot[pos<<1|1]=pot[pos];
            mine[pos<<1|1]=pot[pos];
            pot[pos]=-2;

        }
}

void change(int a,int b,int h,int l,int r,int pos)
{
        if(mine[pos]>h)return;
        if(a<=l&&b>=r){
            if(pot[pos]<=h&&pot[pos]!=-2){
                result+=r-l+1;
                pot[pos]=mine[pos]=h;
                return;
            }
        }
        pushdown(pos);
        int mid=(l+r)>>1;
        if(a<=mid)change(a,b,h,l,mid,pos<<1);
        if(b>mid)change(a,b,h,mid+1,r,pos<<1|1);
        mine[pos]=min(mine[pos<<1],mine[pos<<1|1]);
}

int main()
{
    scanf("%d",&t);
    while(t--){
        result=0;
        build(1,maxn,1);
        pot[1]=0;
        scanf("%d",&n);
        while(n--){
            scanf("%d%d%d",&l,&r,&h);
            change(l,r-1,h,1,maxn,1);
        }
        printf("%d\n",result);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值