HDU 2795 Billboard——特殊查询的线段树

来源:互联网 发布:网络教育规范收费标准 编辑:程序博客网 时间:2024/06/06 13:56
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 1e5 * 2 + 10;int h, w, n, date[maxn], segTree[maxn<<2];void pushup(int root) {    segTree[root] = max(segTree[root<<1], segTree[root<<1|1]);}void build(int L, int R, int root) {    if (L == R) {        segTree[root] = w;        return;    }    int mid = (L + R)>>1;    build(L, mid, root<<1);    build(mid + 1, R, root<<1|1);    pushup(root);}int query(int L, int R, int root, int val) {    if (L == R) {        segTree[root] -= val;        return L;    }    int mid = (L + R)>>1;    int ans = (segTree[root<<1] >= val) ? query(L, mid, root<<1, val) : query(mid + 1, R, root<<1|1, val);    pushup(root);    return ans;}int main(){    while (scanf("%d %d %d", &h, &w, &n) == 3) {        if (h > n) {            h = n;        }        build(1, h, 1);        for (int i = 1; i <= n; i++) {            int temp; scanf("%d", &temp);            if (segTree[1] < temp) {                printf("-1\n");            }            else {                printf("%d\n", query(1, h, 1, temp));            }        }    }    return 0;}

原创粉丝点击