Poj 2828 Buy Tickets is a versatile line segment tree algorithm.

Source: Internet
Author: User

Poj 2828 Buy Tickets is a versatile line segment tree algorithm.
Buy Tickets

Time Limit:4000 MS   Memory Limit:65536 K
Total Submissions:14400   Accepted:7199

Description

Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue...

The Lunar New Year was approaching, but unluckily the Little Cat still had schedules going here and there. now, he had to travel by train to Mianyang, Sichuan Province for the winter camp selection of the national team of Olympus in ICS ICs.

It was one o 'clock a.m. and dark outside. chill wind from the northwest did not scare off the people in the queue. the cold night gave the Little Cat a shiver. why not find a problem to think about? That was none the less better than freezing to death!

People kept jumping the queue. since it was too dark around, such moves wocould not be discovered even by the people adjacent to the queue-jumpers. "If every person in the queue is assigned an integral value and all the information about those who have jumped the queue and where they stand after queue-jumping is given, can I find out the final order of people in the queue?" Thought the Little Cat.

Input

There will be several test cases in the input. Each test case consistsN+ 1 lines whereN(1 ≤N≤ 200,000) is given in the first line of the test case. The nextNLines contain the pairs of valuesPosiAndValiIn the increasing orderI(1 ≤IN). For eachI, The ranges and meaningsPosiAndValiAre as follows:

Posiε [0, I−1]- I-Th person came to the queue and stood right behind Posi-Th person in the queue. The booking office was considered the 0th person and the person at the front of the queue was considered the first person in the queue. Valiε [0, 32767]- I-Th person was assigned the value Vali.

There no blank lines between test cases. Proceed to the end of input.

Output

For each test cases, output a single line of space-separated integers which are the values of people in the order they stand in the queue.

Sample Input

40 771 511 332 6940 205231 192431 38900 31492

Sample Output

77 33 69 5131492 20523 3890 19243

Hint

The figure below shows how the Little Cat found out the final order of people in the queue described in the first test case of the sample input.

 

 

 

My AC status: Yes, I'm not satisfied. It's a little slow, but I'm lazy and don't want to improve it ~~

To be honest, it's easy to understand this question from the perspective of conscience. However, my weakness has always been a line segment tree, so I am desperately refreshing the line segment tree question, but I feel that I have not grown much .. ..

Solution:

 

We can see that the last person inserted to this position is fixed, so we can perform the insert operation from the back. pos and val indicate that val is to be inserted to the pos position, that is to say, a pos position must be set before the pos,

Because it starts from 0.

Line Segment tree: The st Array records that there are currently x vacancies in this range. When each query is inserted, if the left son of this node x> = pos, so you only need to find it in the left son.

Otherwise, you need to find it in the right son, and change pos to pos-left son vacant space ..

 

Pay attention to the big red text. At the beginning, I didn't understand this sentence, and I have been holding down my questions. If you understand the above two sentences, it's so easy!

The following code:

#include 
 
  #define MAX 200100int st[MAX<<4],data[MAX];void build(int left, int right ,int pos){if(left == right){st[pos] = 1;return ;}int mid = (left+right)>>1 ;build(left,mid,pos<<1) ;build(mid+1,right,pos<<1|1) ;st[pos] = st[pos<<1]+st[pos<<1|1] ;}void update(int d , int x , int L , int R , int pos){if(R == L){data[L] = d ;st[pos] = 0 ;return ;}int mid = (L+R)>>1 ;if(x<=st[pos<<1]){update(d,x,L,mid,pos<<1);}else{update(d,x-st[pos<<1],mid+1,R,pos<<1|1) ;}st[pos] = st[pos<<1]+st[pos<<1|1] ;}int main(){int n,pos[MAX],val[MAX];while(~scanf(%d,&n)){build(1,n,1);for(int i = 0 ;i < n ; ++i){scanf(%d%d,&pos[i],&val[i]) ;}for(int i = n-1 ; i >=0 ; --i){update(val[i] , pos[i]+1 , 1 , n , 1);}for(int i = 1 ; i <= n ; ++i){printf(%d,data[i]);if(i != n){printf( ) ;}}printf() ;}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.