http://acm.hdu.edu.cn/showproblem.php?pid=2795
线段树,思路是记录第l行到第r行的空的最大值,在query的时候优先查询左子树。如果左子树不能满足当前的需求,再去右子树中查找。
注意
- 数组只需要开的比n<<2大就行,因为所有行的宽度都相同,就算所有广告各一行也只有200,000行。
- 由于上一个条件,如果输入的h比n大,把h改为n。
- 这题为了方便,在query找到底层后直接把update做掉了。
- 把找不到的情况在main中排除掉,保证query找到的就是答案。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
//head
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
const int N = 2e5+5;
int t[N<<2];
int h,w,n;
void pushup(int rt)
{
t[rt] = max(t[rt<<1], t[rt<<1|1]);
}
void build(int l, int r, int rt)
{
if(l==r)
{
t[rt] = w;
return;
}
int m = (l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
int query(int len, int l, int r, int rt)
{
if(l==r)
{
t[rt] -= len;
return l;
}
int m = (l+r)>>1;
int ret = (t[rt<<1]>=len)?query(len, lson):query(len, rson);
pushup(rt);
return ret;
}
int main()
{
while(scanf("%d%d%d",&h,&w,&n)!=EOF)
{
if(h>n)
h = n;
build(1,h,1);
while(n--)
{
int x;
scanf("%d",&x);
if(t[1]<x)
printf("-1\n");
else
printf("%d\n",query(x, 1, h, 1));
}
}
return 0;
}