HDU-2795-Billboard (line segment tree)

Source: Internet
Author: User

HDU-2795-Billboard (line segment tree)

 

Question transfer: Billboard

 

Idea: There is an h * w wooden board (which can be regarded as h rows, with a maximum of w space per row). Each time you put an item of 1 * L size, returns the position at the top of the list that can hold the length of L. If no value exists,-1 is output;

 

AC code:

 

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        # Include
         
           # Include
          
            # Include
           
             # Define LL long # define INF 0x7fffffffusing namespace std; const int maxn = 200005; int h, w, n; int a [maxn <2]; void build (int l, int r, int rt) {//, record maximum a [rt] = w; if (l = r) return; int mid = (l + r)> 1; build (l, mid, rt <1); build (mid + 1, r, rt <1 | 1);} int query (int x, int l, int r, int rt) {// query and update if (l = r) {// locate the location, update and return the current location a [rt]-= x; return l;} int mid = (l + r)> 1; int ret; if ([ Rt <1]> = x) ret = query (x, l, mid, rt <1); else ret = query (x, mid + 1, r, rt <1 | 1); a [rt] = max (a [rt <1], a [rt <1 | 1]); return ret ;} int main () {while (scanf ("% d", & h, & w, & n )! = EOF) {if (h> n) h = n; // optimization. if h is less than n, you can remove unnecessary build (1, h, 1); for (int I = 0; I <n; I ++) {int x; scanf ("% d", & x ); if (a [1] <x) puts ("-1"); else printf ("% d \ n", query (x, 1, h, 1 )); // Here h is written as n, check for half a day (⊙) B }} return 0 ;}
           
          
         
       
      
     
    
   
  
 


 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.