HDU-2795-Billboard (line segment tree)
Question transfer: Billboard
Idea: There is an h * w wooden board (which can be regarded as h rows, with a maximum of w space per row). Each time you put an item of 1 * L size, returns the position at the top of the list that can hold the length of L. If no value exists,-1 is output;
AC code:
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define LL long # define INF 0x7fffffffusing namespace std; const int maxn = 200005; int h, w, n; int a [maxn <2]; void build (int l, int r, int rt) {//, record maximum a [rt] = w; if (l = r) return; int mid = (l + r)> 1; build (l, mid, rt <1); build (mid + 1, r, rt <1 | 1);} int query (int x, int l, int r, int rt) {// query and update if (l = r) {// locate the location, update and return the current location a [rt]-= x; return l;} int mid = (l + r)> 1; int ret; if ([ Rt <1]> = x) ret = query (x, l, mid, rt <1); else ret = query (x, mid + 1, r, rt <1 | 1); a [rt] = max (a [rt <1], a [rt <1 | 1]); return ret ;} int main () {while (scanf ("% d", & h, & w, & n )! = EOF) {if (h> n) h = n; // optimization. if h is less than n, you can remove unnecessary build (1, h, 1); for (int I = 0; I <n; I ++) {int x; scanf ("% d", & x ); if (a [1] <x) puts ("-1"); else printf ("% d \ n", query (x, 1, h, 1 )); // Here h is written as n, check for half a day (⊙) B }} return 0 ;}