kyopro-lib-cpp

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

View the Project on GitHub packer-jp/kyopro-lib-cpp

:heavy_check_mark: test/onlinejudge.u-aizu.ac.jp/Vector.0.test.cpp

Depends on

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/8/ITP2/1/ITP2_1_A"
#include "../../data_structure/splay_tree.hpp"

#include <bits/stdc++.h>
using namespace std;

int main() {
    int q;
    cin >> q;
    splay_tree<ll> st;
    while (q--) {
        ll ord, x;
        cin >> ord;
        if (ord == 0) {
            cin >> x;
            st.insert(st.size(), x);
        }
        if (ord == 1) {
            cin >> x;
            cout << st[x] << endl;
        }
        if (ord == 2) st.erase(st.size() - 1);
    }
}
#line 1 "test/onlinejudge.u-aizu.ac.jp/Vector.0.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/8/ITP2/1/ITP2_1_A"
#line 2 "data_structure/splay_tree.hpp"

#line 2 "template.hpp"

#include <bits/stdc++.h>
using namespace std;

#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)
#define uniq(a) (a).erase(unique(all(a)), (a).end())
#define t0 first
#define t1 second
using ll = long long;
using ull = unsigned long long;
using pll = pair<ll, ll>;
using vll = vector<ll>;
constexpr double pi = 3.14159265358979323846;
constexpr ll dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
constexpr ll dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
constexpr ll sign(ll a) { return (a > 0) - (a < 0); }
constexpr ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
constexpr ll cdiv(ll a, ll b) { return -fdiv(-a, b); }
constexpr ll pw(ll n) { return 1ll << n; }
constexpr ll flg(ll n) { return 63 - __builtin_clzll(n); }
constexpr ll clg(ll n) { return n == 1 ? 0 : flg(n - 1) + 1; }
constexpr ll safemod(ll x, ll mod) { return (x % mod + mod) % mod; }
template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
template <typename T> constexpr T sq(const T &a) { return a * a; }
template <typename T, typename U> constexpr bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }
template <typename T, typename U> constexpr bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &a) {
    os << "(" << a.first << ", " << a.second << ")";
    return os;
}
template <typename T, typename U, typename V> ostream &operator<<(ostream &os, const tuple<T, U, V> &a) {
    os << "(" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
    os << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
    os << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
    os << ")";
    return os;
}
template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
    os << ")";
    return os;
}
#ifdef ONLINE_JUDGE
#define dump(...) (void(0))
#else
void debug() { cerr << endl; }
template <typename Head, typename... Tail> void debug(Head &&head, Tail &&... tail) {
    cerr << head;
    if (sizeof...(Tail)) cerr << ", ";
    debug(tail...);
}
#define dump(...) cerr << __LINE__ << ": " << #__VA_ARGS__ << " = ", debug(__VA_ARGS__)
#endif
struct rep {
    struct itr {
        ll v;
        itr(ll v) : v(v) {}
        void operator++() { ++v; }
        ll operator*() const { return v; }
        bool operator!=(itr i) const { return v < *i; }
    };
    ll l, r;
    rep(ll l, ll r) : l(l), r(r) {}
    rep(ll r) : rep(0, r) {}
    itr begin() const { return l; };
    itr end() const { return r; };
};
struct per {
    struct itr {
        ll v;
        itr(ll v) : v(v) {}
        void operator++() { --v; }
        ll operator*() const { return v; }
        bool operator!=(itr i) const { return v > *i; }
    };
    ll l, r;
    per(ll l, ll r) : l(l), r(r) {}
    per(ll r) : per(0, r) {}
    itr begin() const { return r - 1; };
    itr end() const { return l - 1; };
};
struct io_setup {
    static constexpr int PREC = 20;
    io_setup() {
        cout << fixed << setprecision(PREC);
        cerr << fixed << setprecision(PREC);
    };
} iOS;
#line 4 "data_structure/splay_tree.hpp"

template <typename V> struct splay_tree {
    struct node {
        node *left, *right, *par;
        ll size;
        V val;
        node(V val) : left(nullptr), right(nullptr), par(nullptr), size(1), val(val) {}
        void rotate() {
            node *p, *pp, *c;
            p = par, pp = p->par;
            if (p->left == this) {
                c = right, right = p, p->left = c;
            } else {
                c = left, left = p, p->right = c;
            }
            if (pp) {
                if (pp->left == p) {
                    pp->left = this;
                } else {
                    pp->right = this;
                }
            }
            if (c) c->par = p;
            par = pp, p->par = this;
            p->update();
            update();
        }
        ll state() {
            if (!par) { return 0; }
            if (par->left == this) {
                return 1;
            } else {
                return -1;
            }
        }
        void splay() {
            while (state() != 0) {
                if (par->state() == 0) {
                    rotate();
                } else if (state() == par->state()) {
                    par->rotate(), rotate();
                } else {
                    rotate(), rotate();
                }
            }
        }
        void update() {
            size = 1;
            if (left) size += left->size;
            if (right) size += right->size;
        }
    };
    node *root;
    splay_tree(node *root = nullptr) : root(root) {}
    ll size() { return root ? root->size : 0; }
    node *get_node(ll i) {
        node *cur = root;
        while (true) {
            ll size_l = cur->left ? cur->left->size : 0;
            if (i < size_l) cur = cur->left;
            if (i == size_l) {
                cur->splay();
                return root = cur;
            }
            if (i > size_l) cur = cur->right, i -= size_l + 1;
        }
    }
    V &operator[](ll i) { return get_node(i)->val; }
    template <typename F> ll lower_bound(F f) {
        if (!root) return 0;
        node *cur = root;
        ll ret = -1;
        while (true) {
            if (cur->state() < 1) {
                ret += cur->left ? cur->left->size + 1 : 1;
            } else {
                ret -= cur->right ? cur->right->size + 1 : 1;
            }
            if (f(cur->val)) {
                if (cur->left) {
                    cur = cur->left;
                } else {
                    cur->splay(), root = cur;
                    return ret;
                }
            } else {
                if (cur->right) {
                    cur = cur->right;
                } else {
                    cur->splay(), root = cur;
                    return ret + 1;
                }
            }
        }
    }
    splay_tree split(ll size_left) {
        if (size_left == 0) {
            node *root_r = root;
            root = nullptr;
            return root_r;
        }
        if (size_left == root->size) return nullptr;
        node *root_r = get_node(size_left);
        root = root_r->left;
        root_r->left = nullptr, root->par = nullptr;
        root_r->update();
        return root_r;
    }
    void merge(splay_tree right) {
        if (!root) {
            root = right.root;
            return;
        }
        if (!right.root) return;
        get_node(root->size - 1);
        root->right = right.root, right.root->par = root;
        root->update();
    }
    void insert(ll i, V val) {
        splay_tree vt = new node(val), right = split(i);
        merge(vt), merge(right);
    }
    void erase(ll i) {
        splay_tree vt = split(i);
        splay_tree right = vt.split(1);
        delete vt.root;
        merge(right);
    }
};
#line 3 "test/onlinejudge.u-aizu.ac.jp/Vector.0.test.cpp"

#line 5 "test/onlinejudge.u-aizu.ac.jp/Vector.0.test.cpp"
using namespace std;

int main() {
    int q;
    cin >> q;
    splay_tree<ll> st;
    while (q--) {
        ll ord, x;
        cin >> ord;
        if (ord == 0) {
            cin >> x;
            st.insert(st.size(), x);
        }
        if (ord == 1) {
            cin >> x;
            cout << st[x] << endl;
        }
        if (ord == 2) st.erase(st.size() - 1);
    }
}
Back to top page