oiwiki-Splay TLE 求调
查看原帖
oiwiki-Splay TLE 求调
467107
Cap1taL楼主2023/2/3 15:58

照着oiwiki写的

第一个和最后一个点AC,其他全部TLE

感激不尽

2R2E验证码纪

// Problem: P3369 【模板】普通平衡树
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3369
// Memory Limit: 128 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#include <bits/extc++.h>
#define INF 0x7fffffff
#define MAXN 100005
#define eps 1e-9
#define foru(a,b,c)	for(int a=b;a<=c;a++)
#define RT return 0;
#define db(x)	cout<<endl<<x<<endl;
#define LL long long
#define LXF int
#define RIN rin()
#define HH printf("\n")
using namespace std;
inline LXF rin(){
	LXF x=0,w=1;
	char ch=0;
	while(ch<'0'||ch>'9'){ 
	if(ch=='-') w=-1;
	ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
	x=x*10+(ch-'0');
	ch=getchar();
	}
	return x*w;
}
template<class T>
class Splay{
	private:
		T rt,tot,fa[MAXN],ch[MAXN][2],val[MAXN],cnt[MAXN],sz[MAXN];
		void maintain(T x){
			sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
		}
		bool get(T x){
			return x==ch[fa[x]][1];
		}
		void clear(T x){
			fa[x]=ch[x][0]=ch[x][1]=val[x]=cnt[x]=sz[x]=0;
		}
		void rotate(T x){
			int y=fa[x],z=fa[y],chk=get(x);
			ch[y][chk]=ch[x][chk^1];
			if(ch[x][chk^1])	fa[ch[x][chk^1]]=y;
			ch[x][chk^1]=y,fa[y]=x;
			fa[x]=z;
			if(z)	ch[z][y==ch[z][1]]=x;
			maintain(x);
			maintain(y);
		}
		void splay(T x){
			for(int f=fa[x];f=fa[x],f;rotate(x)){
				if(fa[f])	rotate(get(x)==get(f)?f:x);
			}
			rt=x;
		}
	public:
		T get_val(T x){
			return val[x];
		}
		void insert(T k){
			if(!rt){
				val[++tot]=k,cnt[tot]=1,rt=tot;
				maintain(rt);
				return ;
			}
			T cur=rt,f=0;
			while(1){
				if(val[cur]==k){
					cnt[cur]++;
					maintain(cur);
					maintain(f);
					splay(cur);
					break;
				}
				f=cur;
				cur=ch[cur][k>val[cur]];
				if(!cur){
					val[++tot]=k,cnt[tot]=1,fa[tot]=f,ch[f][k>val[f]]=tot;
					maintain(tot);
					maintain(f);
					splay(tot);
					break;
				}
			}
		}
		T order_of_key(T k){
			T res=0,cur=rt;
			while(1){
				if(val[cur]==k){
					res+=sz[ch[cur][0]];
					splay(cur);
					return res+1;	
				}
				if(k<val[cur]){
					cur=ch[cur][0];
				}else{
					res+=sz[ch[cur][0]]+cnt[cur];
					cur=ch[cur][1];
				}
			}
		}
		T find_by_order(T k){
			T cur=rt;
			while(1){
				if(ch[cur][0] && k<=sz[ch[cur][0]]){
					cur=ch[cur][0];
				}else{	
					k-=sz[ch[cur][0]]+cnt[cur];
					if(k<=0){
						splay(cur);
						return val[cur];
					}
					cur=ch[cur][1];
				}
			}
		}
		T pre(){
			T cur=ch[rt][0];
			if(!cur)	return 0;
			while(ch[cur][1])	cur=ch[cur][1];
			splay(cur);
			return cur;
		}
		T nxt(){
			T cur=ch[rt][1];
			if(!cur)	return 0;
			while(ch[cur][0])	cur=ch[cur][0];
			splay(cur);
			return cur;
		}
		void del(T k){
			order_of_key(k);
			if(cnt[rt]>1){
				cnt[rt]--;
				maintain(rt);
				return ;
			}
			if(!ch[rt][0] && !ch[rt][1]){
				clear(rt);
				rt=0;
				return ;
			}
			if(!ch[rt][0]){
				T cur=rt;
				rt=ch[cur][1];
				fa[rt]=0;
				clear(rt);
				return ;
			}
			if(!ch[rt][1]){
				T cur=rt;
				rt=ch[cur][0];
				fa[rt]=0;
				clear(rt);
				return ;
			}
			T cur=rt,x=pre();
			fa[ch[cur][1]]=x;
			ch[x][1]=ch[cur][1];
			clear(cur);
			maintain(cur);
		}	
};

Splay<int> tr;
int n;
signed main(){
	n=RIN;
	while(n--){
		int opt=RIN,x=RIN;
		if(opt==1){
			tr.insert(x);
		}else if(opt==2){
			tr.del(x);
		}else if(opt==3){
			printf("%d\n",tr.order_of_key(x));
		}else if(opt==4){
			printf("%d\n",tr.find_by_order(x));
		}else if(opt==5){
			tr.insert(x);
			printf("%d\n",tr.get_val(tr.pre()));
			tr.del(x);
		}else if(opt==6){
			tr.insert(x);
			printf("%d\n",tr.get_val(tr.nxt()));
			tr.del(x);
		}
	}
	return 0;
}
2023/2/3 15:58
加载中...