題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2795
信息最多200000條,所以高度也不會超過200000,以高度爲下標建立線段樹(1 到 n ),初始值全都是w,保留最大值,每次取最大值,取完之後直接在query裏面減去wi的長度,並更新。
#include <iostream>
#include <cstdio>
#define lson l, m, x<<1
#define rson m+1, r, x<<1|1
using namespace std;
const int INF = (1<<31)-1;
const int MAXN = 200000 + 10;
int key[MAXN << 2];
int name[MAXN << 2];
int a[MAXN];
int h, w, n;
void update(int x)
{
key[x] = max(key[x<<1], key[x<<1|1]);
}
void build(int l, int r, int x)
{
if (l == r){
key[x] = w;
name[x] = l;
return ;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
update(x);
}
int query(int l, int r, int x, int wi)
{
if (l == r){
if (name[x] <= h)
key[x] -= wi;
return name[x];
}
int m = (l + r) >> 1;
int ret = -1;
if (key[x<<1] >= wi)
ret = query(lson, wi);
else
ret = query(rson, wi);
update(x);
return ret;
}
int main()
{
while (scanf("%d%d%d", &h, &w,&n) != EOF)
{
build(1,n,1);
for (int i=0; i<n; i++)
{
int wi;
scanf("%d", &wi);
if (key[1] >= wi){
int pos = query(1,n,1,wi);
if (pos > h){
printf("-1\n");
continue;
}
printf("%d\n", pos);
}else{
printf("-1\n");
}
}
}
return 0;
}