Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=2795
Main topic:
Given an h * w size of Blackboard, H is Blackboard High and W is blackboard wide. Now you have to put a lot of posters, each poster size of 1 * m,m is the width of the input poster. From the top left, each time from top to bottom to see if there is enough space to post posters, there is affixed. Given a set of input to indicate the width of the poster, output the height of each poster paste position, if no stickers on the output-1.
Problem Solving Ideas:
At first, I didn't know how to use line tree, see the puzzle after the model is actually very simple, build a size equal to the number of lines of the line tree, that is, each poster affixed to the line. And then maintenance is the length of the tree segment trees, this is also very good understanding, such as the poster size to be affixed to a, if the line segment tree the first node value is greater than or equal to a, then in the segment tree there is a node in the remaining width greater than a, that is, this line can be affixed to this poster. If the first node value of the segment tree is less than a, then the maximum remaining width of this blackboard is less than a, which obviously cannot be affixed. So in fact, after analysis is a maintenance of the left and right sub-node maximum value of the segment tree only.
Code:
1#include <iostream>2#include <cmath>3#include <cstdio>4#include <cstring>5#include <map>6#include <vector>7#include <algorithm>8 using namespacestd;9 Ten #definell Long Long One #defineINF 0x3f3f3f3f A #defineLl_inf 0x3f3f3f3f3f3f3f3f - #defineLson L, M, RT << 1 - #defineRson m + 1, R, RT << 1|1 the #defineMAX 200200 - - inth, W; - intMaxn[max <<2]; + - voidPushup (intRT) + { AMAXN[RT] = max (Maxn[rt <<1], Maxn[rt <<1|1]); at } - - voidBuildintLintRintRT) - { - if(L = =r) { -MAXN[RT] =W; in return; - } to intm = (L + r) >>1; + build (Lson); - build (Rson); the pushup (RT); * } $ Panax Notoginseng intQueryintCintLintRintRT) - { the if(L = =r) { +MAXN[RT]-=C; A returnl; the } + intm = (L + r) >>1; - intres = Maxn[rt <<1] >= c?query (c, Lson): Query (c, Rson); $ pushup (RT); $ returnRes; - } - the intMain () - {Wuyi //freopen ("Debug\\in.txt", "R", stdin); the //freopen ("CON", "w", stdout); - intI, J, K; Wu intN; - while(~SCANF ("%d%d%d", &h, &w, &N)) { About intRight ; $ if(H > N) Right =N; - Elseright =h; -Build1, right,1); - intwidth; A while(n--) { +scanf"%d", &width); the if(maxn[1] <width) -printf"-1\n"); $ Else theprintf"%d\n", Query (width,1, right,1)); the } the } the return 0; -}
HDU 2795-billboard (segment tree)