HDOJ 1890 Robotic Sort

the link to the problem
so recently I spend significant time on data struct learning , this is a splay tree problem .I tried to write the code from the theory but I failed . maybe I can’t learning something new without code…… The code I learning from kuangbin , orzorzorz……

#include <bits/stdc++.h>
#include <cstring>
#define maxn 100010
using namespace std;
int ch[maxn][2],root,n;
int f[maxn];
int val[maxn],rev[maxn],size[maxn];
struct Node
{
    int id,val;
}node[maxn];
inline void update_rev(int x)
{
    if(!x) return ;
    swap(ch[x][0],ch[x][1]);
    rev[x]^=1;
}
inline void up(int x)
{
    size[x] = size[ch[x][0]]+size[ch[x][1]] +1;
}
inline void down(int x)
{
    if(rev[x])
    {
        update_rev(ch[x][0]);
        update_rev(ch[x][1]);
        rev[x] = 0;
    }
}
inline void rotate(int x,int d)
{
    int y = f[x];
    down(y);
    down(x);
    ch[y][!d] = ch[x][d];
    f[ch[x][d]] = y;
    if(f[y]!=0)
    {
        if(ch[f[y]][0] == y) ch[f[y]][0] = x;
        else ch[f[y]][1] = x;
    }
     f[x] = f[y];
     ch[x][d] = y;
     f[y] = x;
    up(y);

}
inline void splay(int x,int s)
{
    down(x);
    while(f[x] != s)
    {
        if(f[f[x]] == s)
        {
            down(f[x]);
            down(x);
            rotate2(x,ch[f[x]][0] == x);
        }
        else
        {
            down(f[f[x]]);
            down(f[x]);
            down(x);
            int y = f[x];
            int d = ch[f[y]][0] == y;
            if(ch[y][d] == x)
            {
                rotate2(x,!d);
                rotate2(x,d);
            }
            else
            {
                rotate2(y,d);
                rotate2(x,d);
            }
        }

    }
    up(x);
    if(s == 0) root = x;
}
inline void build_node(int &x,int fa,int m)
{
    x = m;
    f[x] = fa;
    ch[x][0] = ch[x][1] = 0;
    size[x] = 1;
    rev[x] = 0;

}
inline void build(int &x,int l,int r,int fa)
{
    if(l>r) return;
    int m = (l+r)/2;
    build_node(x,fa,m);
    build(ch[x][0],l,m-1,x);
    build(ch[x][1],m+1,r,x);
    up(x);
}
inline void init()
{
    root = 0;
    ch[root][0] = ch[root][1] = f[root] = size[root] = rev[root] = 0;
    build_node(root,0,n+1);
    build_node(ch[root][1],root,n+2);
    build(ch[ch[root][1]][0],1,n,ch[root][1]);
    up(ch[root][1]);
    up(root);
}
inline int cmp(Node a,Node b)
{
    if(a.val!= b.val) return a.val<b.val;
    else return a.id<b.id;
}
inline int get_kth(int x,int k)
{
    down(x);
    int t = size[ch[x][0]]+1;
    if(t == k) return x;
    if(t>k) return get_kth(ch[x][0],k);
    else return get_kth(ch[x][1],k-t);
}
inline int get_next(int x)
{
    down(x);
    if(ch[x][1] == 0) return -1;
    x = ch[x][1];
    while(ch[x][0])
    {
        x = ch[x][0];
        down(x);
    }
    return x;
}
int main()
{
    while(cin>>n)
    {
        if(!n) break;
        for(int i=1;i<=n;i++)
        {
            node[i].id = i;
            scanf("%d",&node[i].val);
        }
        sort(node+1,node+n+1,cmp);
        init();
        for(int i =1;i<=n;i++)
        {
            splay2(node[i].id,0);
            printf("%d",size[ch[root][0]]);
            if(i<n) printf(" ");
            else printf("\n");
            splay2(get_kth(root,i),0);
            splay2(get_next(node[i].id),root);
            update_rev(ch[ch[root][1]][0]);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值