Poj-2828 Buy Tickets

Source: Internet
Author: User

Poj-2828 Buy Tickets

 

 

Question: There are n queues, each of which has a val, and each of the Later users inserts a position pos in the current team. Output the final status of the team.

Reverse Thinking. In this way, the last person will surely get the position he wants from the current team. If we move forward, the last person will surely get the position he wants ......, That is to say, we can do this. We insert the last person, ignore it, and insert the last person. That is, we can find the real coordinates of the position pos that the current team wants to insert. Then update the length of the entire team. This loop continues until the last person. When a line segment tree is updated at a single point, it feels very similar to binary search and can be used for implementation.

 

 

#include 
 
  #include 
  
   #include 
   
    #include 
    
     #include 
     
      #include 
      
       #include #include 
       
        #include 
        
         #include 
         
          #include
          #include 
           
            #define Mid(a,b) ( a+((b-a)>>1))#define ll(x) (x<<1)#define rr(x) (x<<1|1)const int N = 200010;using namespace std;int n;int pos[N];int val[N];int ans[N];struct node{int left;int right;int sum;int mid() { return Mid(left, right); }};struct segtree{node tree[N * 4];void buildtree(int left,int right,int ind){tree[ind].left = left;tree[ind].right = right;tree[ind].sum = right - left + 1;if (left !=right){int mid = tree[ind].mid();buildtree(left,mid,ll(ind));buildtree(mid+1,right,rr(ind));}}int query(int val,int pos){int left = tree[pos].left;int right = tree[pos].right;if (left == right){tree[pos].sum = 0;return left;}else{int ind;if (tree[ll(pos)].sum >= val)ind = query(val, ll(pos));elseind = query(val - tree[ll(pos)].sum , rr(pos));tree[pos].sum = tree[ll(pos)].sum + tree[rr(pos)].sum;return ind;}}}seg;int main(){while (scanf(%d,&n)!=EOF){seg.buildtree(1,n,1);for (int i = 1; i <= n; i++){scanf(%d %d,&pos[i],&val[i]);}for(int i = n; i >=1; i--){int ps = seg.query(pos[i]+1,1);ans[ps] = val[i];}for (int i = 1; i <= n; i++){printf(%d,ans[i]);if (i != n)printf( );}printf();}return 0;}
           
         
        
       
      
     
    
   
  
 


 

Related Article

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.