社区讨论

51pts球条

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

讨论操作

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

当前回复
1 条
当前快照
1 份
快照标识符
@mk5e3kn1
此快照首次捕获于
2026/01/08 19:54
上个月
此快照最后确认于
2026/01/11 10:25
上个月
查看原帖
CPP
#include<bits/stdc++.h>
using namespace std;
struct tree{
	int x,si=1,h=0;
	tree *left=NULL,*right=NULL;
}*top=NULL;
void geth(tree *t){
	t->h=max(t->left?t->left->h:0,t->right?t->right->h:0)+1;
	t->si=(t->left?t->left->si:0)+(t->right?t->right->si:0)+1;
}void LL(tree *&t){
	tree *tmp=t->left;
	t->left=tmp->right;
	tmp->right=t;
	geth(t);
	geth(tmp);
	t=tmp;
}void RR(tree *&t){
	tree *tmp=t->right;
	t->right=tmp->left;
	tmp->left=t;
	geth(t);
	geth(tmp);
	t=tmp;
}void LR(tree *&t){
	RR(t->left);
	LL(t);
}void RL(tree *&t){
	LL(t->right);
	RR(t);
}
void check(tree *&t){
	int l=t->left?t->left->h:0,r=t->right?t->right->h:0;
	if(abs(l-r)<2){
		return;
	}if(l>r){
		tree *tmp=t->left;
		int ll=tmp->left?tmp->left->h:0,lr=tmp->right?tmp->right->h:0;
		if(ll>=lr){
			LL(t);
			return;
		}else{
			LR(t);
			return;
		}
	}else{
		tree *tmp=t->right;
		int ll=tmp->left?tmp->left->h:0,lr=tmp->right?tmp->right->h:0;
		if(ll<=lr){
			RR(t);
			return;
		}else{
			RL(t);
			return;
		}
	}
}void insert(tree *&t,int x){
	if(!t){
		t=new tree;
		t->x=x;
		return;
	}if(x<=t->x){
		insert(t->left,x);
	}else{
		insert(t->right,x);
	}geth(t);
	check(t);
}void erase(tree *&t,int x){
	if(t->x==x){
		if(!t->left){
			t=t->right;
		}else if(!t->right){
			t=t->left;
		}else{
			tree *p=t->left;
			while(p->right && p->right->right){
				p=p->right;
			}if(p->right){
				int xi=p->right->x;
				erase(p->right,p->right->x);
				t->x=xi;
			}else{
				int xi=p->x;
				erase(t->left,p->x);
				t->x=xi;
			}
		}if(t){
			geth(t);
			check(t);
		}return;
	}if(x<t->x){
		erase(t->left,x);
	}else{
		erase(t->right,x);
	}geth(t);
	check(t);
}int query_count(tree *t,int x){
	if(!t)return 0;
	if(x<=t->x){
		return query_count(t->left,x);
	}else{
		return (t->left?t->left->si+1:1)+query_count(t->right,x);
	}
}int query_sort(tree *t,int k){
	if(!t)return 0;
	if(k<=(t->left?t->left->si:0)){
		return query_sort(t->left,k);
	}else if(k==(t->left?t->left->si:0)+1){
		return t->x;
	}else{
		return query_sort(t->right,k-(t->left?t->left->si:0)-1);
	}
}int pre(tree *t,int x){
    int maxi=-1e9;
    while(t){
        if(t->x>=x){
			t=t->left;
		}else{
			maxi=t->x;
			t=t->right;
		}
    }return maxi;
}int next(tree *t,int x){
    int mini=1e9;
    while(t){
        if(t->x<=x){
			t=t->right;
		}else{
			mini=t->x;
			t=t->left;
		}
    }return mini;
}void dfs(tree *t){
	if(!t)return;
	cout<<t->x<<"( ";
	dfs(t->left);
	cout<<" | ";
	dfs(t->right);
	cout<<" )";
}
int t;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>t;
	while(t--){
		int op;
		cin>>op;
		if(op==1){
			int x;
			cin>>x;
			insert(top,x);
		}if(op==2){
			int x;
			cin>>x;
			erase(top,x);
		}if(op==3){
			int x;
			cin>>x;
			cout<<query_count(top,x)+1<<"\n";
		}if(op==4){
			int x;
			cin>>x;
			cout<<query_sort(top,x)<<"\n";
		}if(op==5){
			int x;
			cin>>x;
			cout<<pre(top,x)<<"\n";
		}if(op==6){
			int x;
			cin>>x;
			cout<<next(top,x)<<"\n";
		}//dfs(top);cout<<"\n";
	}
	return 0;
}
https://www.luogu.com.cn/record/256844171
已经从2025年7月调到2026年了

回复

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

正在加载回复...