阅读743 返回首页    go 阿里云 go 技术社区[云栖]


HDU 2795 单点更新查询最大值

题意:给你一个h*w的广告板,每条通知的长度为1*wi,给出n个通知,如果可以放下尽量靠上放,对应的行里尽量靠左方,对于每条通知给出存放在哪条上。

这题最多n个节点,所以不用考虑h的最大范围。线段树中每点维护当前剩余宽度,查询最大剩余宽度是否满足,满足即查询靠上的节点并更新。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=200005;
int h,w,n;
int node[maxn<<2];
void build(int l,int r,int rt)
{
    node[rt]=w;
    if(l==r) return;
    int m=(r+l)>>1;
    build(lson);
    build(rson);
}
int query(int x,int l,int r,int rt)
{
    if(l==r)
    {
        node[rt]-=x;
        return l;
    }
    int m=(l+r)>>1;
    int ret=node[rt<<1]<x?query(x,rson):query(x,lson);
    node[rt]=max(node[rt<<1],node[rt<<1|1]);
    return ret;
}
int main()
{
    int a;
    while(~scanf("%d%d%d",&h,&w,&n))
    {
        h=h>n?n:h;
        build(1,h,1);
        while(n--)
        {
            scanf("%d",&a);
            if(node[1]<a) puts("-1");
            else printf("%d\n",query(a,1,h,1));
        }
    }
    return 0;
}


最后更新:2017-04-03 18:51:58

  上一篇:go redhat6 搭建开发环境
  下一篇:go linux重定向