首页 > 代码库 > BZOJ 3224: Tyvj 1728 普通平衡树

BZOJ 3224: Tyvj 1728 普通平衡树

题目

3224: Tyvj 1728 普通平衡树

Time Limit: 10 Sec  Memory Limit: 128 MB

Description

您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)

Input

第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号(1<=opt<=6)

Output

对于操作3,4,5,6每行输出一个数,表示对应答案

Sample Input

10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598

Sample Output

106465
84185
492737

HINT

 

1.n的数据范围:n<=100000

2.每个数的数据范围:[-1e7,1e7]

 

Source

平衡树

题解

STL大法好!vector直接过,虽然可能会慢一点。

代码

 1 /*Author:WNJXYK*/ 2 #include<cstdio> 3 #include<algorithm> 4 #include<vector>  5 using namespace std; 6 vector<int> v; 7 int n; 8 int opt,num; 9 inline int read(){10     int x=0,f=1;char ch=getchar();11     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}12     while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}13     return x*f;14 }15 inline void insert(int x){16     v.insert(upper_bound(v.begin(),v.end(),x),x);17 }18 inline void del(int x){19     v.erase(lower_bound(v.begin(),v.end(),x));20 }21 inline int find(int x){22     return lower_bound(v.begin(),v.end(),x)-v.begin()+1; 23 }24 int main(){25     n=read();26     v.reserve(200000);27     for (int i=1;i<=n;i++){28         opt=read();num=read();29         switch(opt){30             case 1:insert(num);break;31             case 2:del(num);break;32             case 3:printf("%d\n",find(num));break;33             case 4:printf("%d\n",v[num-1]);break; 34             case 5:printf("%d\n",*--lower_bound(v.begin(),v.end(),num));break;35             case 6:printf("%d\n",*upper_bound(v.begin(),v.end(),num));break;36         }37     }38     return 0;39 }
View Code

 

BZOJ 3224: Tyvj 1728 普通平衡树