POJ-2828 Buy Tickets (平衡树 splay 节点个数维护排名)

Buy Tickets
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 21536 Accepted: 10542

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.



#include <stdio.h>
#define maxn 200005
int pre[maxn], ch[maxn][2], key[maxn], sz[maxn], rt, tot;
void newNode(int& r, int fa, int v){
    r = ++tot;
    pre[r] = fa;
    key[r] = v;
    sz[r] = 1;
    ch[r][1] = ch[r][0] = 0;
}
void rotate(int r, int kind){
    int y = pre[r];
    ch[y][!kind] = ch[r][kind];
    pre[ch[r][kind]] = y;
    if(pre[y]){
        ch[pre[y]][ch[pre[y]][1] == y] = r;
    }
    pre[r] = pre[y];
    ch[r][kind] = y;
    pre[y] = r;
    sz[r] = sz[y];
    sz[y] = 1 + sz[ch[y][0]] + sz[ch[y][1]];
}
void splay(int r, int goal){
    while(pre[r] != goal){
        if(pre[pre[r]] == goal){
            rotate(r, ch[pre[r]][0] == r);
        }
        else{
            int y = pre[r];
            int kind = ch[pre[y]][0] == y;
            if(ch[pre[r]][kind] == r){
                rotate(r, !kind);
                rotate(r, kind);
            }
            else{
                rotate(y, kind);
                rotate(r, kind);
            }
        }
    }
    if(goal == 0) rt = r;
}
void findkth(int k){
    int r = rt;
    while(sz[ch[r][0]] + 1 != k){
        if(sz[ch[r][0]] + 1 < k){
            k -= sz[ch[r][0]] + 1;
            r = ch[r][1];
        }
        else{
            r = ch[r][0];
        }
    }
    splay(r, 0);
}
void add(int p, int v){
    if(p == 0){
        int r = rt;
        sz[r]++;
        while(ch[r][0]){
            r = ch[r][0];
            sz[r]++;
        }
        newNode(ch[r][0], r, v);
        splay(ch[r][0], 0);
        return;
    }
    findkth(p);
    int r = rt;
    newNode(rt, 0, v);
    ch[rt][1] = ch[r][1];
    pre[ch[r][1]] = rt;
    ch[r][1] = 0;
    sz[r] -= sz[ch[rt][1]];
    ch[rt][0] = r;
    pre[r] = rt;
    sz[rt] = 1 + sz[ch[rt][1]] + sz[ch[rt][0]];
}
void dfs(int r, int& d){
    if(ch[r][0]){
        dfs(ch[r][0], d);
    }
    printf("%d%s", key[r], d == tot ? "\n" : " ");
    d++;
    if(ch[r][1]){
        dfs(ch[r][1], d);
    }
}
int main(){
    int n, p, v;
    while(scanf("%d", &n) != EOF){
        rt = tot = 0;
        for(int i = 1; i <= n; ++i){
            scanf("%d %d", &p, &v);
            if(rt == 0){
                newNode(rt, 0, v);
            }
            else{
                add(p, v);
            }
        }
        int d = 1;
        dfs(rt, d);
    }
}

/*
题意:2e5个人排队,一个个的插入,每个人都有一个ID和插入数字x,插入数字表示它插入前面有x人的位置。
比如x=0,则排在第一位,x=3则排在第4位,后面的人依次顺位到它后面。

思路:
这里插入的时候要把后面的数字全部往后移一位,暴力移很麻烦,用splay的话,只需要cut掉相应的边,然后把新人插
进去,再把后面的子树接上就可以了。唯一要注意的是sz[]数组的转移。
最后输出答案的时候dfs一遍即可。splay的一道不错的练手和模版题。
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值