HDU 1890 Robotic sort (splay)

就是一些简单的旋转和删除操作
建议在这道题的时候把SPLAY写成自己的风格,不要一味的套模板,手写能更好的理解splay
注意一下什么时候该push_down 什么时候该push_up 一步错就WA

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define rfor(i,a,b) for(i=a;i<=b;++i)
#define lfor(i,a,b) for(i=a;i>=b;--i)
#define sfor(i,a,h) for(i=h[a];i!=-1;i=e[i].next)
#define mem(a,b) memset(a,b,sizeof(a))
#define mec(a,b) memcpy(a,b,sizeof(b))
#define cheak(i) printf("%d ",i)
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
#define inf 0x3f3f3f3f
#define lowbit(x) (x&(-x))
typedef long long LL;
#define maxn 100005
#define maxm maxn*maxn
#define lson(x) (splay[x].son[0])
#define rson(x) (splay[x].son[1])
struct node
{
    int val,pos;
}A[maxn];
int cmp(node u,node v)
{
    if(u.val==v.val) return u.pos<v.pos;
    return u.val<v.val;
}
struct SSplay
{
    int fa,size,flag,son[2];
    void init()
    {
        fa=size=flag=son[0]=son[1]=0;
    }
}splay[maxn];
int tots,root;
void NewNode(int &x,int fa,int k)
{
    x=k;
    splay[x].fa=fa;
    splay[x].size=1;
    splay[x].flag=0;
    splay[x].son[0]=splay[x].son[1]=0;
}
void Push_up(int x);
void build(int &x,int l,int r,int fa)
{
    if(l>r) return ;
    int mid=(l+r)/2;
    NewNode(x,fa,mid);
    if(l<mid) build(lson(x),l,mid-1,x);
    if(r>mid) build(rson(x),mid+1,r,x);
    Push_up(x);
}
void init(int n)
{
    tots=root=0;
    splay[0].init();
    build(root,1,n,0);
}
void Push_down(int x)
{
    if(!splay[x].flag) return ;
    swap(lson(x),rson(x));
    splay[x].flag=0;
    splay[lson(x)].flag^=1;
    splay[rson(x)].flag^=1;
}
void Push_up(int x)
{
    splay[x].size=splay[lson(x)].size+splay[rson(x)].size+1;
}
void Rotate(int x,int kind)
{
    int pre=splay[x].fa;
    Push_down(pre);Push_down(x);
    splay[pre].son[!kind]=splay[x].son[kind];
    splay[splay[pre].son[!kind]].fa=pre;
    if(splay[pre].fa)
    splay[splay[pre].fa].son[splay[splay[pre].fa].son[1]==pre]=x;
    splay[x].fa=splay[pre].fa;
    splay[x].son[kind]=pre;
    splay[pre].fa=x;
    Push_up(x);
    Push_up(pre);
}
void Splay(int x,int goal)
{
    Push_down(x);
    while(splay[x].fa!=goal)
    {
        int pre=splay[x].fa;
        if(splay[pre].fa==goal)
        {
            Push_down(pre);
            Push_down(x);
            Rotate(x,splay[pre].son[0]==x);
        }
        else
        {
            Push_down(splay[pre].fa);
            Push_down(pre);
            Push_down(x);
            int kind=splay[splay[pre].fa].son[0]==pre;
            if(splay[pre].son[kind]==x)
            Rotate(x,!kind),Rotate(x,kind);
            else Rotate(pre,kind),Rotate(x,kind);
        }
    }
    Push_up(x);
    if(!goal) root=x;
}
void Delete()
{
    if(lson(root))
    {
        int x=lson(root);
        Push_down(x);
        while(rson(x))
        {
            x=rson(x);
            Push_down(x);
        }
        Splay(x,root);
        rson(x)=rson(root);
        splay[rson(x)].fa=x;
        root=x;
        splay[x].fa=0;
        Push_up(root);
    }
    else
    {
        root=rson(root);
        splay[root].fa=0;
    }
}
void print(int x)
{
    printf("%d %d %d\n",splay[x].size,splay[lson(x)].size,splay[rson(x)].size);
    if(lson(x)) print(lson(x));
    if(rson(x)) print(rson(x));
}
int main()
{
    int i,n;
    while(~scanf("%d",&n)&&n)
    {
        init(n);
        rfor(i,1,n) scanf("%d",&A[i].val),A[i].pos=i;
        sort(A+1,A+n+1,cmp);
        //print(root);
        rfor(i,1,n)
        {
            Splay(A[i].pos,0);
            if(i!=1) printf(" ");
            printf("%d",splay[lson(root)].size+i);
            splay[lson(root)].flag^=1;
            Push_down(lson(root));
            Delete();
        }
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(数据结构,ACM,HDU,splay)