HDU 1890 Robotic Sort 伸展树

题目:

http://acm.hdu.edu.cn/showproblem.php?pid=1890

题意:

给定一个数列,数列中的元素可能重复,给这个数列排序,输出每次排序时第i个数所在的位置。保持相等元素的相对位置不变

思路:

有数列序号建树,记录数列中每个元素在树中的编号,每次删掉排完序的元素,然后每次把第i大的元素旋转到根,那么i + siz[son[root][0]]就是元素所在的位置。这个题怎么记录每个元素在树中的位置,有两种方法

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 100010;
struct node
{
    int val, idx;
}arr[N];
int son[N][2], pre[N], key[N], siz[N], lazy[N];
int root, num;
int n;
void new_node(int &x, int fa, int v)
{
    x = ++num;
    arr[v].idx = x;
    pre[x] = fa, key[x] = v;
    siz[x] = 1, lazy[x] = 0;
    son[x][0] = son[x][1] = 0;
}
void push_up(int x)
{
    siz[x] = siz[son[x][0]] + siz[son[x][1]] + 1;
}
void push_down(int x)
{
    if(lazy[x])
    {
        lazy[son[x][0]] ^= 1, lazy[son[x][1]] ^= 1;
        swap(son[x][0], son[x][1]);
        lazy[x] = 0;
    }
}
void _rotate(int x, int dir)
{
    int y = pre[x];
    push_down(y), push_down(x);
    son[y][!dir] = son[x][dir], pre[son[x][dir]] = y;
    if(pre[y]) son[pre[y]][son[pre[y]][1]==y] = x;
    pre[x] = pre[y];
    son[x][dir] = y, pre[y] = x;
    push_up(y);
}
void splay(int x, int goal)
{
    push_down(x);
    while(pre[x] != goal)
    {
        int y = pre[x];
        int z = pre[y];
        push_down(z), push_down(y), push_down(x);
        if(pre[y] == goal) _rotate(x, son[y][0] == x);
        else
        {
            int dir = son[pre[y]][0] == y;
            if(son[y][dir] == x) _rotate(x, !dir), _rotate(x, dir);
            else _rotate(y, dir), _rotate(x, dir);
        }
    }
    push_up(x);
    if(goal == 0) root = x;
}
int get_subs(int x)
{
    push_down(x);
    int t = son[x][1];
    push_down(t);
    while(son[t][0]) t = son[t][0], push_down(t);
    return t;
}
void _delete(int x)
{
    if(!x) return;
    splay(x, 0);
    if(!son[root][0] || !son[root][1])
        root = son[root][0] + son[root][1], pre[root] = 0;
    else
    {
        int t = get_subs(root);
        splay(t, root);
        son[son[root][1]][0] = son[root][0], root = son[root][1];
        pre[son[root][0]] = root, pre[root] = 0;
        push_up(root);
    }
}
void build(int &x, int l, int r, int fa)
{
    if(l > r) return;
    int mid = (l + r) >> 1;
    new_node(x, fa, mid);
    build(son[x][0], l, mid-1, x);
    build(son[x][1], mid+1, r, x);
    push_up(x);
}
int main()
{
    while(scanf("%d", &n), n)
    {
        for(int i = 1; i <= n; i++) scanf("%d", &arr[i].val);
        root = num = 0;
        son[root][0] = son[root][1] = key[root] = siz[root] = pre[root] = lazy[root] = 0;
        build(root, 1, n, 0);
        stable_sort(arr+1, arr+1+n, [](node a, node b){return a.val < b.val;});
        for(int i = 1; i <= n; i++)
        {
            splay(arr[i].idx, 0);
            printf("%d%c", i + siz[son[root][0]], i == n ? '\n' : ' ');
            lazy[son[root][0]] ^= 1;
            _delete(root);
        }
    }
    return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define key_val son[son[root][1]][0];
using namespace std;

const int N = 100010;
int son[N][2], pre[N], key[N], siz[N], lazy[N];
int n, num, root;
struct node
{
    int val, idx;
}arr[N];
bool cmp(node a, node b)
{
    return a.val != b.val ? a.val < b.val : a.idx < b.idx;
}
void new_node(int &x, int fa, int v)
{
    x = v;
    pre[x] = fa;
    siz[x] = 1, lazy[x] = 0;
    son[x][0] = son[x][1] = 0;
}
void push_up(int x)
{
    siz[x] = siz[son[x][0]] + siz[son[x][1]] + 1;
}
void push_down(int x)
{
    if(lazy[x])
    {
        lazy[son[x][0]] ^= 1, lazy[son[x][1]] ^= 1;
        swap(son[x][0], son[x][1]);
        lazy[x] = 0;
    }
}
void _rotate(int x, int dir)
{
    int y = pre[x];
    push_down(y), push_down(x);
    son[y][!dir] = son[x][dir], pre[son[x][dir]] = y;
    if(pre[y]) son[pre[y]][son[pre[y]][1]==y] = x;
    pre[x] = pre[y];
    son[x][dir] = y, pre[y] = x;
    push_up(y);
}
void splay(int x, int goal)
{
    push_down(x);
    while(pre[x] != goal)
    {
        int y = pre[x], z = pre[y];
        push_down(z), push_down(y), push_down(x);
        if(pre[y] == goal) _rotate(x, son[y][0] == x);
        else
        {
            int dir = son[pre[y]][0] == y;
            if(son[y][dir] == x) _rotate(x, !dir), _rotate(x, dir);
            else _rotate(y, dir), _rotate(x, dir);
        }
    }
    push_up(x);
    if(goal == 0) root = x;
}
void build(int &x, int l, int r, int fa)
{
    if(l > r) return;
    int mid = (l + r) >> 1;
    new_node(x, fa, mid);
    build(son[x][0], l, mid - 1, x);
    build(son[x][1], mid + 1, r, x);
    push_up(x);
}
int get_subs(int x)
{
    push_down(x);
    int t = son[x][1];
    push_down(t);
    while(son[t][0]) t = son[t][0], push_down(t);
    return t;
}
void _delete(int x)
{
    //int x = _find(v);
    if(!x) return;
    splay(x, 0);
    if(!son[root][0] || !son[root][1])
        root = son[root][0] + son[root][1], pre[root] = 0;
    else
    {
        int t = get_subs(root);
        splay(t, root);
        son[son[root][1]][0] = son[root][0], root = son[root][1];
        pre[son[root][0]] = root, pre[root] = 0;
        push_up(root);
    }
}
void init()
{
    root = num = 0;
    build(root, 1, n, 0);
}
int main()
{
    while(scanf("%d", &n), n)
    {
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &arr[i].val);
            arr[i].idx = i;
        }
        sort(arr+1, arr+1+n, cmp);
        init();
        for(int i = 1; i <= n; i++)
        {
            splay(arr[i].idx, 0);
            printf("%d%c", i + siz[son[root][0]], i == n ? '\n' : ' ');
            lazy[son[root][0]] ^= 1;
            _delete(root);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值