hdu 2795 Billboard

http://acm.hdu.edu.cn/showproblem.php?pid=2795


线段树,思路是记录第l行到第r行的空的最大值,在query的时候优先查询左子树。如果左子树不能满足当前的需求,再去右子树中查找。

注意

  • 数组只需要开的比n<<2大就行,因为所有行的宽度都相同,就算所有广告各一行也只有200,000行。
  • 由于上一个条件,如果输入的h比n大,把h改为n。
  • 这题为了方便,在query找到底层后直接把update做掉了。
  • 把找不到的情况在main中排除掉,保证query找到的就是答案。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
//head
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
const int N = 2e5+5;
int t[N<<2];
int h,w,n;

void pushup(int rt)
{
    t[rt] = max(t[rt<<1], t[rt<<1|1]);
}

void build(int l, int r, int rt)
{
    if(l==r)
    {
        t[rt] = w;
        return;
    }
    int m = (l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}


int query(int len, int l, int r, int rt)
{
    if(l==r)
    {
        t[rt] -= len;
        return l;
    }
    int m = (l+r)>>1;
    int ret = (t[rt<<1]>=len)?query(len, lson):query(len, rson);
    pushup(rt);
    return ret;
}

int main()
{
    while(scanf("%d%d%d",&h,&w,&n)!=EOF)
    {
        if(h>n)
            h = n;
        build(1,h,1);
        while(n--)
        {
            int x;
            scanf("%d",&x);
            if(t[1]<x)
                printf("-1\n");
            else
                printf("%d\n",query(x, 1, h, 1));
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值