Description
At the entrance to the university, there is a huge rectangular billboard of size h*w (H was its height and w are its width). The board is the place where all possible announcements be posted:nearest programming competitions, changes in the Dini Ng the menu, and other important information.
On September 1, the billboard is 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 all choose the topmost possible position for the Annou Ncement. Among all possible topmost positions she would always choose the leftmost one.
If There is no valid location for a new announcement, it's not put in the billboard (that's why some programming contests Has 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 announcement S is placed.
Input
There is multiple cases (no more than-cases).
The first line of the input file contains three integer numbers, H, W, and N (1 <= h,w <= 10^9; 1 <= n <= 200, )-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.
Output
For each announcement (in the order they is given in the input file) output one number-the number of the row in which T His announcement is placed. Rows is numbered from 1 to H and starting with the top row. If an announcement can ' t is put on the billboard, output "1" for this announcement.
Sample Input
3552433
Sample Output
1213-1 the problem at first glance 10^9 is really scary, but N at most only 200,000, so that, even if each billboard occupies a row, the first 200,000 rows are occupied, the back all soy sauce. Furthermore, the billboard needs to be inserted in the front line that can put it down, and the volume of this billboard will be reduced. Finally, look at this need O (NLOGN) to be over, logn query, always feel like a line tree. And then a little thought, is a segment tree to the interval maximum maintenance. Each point starts with a value of W, that is, the value of each interval is W.
Then according to the line segment tree query, from the left son to the right son query.
Until the corresponding interval is checked, the Val value of the interval is modified and the LT or RT value is returned.
However, the size of the data is a little BT, you must not meet at any time return-1, can not check the bottom of the return. Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<Set>#include<map>#include<queue>#include<string>#include<algorithm>#defineLL Long Longusing namespacestd;intH, W, N;//Segment Tree//increment of a certain point in interval to find the interval maximum valueConst intMAXN =200005;structnode{intLT, RT; intVal;} tree[4*MAXN];//Update upvoidPushup (intID) {Tree[id].val= Max (tree[id<<1].val, tree[id<<1|1].val);}//Create a line segment treevoidBuildintLtintRtintID) {tree[id].lt=lt; Tree[id].rt=RT; Tree[id].val=0;//the initial value of each paragraph, according to the topic requirements if(LT = =RT) {Tree[id].val=W; return; } intMid = (lt + rt) >>1; Build (LT, Mid, id<<1); Build (Mid+1, RT, id<<1|1); Pushup (ID);}//query for a range of P-compliantintQueryintLtintRtintIdintp) { if(Tree[id].val <p)return-1; if(tree[id].lt = =tree[id].rt) {Tree[id].val-=p; returntree[id].lt; } inttmp; TMP= Query (LT, RT, id<<1, p); if(tmp! =-1) {pushup (ID); returntmp; } tmp= Query (LT, RT, id<<1|1, p); Pushup (ID); returntmp;}voidWork () {intk, ans; intLen =min (h, N); Build (1Len1); for(inti =0; I < n; ++i) {scanf ("%d", &k); Ans= Query (1Len1, K); printf ("%d\n", ans); }}intMain () {//freopen ("test.in", "R", stdin); while(SCANF ("%d%d%d", &h, &w, &n)! =EOF) {work (); } return 0;}
ACM Learning process-hdu 2795 Billboard (segment tree)