P3369 【模板】普通平衡树(容器做法)

题目描述

您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:

  1. 插入xx数
  2. 删除xx数(若有多个相同的数,因只删除一个)
  3. 查询xx数的排名(排名定义为比当前数小的数的个数+1+1。若有多个相同的数,因输出最小的排名)
  4. 查询排名为xx的数
  5. 求xx的前驱(前驱定义为小于xx,且最大的数)
  6. 求xx的后继(后继定义为大于xx,且最小的数)

输入输出格式

输入格式:

第一行为nn,表示操作的个数,下面nn行每行有两个数optopt和xx,optopt表示操作的序号( 1 \leq opt \leq 61≤opt≤6 )

输出格式:

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

输入输出样例

输入样例#1: 复制

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

输出样例#1: 复制

扫描二维码关注公众号,回复: 3732274 查看本文章
106465
84185
492737

说明

时空限制:1000ms,128M

1.n的数据范围: n \leq 100000n≤100000

2.每个数的数据范围: [-{10}^7, {10}^7][−107,107]

来源:Tyvj1728 原名:普通平衡树

在此鸣谢

vector真香

#include <bits/stdc++.h>
 
typedef long long ll;
 
using namespace std;
 
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
 
    vector<ll>a;
    vector<ll>::iterator it;
    ll t;
    cin >> t;
    while(t--)
    {
        ll x,y;
        cin >> x >> y;
        if(x == 1)
        {
            it = lower_bound(a.begin(),a.end(),y);
            a.insert(it,y);
        }
        if(x == 2)
        {
            it = lower_bound(a.begin(),a.end(),y);
            a.erase(it);
        }
        if(x == 3)
        {
            it = lower_bound(a.begin(),a.end(),y);
            cout << it - a.begin() + 1 << endl;
        }
        if(x == 4)
        {
            cout << a[y-1] << endl;
        }
        if(x == 5)
        {
            it = lower_bound(a.begin(),a.end(),y);
            it --;
            cout << *it << endl;
        }
        if(x == 6)
        {
            it = upper_bound(a.begin(),a.end(),y);
            cout << *it << endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zzzanj/article/details/83239764
今日推荐