社区讨论

splay51分求调,TLE#6-12

P3369【模板】普通平衡树参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@micsu3gu
此快照首次捕获于
2025/11/24 15:01
3 个月前
此快照最后确认于
2025/11/24 16:56
3 个月前
查看原帖
CPP
#include <bits/stdc++.h>
#define lowbit(x) ((x) & -(x))
#define PII pair < int , int >
#define x first
#define y second
using namespace std ; 
const int N = 1e5 + 5 ; 
int n , rt , tot ; 
struct Tree{
	int fa , ch[2] , val , sz , cnt ; 
}t[N] ; 
inline bool dir(int x){ return x == t[t[x].fa].ch[1] ; }
inline void pushup(int x){ t[x].sz = t[t[x].ch[0]].sz + t[t[x].ch[1]].sz + t[x].cnt ; }
inline void rotate(int x){
	int y = t[x].fa , z = t[y].fa , r = dir(x) ; 
	t[y].ch[r] = t[x].ch[!r] ; 
	t[x].ch[!r] = y ; 
	if(z) t[z].ch[dir(y)] = x ; 
	if(t[y].ch[r]) t[t[y].ch[r]].fa = y ; 
	t[x].fa = z , t[y].fa = x ; 
	pushup(y) , pushup(x) ; 
}
inline void splay(int x){
	int w = t[rt].fa ; 
	while(t[x].fa != w){
		int y = t[x].fa ; 
		if(t[y].fa != w) rotate(dir(x) == dir(y) ? y : x) ; 
		rotate(x) ; 
	}
	rt = x ; 
}
inline void find(int v){
	int x = rt , y = t[x].fa ; 
	while(x && t[x].val != v) y = x , x = t[x].ch[v > t[x].val] ; 
	splay(x ? x : y) ; 
}
inline void loc(int v , int k){
	int x = v ; 
	while(1){
		if(k <= t[t[x].ch[0]].sz) x = t[x].ch[0] ; 
		else if(k <= t[t[x].ch[0]].sz + t[x].cnt) break ; 
		else k -= t[t[x].ch[0]].sz + t[x].cnt , x = t[x].ch[1] ; 
	}
	splay(x) ; 
}
inline int merge(int x , int y){
	if(!x || !y) return x | y ; 
	loc(y , 1) ; 
	t[y].ch[0] = x , t[x].fa = y ; 
	pushup(y) ; 
	return y ; 
}
inline void insert(int v){
	int x = rt , y = 0 ; 
	while(x && t[x].val != v) y = x , x = t[x].ch[v > t[x].val] ; 
	if(x) t[x].cnt++ , t[x].sz++ ; 
	else{
		t[x = ++tot].val = v ; 
		t[x].cnt = t[x].sz = 1 ; 
		t[x].fa = y ; 
		if(y) t[y].ch[v > t[y].val] = x ; 
	}
	splay(x) ; 
}
inline bool remove(int v){
	find(v) ; 
	if(!rt || t[rt].val != v) return 0 ; 
	t[rt].cnt-- , t[rt].sz-- ; 
	if(!t[rt].cnt){
		int x = t[rt].ch[0] , y = t[rt].ch[1] ; 
		t[x].fa = t[y].fa = 0 ; 
		rt = merge(x , y) ; 
	}
	return 1 ; 
}
inline int gtr(int v){ 
	find(v) ; 
	return t[t[rt].ch[0]].sz + (t[rt].val < v ? t[rt].cnt : 0) + 1 ; 
}
inline int gtk(int k){ 
	if(k > t[rt].sz) return -1 ; 
	loc(rt , k) ; 
	return t[rt].val ; 
}
inline int gtq(int v){
	find(v) ; 
	if(rt && t[rt].val < v) return t[rt].val ; 
	int x = t[rt].ch[0] ; 
	if(!x) return -1 ; 
	while(t[x].ch[1]) x = t[x].ch[1] ; 
	splay(x) ; 
	return t[rt].val ; 
}
inline int gth(int v){
	find(v) ; 
	if(rt && v < t[rt].val) return t[rt].val ; 
	int x = t[rt].ch[1] ; 
	if(!x) return -1 ; 
	while(t[x].ch[0]) x = t[x].ch[0] ; 
	splay(x) ; 
	return t[rt].val ; 
}
signed main(){
//	freopen(".in" , "r" , stdin) ; 
//	freopen(".out" , "w" , stdout) ; 
	ios::sync_with_stdio(0) ; 
	cin.tie(0) ; cout.tie(0) ; 
	cin >> n ; 
	while(n--){
		int op , x ; cin >> op >> x ; 
		if(op == 1) insert(x) ; 
		else if(op == 2) remove(x) ; 
		else if(op == 3) cout << gtr(x) << "\n" ; 
		else if(op == 4) cout << gtk(x) << "\n" ; 
		else if(op == 5) cout << gtq(x) << "\n" ; 
		else cout << gth(x) << "\n" ; 
	}
	return 0 ; 
}
/*

*/

回复

0 条回复,欢迎继续交流。

正在加载回复...