On September 1, the billboard was empty. One by one, the announcements started being put on the billboard.
Each announcement is a stripe of paper of unit height. More specifically, the i-th announcement is a rectangle of size 1 * wi.
When someone puts a new announcement on the billboard, she would always choose the topmost possible position for the announcement. Among all possible topmost positions she would always choose the leftmost one.
If there is no valid location for a new announcement, it is not put on the billboard (that's why some programming contests have no participants from this university).
Given the sizes of the billboard and the announcements, your task is to find the numbers of rows in which the announcements are placed.
The first line of the input file contains three integer numbers, h, w, and n (1 <= h,w <= 10^9; 1 <= n <= 200,000) - the dimensions of the billboard and the number of announcements.
Each of the next n lines contains an integer number wi (1 <= wi <= 10^9) - the width of i-th announcement.
3 5 5 2 4 3 3 3
1 2 1 3-1
公告的高度固定为1,可以对公告板的高度进行线段花费,将其现在的宽度值存起来,然后每次遍历从左子树开始往下走,知道走到叶子节点满足要求即可
#include<cstdio> #include<cstring> #include<algorithm> #define maxn 200000 using namespace std; int ans; struct node { int l,r,val; }tree[maxn*4]; void build(int l,int r,int t,int w) { tree[t].l=l; tree[t].r=r; tree[t].val=w; if(l!=r){ build(l,(l+r)/2,2*t,w); build((l+r)/2+1,r,2*t+1,w); } } void Query(int t,int w) { if(tree[t].l==tree[t].r){ tree[t].val-=w; ans=tree[t].l; return ; } if(w<=tree[2*t].val) Query(2*t,w); else Query(2*t+1,w); tree[t].val=max(tree[2*t].val,tree[2*t+1].val);/维护父亲节点的左右子书的最大值 } int main() { int h,w,n; while(~scanf("%d %d %d",&h,&w,&n)){ h=min(h,n); build(1,h,1,w); ans=-1; int k; for(int i=1;i<=n;i++){ scanf("%d",&k); if(tree[1].val>=k) Query(1,k); printf("%d\n",ans); ans=-1; } } return 0; }