hdu 2795 Billboard -- 线段树

hdu 2795 Billboard
以h建立线段树维护最大值每个节点初始化最大值为w
对于每次改,从最左端开始找满足要求的位置

//#include<bits/stdc++.h>
#include<iostream>
#include<vector>
//#include <unordered_map>
using namespace std;
//template<class...Args>
//void debug(Args... args) {//Parameter pack
//    auto tmp = { (cout << args << ' ', 0)... };
//    cout << "\n";
//}
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>pll;
typedef pair<int, int>pii;
const ll N = 5e5 + 5;
const ll INF = 0x7fffffff;
const ll MOD = 1e9+7;

int h, w;
struct node {
    int l, r;
    int maxv;
}tree[N<<2];
void push_up(int root) {
    tree[root].maxv = max(tree[root << 1].maxv , tree[root << 1 | 1].maxv);
}
void build(int root, int l, int r) {
    if (l == r)tree[root] = { l,r,w };
    else {
        tree[root] = { l,r };
        int mid = l + r >> 1;
        build(root << 1, l, mid);
        build(root << 1 | 1, mid + 1, r);
        push_up(root);
    }
}
int update(int root,int k) {
    if (tree[root].l == tree[root].r) {
        tree[root].maxv -= k;
        return tree[root].l;
    }
    int t;
    if (tree[root << 1].maxv >= k) t = update(root << 1, k);
    else t = update(root << 1 | 1, k);
    push_up(root);
    return t;
}
int main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    int n;
    while (cin >> h >> w >> n) {
        build(1, 1, min(200000, h));
        while (n--) {
            int ww;
            cin >> ww;
            if (tree[1].maxv >= ww)cout << update(1, ww) << "\n";
            else cout << -1 << "\n";
        }
    }
    
    return 0;
}
上一篇:[蓝桥杯][算法提高VIP]数字黑洞


下一篇:PAT 1018 Public Bike Management (dijkstra)