Question connection: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2795
Q: There is an H * w advertisement version, and there are n 1 * W [I] advertisements. The principle of putting advertisements on the top is as much as possible and putting them on the left is as far as possible, ask the number of rows where the advertisement can be put down. If the advertisement cannot be put down, print-1;
Idea: we can build a line based on each row. Each leaf represents the capacity of each row, and the node stores the maximum value of the sub-node, and then quickly finds a row that can store and put down the advertisement.
In short, it is still a simple line segment tree problem. The difficulty lies in the abstract model.
# Include <iostream> # include <cstdio> # define mid (L + r)> 1 # define ls RT <1 # define Rs RT <1 | 1 # define lson L, M, RT <1 # define rson m + 1, R, RT <1 | 1 # define Max 999999 using namespace STD; int IMAX [max <2]; inline void pushup (int rt) {IMAX [RT] = max (IMAX [ls], IMAX [RS]);} void build (int w, int L, int R, int RT) {If (L = r) {IMAX [RT] = W; return;} int M = mid; build (W, lson); Build (W, rson); pushup (RT);} int query (int x, int L, int R, int RT) {If (L = r) {IMAX [RT]-= X; return L;} int M = mid; int ret = (IMAX [ls]> = x )? Query (x, lson): Query (x, rson); pushup (RT); return ret;} int main () {int H, W, N; while (~ Scanf ("% d", & H, & W, & N) {// optimized: If the space is far better than h> N, then let H = N (one row and one advertisement) if (h> N) H = N; build (W, 1, h, 1); While (n --) {int X; scanf ("% d", & X); If (IMAX [1] <X) // if no position is found at the top of puts ("-1 "); else printf ("% d \ n", query (x, 1, h, 1) ;}} return 0 ;}
HDU 2795 billboard (calculates the maximum value for the single-point update interval of a line segment tree)