splay TLE 60pts
  • 板块学术版
  • 楼主Engulf
  • 当前回复3
  • 已保存回复3
  • 发布时间2022/3/20 15:06
  • 上次更新2023/10/28 06:05:54
查看原帖
splay TLE 60pts
482728
Engulf楼主2022/3/20 15:06

学习了这篇题解,但 TLE 60pts,求求各位 dalao 了qwq

@智子

会给关注

#include <bits/stdc++.h>
using namespace std;

inline int read(){
    int x=0,f=0;char ch=getchar();
    while(!isdigit(ch))f^=!(ch^45),ch=getchar();
    while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return f?-x:x;
}
inline void write(int x){
    if(x<0)x=-x,putchar('-');
    if(x>=10)write(x/10);
    putchar(x%10+'0');
}
inline void writeln(int x){write(x);puts("");}

int n,root,idx;
struct node{
    int son[2],fa,siz,val,cnt;
}tr[100005];
int New(int val,int fa){
    idx++;
    tr[idx].cnt=tr[idx].siz=1;
    tr[idx].val=val;tr[idx].fa=fa;
    return idx;
}
void pushup(int p){tr[p].siz=tr[tr[p].son[0]].siz+tr[tr[p].son[1]].siz+tr[p].cnt;}
void rotate(int x){
    int y=tr[x].fa,z=tr[y].fa;
    int k=tr[y].son[1]==x;
    tr[z].son[tr[z].son[1]==y]=x,tr[x].fa=z;
    tr[y].son[k]=tr[x].son[k^1],tr[tr[x].son[k^1]].fa=y;
    tr[x].son[k^1]=y,tr[y].fa=x;
    pushup(y);pushup(x);
}
void splay(int x,int goal){
    while(tr[x].fa!=goal){
        int y=tr[x].fa,z=tr[y].fa;
        if(z!=goal){
            if((tr[z].son[1]==y) ^ (tr[y].son[1]==x))rotate(x);
            else rotate(y);
        }
        rotate(x);
    }
    if(!goal)root=x;
}
void insert(int val){
    int p=root,fa=0;
    while(p&&tr[p].val!=val){
        fa=p;p=tr[p].son[val>tr[p].val];
    }
    if(p)tr[p].cnt++;
    else{
        p=New(val,fa);
        if(fa)tr[fa].son[val>tr[fa].val]=p;
    }
    splay(p,0);
}
void find(int val){
    int p=root;
    while(tr[p].son[val>tr[p].val]&&tr[p].val!=val){
        p=tr[p].son[val>tr[p].val];
    }
    splay(p,0);
}
int kth(int k){
    int p=root;
    while(1){
        if(tr[tr[p].son[0]].siz>=k)p=tr[p].son[0];
        else if(tr[tr[p].son[0]].siz+tr[p].cnt==k)return p;
        else k-=tr[tr[p].son[0]].siz+tr[p].cnt,p=tr[p].son[1];
    }
}
int pre(int val){
    find(val);
    if(tr[root].val<val)return root;
    int p=tr[root].son[0];
    while(tr[p].son[1])p=tr[p].son[1];
    return p;
}
int nxt(int val){
    find(val);
    if(tr[root].val>val)return root;
    int p=tr[root].son[1];
    while(tr[p].son[0])p=tr[p].son[0];
    return p;
}
void remove(int val){
    int x=pre(val),y=nxt(val);
    splay(x,0);splay(y,x);
    if(tr[tr[y].son[0]].cnt>1)tr[tr[y].son[0]].cnt--,splay(tr[y].son[0],0);
    else{
        tr[y].son[0]=0;
        pushup(y);pushup(x);
    }
}

int main(){
    scanf("%d",&n);
    insert(INT_MAX);insert(INT_MIN);
    while(n--){
        int p=read(),x=read();
        switch(p){
            case 1:insert(x);break;
            case 2:remove(x);break;
            case 3:find(x);printf("%d\n",tr[tr[root].son[0]].siz);break;
            case 4:printf("%d\n",tr[kth(x+1)].val);break;
            case 5:printf("%d\n",tr[pre(x)].val);break;
            case 6:printf("%d\n",tr[nxt(x)].val);break;
        }
    }
    return 0;
}
2022/3/20 15:06
加载中...