题意:给出一个广告板,然后要将n个广告贴在广告板上,然后要求每次贴广告的时候要越靠上越好,求每个广告贴的位置,找不到的话输出-1;
思路:以高 为线段构造线段树,然后上面的值为区间的能放的最大值,每次查询的时候优先往左子树走;
当高很大的时候没有必要以高为线段进行构造线段树,直接以n来构造,因为最多有那么多的线段;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 200000 + 5;
#define INF 0x3f3f3f3f
#define clr(x,y) memset(x,y,sizeof x )
typedef long long ll;
#define eps 10e-10
const ll Mod = 1000000007;
struct Tree
{
int l;
int r;
int maxs;
}tree[maxn << 2];;
int h,w,n;
void build(int rt,int l,int r)
{
tree[rt].l = l;
tree[rt].r = r;
if(l == r)
{
tree[rt].maxs = w;
return ;
}
int mid = (l + r) >> 1;
build(rt << 1,l,mid);
build(rt<<1 | 1,mid+1,r);
tree[rt].maxs = w;
}
int query(int rt,int x)
{
if(tree[rt].l == tree[rt].r)
{
tree[rt].maxs -= x;
return tree[rt].l;
}
int mid = (tree[rt].l + tree[rt].r) >> 1;
int ret = -1;
if(tree[rt << 1].maxs >= x)
{
ret = query(rt<<1,x);
}
else if(tree[rt<<1 |1].maxs >= x)ret = query(rt<<1|1,x);
tree[rt].maxs = max(tree[rt << 1].maxs,tree[rt<<1|1].maxs);
return ret;
}
int main()
{
while( ~ scanf("%d%d%d",&h,&w,&n))
{
build(1,1,n > h ? h : n);
for(int i = 1; i <= n; i ++)
{
int x;
scanf("%d",&x);
if(x > w)
{
cout << -1 << endl;
continue;
}
cout << query(1,x) << endl;
}
}
return 0;
}