用了Splay,T了4个点.

P3391 【模板】文艺平衡树

Cyan_rose @ 2019-01-23 17:08:00

如题。调了一下午整个人都不好了QAQ。求助各位大佬。。。

#include<bits/stdc++.h>
#define rint register int
#define endll '\n'
#define ivoid inline void
#define iint inline int
#define ll long long
using namespace std;
const int N=1e6+5;
const int M=3e3+5;
const int inf=0x3f3f3f3f;
int x,y,u,v,t,s,w,m,n,r,p,q;
int head[N],ref[N];
char c[8],c1;
ll ans,tot,sum,res,cnt,num,rt;
struct Node{
    int son[2],fa,val,id,num,size;
    ivoid crate(int x,int y){val=x;fa=y;son[0]=son[1]=0;num=size=1;}
    ivoid show(){
        cout<<"val="<<val<<" id="<<id<<" fa="<<fa<<" num="<<num<<" size="<<size<<endll;
        cout<<"lson="<<son[0]<<" rson="<<son[1]<<endll;
    }
}node[N<<2];

inline int rad(){
    int x=0,f=1;char c;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+c-'0',c=getchar();
    return x*f;
}

iint side(int x){return x==node[node[x].fa].son[1];}
ivoid update(int x){node[x].size=node[x].num+node[node[x].son[0]].size+node[node[x].son[1]].size;}
ivoid connect(int x,int y,int side){node[x].fa=y;node[y].son[side]=x;}
ivoid rotate(int x)
{
    int f=node[x].fa,ff=node[f].fa;
    int sidex=side(x),sidef=side(f);
    connect(node[x].son[sidex^1],f,sidex);
    connect(x,ff,sidef);
    connect(f,x,sidex^1);
    update(f);update(x);
}

ivoid splay(int x,int pos)
{ 
    int pos1=pos,f=node[x].fa,ff;pos=node[pos].fa;
    while(node[x].fa!=pos){
        f=node[x].fa,ff=node[f].fa;
        if(ff!=pos){side(x)==side(f)?rotate(f):rotate(x);}
        rotate(x);
    }
    if(pos1==rt)rt=x;
}

ivoid insert(int val,int id)
{
    int pos=rt,last=0;
    while(pos&&node[pos].val!=val)
    last=pos,node[pos].size++,pos=node[pos].son[val>node[pos].val];
    if(pos)node[pos].num++;
    else{
        pos=++cnt;
        node[pos].crate(val,last);
        node[pos].id=id;
        node[last].son[val>node[last].val]=cnt;
    }
    splay(pos,rt);
}

iint rank_pos(int rank)
{
    int pos=rt;
    while(pos){
        if(!pos)break;
        if(node[node[pos].son[0]].size+node[pos].num<rank){
            rank-=(node[node[pos].son[0]].size+node[pos].num);
            pos=node[pos].son[1];
            continue;
        }
        if(node[node[pos].son[0]].size<rank)break;
        pos=node[pos].son[0];
    }
    return pos;
}

iint ex_rank_pos(int rank)
{
    int pos=rt;
    while(pos){
        if(!pos)break;
        if(node[node[pos].son[0]].size+node[pos].num<rank){
            rank-=(node[node[pos].son[0]].size+node[pos].num);
            pos=node[pos].son[1];
            continue;
        }
        if(node[node[pos].son[0]].size<rank)break;
        pos=node[pos].son[0];
    }
    splay(pos,rt);
    return pos;
}

ivoid print(int now)
{
    if(node[now].son[0])print(node[now].son[0]);
    cout<<node[now].id<<" ";
    if(node[now].son[1])print(node[now].son[1]);
}

int main()
{
    n=rad();m=rad();
    for(rint i=1;i<=n;i++){x=rad();insert(i,x);ref[x]=i;}
    while(m--){
        scanf("%s",c);x=rad();
        if(c[0]=='T'){
            int pos=ref[x];splay(pos,rt);
            splay(rank_pos(node[node[rt].son[0]].size+2),node[rt].son[1]);
            connect(node[rt].son[0],node[rt].son[1],0);update(node[rt].son[1]);
            node[rt].son[0]=0;
        }
        if(c[0]=='B'){
            int pos=ref[x];splay(pos,rt);
            splay(rank_pos(node[node[rt].son[0]].size),node[rt].son[0]);
            connect(node[rt].son[1],node[rt].son[0],1);update(node[rt].son[0]);
            node[rt].son[1]=0;
        }
        if(c[0]=='I'){
            y=rad();int pos=ref[x],pos1=0;splay(pos,rt);
            if(y==0)continue;
            if(y==-1)pos1=ex_rank_pos(node[node[rt].son[0]].size);
            if(y==1)pos1=ex_rank_pos(node[node[rt].son[0]].size+2);
            swap(ref[x],ref[node[pos1].id]);
            swap(node[pos].id,node[pos1].id);
        }
        if(c[0]=='A'){
            int pos=ref[x];
            splay(pos,rt);
            cout<<node[node[rt].son[0]].size<<endll;
        }
        if(c[0]=='Q'){cout<<node[ex_rank_pos(x)].id<<endll;}
    }
    return 0;
}

by Cyan_rose @ 2019-01-23 17:08:35

等等我是不是发错地方了???


by Cyan_rose @ 2019-01-23 17:09:35

抱歉抱歉,脑子已经昏了QAQ


by Substitute0329 @ 2019-01-23 17:23:08

@Cyan_rose 吓得我以为是双倍经验


|