【悬赏关注】Splay 37 pts 求调
查看原帖
【悬赏关注】Splay 37 pts 求调
999274
CNS_5t0_0r2楼主2024/4/2 13:31
#include<iostream>
//#include<windows.h>
#include<ctime>
#include<climits>
using namespace std;
const int N = 1e5 + 9;
struct node{
	int child[2],fa;
	int val,cnt,tree_siz;
} 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){
	t[root].fa = 0;
	t[root].child[0] = t[root].child[1] = 0;
}
void new_node(int val,int Fa){
	node_cnt++;
	t[node_cnt].fa = Fa;
	t[node_cnt].val = val;
	t[node_cnt].cnt = t[node_cnt].tree_siz = 1;
}
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);
	t[Fa].child[side_root] = t[root].child[opp_root];
	if(t[root].child[opp_root])
		t[t[root].child[opp_root]].fa = Fa;
	t[root].child[opp_root] = 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){
		new_node(k,0);
		Root = node_cnt;
		push_up(Root);
		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){
			new_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){
		//cout << root << " :" << t[root].tree_siz << '\n';
		//Sleep(1000);
    	if (k < t[root].val){
			if(!t[root].child[0]){
				splay(root);
        		return ret + 1;
			}
    		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;
      		if(!t[root].child[1]){
        		splay(root);
        		return ret + 1;
			}
      		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;
	insert(INT_MAX);
	for(int i = 1;i <= n;i++){
		cin >> op >> x;
		//cout << "\n";
		//cout << "\n\n" << t[Root].val << '\n';
		if(op == 1)
			insert(x);
		if(op == 2)
			remove(x);
		if(op == 3){
			//cout << "\n";
			cout << k_rank(x) << '\n';
			//cout << "\n";
		}
		if(op == 4){
			//cout << "\n";
			cout << kth(x) << '\n';
			//cout << "\n";
		}
		if(op == 5){
			//cout << "\n";
			cout << pre(x) << '\n';
			//cout << "\n";
		}
		if(op == 6){
			//cout << "\n";
			cout << nex(x) << '\n';
			//cout << "\n";
		}
		//cout << t[Root].val << '\n';
		
	}
	return 0;
}
/*
20
1 964673
5 968705
4 1
3 964673
5 965257
1 915269
1 53283
3 964673
3 53283
3 53283
1 162641
5 973984
1 948119
2 915269
2 53283
6 959161
1 531821
1 967521
2 531821
1 343410
*/
2024/4/2 13:31
加载中...