线段树单点更新~
建立线段树存储区间内的最大剩余宽度,优先询问左子树~
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int maxn=1e6+14; struct node { int l,r,sum; }segTree[maxn*4]; int h,w,n; void build (int i,int l,int r) { segTree[i].l=l; segTree[i].r=r; segTree[i].sum=w; if (l==r) return; int mid=(l+r)>>1; build(i<<1,l,mid); build(i<<1|1,mid+1,r); segTree[i].sum=max(segTree[i<<1].sum,segTree[i<<1|1].sum); } int query (int i,int x) { if (segTree[i].l==segTree[i].r) { segTree[i].sum-=x; return segTree[i].l; } int mid=(segTree[i].l+segTree[i].r)>>1; int ans; if (segTree[i<<1].sum>=x) ans=query(i<<1,x); else ans=query(i<<1|1,x); segTree[i].sum=max(segTree[i<<1].sum,segTree[i<<1|1].sum); return ans; } int main () { while (~scanf("%d %d %d",&h,&w,&n)) { h=min(h,n); build(1,1,h); int x; while (n--) { scanf ("%d",&x); if (segTree[1].sum<x) printf ("-1 "); else printf ("%d ",query(1,x)); } } return 0; }