hdu2795 Billboard(单点更新)


这里h是宽,w是长,刚开始老是翻不过来。。

然后要想清楚,这里处理的是宽度,所以以宽度作为线段建树。注意优化,因为最坏情况也就每行贴一张告示(告示宽度为1),所以树的最长宽度也就n,不处理这一步,这个树就建不起来,所以运行错误。


#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <math.h>

using namespace std;

typedef long long LL;

const int N = 200010;
const int INF = 1e8;

struct line
{
    int l;
    int r;
    int value;
}tree[4 * N];

int ans;

void build(int i, int l, int r, int val)
{
    tree[i].l = l;
    tree[i].r = r;
    tree[i].value = val;
    if(l == r)
    {
        return;
    }
    int mid = (l + r) >> 1;
    build(i*2, l, mid, val);
    build(i*2+1, mid+1, r, val);
}

void update(int i, int x)
{
    if(tree[i].l == tree[i].r)
    {
        tree[i].value -= x;
        ans = tree[i].l;
        return;
    }
    if(tree[2*i].value >= x) update(i*2, x);//判断左子树是否可以走
    else update(i*2+1, x);
    tree[i].value = max(tree[i * 2].value, tree[i * 2 + 1].value);
}

int main()
{
   // freopen("in.txt", "r", stdin);
    int h, w, n, x;
    while(~scanf("%d%d%d", &h, &w, &n))
    {
        if(h > n) h = n;
        build(1, 1, h, w);
        ans = -1;
        for(int i = 0; i < n; i++)
        {
            scanf("%d", &x);
            if(tree[1].value >= x) update(1, x);
            printf("%d\n", ans);
            ans = -1;
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值