书架

splay

#include<bits/stdc++.h>
#define N 80005
using namespace std;
char str[20];
int pos[N],size[N],father[N];
int c[N][2],v[N];
int n,m,num,num1,sz,root;
int inline read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
void update(int x){
    size[x]=size[c[x][0]]+size[c[x][1]]+1;
    pos[v[c[x][0]]]=c[x][0];
    pos[v[c[x][1]]]=c[x][1];
}
void rotate(int &root,int x){
    int y=father[x];
    int z=father[y];
    int p,q;
    if (c[y][0]==x)
        p=0;
    else 
        p=1;
    q=p^1;
    if (y==root)
        root=x;
    else {
        if (c[z][0]==y)
            c[z][0]=x;
        else 
            c[z][1]=x;
    }
    father[x]=z;
    father[y]=x;
    father[c[x][q]]=y;
    c[y][p]=c[x][q];
    c[x][q]=y;
    update(y);
    update(x);
}
void splay(int &root,int x){
    int y,z;
    while (x!=root){
        y=father[x];
        z=father[y];
        if (y!=root){
            if ((c[z][0]==y)^(c[y][0]==x))
                rotate(root,x);
            else rotate(root,y);
        }
        rotate(root,x);
    }
    pos[v[x]]=x;
}
void Insert(int x){
    v[++sz]=x;
    size[sz]=1;
    pos[x]=sz;
    c[sz][0]=c[sz][1]=0;
    if (sz>1){
        c[sz-1][1]=sz;
        father[sz]=sz-1;
        splay(root,sz);
    }
}
int find(int x,int k){
    int y=c[x][0];
    if (size[y]+1==k)
        return x;
    else 
        if (size[y]>=k)
            return find(y,k);
    else 
        return find(c[x][1],k-size[y]-1);
}
void top(int x){
    x=pos[x];
    splay(root,x);
    if (!c[x][0])
        return;
    if (!c[x][1]){
        c[x][1]=c[x][0];
        c[x][0]=0;
    }
    else{
        int y=find(root,size[c[x][0]]+2);
        father[c[root][0]]=y;
        c[y][0]=c[root][0];
        c[root][0]=0;
        splay(root,y);
    }
}
void bottom(int x){
    x=pos[x];
    splay(root,x);
    if (!c[x][1])
        return;
    if (!c[x][0]){
        c[x][0]=c[x][1];
        c[x][1]=0;
    }
    else{
        int y=find(root,size[c[x][0]]);
        father[c[root][1]]=y;
        c[y][1]=c[root][1];
        c[root][1]=0;
        splay(root,y);
    }
}
void insert1(int f,int x){
    if (!f)
        return;
    splay(root,pos[x]);
    int y=find(root,f==1?size[c[pos[x]][0]]+2:size[c[ pos[x] ] [0]]);
    int x1=v[y];
    int x2=pos[x];
    swap(pos[x],pos[x1]);
    swap(v[x2],v[y]);
}
void ask1(int x){
    splay(root,x);
    printf("%d
",size[c[x][0]]);
}
int main(){
    //scanf("%d%d",&n,&m);
    n=read();
    m=read();
    root=1;
    for (int i=1;i<=n;i++){
        //scanf("%d",&num);
        num=read();
        Insert(num);
    }
    for (int i=1;i<=m;i++){
        scanf("%s",str);
        switch(str[0]){
            case 'T':/*scanf("%d
",&num);top(num);*/top(read());break;
            case 'B':/*scanf("%d
",&num);bottom(num);*/bottom(read());break;
            case 'I':/*scanf("%d%d
",&num,&num1);insert1(num,num1);*/insert1(read(),read());break;
            case 'A':/*scanf("%d
",&num);ask1(pos[num]);*/ask1(pos[read()]);break;
            case 'Q':/*scanf("%d
",&num);printf("%d
",v[find(root,num)]);*/printf("%d
",v[find(root,read())]);break;
        }
    }
}
原文地址:https://www.cnblogs.com/wjnclln/p/9596122.html