普通二叉树(multiset实现)

题目链接:https://www.luogu.com.cn/problem/P5076

题意:

实现一颗二叉搜索树

multiset

multiset中的元素是有序的,且可重(不同于set)

multiset<int>st;
void solve(){
	int q;cin>>q;
	st.insert(-2147483647);//充当哨兵,避免迭代器越出边界
	st.insert(2147483647);
	while(q--){
		int opt,x;cin>>opt>>x;
		switch(opt){
			case 1:{
				cout<<distance(st.begin(),st.lower_bound(x))<<endl;//distanc()函数,调用得到两个迭代器位置的距离
				break;
			}
			case 2:{
				auto it=st.begin();
				advance(it,x);//将it前进x格
				cout<<(*it)<<endl;
				break;
			}
			case 3:{
				auto it=st.lower_bound(x);
				cout<<(*prev(it))<<endl;//prev()函数,获得it前一个的迭代器
				break;
			}
			case 4:{
				cout<<(*(st.upper_bound(x)))<<endl;
				break;
			}
			case 5:
				st.insert(x);break;	
		}
	}
}
posted @ 2025-03-31 19:36  Marinaco  阅读(19)  评论(0)    收藏  举报
//雪花飘落效果