P3369 【模板】普通平衡树

Aimee

考试前看到某份考纲提到了平衡树

突发奇想想学一个

但是来不及了,为了平衡学习时间,以及大佬的建议下,毅然决定用vector搞一个

额,要是vector都过不了,那以我的水平,也拿不到更多的分了


配方,lower_bound+upper_bound+vector

对于操作1,使用vector的insect用二分来找到第一个比它大的位置按照顺序的位置插进去

对于操作2 用erase删就行了

对于操作三 找到他的下标就行了,不过记得考虑vector从0开始的,记得+1

对于4 直接访问就行

5和6 用二分查就行了

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
vector <int> v;
int n;
int x;
int Aimee;
int  read(){
	int res=0;
	int f=1;
	char c=getchar();
	while(c!='-'&&(c<'0'||c>'9')) c=getchar();
	if(c=='-') f=-1;
	c='0';
	while(c>='0'&&c<='9') res=(res<<1)+(res<<3)+c-'0',c=getchar();
	return res*f;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;++i){
		scanf("%d",&Aimee); 
		scanf("%d",&x);
		if(Aimee==1) v.insert(lower_bound(v.begin(),v.end(),x),x);
		if(Aimee==2) v.erase(lower_bound(v.begin(),v.end(),x));
		if(Aimee==3) printf("%d
",lower_bound(v.begin(),v.end(),x)-v.begin()+1);
		if(Aimee==4) printf("%d
",v[x-1]);
		if(Aimee==5) printf("%d
",*(--lower_bound(v.begin(),v.end(),x)));
		if(Aimee==6) printf("%d
",*(upper_bound(v.begin(),v.end(),x)));
	}
	return 0;
} 
原文地址:https://www.cnblogs.com/For-Miku/p/13922057.html