HDU 2795 Billboard (线段树 单点更新, 询问位置)

题目大意:

就是h*w的板, 每次向上面空白位置贴1*w的条(不能旋转)

问每次贴能找到的最上面的位置


大致思路:

线段树练习题, 刷刷刷


代码如下:

Result  :  Accepted     Memory  :  3668 KB     Time  :  3120 ms

/*
 * Author: Gatevin
 * Created Time:  2015/8/14 22:44:53
 * File Name: Sakura_Chiyo.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

#define maxn 200020

struct Segment_Tree
{
    #define lson l, mid, rt << 1
    #define rson mid + 1, r, rt << 1 | 1
    int val[maxn << 2];
    void pushUp(int rt)
    {
        val[rt] = max(val[rt << 1], val[rt << 1 | 1]);
        return;
    }
    void build(int l, int r, int rt, int value)
    {
        if(l == r)
        {
            val[rt] = value;
            return;
        }
        int mid = (l + r) >> 1;
        build(lson, value);
        build(rson, value);
        pushUp(rt);
        return;
    }
    void update(int l, int r, int rt, int pos, int value)
    {
        if(l == r)
        {
            val[rt] -= value;
            return;
        }
        int mid = (l + r) >> 1;
        if(mid >= pos) update(lson, pos, value);
        else update(rson, pos, value);
        pushUp(rt);
        return;
    }
    int query(int l, int r, int rt, int L)
    {
        if(l == r)
        {
            if(val[rt] < L) return -1;
            return l;
        }
        int mid = (l + r) >> 1;
        if(val[rt << 1] >= L) return query(lson, L);
        if(val[rt << 1 | 1] >= L) return query(rson, L);
        return -1;
    }
};

Segment_Tree ST;

int main()
{
    int h, w, n;
    while(~scanf("%d %d %d", &h, &w, &n))
    {
        ST.build(1, min(h, n), 1, w);
        for(int i = 1; i <= n; i++)
        {
            int len;
            scanf("%d", &len);
            int pos = ST.query(1, min(h, n), 1, len);
            printf("%d\n", pos);
            if(pos != -1)
                ST.update(1, min(h, n), 1, pos, len);
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值