POJ 2828 Buy Tickets

Source: Internet
Author: User

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
  
   
    This problem with a line tree to do, look at other people's blog ideas after I made it out, in fact, the problem and reverse of the problem is similar, is inserted from the back, because the last position must be determined, so from the last start to determine the location. Determine the time can build a line tree, with the POS store the current point where the remaining position, value represents the value of this point, this is only used when B[I].L==B[I].R, equivalent to the root node. Each time you enter a node to see if the left sub-tree's POS is larger than the inserted position, if it is inserted directly, if not go to the right subtree, and then the position of the left subtree to subtract the position, that is pos-b[i*2].pos.
   
  #include <stdio.h> #include <string.h> #define MAXN 200006int a[maxn],c[maxn],d[maxn];struct node{int pos,value,l,r;} b[4*maxn];void Build (int l,int r,int i) {int mid;B[i].l=l;b[i].r=r;if (l==r) {B[i].pos=1;return;}Mid= (L+R)/2;Build (L,MID,I*2);Build (mid+1,r,i*2+1);B[i].pos=b[i*2].pos+b[i*2+1].pos;} void update (int pos,int value,int i) {if (B[I].L==B[I].R) {b[i].value=d[b[i].l]=value;b[i].pos=0;Return}b[i].pos--;if (b[i*2].pos>pos) update (POS,VALUE,I*2);else {Update (POS-B[I*2].POS,VALUE,I*2+1);}}int Main () {int n,m,i,j,t;while (scanf ("%d", &n)!=eof){for (i=1;i<=n;i++) {scanf ("%d%d", &a[i],&c[i]);}Build (1,maxn,1);for (i=n;i>=1;i--) {Update (a[i],c[i],1);}for (i=1;i<=n;i++) {printf ("%d", d[i]);}printf ("\ n");}return 0;}


POJ 2828 Buy Tickets

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.