hdu 2795 Billboard 线段树

Billboard

这个题想到怎么建树就简单了,叶子节点为“一层”,树的大小最大200000就行了,因为最多就每个贴一层


#include<stdio.h>
#include<string.h>
#define MAXN 200005
struct node
{
    int l,r;
    int max;
    int lmax,rmax;
}t[MAXN*4];
int h,w;
int max(int a,int b) {return a>b?a:b;}
int min(int a,int b) {return a<b?a:b;}
void construct(int l,int r,int p)
{
    t[p].l=l,t[p].r=r;
    if(l==r)
    {
        t[p].max=w;
        t[p].lmax=t[p].rmax=0;
        return ;
    }
    int m=(l+r)>>1;
    construct(l,m,p<<1);
    construct(m+1,r,p<<1|1);
    t[p].lmax=t[p<<1].max;
    t[p].rmax=t[p<<1|1].max;
    t[p].max=max(t[p<<1].max,t[p<<1|1].max);
}
int ans;
void modify(int x,int p)
{
    if(t[p].max<x)
    {
        ans=-1;
        return ;
    }
    if(t[p].l==t[p].r)
    {
        t[p].max-=x;
        ans=t[p].l;
        return ;
    }
    if(x<=t[p].lmax) modify(x,p<<1);
    else modify(x,p<<1|1);
    
    t[p].lmax=t[p<<1].max;
    t[p].rmax=t[p<<1|1].max;
    t[p].max=max(t[p<<1].max,t[p<<1|1].max);
}
int main()
{
    int n,x;
    while(scanf("%d%d%d",&h,&w,&n)!=EOF)
    {
        ans=1;
        construct(1,min(h,n),1);
        while(n--)
        {
            scanf("%d",&x);
            modify(x,1);
            printf("%d\n",ans);
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值