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 is approaching, but unluckily the Little Cat still had schedules going here and there. Now, he had-to-train-Mianyang, Sichuan Province for the winter camp selection of the national team of Olympia D in Informatics.
It was one o ' clock a.m. and dark outside. Chill Wind from the northwest does not scare off the people in the queue. The cold night gave the Little Cat a shiver. Why isn't find a problem to think? That is none the less better than freezing to death!
People kept jumping the queue. Since It is too dark around, such moves would not being discovered even by the people adjacent to the queue-jumpers. "If every person in the queue is assigned a integral value and all the information to those who has jumped the queue And where they stand after queue-jumping are given, can I find out the final order of people in the queue? " Thought the Little Cat.
Input
There'll is 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 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 is considered the first person In the queue.
- Vali ∈[0, 32767]-the i-th person is assigned the value Vali.
There no blank lines between test cases. Proceed to the end of input.
Output
For each test cases, output a space-separated integers which is the values of people in the order they STA nd 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 CAs E of the sample input.
Source
POJ monthly--2006.05.28, Zhu, Zeyuan submit address: Buy TicketsThe main topic: n people, each person in the queue selected a position to insert, ask the final sequence is what;Analysis:back in time: Because the last person must be standing in the place they want to occupy, so from the forward to facilitate the processing;first of all, there is no one, the sequence is all 1 (behind why);then insert the last person, and in that case two, he must be inserted in the place where he wants to be, so the place where he stands is 0, the sequence becomes 0 1 1 1;then consider the penultimate person, who will go to the second position but before he has inserted the last person, so his final position must not be in the second position;Where is it in the first place? The answer is: the second one 1! Why, because 1 means that this position is not occupied, so he will stand in the second not occupied position;we can use a tree-like array to maintain the value of the prefix 1 (this is why the initial value is 1!). );but for no one to enumerate once is obviously unrealistic, and because of the prefix and to satisfy the monotony, so direct violence two minutes OK;should be particularly well understood;do not understand the code to see the same;The code:
//by Zzhbr#include <iostream>#include<cstdio>#include<algorithm>using namespacestd;intN;structpro{intPOS; intnum;} pr[200010];intans[200010];inttr[200010];intLowbit (intx) { returnX &-x;}voidAddintXinty) { while(x <=N) {tr[x]+=y; X+=lowbit (x); }}intSumintx) { intAns =0; while(X! =0) {ans+=Tr[x]; X-=lowbit (x); } returnans;}intMain () { while(SCANF ("%d", &n)! =EOF) { for(Registerinti =1; I <= N; i + +) ans[i] =0; for(Registerinti =1; I <= N; i + +) {scanf ("%d%d", &pr[i].pos, &pr[i].num); Add (i,1); } for(Registerinti = n; I >=1; I--) { intp = Pr[i].pos +1; if(SUM (p) = =p) {Ans[p]=Pr[i].num; Add (P,-1); Continue; } intL = p, r =N; while(L <r) {intMID = L + R >>1; if(Sum (mid) >= p) r =mid; ElseL = mid +1; } Ans[l]=Pr[i].num; Add (L,-1); } for(Registerinti =1; I <= N; i + +) {printf ("%d", Ans[i]); } printf ("\ n"); } return 0;}
ZZHBR
POJ2828 Buy Tickets tree-like array