POJ-2828-Buy Tickets (line segment tree)

Source: Internet
Author: User

POJ-2828-Buy Tickets (line segment tree)

 

Topic transfer: Buy Tickets

 

Idea: Line Segment tree, which is inserted in sequence from the back to the back, and inserted with an update

 

AC code:

 

#include 
 
  #include 
  
   #include 
   
    #include #include 
    
     #include 
     
      #include 
      
       #include 
       
        #include
        #include 
         
          #include 
          
           #include 
           
            #define LL long long#define INF 0x7fffffffusing namespace std;const int maxn = 200005;struct node {int pos, v;}a[maxn];int x[maxn << 2], num[maxn];void build(int l, int r, int rt) {x[rt] = r - l + 1;if(r == l) return;int mid = (r + l) >> 1;build(l, mid, rt << 1);build(mid + 1, r, rt << 1 | 1);}int query(int p, int l, int r, int rt) {x[rt] --;if(l == r) return l;int mid = (l + r) >> 1;if(x[rt << 1] >= p) return query(p, l, mid, rt << 1);else return query(p - x[rt << 1], mid + 1, r, rt << 1 | 1);}int main() {int n;while(scanf("%d", &n) != EOF) {for(int i = 1; i <= n; i ++) {scanf("%d %d", &a[i].pos, &a[i].v);}build(1, n, 1);for(int i = n; i >= 1; i --) {int p = query(a[i].pos + 1, 1, n, 1);num[p] = a[i].v;}for(int i = 1; i < n; i ++) {printf("%d ", num[i]);}printf("%d\n", num[n]);}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.