Problem 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
3 5 5
2
4
3
3
3
Sample Output
1
2
1
3
-1
Thinking of solving problems
To look at the entire board, the length of each column (the original row) is the initial value of the segment tree. Note here that the data actually takes the maximum value of n, because the height of greater than n is meaningless. The segment tree maintains the largest remaining space on the interval.
Both the update and the query are placed in query. Note that recursion in query should be preceded by maintenance and should not be return! directly
Code
#include <cstdio>#include <cstring>#include <algorithm>using namespace STD;Const intMAXN =200010;intsegtree[maxn<<2];intN,height,width;voidBuildintLintRintnode) {if(L = = r) {Segtree[node] = width;return; } build ((L+R)/2+1, R, (node<<1)+1); Build (L, (l+r)/2,node<<1); Segtree[node] = max (segtree[node<<1],segtree[(node<<1)+1]);}intQueryintXintLintRintnode) {if(L = = r) {if(Segtree[node] >= x) {Segtree[node]-= x;returnL }Else return-1; }inttmpif(x <= segtree[node<<1]) tmp = query (x,l, (l+r)/2,node<<1);ElseTMP = query (x, (L+R)/2+1, R, (node<<1)+1); Segtree[node] = max (segtree[node<<1],segtree[(node<<1)+1]);returnTMP;}intMain () { while(scanf("%d%d%d", &height,&width,&n)! = EOF) {height = min (height,n); Build1, Height,1); while(n--) {intAscanf("%d", &a);printf("%d\n", Query (A,1, Height,1)); } }return 0;}
HDU2795 Billboard Line Segment Tree Single point update