POJ2828 Buy tickets[tree Array k small value reverse]

Source: Internet
Author: User

Buy Tickets
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 19012 Accepted: 9442

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 Test Instructions: n Individuals, queue line position pi, value VI, after the end of the value sequence consider the last person must be in the p[n]+1 position, the first n-1 person in the p[n-1]+1 position (nth person does not count the occupied position)reverse processing, everyone's position is the current p[i]+1 small value, but also to delete this personusing tree-like arrays to dynamically find K-values and deletionsWARN:Binary starting from 16 small, starting from 18 2^18=262144
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespaceStd;typedefLong Longll;Const intn=2e5+5; inlineintRead () {CharC=getchar ();intx=0, f=1;  while(c<'0'|| C>'9'){if(c=='-') f=-1; c=GetChar ();}  while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx*F;}intN,ans[n];structpeople{intP,v;} A[n];intC[n];inlineintLowbit (intx) {returnx&-x;} Inlinevoidbuild () {//memset (c,0,sizeof (c));     for(intI=1; i<=n;i++) {C[i]++; if(I+lowbit (i) <=n) c[i+lowbit (i)]+=C[i]; }}inlinevoidAddintPintv) {     for(;p <=n;p+=lowbit (p)) c[p]+=v;} InlineintKthintk) {    intx=0, cnt=0;  for(intI= -; i>=0; i--) {x+=(1<<i); if(x>=n| | CNT+C[X]&GT;=K) x-= (1<<i); Elsecnt+=C[x]; }    returnx+1;}intMain () { while(SCANF ("%d", &n)! =EOF) {         for(intI=1; i<=n;i++) A[i].p=read (), a[i].v=read ();        Build ();  for(inti=n;i>=1; i--){            intPos=kth (a[i].p+1); Ans[pos]=a[i].v; Add (POS,-1); }         for(intI=1; i<=n;i++) printf ("%d%c", Ans[i],i==n?'\ n':' '); }}

POJ2828 Buy tickets[tree Array k small value reverse]

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.