题意:h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子
思路:每次找到最大值的位子,然后减去L
线段树功能:query:区间求最大值的位子(直接把update的操作在query里做了)

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
#define M 200005
#define ls node<<1,l,m
#define rs node<<1|1,m+1,r
int h,w,n,tree[M*4];
void buildtree(int node,int l,int r)
{
    tree[node]=w;
    if(l==r) return ;
    int m=(l+r)>>1;
    buildtree(ls);
    buildtree(rs);
}
int query(int node,int l,int r,int x)
{
    if(l==r)
    {
        tree[node]-=x;
        return l;
    }
    int m=(l+r)>>1;
    int p=(tree[node<<1]>=x)?query(ls,x):query(rs,x);
    tree[node]=max(tree[node<<1],tree[node<<1|1]);
    return p;
}
int main()
{
    //freopen("in.txt","r",stdin);
    while(~scanf("%d%d%d",&h,&w,&n))
    {
        if(h>n) h=n;
        buildtree(1,1,h);
        int a;
        while(n--)
        {
            scanf("%d",&a);
            if(tree[1]<a) printf("-1\n");
            else printf("%d\n",query(1,1,h,a));
        }
    }
    return 0;
}