HDU - 2795 Billboard (线段树

本文介绍了一种使用线段树解决特定查询问题的方法。通过构建线段树并进行高效的更新和查询操作,确保了在面对大量数据时仍能快速响应。文章详细展示了线段树的构建过程及查询算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:

没看出来是个线段树,,, 很惭愧,,
n 层 对应 n个节点,每次查询更新整颗线段树,并且将根节点记录为最大的剩余的值,

建树的时候, 我们要知道 线段树节点数 最大也仅仅为min(n,h),h>nmin(n,h),h>n多的没有意义

#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <cstdio>
using namespace std;

#define CLR(a,b) memset(a,(b),sizeof(a))
#define pb push_back
#define ll long long
#define ls st<<1
#define rs st<<1|1

const int MAXN = 2e5+10;

int h, w, n;
struct node {
    int l, r;
    int x;
}t[MAXN<<2];

void build(int l, int r, int st) {
    t[st].l = l; t[st].r = r;
    t[st].x = w;
    if(l == r) {
        return;
    }
    int mid = (l+r)>>1;
    build(l,mid,ls); build(mid+1,r,rs);
}
int query(int x, int st) {
    if(t[st].l == t[st].r) {
        t[st].x -= x;
        return t[st].l;
    }
    int x1 = 0, x2 = 0;
    if(x <= t[ls].x)
        x1 = query(x, ls);
    else
        x2 = query(x, rs);
    t[st].x = max(t[ls].x, t[rs].x);
    return x1+x2;
}
int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    while(~scanf("%d%d%d",&h,&w,&n)) {
        int x;
        build(1,min(h,n),1);
        for(int i = 1; i <= n; ++i) {
            scanf("%d",&x);
            if(t[1].x >= x)
                printf("%d\n",query(x,1));
            else
                printf("-1\n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值