POJ 2828 Buy Tickets (segment tree)

Source: Internet
Author: User

Buy Tickets
Time Limit: 4000MS Memory Limit: 65536KB 64bit IO Format: %i64d &%i64u

Submit Status

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 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

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.


Main topic:

N Personal Queued (can jump in line), Pi represents I queue in front of his number, vi for I mark, the final output is the final team sequence.

Problem Solving Ideas:
because n is so big, we can't just add it all by ourselves. We can choose to maintain with the segment tree, the content of the maintenance is the number of slots within the interval. Each position is initially 1, and if the position is a person, it becomes 0.
If we were to jump in line from the past to the next, then because later people may be standing in front of the people who already exist, then the position we get at once is not necessarily the final position. So we have to choose from the back to the queue, the people behind will not affect the front of the people, if the front of the people did not come, the location can be vacated, so as to ensure that everyone's first queued position is the final position.

Reference code:

#include <set> #include <map> #include <stack> #include <queue> #include <cmath> #include <vector> #include <cctype> #include <cstdio> #include <cstring> #include <iostream># include<algorithm>using namespace std; #define Lson L,mid,cur<<1#define Rson mid+1,r,cur<<1|1# Define root 1,n,1const double eps=1e-10;const int inf=0x3f3f3f3f;const int maxn=200000+50;typedef long Long ll;int n,sum[m axn<<2],loc[maxn],num[maxn],people[maxn];void pushup (int cur) {sum[cur]=sum[cur<<1]+sum[cur<<1| 1];}        void build (int l,int r,int cur) {if (l==r) {sum[cur]=1;    return;    } int mid= (L+R)/2;    Build (Lson);    Build (Rson); Pushup (cur);}        void update (int a,int b,int l,int r,int cur) {if (l==r) {people[l]=b;        sum[cur]=0;    return;    } int mid= (L+R)/2;    if (sum[cur<<1]>=a) update (A,b,lson);    else update (A-sum[cur<<1],b,rson); Pushup (cur);} int main() {#ifndef Online_judge freopen ("In.txt", "R", stdin), #endif//Online_judge while (scanf ("%d", &n)!=eof) {        Build (root);        for (int i=1;i<=n;i++) scanf ("%d%d", &loc[i],&num[i]);        for (int i=n;i>=1;i--)//queue up from the back to update (loc[i]+1,num[i],root),//note loc[i] to add 1 for (int i=1;i<=n;i++)            {if (i!=n) printf ("%d", people[i]);        else printf ("%d\n", People[i]); }} return 0;}


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

POJ 2828 Buy Tickets (segment tree)

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.