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.
#include <cstdio> #include <algorithm> #include <iostream> #define ll o<<1 #define rr o<<1|1 using namespace std; const int MAXN = 2 * 1e5 + 10; struct Tree { int l, r, Max; }tree[MAXN<<2]; void PushUp(int o) { tree[o].Max = max(tree[ll].Max, tree[rr].Max); } void Build(int o, int l, int r, int v) { tree[o].l = l; tree[o].r = r; tree[o].Max = v; if(l == r) { return ; } int mid = (l + r) >> 1; Build(ll, l, mid, v); Build(rr, mid+1, r, v); } void Update(int o, int pos, int v) { if(tree[o].l == tree[o].r) { tree[o].Max += v; return ; } int mid = (tree[o].l + tree[o].r) >> 1; if(pos <= mid) Update(ll, pos, v); else Update(rr, pos, v); PushUp(o); } int Query(int o, int v) { if(tree[o].l == tree[o].r) { return tree[o].l; } if(tree[ll].Max >= v) return Query(ll, v); else return Query(rr, v); } int main() { int h, w, n; while(scanf("%d%d%d", &h, &w, &n) != EOF) { Build(1, 1, min(n, h), w); for(int i = 1; i <= n; i++) { int v; scanf("%d", &v); if(tree[1].Max >= v) { int id = Query(1, v); printf("%d\n", id); Update(1, id, -v); } else { printf("-1\n"); } } } return 0; }