(线段树)hdoj2795-Billboard
At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes in the dining room menu, and other important information.
On September 1, the billboard was 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 always choose the topmost possible position for the announcement. Among all possible topmost positions she would always choose the leftmost one.
If there is no valid location for a new announcement, it is not put on the billboard (that's why some programming contests have 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 announcements are placed.
On September 1, the billboard was 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 always choose the topmost possible position for the announcement. Among all possible topmost positions she would always choose the leftmost one.
If there is no valid location for a new announcement, it is not put on the billboard (that's why some programming contests have 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 announcements are placed.
InputThere are multiple cases (no more than 40 cases).
The first line of the input file contains three integer numbers, h, w, and n (1 <= h,w <= 10^9; 1 <= n <= 200,000) - 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.OutputFor each announcement (in the order they are given in the input file) output one number - the number of the row in which this announcement is placed. Rows are numbered from 1 to h, starting with the top row. If an announcement can't be 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
解题思路
线段树结点表示x——y区间内的最大值,每次查询之后直接更新。
1 #include <iostream> 2 #include <queue> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7 typedef long long ll; 8 typedef unsigned long long ull; 9 struct node 10 { 11 int left,right,mid; 12 int num; 13 }st[2000005]; 14 int h,w,n; 15 int tem; 16 void init(int l,int r,int n) 17 { 18 st[n].num=w; 19 st[n].left=l; 20 st[n].right=r; 21 st[n].mid=(l+r)/2; 22 if(l+1==r) 23 return; 24 init(l,(l+r)/2,2*n); 25 init((l+r)/2,r,2*n+1); 26 } 27 void push(int n) 28 { 29 if(n<=1) 30 return; 31 int x=n/2; 32 st[n].num=max(st[2*n].num,st[2*n+1].num); 33 push(x); 34 } 35 int query(int value,int n) 36 { 37 if(st[n].num<value) 38 return -1; 39 if(st[n].left+1==st[n].right) 40 { 41 st[n].num=st[n].num-value; 42 push(n/2); 43 return st[n].left; 44 } 45 if(value<=st[2*n].num) 46 return query(value,2*n); 47 else 48 return query(value,2*n+1); 49 } 50 int main() 51 { 52 while(~scanf("%d %d %d",&h,&w,&n)) 53 { 54 init(1,min(h,n)+1,1); 55 int i; 56 int an; 57 for(i=1;i<=n;i++) 58 { 59 scanf("%d",&tem); 60 an=query(tem,1); 61 printf("%d\n",an); 62 } 63 } 64 }