天天看點

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

const int N = ;
int son[N][], 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] = , lazy[x] = ;
    son[x][] = son[x][] = ;
}
void push_up(int x)
{
    siz[x] = siz[son[x][]] + siz[son[x][]] + ;
}
void push_down(int x)
{
    if(lazy[x])
    {
        lazy[son[x][]] ^= , lazy[son[x][]] ^= ;
        swap(son[x][], son[x][]);
        lazy[x] = ;
    }
}
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]][]==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][] == x);
        else
        {
            int dir = son[pre[y]][] == y;
            if(son[y][dir] == x) _rotate(x, !dir), _rotate(x, dir);
            else _rotate(y, dir), _rotate(x, dir);
        }
    }
    push_up(x);
    if(goal == ) root = x;
}
void build(int &x, int l, int r, int fa)
{
    if(l > r) return;
    int mid = (l + r) >> ;
    new_node(x, fa, mid);
    build(son[x][], l, mid - , x);
    build(son[x][], mid + , r, x);
    push_up(x);
}
int get_subs(int x)
{
    push_down(x);
    int t = son[x][];
    push_down(t);
    while(son[t][]) t = son[t][], push_down(t);
    return t;
}
void _delete(int x)
{
    //int x = _find(v);
    if(!x) return;
    splay(x, );
    if(!son[root][] || !son[root][])
        root = son[root][] + son[root][], pre[root] = ;
    else
    {
        int t = get_subs(root);
        splay(t, root);
        son[son[root][]][] = son[root][], root = son[root][];
        pre[son[root][]] = root, pre[root] = ;
        push_up(root);
    }
}
void init()
{
    root = num = ;
    build(root, , n, );
}
int main()
{
    while(scanf("%d", &n), n)
    {
        for(int i = ; i <= n; i++)
        {
            scanf("%d", &arr[i].val);
            arr[i].idx = i;
        }
        sort(arr+, arr++n, cmp);
        init();
        for(int i = ; i <= n; i++)
        {
            splay(arr[i].idx, );
            printf("%d%c", i + siz[son[root][]], i == n ? '\n' : ' ');
            lazy[son[root][]] ^= ;
            _delete(root);
        }
    }
    return ;
}