【深基16.例7】普通二叉树(简化版)

题目描述

您需要写一种数据结构,来维护一些数( 都是 【洛谷 P5076】【深基16.例7】普通二叉树(简化版)题解(多重集合+lower_bound+upper_bound)_i++ 以内的数字)的集合,最开始时集合是空的。其中需要提供以下操作,操作次数 【洛谷 P5076】【深基16.例7】普通二叉树(简化版)题解(多重集合+lower_bound+upper_bound)_ci_02 不超过 【洛谷 P5076】【深基16.例7】普通二叉树(简化版)题解(多重集合+lower_bound+upper_bound)_#include_03

  1. 查询 【洛谷 P5076】【深基16.例7】普通二叉树(简化版)题解(多重集合+lower_bound+upper_bound)_#include_04 数的排名(排名定义为比当前数小的数的个数 【洛谷 P5076】【深基16.例7】普通二叉树(简化版)题解(多重集合+lower_bound+upper_bound)_#include_05。若有多个相同的数,应输出最小的排名)。
  2. 查询排名为 【洛谷 P5076】【深基16.例7】普通二叉树(简化版)题解(多重集合+lower_bound+upper_bound)_#include_04 的数。
  3. 【洛谷 P5076】【深基16.例7】普通二叉树(简化版)题解(多重集合+lower_bound+upper_bound)_#include_04 的前驱(前驱定义为小于 【洛谷 P5076】【深基16.例7】普通二叉树(简化版)题解(多重集合+lower_bound+upper_bound)_#include_04,且最大的数)。若未找到则输出 【洛谷 P5076】【深基16.例7】普通二叉树(简化版)题解(多重集合+lower_bound+upper_bound)_i++_09
  4. 【洛谷 P5076】【深基16.例7】普通二叉树(简化版)题解(多重集合+lower_bound+upper_bound)_#include_04 的后继(后继定义为大于 【洛谷 P5076】【深基16.例7】普通二叉树(简化版)题解(多重集合+lower_bound+upper_bound)_#include_04,且最小的数)。若未找到则输出 【洛谷 P5076】【深基16.例7】普通二叉树(简化版)题解(多重集合+lower_bound+upper_bound)_i++_12
  5. 插入一个数 【洛谷 P5076】【深基16.例7】普通二叉树(简化版)题解(多重集合+lower_bound+upper_bound)_#include_04

输入格式

第一行是一个整数 【洛谷 P5076】【深基16.例7】普通二叉树(简化版)题解(多重集合+lower_bound+upper_bound)_ci_02,表示操作次数。

接下来 【洛谷 P5076】【深基16.例7】普通二叉树(简化版)题解(多重集合+lower_bound+upper_bound)_ci_02 行,每行两个整数 【洛谷 P5076】【深基16.例7】普通二叉树(简化版)题解(多重集合+lower_bound+upper_bound)_i++_16,分别表示操作序号以及操作的参数 【洛谷 P5076】【深基16.例7】普通二叉树(简化版)题解(多重集合+lower_bound+upper_bound)_#include_04

输出格式

输出有若干行。对于操作 【洛谷 P5076】【深基16.例7】普通二叉树(简化版)题解(多重集合+lower_bound+upper_bound)_i++_18,输出一个整数,表示该操作的结果。

样例 #1

样例输入 #1

7
5 1
5 3
5 5
1 3
2 2
3 3
4 3

样例输出 #1

2
3
1
5

思路

使用一个 multiset 类型的容器 ms,用来存储二叉树中的节点值。

在插入节点时,直接使用 multiset 的 insert 函数即可。在查询节点值时,使用 multiset 的 lower_bound 函数找到大于等于 x 的第一个数即可。在查询排名和求前驱后继时,同样使用 multiset 的 lower_bound 和 upper_bound 函数找到相应的节点即可。


AC代码

#include <iostream>
#include <set>
#include <algorithm>
#define AUTHOR "HEX9CF"
using namespace std;

int main()
{
    int q;
    multiset<int> ms;
    ms.insert(2147483647);
    ms.insert(-2147483647);
    cin >> q;
    while (q--)
    {
        int op, x;
        int rank;
        multiset<int>::iterator it;
        cin >> op >> x;
        switch (op)
        {
        case 1:
            // 查询排名
            it = ms.lower_bound(x);
            rank = 0;
            for (auto i = ms.begin(); i != it; i++)
            {
                rank++;
            }
            cout << rank << endl;
            break;
        case 2:
            // 查询数
            it = ms.begin();
            for (int i = 0; i < x; i++)
            {
                it++;
            }
            cout << *it << endl;
            break;
        case 3:
            // 求前驱
            it = ms.lower_bound(x);
            it--;
            cout << *it << endl;
            break;
        case 4:
            // 求后继
            it = ms.upper_bound(x);
            cout << *it << endl;
            break;
        case 5:
            // 插入一个数
            ms.insert(x);
            break;
        }
    }
    return 0;
}