P3369 【模板】普通平衡树
题解1
stl模拟,注意lowerbound的效果和pos的返回值
code1
#include<bits/stdc++.h>
using namespace std;
vector<int> a;
int main()
{
int n;
cin>>n;
while(n--)
{
int op,x;
cin>>op>>x;
if(op==1) a.insert(lower_bound(a.begin(),a.end(),x),x);
else if(op==2) a.erase(lower_bound(a.begin(),a.end(),x));
else if(op==3)
{
int pos=lower_bound(a.begin(),a.end(),x)-a.begin();//迭代器减迭代器
cout<<pos+1<<endl;
}
else if(op==4) cout<<a[x-1]<<endl;
else if(op==5)
{
int pos=lower_bound(a.begin(),a.end(),x)-a.begin();//pos返回的是大于等于x的第一个元素在序列里的下标
if(pos>0) cout<<a[pos-1]<<endl;//前缀
}
else
{
int pos=lower_bound(a.begin(),a.end(),x+1)-a.begin();//大于等于x+1 相当于大于x
if(a[pos]>x) cout<<a[pos]<<endl;
}
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探