POJ 2828 Buy Tickets "segment tree" "Reverse Insert + single point update + interval sum"

Source: Internet
Author: User

Buy Tickets
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 16067 Accepted: 8017

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.


Test instructions: There are N personal rankings to buy tickets and now gives everyone to insert the position POS (0<=pos<=n-1) as well as his value Val. After inserting n individuals, a new sequence is formed. Now let you sequentially output the value Val corresponding to each position pos in the new sequence.



Analysis: First we know-the last person will definitely get the current team where he wants to be, and if we move forward one stage, the penultimate person will be able to get the position he wants ... So? We can choose the inverse procedure to insert, so that, for the person inserted in the POS position, he must have a POS slot (here 0<=pos<=n-1), so we just need to find the front of the POS empty node insertion, The number of slots in the node is then cleared by 0 and the number of slots in other intervals is maintained.


idea: Use sum[] to record the number of remaining slots in the interval. For each insertion, if the left son's sum[] value is greater than POS, then only the left son needs to be found. Otherwise in the right son to find, but note pos = pos-left son's sum[] (because the left son already has sum[], we just need to find pos-sum[] a line).  



AC Code:


#include <cstdio> #include <cstring> #include <algorithm> #define MAXN 200000+10using namespace std; struct node{int pos, Val;}; Node num[maxn];int sum[maxn<<2];//record interval vacancy number void pushup (int o) {Sum[o] = sum[o<<1] + sum[o<<1|1];}    void build (int o, int l, int r) {if (L = = r) {Sum[o] = 1;//Initial Each node has an empty return;    } int mid = (L + r) >> 1;    Build (O<<1, L, mid);    Build (O<<1|1, mid+1, R); Pushup (o);}  int rec[maxn];//Record order int query (int o, int l, int r, int pos) {if (L = = r) {Sum[o] = 0;//Insert Empty number less one return    L    } int mid = (L + r) >> 1;    int res;    if (Sum[o<<1] > POS)//Find res in the left son = query (O<<1, L, Mid, POS);    else//the right son for res = query (o<<1|1, mid+1, R, Pos-sum[o<<1]); Pushup (o);//maintain the number of vacancies in other intervals return res;    int main () {int N; while (scanf ("%d", &n)! = EOF) {for (int i = 1; I <= N; i++) scanf ("%d%d", &num[I].pos, &num[i].val);        Build (1, 1, N);        for (int i = N; I >= 1; i--) rec[query (1, 1, N, num[i].pos)] = Num[i].val;            for (int i = 1; I <= N; i++) {if (i > 1) printf ("");        printf ("%d", rec[i]);    } printf ("\ n"); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2828 Buy Tickets "segment tree" "Reverse Insert + single point update + interval sum"

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.