<span>HDU2795 Billboard(线段树基础题单节点更新区间查询)</span>

题意:h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子
思路:每次找到最大值的位子,然后减去L
线段树功能:query:区间求最大值的位子(直接把update的操作在query里做了)

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
#define M 200005
#define ls node<<1,l,m
#define rs node<<1|1,m+1,r
int h,w,n,tree[M*4];
void buildtree(int node,int l,int r)
{
    tree[node]=w;
    if(l==r) return ;
    int m=(l+r)>>1;
    buildtree(ls);
    buildtree(rs);
}
int query(int node,int l,int r,int x)
{
    if(l==r)
    {
        tree[node]-=x;
        return l;
    }
    int m=(l+r)>>1;
    int p=(tree[node<<1]>=x)?query(ls,x):query(rs,x);
    tree[node]=max(tree[node<<1],tree[node<<1|1]);
    return p;
}
int main()
{
    //freopen("in.txt","r",stdin);
    while(~scanf("%d%d%d",&h,&w,&n))
    {
        if(h>n) h=n;
        buildtree(1,1,h);
        int a;
        while(n--)
        {
            scanf("%d",&a);
            if(tree[1]<a) printf("-1\n");
            else printf("%d\n",query(1,1,h,a));
        }
    }
    return 0;
}

 

全部评论

相关推荐

11-20 22:03
东北大学 Java
用哈基米写的简历,有点夸大,等我后面改谦虚点,能不能找个日常实习,项目是点评和天机,没什么荣誉要不要把蓝桥杯和六级删了算了,实在没门面
程序员花海:日常实习这份简历够用的,等实习之后把实习经历结合业务好好写一下 到时候把实习经历放在项目经历的前面 可以看我主页修改简历的模板
如何写一份好简历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务