Buy Tickets
Time Limit: 4000MS |
|
Memory Limit: 65536K |
Total Submissions: 16584 |
|
Accepted: 8268 |
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 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 is 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
4
0
1, 1 2, 4
0 20523
1 19243
1 3890
0 31492
Sample Output
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 CAs E of the sample input.
Test instructions
There are n people in the railway station to buy tickets, because of the dark, so you jump in the queue no one will see, now give the N-person line-up goal (allow yourself in front of a few people), and his value (here is no use, only in the output), let you find out the N personal goals, output their corresponding value.
#include <stdio.h> #include <string.h> #include <algorithm> #define N 200010 using namespace std;
struct ZZ {int l;
int R;
int V;
int p;
}q[n<<2];
struct Z {int p;
int V;
}q[n];
int dis[n];
int k;
void build (int gen,int l,int r) {q[gen].l=l;
Q[gen].r=r;
if (l==r) {q[gen].p=1;
return;
} int mid= (L+R)/2;
Build (Gen<<1,l,mid);
Build (Gen<<1|1,mid+1,r);
q[gen].p=q[gen<<1].p+q[gen<<1|1].p;
} void Update (int gen,int V,int p) {if (Q[GEN].L==Q[GEN].R) {q[gen].v=v;
Q[gen].p=0;
return;
} if (q[gen<<1].p>p) update (GEN<<1,V,P);
else update (GEN<<1|1,V,P-Q[GEN<<1].P);
q[gen].p=q[gen<<1].p+q[gen<<1|1].p;
} void query (int gen) {if (Q[GEN].L==Q[GEN].R) {dis[k++]=q[gen].v;
return;
} query (gen<<1);
Query (gen<<1|1);
} int main () {int n;
while (scanf ("%d", &n)!=eof) {k=0;
for (int i=0;i<n;i++) scanf ("%d%d", &Q[I].P,&Q[I].V);
Build (1,1,n); for (int i=n-1;i>=0;i--) update (1,Q[I].V,Q[I].P);
Query (1);
for (int i=0;i<n-1;i++) printf ("%d", dis[i]);
printf ("%d\n", dis[n-1]);
} return 0; }