Billboard(线段树)

Billboard
Time Limit:8000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

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.
 

Input

There 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.
 

Output

For 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
 

 

 
 1 #include <stdio.h>
 2 #include <algorithm>
 3 #define lson o << 1 , l , mid
 4 #define rson o << 1 | 1 , mid + 1 , r
 5 using namespace std;
 6 
 7 int h , w , n ;
 8 int maxn[1000000] ;
 9 int x ;
10 
11 void build ( int o , int l , int r )
12 {
13     maxn[o] = w ;
14     if ( l == r ) {
15         return ;
16     }
17     int mid = ( l + r ) >> 1 ;
18     build ( lson ) ;
19     build ( rson ) ;
20 }
21 
22 int query ( int o , int l , int r )
23 {
24     if ( l == r ) {
25         maxn[o] -= x ;
26         return l ;
27     }
28     int mid = ( l + r ) >> 1 , ret ;
29     if ( x <= maxn[o << 1] ) {
30         ret = query ( lson ) ;
31     }
32     else {
33         ret = query ( rson ) ;
34     }
35     maxn[o] = max ( maxn[o << 1] , maxn[o << 1 | 1] ) ;
36     return ret ;
37 }
38 int main ()
39 {
40    // freopen ( "a.txt" , "r" , stdin ) ;
41     while ( scanf ("%d%d%d" , &h , &w , &n ) != EOF ) {
42        if ( h > n ) 
43              h = n ;
44         build ( 1 , 1 , h ) ;
45         for (int i = 1 ; i <= n ; i++ ) {
46             scanf ( "%d" , &x ) ;
47             if ( x > maxn[1] ) {
48                 printf ("-1\n") ;
49             }
50             else {
51                 printf ("%d\n" , query ( 1 , 1 , h ) ) ;
52             }
53         }
54     }
55     return 0 ;
56 }
因为 h <= 10^9 , 而 n <= 200,000 , 所以设node时 ,不要忘记 if ( h > n ) h = n ;这步

 

posted @ 2015-02-06 09:30  92度的苍蓝  阅读(198)  评论(0编辑  收藏  举报
http://images.cnblogs.com/cnblogs_com/Running-Time/724426/o_b74124f376fc157f352acc88.jpg