poj-2828 Buy Tickets (tree line, queuing problem, reverse thinking)

Topic Address: POJ 2828 Buy Tickets

 

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 Olympiad in Informatics.

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 would 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 consists of N + 1 lines where N (1 ≤ N ≤ 200,000) is given in the first line of the test case. The next N lines contain the pairs of values Posi and Vali in the increasing order of i(1 ≤ i ≤ N). For each i, the ranges and meanings of Posi and Vali are as follows:

  • Posi ∈ [0, i − 1] — The i-th person came to the queue and stood right behind the 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] — The 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

4
0 77
1 51
1 33
2 69
4
0 20523
1 19243
1 3890
0 31492

Sample Output

77 33 69 51
31492 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.

 

Meaning of the questions: 
queue up for tickets when to jump the queue. 
Give some number pairs representing a person's position pos_i want to insert and his val_i, val obtained the final order of the queue.

Analysis: 
This is a very clever title. 

Observed, inserted into the last position of the person position is fixed, we can insert from the rear, pos, val val to be inserted into the representative position pos, then that set aside pos pos front position,

We can be considered the opposite of, start from the back of the station, people behind the position will not change once it is determined, and assumptions people have stood behind the right place, then when that person station, current position those who are already on the back, as long as the number num spaces, and that it can determine the position of the individual stations.

For example, in the example of crazy_apple, Xie ~

Segment tree node number stored in the gaps in this paragraph, and then reverse to insert pos:

    For example: 77 0
         . 1 51 is
         . 1 33 is
         2 69

  First take: 2 69 - - -69- - (need to front insertion slot 3)

       Then take: 1 33---33--69--- (two front space required to insert)

       Then take: 151---33--69- -51- (2 gaps need to front insertion) so that the front only a gap into the rear space

  Then take: 0 77-77--33--69- -51- (in front of a space required to insert)

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <set>
 9 #include <math.h>
10 const int INF=0x3f3f3f3f;
11 typedef long long LL;
12 const int maxn=2e5+10;
13 using namespaceSTD;
 14  
15  int n-;
 16  int the Add [MAXN << 2 ]; // number of gaps 
. 17  int POS [MAXN]; // insert position 
18 is  int Val [MAXN]; // initial values 
. 19  int ANS [ MAXN]; // final station 
20 is  
21 is  void the Build ( int L, int R & lt, int RT)
 22 is  {
 23 is      the Add [RT] = L-R & lt + . 1 ; // initial number of gaps 
24      IF (L == R & lt)
 25         return ;
 26 is      int m = (L + R & lt) >> . 1 ;
 27      the Build (L, m, RT << . 1 );
 28      the Build (m + . 1 , R & lt, RT << . 1 | . 1 ); 
 29  }
 30  
31 is  int the Update ( int L, int R & lt, int NUM, int RT) // NUM interruption is required in front of the number of vacancies 
32  {
 33 is      the Add [RT] -; // number of gaps Save. 1 
34 is      IF (L == R & lt)
 35          return L; // return the insertion position
36      int m = (L + R & lt) >> . 1 ;
 37 [      IF (the Add [RT << . 1 ]> = NUM) // if left child spaces greater than or equal to the left of the insertion position of the insertion 
38 is          the Update (L, m, NUM , RT << . 1 );
 39      the else // if the number is less than the space left blank num, insert the right, num right position to be inserted to the left of minus 
40      {
 41 is          num-the Add = [RT << . 1 ];
 42 is          the Update (m + . 1 , R & lt, NUM, RT << . 1 | . 1 );
 43 is      }
 44 is  }
 45  
46 is  int main ()
 47  {
 48      the while(~ Scanf ( " % D " , & n-))
 49      {
 50          the Build ( . 1 , n-, . 1 );
 51 is          for ( int I = . 1 ; I <= n-; I ++ )
 52 is          {
 53 is              Scanf ( " % D% D " , & POS [I], & Val [I]);
 54 is          }
 55          for ( int I = n-; I> = . 1 ; i--) // reversed updates, it may be determined the position of the last 
56 is          {
 57 is              int Key Update = ( 1, n-, POS [I] + . 1 , . 1 ); // get inserted where 
58              ans [Key] Val = [I]; // stored ans array 
59          }
 60          for ( int I = . 1 ; I <= n-; ++ I )
 61 is          {
 62 is              IF ! (I = . 1 )
 63 is                  the printf ( "  " );
 64              the printf ( " % D " , ANS [I]);
 65          }
 66          the printf ( " \ n- " );
 67     }
 68      return  0 ;
69 }

 

Guess you like

Origin www.cnblogs.com/jiamian/p/11281076.html