【悬赏关注】Splay求条
查看原帖
【悬赏关注】Splay求条
999274
CNS_5t0_0r2楼主2024/3/21 12:38
#include<iostream>
using namespace std;
const int N = 1e5 + 9;
struct node{
	int child[2],fa;
	int val,cnt,tree_siz;
	node(int v = 0,int Fa = 0){child[0] = child[1] = 0;fa = Fa;val = v;tree_siz = 1;cnt = 1;}
} t[N];
int node_cnt,Root;
void push_up(int root){
	t[root].tree_siz = 
		t[root].cnt + 
		t[t[root].child[0]].tree_siz + 
		t[t[root].child[1]].tree_siz;
}
int get_side(int root){
	return root == t[t[root].fa].child[1];
}
void clear(int root){
	node tmp;
	t[root] = tmp;
}
void rotate(int root){
	int Fa = t[root].fa,Gfa = t[Fa].fa;
	int side_root = get_side(root),opp_root = side_root ^ 1;
	int side_fa = get_side(Fa);
	int &x_opp_child = t[root].child[opp_root];
	if(x_opp_child)
		t[x_opp_child].fa = Fa;
	x_opp_child = Fa;
	t[Fa].fa = root;
	t[root].fa = Gfa;
	if(Gfa)
		t[Gfa].child[side_fa] = root;
	push_up(Fa);
	push_up(root);
}
void splay(int root){
	int Fa = t[root].fa;
	for(;Fa = t[root].fa,Fa;rotate(root)){
		if(t[Fa].fa)
			rotate(get_side(root) == get_side(Fa) ? Fa : root);
	}
	Root = root;
}
void insert(int k){
	if(!Root){
		t[++node_cnt] = (node)(k);
		Root = node_cnt;
		return;
	}
	int root = Root,Fa = 0;
	while(1){
		if(t[root].val == k){
			t[root].cnt++;
			push_up(root);
			push_up(Fa);
			splay(root);
			return;
		}
		Fa = root;
		root = t[root].child[t[root].val < k];
		if(!root){
			t[++node_cnt] = (node)(k,Fa);
			t[Fa].child[t[Fa].val < k] = node_cnt;
			push_up(node_cnt);
			push_up(Fa);
			splay(node_cnt);
			return;
		}
	}
}
int k_rank(int k){
	int ret = 0,root = Root;
	while(1){
    	if (k < t[root].val)
    		root = t[root].child[0];
		else {
      		ret += t[t[root].child[0]].tree_siz;
      		if(k == t[root].val) {
        		splay(root);
        		return ret + 1;
      		}
      		ret += t[root].cnt;
      		root = t[root].child[1];
    	}
  	}
}
int kth(int k){
	int root = Root;
	while(1){
		if(t[root].child[0] && k <= t[t[root].child[0]].tree_siz)
			root = t[root].child[0];
		else{
			k -= t[root].cnt + t[t[root].child[0]].tree_siz;
			if(k <= 0){
				splay(root);
				return t[root].val;
			}
			root = t[root].child[1];
		}
	}
}
int root_pre(){
	int root = t[Root].child[0];
	if(!root)
		return root;
	while(t[root].child[1])
		root = t[root].child[1];
	splay(root);
	return root;
}
int root_nex(){
	int root = t[Root].child[1];
	if(!root)
		return root;
	while(t[root].child[0])
		root = t[root].child[0];
	splay(root);
	return root;
}
void remove(int k){
	k_rank(k);
	if(t[Root].cnt > 1){
		t[Root].cnt--;
		push_up(Root);
		return;
	}
	if(!t[Root].child[0] && !t[Root].child[1]){
		clear(Root);
		Root = 0;
		return;
	}
	if(!t[Root].child[0]){
		int root = Root;
		Root = t[Root].child[1];
		t[Root].fa = 0;
		clear(root);
		return;
	}
	if(!t[Root].child[1]){
		int root = Root;
		Root = t[Root].child[0];
		t[Root].fa = 0;
		clear(root);
		return;
	}
	int tmp = Root,root = root_pre();
	t[t[tmp].child[1]].fa = root;
	t[root].child[1] = t[tmp].child[1];
	clear(tmp);
	push_up(Root);
}
int pre(int k){
	insert(k);
	return t[root_pre()].val;
	remove(k);
}
int nex(int k){
	insert(k);
	return t[root_nex()].val;
	remove(k);
}
int n,op,x;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n;
	for(int i = 1;i <= n;i++){
		cin >> op >> x;
		if(op == 1)
			insert(x);
		if(op == 2)
			remove(x);
		if(op == 3)
			cout << k_rank(x) << '\n';
		if(op == 4)
			cout << kth(x) << '\n';
		if(op == 5)
			cout << pre(x) << '\n';
		if(op == 6)
			cout << nex(x) << '\n';
	}
	return 0;
}
2024/3/21 12:38
加载中...