HDOJ 1890 Robotic Sort

来源:互联网 发布:软考程序员有必要考吗 编辑:程序博客网 时间:2024/06/05 15:55

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 100010using 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;}
原创粉丝点击