求助,我校 OJ 参考代码(Splay)88 pts
查看原帖
求助,我校 OJ 参考代码(Splay)88 pts
576363
micorsfot楼主2023/4/8 11:38

最后一个点 TLE

#include<iostream>
#include<cstdio>
#define inf 1<<30
using namespace std;
struct Node{
	int lc,rc,fa,size,v;
}t[100005];
int n,num=0,root=0;
void pushup(int k){  
	t[k].size=t[t[k].lc].size+t[t[k].rc].size+1;
}
void rotate(int x,int d){ 
    int y=t[x].fa,z=t[y].fa;
    if(d==1){  
        t[y].lc=t[x].rc;t[t[x].rc].fa=y;  
        t[x].rc=y;t[y].fa=x;  
        t[x].fa=z; 
        if(t[z].lc==y) t[z].lc=x;
        if(t[z].rc==y) t[z].rc=x;
        pushup(y);pushup(x);
    }
    if(d==0){  
        t[y].rc=t[x].lc;t[t[x].lc].fa=y;
        t[x].lc=y;t[y].fa=x;
        t[x].fa=z;
        if(t[z].rc==y) t[z].rc=x;
        if(t[z].lc==y) t[z].lc=x;
        pushup(y);pushup(x);
    }
}
void splay(int x,int f){ 
    while(t[x].fa!=f){
        int y=t[x].fa,z=t[y].fa;
        if(z==f&&t[y].lc==x) rotate(x,1);  
        else if(z==f&&t[y].rc==x) rotate(x,0); 
        else if(t[z].lc==y&&t[y].lc==x) rotate(y,1),rotate(x,1); 
        else if(t[z].rc==y&&t[y].rc==x) rotate(y,0),rotate(x,0); 
        else if(t[z].lc==y&&t[y].rc==x) rotate(x,0),rotate(x,1); 
        else if(t[z].rc==y&&t[y].lc==x) rotate(x,1),rotate(x,0); 
    }
}
void newnode(int x){
	t[++num].size=1;t[num].v=x;
}
int Rank(int x,int k){
    int tmp=inf,ans=0;
    while(k){
        if(t[k].v==x) tmp=min(tmp,ans+t[t[k].lc].size+1);
        if(t[k].v<x) ans+=t[t[k].lc].size+1,k=t[k].rc;
        else k=t[k].lc;
    }
    return tmp==inf?ans:tmp;
}
int xth(int x,int k){
    while(1){
        if(t[t[k].lc].size==x-1) return t[k].v;
        if(t[t[k].lc].size<x-1) x-=(t[t[k].lc].size+1),k=t[k].rc;
        else k=t[k].lc;
    }
}
int pre(int x,int k){
    int ans=-inf;
    while(k){
        if(t[k].v<x) ans=max(ans,t[k].v),k=t[k].rc;
        else k=t[k].lc;
    }
    return ans;
}
int suc(int x,int k){
    int ans=inf;
    while(k){
        if(t[k].v>x) ans=min(ans,t[k].v),k=t[k].lc;
        else k=t[k].rc;
    }
    return ans;
}
void ins(int x,int k){
    if(t[k].v<x){
        if(t[k].rc==0){
        	newnode(x),t[k].rc=num,t[num].fa=k,splay(num,0),root=num;
		} else ins(x,t[k].rc),pushup(k);
    }
    else{
        if(t[k].lc==0) newnode(x),t[k].lc=num,t[num].fa=k,splay(num,0),root=num;
        else ins(x,t[k].lc),pushup(k);
    }
}
int getk(int now,int k){
    int wtf=t[t[now].lc].size+1;
    if(wtf==k) return now;
    if(wtf<k) return getk(t[now].rc,k-wtf);
    else return getk(t[now].lc,k);
}
void del(int x){
    int k=Rank(x,root);
    int n1=getk(root,k-1);
    splay(n1,0);root=n1;
    splay(getk(root,k+1),root);
    t[t[root].rc].lc=0;
}
int main(){
    scanf("%d",&n);
    ins(-inf,root);
    ins(inf,root);
    while(n--){
        int opt,x;
        scanf("%d%d",&opt,&x);
        if(opt==1) ins(x,root);
        if(opt==2) del(x);
        if(opt==3) printf("%d\n",Rank(x,root)-1);
        if(opt==4) printf("%d\n",xth(x+1,root));
        if(opt==5) printf("%d\n",pre(x,root));
        if(opt==6) printf("%d\n",suc(x,root));
    }
    return 0;
}
2023/4/8 11:38
加载中...