description:

设计一种数据结构 实现以下操作:

1.插入一个数x(insert)
2.删除一个数x(delete)(如果有多个相同的数,则只删除一个)
3.查询一个数x的排名(若有多个相同的数,就输出最小的排名)
4.查询排名为x的数
5.求一个数x的前驱
6.求一个数x的后继

solution:

这其实是一个平衡树的模板题 但是操作数少 STL也能过 看到要求可以知道得是保证有序性的数据结构 但是不需要去重 所以把set排除
其实vector也有对应的insert and erase操作 结合二分 以上六种操作可以在O(logn) 或者 O(1) 内实现
特别的 对于求前驱和后继 分别使用lower and upper 二分函数即可

code:

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define ULL unsigned long long
#define mes(x, a) memset(x, a, sizeof(x));
#define sca(a) scanf("%d", &a)
#define lowbit(x) x&(-x)
#define mk make_pair
#define pb(x) push_back(x)
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define lson v << 1
#define rson v << 1 | 1
#define pii pair<int, int>

inline void read(int &x)
{
    x=0;
    int flag_read=1;
    char c=getchar();
    while(c<'0'||c>'9')
    {
        if(c=='-')
            flag_read=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        x=(x<<3)+(x<<1)+c-'0';
        c=getchar();
    }
    x *= flag_read;
}

void out(int x){
    if(x > 9){
        out(x / 10);
    }
    putchar(x % 10 + '0');
}

vector <int> v;

int main(){
    ios::sync_with_stdio(0);
    int n;cin >> n ;

    while(n --){
        int op,x;
        cin >> op >> x;

        if(op == 1){
            v.insert(lower_bound(all(v),x),x);
        }else if(op == 2){
            v.erase(lower_bound(all(v),x));
        }else if(op == 3){
            auto it = lower_bound(all(v),x) - v.begin() + 1;
            cout << it << '\n';
        }else if(op == 4){
            cout << v[x - 1] << '\n';
        }else if(op == 5){
            auto it = lower_bound(all(v),x);
            cout << *(-- it) << '\n';
        }else if(op == 6){
            auto it = upper_bound(all(v),x);
            cout << *(it);
            cout << '\n';
        }
    }
    return 0;
}