Billboard hdu 2795(线段树区间最大值的更新和查询)

线段树区间最大值的维护:

#include<bits/stdc++.h>
using namespace std;
#define lson l,mid,i<<1
#define rson mid+1,r,i<<1|1

const int maxn = 2e5 + 500;
int h,w,n;
int Max[maxn<<2];
int a[maxn<<2];

void PushUp(int i)
{
    Max[i] = max(Max[i<<1], Max[i<<1|1]);
}

void Build(int i, int l, int r)
{
    Max[i] = w;
    if(l == r) return;
    int mid = l + r >> 1;
    Build(i<<1,l,mid);
    Build(i<<1|1,mid+1,r);
}

int Query(int i, int l, int r, int x)
{
    if(l == r){
        Max[i] -= x; return l;
    }
    int mid = (l + r) >> 1;
    int ans =  (Max[i<<1] >= x) ? Query(i<<1,l,mid,x) : Query(i<<1|1, mid+1, r, x);
    PushUp(i);
    return ans;
}


int main()
{
    //freopen("DATA.c", "r", stdin);
    while(~scanf("%d%d%d", &h,&w,&n)){
        h = min(h,n);
        Build(1,1,h);
        for(int i = 1; i <= n; i ++){
            int x;
            scanf("%d", &x);
            if(Max[1] < x){
                printf("-1\n");
            }
            else{
                printf("%d\n", Query(1,1,h,x));
            }
        }
    }
    return 0;
}

 

点赞