ABC241D 题解

· · 题解

multiset 以及 upper_bound()lower_bound() 做此题。

定义一个 multiset,命名为 a;

#include <bits/stdc++.h>
#define int long long

using namespace std;

multiset<int> a;

int q, p, x, k;

signed main() {
    cin >> q;
    while (q--) {
        cin >> p >> x;
        if (p == 1) a.insert(x);
        if (p == 2) {
            cin >> k;
            multiset<int>::iterator n = a.upper_bound(x);
            while (n != a.begin() && k) {
                k--;
                n--;
            }
            if (k) cout << -1 << '\n';
            else cout << *n << '\n';
        }
        if (p == 3) {
            cin >> k;
            multiset<int>::iterator n = a.lower_bound(x);
            while (n != a.end() && k > 1) {
                k--;
                ++n;
            }
            if (n == a.end()) cout << -1 << '\n';
            else cout << *n << '\n';
        }
    }
    return 0;
}