"poj2828" Buy tickets segment tree line queue problem

Source: Internet
Author: User

"poj2828" 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 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 next N lines contain the pairs of values posiand Vali in the increasing order of I (1≤ iN). 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 Inpu

4

0 1 Wuyi 1 2 40 20523 1 19243 1 3890 0 31492

Sample Output

The

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.

Exercises

The magic of line tree, deepen understanding .....

Code

1#include <cstdio>2#include <cmath>3#include <cstring>4#include <ctime>5#include <iostream>6#include <algorithm>7#include <Set>8#include <vector>9#include <queue>Ten#include <typeinfo> One#include <map> A#include <stack> -typedefLong Longll; - #defineINF 0x7fffffff the using namespacestd; - inline ll read () - { -ll x=0, f=1; +     CharCh=GetChar (); -      while(ch<'0'|| Ch>'9') +     { A         if(ch=='-') f=-1; atCh=GetChar (); -     } -      while(ch>='0'&&ch<='9') -     { -x=x*Ten+ch-'0'; -Ch=GetChar (); in     } -     returnx*F; to } +  - //************************************************************************************** the structSS * { $     intl,r,x;Panax Notoginseng} tr[200001*5]; - intN; the inta[200001]; + voidBuildintKintSintt) A { theTr[k].l=s; +Tr[k].r=T; -     if(s==t) $     { $tr[k].x=1; -         return; -     } the     intMid= (s+t) >>1; -Build (k<<1, s,mid);WuyiBuild (k<<1|1, mid+1, T); thetr[k].x=tr[k<<1].x+tr[k<<1|1].x; - } Wu voidUpdateintKintXinty) - { About     if(tr[k].l==TR[K].R) $     { -tr[k].x=0; -a[tr[k].l]=y; -         return; A     } +     if(tr[k<<1].X&GT;=X) Update (k<<1, x, y);//can be left down as far as possible, and you have to understand that this is a queue problem ^-^!! the     ElseUpdate (k<<1|1,x-tr[k<<1].x,y); -tr[k].x=tr[k<<1].x+tr[k<<1|1].x; $ } the intMain () the { the     intx[200001]; the     inty[200001]; -      while(SCANF ("%d", &n)! =EOF) in     { theBuild1,1,200001); the          for(intI=1; i<=n; i++) About         { thescanf"%d%d",&x[i],&y[i]); thex[i]++; the         } +          for(intI=n; i>=1; i--) -         { theUpdate1, X[i],y[i]);Bayi         } the          for(intI=1; i<n; i++) the         { -printf"%d", A[i]); -         } theprintf"%d\n", A[n]); the     } the  the  -     return 0; the}

"poj2828" Buy tickets segment tree line queue problem

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.