0
点赞
收藏
分享

微信扫一扫

POJ 2828 Buy Tickets(线段树)


Buy Tickets


Time Limit: 4000MS

 

Memory Limit: 65536K

Total Submissions: 14905

 

Accepted: 7435


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 was approaching, but unluckily the Little Cat still had schedules going here and there. Now, he had to travel by train to Mianyang, Sichuan Province for the winter camp selection of the national team of Olympiad in Informatics.

It was one o’clock a.m. and dark outside. Chill wind from the northwest did not scare off the people in the queue. The cold night gave the Little Cat a shiver. Why not find a problem to think about? That was none the less better than freezing to death!

People kept jumping the queue. Since it was too dark around, such moves would not be discovered even by the people adjacent to the queue-jumpers. “If every person in the queue is assigned an integral value and all the information about those who have jumped the queue and where they stand after queue-jumping is given, can I find out the final order of people in the queue?” Thought the Little Cat.


Input


There will be 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 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 was considered the first person in the queue.
  • Vali ∈ [0, 32767] — The i-th person was assigned the value Vali.

There no blank lines between test cases. Proceed to the end of input.


Output


For each test cases, output a single line of space-separated integers which are the values of people in the order they stand in the queue.


Sample Input


4 0 77 1 51 1 33 2 69 4 0 20523 1 19243 1 3890 0 31492


Sample Output


77 33 69 51 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 case of the sample input.



POJ   2828  Buy Tickets(线段树)_#include


Source


POJ Monthly--2006.05.28, Zhu, Zeyuan





     题意:第一行输入一个整数n,代表有n个人,以后的n行每行有两个数x,y代表把值为y的数放在第x个数之后(x==0代表值为y的数在第一个位置),要求按顺序输出这个序列。


     思路:用线段树来做,将输入的数据倒着用线段树更新,这样可以保证当前的数会是它的最后一次变动。



#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>

using namespace std;

const int maxn = 200001;

struct node
{
    int l;
    int r;
    int cnt;
    int sum;
}q[maxn<<4];

int a[200001],b[200001];
int n;
int pf;

void build(int l,int r,int rt)
{
    q[rt].l = l;
    q[rt].r = r;
    q[rt].cnt = 0;
    q[rt].sum = 0;
    if(l == r)
    {
        q[rt].sum = 1;
        return ;
    }
    int mid = (l+r)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    q[rt].sum = q[rt<<1].sum + q[rt<<1|1].sum;
}

void updata(int id,int k,int l,int r,int rt)
{
    if(id<0)
    {
        pf = 1;
        return ;
    }
    if(pf == 1)
    {
        return ;
    }
    if(l == r && q[rt].sum == 1)
    {
        q[rt].cnt = k;
        q[rt].sum = 0;
        return ;
    }
    int mid = (l+r)>>1;
    if(id>q[rt<<1].sum)
    {
        id = id - q[rt<<1].sum;
        updata(id,k,mid+1,r,rt<<1|1);
    }
    else
    {
        updata(id,k,l,mid,rt<<1);
    }
    q[rt].sum = q[rt<<1].sum + q[rt<<1|1].sum;
}

void qurry(int l,int r,int rt)
{
    if(l == r)
    {
        if(l == 1)
        {
            printf("%d",q[rt].cnt);
        }
        else
        {
            printf(" %d",q[rt].cnt);
        }
        return ;
    }
    int mid = (l+r)>>1;
    qurry(l,mid,rt<<1);
    qurry(mid+1,r,rt<<1|1);
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&a[i],&b[i]);
        }
        build(1,n,1);
        for(int i=n-1;i>=0;i--)
        {
            pf = 0;
            updata(a[i]+1,b[i],1,n,1);
        }
        qurry(1,n,1);
        printf("\n");
    }
    return 0;
}




举报

相关推荐

0 条评论