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) was given in the first line of The test case. The nextN lines contain the pairs of values posi and Vali in the increasing order ofI (1≤ i ≤ N). For each i, the ranges and meanings ofposi and Vali is as follows:
- posi ∈[0, i ? 1]-the i-th person came to the queue and stood right behind theposi-T H 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 valueVali.
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.
The main reason: Because the data is too large, violent failure. It can be solved with a line tree. Because the last inserted queue is definitely at that location. So reverse insertion is a good choice. Each point represents the remaining empty space for the interval. If a position is less than the left child space equal to the point, then the left interval must be inserted, but if it is greater than the vacancy, the left interval is subtracted, and the position of the person should be ranked in the right interval. In fact, it is very messy, as long as you draw a simulation will know the truth of it.
#include <iostream> #include <cstdio> #include <cstring> #define MEM (a) memset (A,0,1izeof (a)); using namespace Std;int pos[200005]; int value[200005]; int res[200005]; Save result int arr[800005]; Record segment tree void pushup (int k) {Arr[k] = arr[k << 1] + arr[(k << 1) | 1];//update the number of seats in the parent node}void segtree (int k,int l,in T r) {if (L = = r) {Arr[k] = 1; The leaves are initially 1 return;} Segtree (k << 1,l, (l + R) >> 1), Segtree ((k << 1) | 1, (l + R)/2 + 1,r);p ushup (k);} void update (int k,int l,int r,int x,int y) {if (L = = r) {Arr[k] = 0; The position is accounted for and becomes 0 res[l] = y; Record the result of the point return;} int mid = (L + R) >> 1;if (x <= arr[k << 1])//The position is less than or equal to the left child vacancy number, description insert left child, {//Because the person in front of the position must be X-1 Update (k << 1,l,mid,x,y);} else{//Insert right child, here to think carefully, the front position is occupied equivalent to this person post-move update ((k << 1) | 1,mid + 1,r,x-arr[k << 1],y);} Pushup (k);} int main () {int n;while (scanf ("%d", &n)! = EOF) {for (int i = 1;i <= n;i++) {scanf ("%d%d", &pos[i],&value[i]);} SegTree (1,1,n); for (int i = n;i > 0;i--) {update (1,1,n,pos[i] + 1,value[i]);} for (int i = 1;i < n;i++) {printf ("%d", Res[i]);} printf ("%d\n", Res[n]);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2828-buy Tickets