HDOJ 2795 Billboard (线段树)

74/100
link is here
according to the question , we should control the direction of update ,it really easy but still hard to think . this time I learn deeper in “the tree”.

And I unconsciously become the first in the number of problem that have been solved among XUPT ‘s students in HDOJ,the website that record my way in NOIP/ICPC .The only achievement I made is that I become more confident than I started.Let we see if I can work continuously to the end!

#include <bits/stdc++.h>
#include <cstring>
#define maxn 200010
using namespace std;
int mn[maxn<<2];
int w;
int update(int l,int r,int v,int root)
{
    if(l == r) {mn[root]+=v;return l;}
    int m = (l+r)>>1;
    int ans;
    if(mn[root<<1]+v<=w) ans = update(l,m,v,root<<1);
    else ans = update(m+1,r,v,root<<1|1);
    mn[root] = min(mn[root<<1],mn[root<<1|1]);
    return ans;
}
int main()
{
    int h,n,wid;
    while(scanf("%d%d%d",&h,&w,&n)!=EOF)
    {
        memset(mn,0,sizeof(mn));
        for(int i=0;i<n;i++)
        {
            scanf("%d",&wid);    //mn[31] is the minimum number
            if(mn[1]+wid>w)
            {
                printf("-1\n");
                continue;
            }
            int ans;
            if(h<=200000) ans = update(1,h,wid,1);
            else ans = update(1,n,wid,1);
            printf("%d\n",ans);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值