hdu 2795 Billboard

本文介绍了一种数据结构实现方法,通过构建区间树来高效处理区间内最大值的更新和查询操作。适用于需要动态调整区间内数值并快速获取最大值的应用场景。

题目链接如下所示:

Problem - 2795

这题将每一行当成一个叶子节点的区间去维护。我们维护的是区间内的点的最大宽度。

当然,h和n在建树的时候取最小的那一个,因为如果n比较小的话,其实我们不需要那么多叶子节点,这在更新查询的时候也是要注意区间的范围是什么。

代码如下所示:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<vector>
#include<algorithm>
#include<cstring>
#include<set>
#include<map>
#include<queue>
#include<string>
#include<cmath>
#include<climits>

using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int MAXN=200100;
const int INF=0x3f3f3f3f;
struct {
    int l;
    int r;
    int sum;
}tree[MAXN<<2];
int h,w,n;
int val[MAXN];

void push_up(int rt){
//    tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
    tree[rt].sum= max(tree[rt<<1].sum,tree[rt<<1|1].sum);
}

void build(int l,int r,int rt){
    if (l==r) {
        tree[rt].sum=w;
        return;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    push_up(rt);
}

int update(int len,int l,int r,int rt){
    if (l==r){
        if (tree[rt].sum>=len){
            tree[rt].sum-=len;
        }else{
            return -1;
        }
        return l;
    }
    int ans=-1;
    int mid=(l+r)>>1;
//    cout<<"rt:"<<rt<<endl;
//    cout<<"left sum:"<<tree[rt<<1].sum<<endl;
//    cout<<"right sum:"<<tree[rt<<1|1].sum<<endl;
//    cout<<"("<<l<<","<<r<<")"<<endl;
    if (tree[rt<<1].sum>=len){
        ans=update(len,lson);
    }else if (tree[rt<<1|1].sum>=len){
        ans=update(len,rson);
    }
    push_up(rt);
    return ans;
}

int main()
{
    while (scanf("%d %d %d",&h,&w,&n)!=EOF){
//        cout<<"h,w,n:"<<h<<","<<w<<","<<n<<endl;
        if (h>n) h=n;
        build(1,h,1);
//        for (int i = 1; i <= 8; ++i) {
//            cout<<"rt:"<<i<<",sum:"<<tree[i].sum<<endl;
//        }
        for (int i = 0; i < n; ++i) {
            scanf("%d",&val[i]);
//            cout<<val[i]<<endl;
            printf("%d\n",update(val[i],1,h,1));
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值