题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795
以高度作为区间,每个节点存区间内所剩的最长长度。
代码如下。
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#include <cstring>
#include <cassert>
using namespace std;
typedef long long ll;
const int maxn=222222;
const int INF=0x7fffffff;
const int mod=1e7+7;
#define LSON l,m,rt<<1
#define RSON m+1,r,rt<<1|1
#define ESP 1e-7
int mm[maxn<<2],h,w,n;
void pushup(int rt) {
mm[rt]=max(mm[rt<<1],mm[rt<<1|1]);//节点存该区间最长的宽
}
void build(int l,int r,int rt) {
mm[rt]=w;
if(l==r) return ;
int m=(l+r)>>1;
build(LSON);
build(RSON);
}
int query(int x,int l,int r,int rt) {
if(l==r) {
mm[rt]-=x;//对每次查询同时进行更新
return l;
}
int temp;
int m=(l+r)>>1;
if(mm[rt<<1]>=x) temp=query(x,LSON);
else temp=query(x,RSON);
pushup(rt);
return temp;
}
int main() {
while(~scanf("%d%d%d",&h,&w,&n)) {
h=min(h,n);
build(1,h,1);
for(int i=0;i<n;i++) {
int x;
scanf("%d",&x);
if(mm[1]<x) printf("-1\n");
else printf("%d\n",query(x,1,h,1));
}
}
return 0;
}