普通二叉树(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;
}
}
}

浙公网安备 33010602011771号