Kuro-orzz's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View on GitHub

:heavy_check_mark: DataStructure/Ordered_set.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/ordered_set"

#include "../template.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;

void solve() {
    int n, q; cin >> n >> q;
    ordered_set s;
    for (int i = 0; i < n; i++) {
        int x; cin >> x;
        s.insert(x);
    }
    while (q--) {
        int tv, x; cin >> tv >> x;
        if (tv == 0) {
            s.insert(x);
        } else if (tv == 1) {
            s.erase(x);
        } else if (tv == 2) {
            if (x > (int)s.size()) cout << "-1\n";
            else cout << *s.find_by_order(x-1) << '\n';
        } else if (tv == 3) {
            cout << s.order_of_key(x+1) << '\n';
        } else if (tv == 4) {
            int idx = s.order_of_key(x+1);
            if (idx == 0) cout << "-1\n";
            else cout << *s.find_by_order(idx-1) << '\n';
        } else if (tv == 5) {
            int idx = s.order_of_key(x);
            if (idx == (int)s.size()) cout << "-1\n";
            else cout << *s.find_by_order(idx) << '\n';
        }
    }
}
#line 1 "DataStructure/Ordered_set.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/ordered_set"

#line 2 "template.h"

#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define MOD (ll)(1e9+7)
#define all(x) (x).begin(),(x).end()
#define unique(x) x.erase(unique(all(x)), x.end())
#define INF32 ((1ull<<31)-1)
#define INF64 ((1ull<<63)-1)
#define inf (ll)1e18

#define vi vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second

const int mod = 998244353;

void solve();

int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    // cin.exceptions(cin.failbit);
    // int t; cin >> t;
    // while(t--)
        solve();
    cerr << "\nTime run: " << 1000 * clock() / CLOCKS_PER_SEC << "ms" << '\n';
    return 0;
}
#line 4 "DataStructure/Ordered_set.test.cpp"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;

void solve() {
    int n, q; cin >> n >> q;
    ordered_set s;
    for (int i = 0; i < n; i++) {
        int x; cin >> x;
        s.insert(x);
    }
    while (q--) {
        int tv, x; cin >> tv >> x;
        if (tv == 0) {
            s.insert(x);
        } else if (tv == 1) {
            s.erase(x);
        } else if (tv == 2) {
            if (x > (int)s.size()) cout << "-1\n";
            else cout << *s.find_by_order(x-1) << '\n';
        } else if (tv == 3) {
            cout << s.order_of_key(x+1) << '\n';
        } else if (tv == 4) {
            int idx = s.order_of_key(x+1);
            if (idx == 0) cout << "-1\n";
            else cout << *s.find_by_order(idx-1) << '\n';
        } else if (tv == 5) {
            int idx = s.order_of_key(x);
            if (idx == (int)s.size()) cout << "-1\n";
            else cout << *s.find_by_order(idx) << '\n';
        }
    }
}
Back to top page