POJ 2828 Buy Tickets 线段树

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 ≤ iN). 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.

题意:

给你N个人的队列,每个人都有想站的位置,要你从前往后的给他们排序,输出最后的结果。注意,后面的人会覆盖前面的。就是是原本在该位置上的人往后移动一个位置。

思路:

我们发现, 最后一个元素的值一定是题目所给的值,最后一个元素的值确定了, 那么我们只需要从后往前遍历就可以确定整个序列的顺序了。

那么重点来了, 我们如何建立线段树呢, 我们维护线段树的什么呢。 恩,我们可以建立一个表示区间有几个空位的线段数, 然后将所要求的序列倒着遍历, 然后更新线段树的值。

重复此步骤。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=200005;
int n;
int a[maxn];//表示插入位置
int data[maxn];//表示编号
int tree[maxn<<2];//线段树
int b[maxn];//表示所求的位置
void pushup (int re)
{
    tree[re]=tree[re<<1]+tree[re<<1|1];
}
void build (int l,int r,int re)
{
    if(l==r)
    {
        tree[re]=1;
        return;
    }
    int mid=(l+r)>>1;
    build (l,mid,re<<1);
    build (mid+1,r,re<<1|1);
    pushup (re);
}
void update (int data,int l,int r,int re,int ans)
{
    if(l==r)
    {
        //位置已经确定下来了
        b[l]=ans;
        tree[re]--;
        return ;
    }
    int mid=(l+r)>>1;
    if(data<=tree[re<<1])
        update (data,l,mid,re<<1,ans);
    else
        update (data-tree[re<<1],mid+1,r,re<<1|1,ans);
    pushup (re);
}
int main()
{
    while (scanf("%d",&n)!=EOF)
    {
        build (1,n,1);
        for (int i=0;i<n;i++)
        {
            scanf("%d%d",&a[i],&data[i]);
        }
        for (int i=n-1;i>=0;i--)
        {
            update (a[i]+1,1,n,1,data[i]);
        }
        for (int i=1;i<=n;i++)
            printf("%d ",b[i]);
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/81673659