結果

問題 No.649 ここでちょっとQK!
ユーザー suisensuisen
提出日時 2023-02-04 04:59:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 193 ms / 3,000 ms
コード長 27,006 bytes
コンパイル時間 1,114 ms
コンパイル使用メモリ 114,904 KB
実行使用メモリ 14,840 KB
最終ジャッジ日時 2024-07-03 03:03:15
合計ジャッジ時間 5,193 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 22 ms
6,940 KB
testcase_04 AC 81 ms
14,664 KB
testcase_05 AC 81 ms
14,840 KB
testcase_06 AC 90 ms
13,548 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 81 ms
8,564 KB
testcase_13 AC 81 ms
8,560 KB
testcase_14 AC 79 ms
8,564 KB
testcase_15 AC 84 ms
8,564 KB
testcase_16 AC 85 ms
8,432 KB
testcase_17 AC 91 ms
8,560 KB
testcase_18 AC 98 ms
8,564 KB
testcase_19 AC 107 ms
8,564 KB
testcase_20 AC 119 ms
8,560 KB
testcase_21 AC 130 ms
8,560 KB
testcase_22 AC 142 ms
13,620 KB
testcase_23 AC 160 ms
13,684 KB
testcase_24 AC 172 ms
13,680 KB
testcase_25 AC 181 ms
13,680 KB
testcase_26 AC 193 ms
13,812 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 3 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 71 ms
8,560 KB
testcase_31 AC 74 ms
8,560 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define PROBLEM "https://yukicoder.me/problems/no/649"

#include <iostream>

#include <cassert>
#include <cstdint>
#include <optional>
#include <string>
#include <random>
#include <tuple>
#include <vector>
#include <utility>

namespace suisen::internal::implicit_treap {
    template <typename T, typename Derived>
    struct Node {
        using random_engine = std::mt19937;
        static inline random_engine rng{ std::random_device{}() };

        using node_type = Derived;
        using node_pointer = uint32_t;
        using priority_type = std::invoke_result_t<random_engine>;

        using size_type = uint32_t;

        using difference_type = int32_t;
        using value_type = T;
        using pointer = value_type*;
        using const_pointer = const value_type*;
        using reference = value_type&;
        using const_reference = const value_type&;

        static inline std::vector<node_type> _nodes{};
        static inline std::vector<node_pointer> _erased{};

        static constexpr node_pointer null = ~node_pointer(0);

        node_pointer _ch[2]{ null, null };
        value_type _val;
        size_type _size;
        priority_type _priority;

        node_pointer _prev = null, _next = null;

        Node(const value_type val = {}): _val(val), _size(1), _priority(rng()) {}

        static void reserve(size_type capacity) { _nodes.reserve(capacity); }

        static bool is_null(node_pointer t) { return t == null; }
        static bool is_not_null(node_pointer t) { return not is_null(t); }

        static node_type& node(node_pointer t) { return _nodes[t]; }
        static const node_type& const_node(node_pointer t) { return _nodes[t]; }

        static value_type& value(node_pointer t) { return node(t)._val; }
        static value_type set_value(node_pointer t, const value_type& new_val) { return std::exchange(value(t), new_val); }

        static bool empty(node_pointer t) { return is_null(t); }
        static size_type& size(node_pointer t) { return node(t)._size; }
        static size_type safe_size(node_pointer t) { return empty(t) ? 0 : size(t); }

        static priority_type priority(node_pointer t) { return const_node(t)._priority; }

        static node_pointer& prev(node_pointer t) { return node(t)._prev; }
        static node_pointer& next(node_pointer t) { return node(t)._next; }
        static void link(node_pointer l, node_pointer r) { next(l) = r, prev(r) = l; }

        static node_pointer min(node_pointer t) {
            while (true) {
                node_type::push(t);
                node_pointer nt = child0(t);
                if (is_null(nt)) return t;
                t = nt;
            }
        }
        static node_pointer max(node_pointer t) {
            while (true) {
                node_type::push(t);
                node_pointer nt = child1(t);
                if (is_null(nt)) return t;
                t = nt;
            }
        }

        static node_pointer& child0(node_pointer t) { return node(t)._ch[0]; }
        static node_pointer& child1(node_pointer t) { return node(t)._ch[1]; }
        static node_pointer& child(node_pointer t, bool b) { return node(t)._ch[b]; }
        static node_pointer set_child0(node_pointer t, node_pointer cid) { return std::exchange(child0(t), cid); }
        static node_pointer set_child1(node_pointer t, node_pointer cid) { return std::exchange(child1(t), cid); }
        static node_pointer set_child(node_pointer t, bool b, node_pointer cid) { return std::exchange(child(t, b), cid); }

        static node_pointer update(node_pointer t) { // t : not null
            size(t) = safe_size(child0(t)) + safe_size(child1(t)) + 1;
            return t;
        }
        static void push(node_pointer) {}

        static node_pointer empty_node() { return null; }
        template <typename ...Args>
        static node_pointer create_node(Args &&...args) {
            if (_erased.size()) {
                node_pointer res = _erased.back();
                _erased.pop_back();
                node(res) = node_type(std::forward<Args>(args)...);
                return res;
            } else {
                node_pointer res = _nodes.size();
                _nodes.emplace_back(std::forward<Args>(args)...);
                return res;
            }
        }
        static void delete_node(node_pointer t) { _erased.push_back(t); }
        static void delete_tree(node_pointer t) {
            if (is_null(t)) return;
            delete_tree(child0(t));
            delete_tree(child1(t));
            delete_node(t);
        }

        template <typename ...Args>
        static node_pointer build(Args &&... args) {
            node_pointer res = empty_node();
            for (auto&& e : std::vector<value_type>(std::forward<Args>(args)...)) {
                res = push_back(res, std::move(e));
            }
            return res;
        }

        static std::pair<node_pointer, node_pointer> split(node_pointer t, size_type k) {
            if (k == 0) return { null, t };
            if (k == size(t)) return { t, null };

            static std::vector<node_pointer> lp{}, rp{};

            while (true) {
                node_type::push(t);
                if (const size_type lsiz = safe_size(child0(t)); k <= lsiz) {
                    if (rp.size()) set_child0(rp.back(), t);
                    rp.push_back(t);
                    if (k == lsiz) {
                        if (lp.size()) set_child1(lp.back(), child0(t));

                        node_pointer lt = set_child0(t, null), rt = null;

                        while (lp.size()) node_type::update(lt = lp.back()), lp.pop_back();
                        while (rp.size()) node_type::update(rt = rp.back()), rp.pop_back();

                        return { lt, rt };
                    }
                    t = child0(t);
                } else {
                    if (lp.size()) set_child1(lp.back(), t);
                    lp.push_back(t);
                    t = child1(t);
                    k -= lsiz + 1;
                }
            }
        }
        static std::tuple<node_pointer, node_pointer, node_pointer> split(node_pointer t, size_type l, size_type r) {
            auto [tlm, tr] = split(t, r);
            auto [tl, tm] = split(tlm, l);
            return { tl, tm, tr };
        }

        static node_pointer merge_impl(node_pointer tl, node_pointer tr) {
            if (priority(tl) < priority(tr)) {
                node_type::push(tr);
                if (node_pointer tm = child0(tr); is_null(tm)) {
                    link(max(tl), tr);
                    set_child0(tr, tl);
                } else {
                    set_child0(tr, merge(tl, tm));
                }
                return node_type::update(tr);
            } else {
                node_type::push(tl);
                if (node_pointer tm = child1(tl); is_null(tm)) {
                    link(tl, min(tr));
                    set_child1(tl, tr);
                } else {
                    set_child1(tl, merge(tm, tr));
                }
                return node_type::update(tl);
            }
        }
        static node_pointer merge(node_pointer tl, node_pointer tr) {
            if (is_null(tl)) return tr;
            if (is_null(tr)) return tl;
            return merge_impl(tl, tr);
        }
        static node_pointer merge(node_pointer tl, node_pointer tm, node_pointer tr) {
            return merge(merge(tl, tm), tr);
        }

        static node_pointer insert_impl(node_pointer t, size_type k, node_pointer new_node) {
            if (is_null(t)) return new_node;
            static std::vector<node_pointer> st;
            bool b = false;

            while (true) {
                if (is_null(t) or priority(new_node) > priority(t)) {
                    if (is_null(t)) {
                        t = new_node;
                    } else {
                        auto [tl, tr] = split(t, k);
                        if (is_not_null(tl)) link(max(tl), new_node);
                        if (is_not_null(tr)) link(new_node, min(tr));
                        set_child0(new_node, tl);
                        set_child1(new_node, tr);
                        t = node_type::update(new_node);
                    }
                    if (st.size()) {
                        set_child(st.back(), b, t);
                        do t = node_type::update(st.back()), st.pop_back(); while (st.size());
                    }
                    return t;
                } else {
                    node_type::push(t);
                    if (const size_type lsiz = safe_size(child0(t)); k <= lsiz) {
                        if (k == lsiz) link(new_node, t);
                        st.push_back(t), b = false;
                        t = child0(t);
                    } else {
                        if (k == lsiz + 1) link(t, new_node);
                        st.push_back(t), b = true;
                        t = child1(t);
                        k -= lsiz + 1;
                    }
                }
            }
        }
        template <typename ...Args>
        static node_pointer insert(node_pointer t, size_type k, Args &&...args) {
            return insert_impl(t, k, create_node(std::forward<Args>(args)...));
        }
        template <typename ...Args>
        static node_pointer push_front(node_pointer t, Args &&...args) {
            return insert(t, 0, std::forward<Args>(args)...);
        }
        template <typename ...Args>
        static node_pointer push_back(node_pointer t, Args &&...args) {
            return insert(t, safe_size(t), std::forward<Args>(args)...);
        }

        static std::pair<node_pointer, value_type> erase(node_pointer t, size_type k) {
            node_type::push(t);
            if (const size_type lsiz = safe_size(child0(t)); k == lsiz) {
                delete_node(t);
                return { merge(child0(t), child1(t)), std::move(value(t)) };
            } else if (k < lsiz) {
                auto [c0, v] = erase(child0(t), k);
                set_child0(t, c0);
                if (is_not_null(c0) and k == lsiz - 1) link(max(c0), t);
                return { node_type::update(t), std::move(v) };
            } else {
                auto [c1, v] = erase(child1(t), k - (lsiz + 1));
                set_child1(t, c1);
                if (is_not_null(c1) and k == lsiz + 1) link(t, min(c1));
                return { node_type::update(t), std::move(v) };
            }
        }
        static std::pair<node_pointer, value_type> pop_front(node_pointer t) { return erase(t, 0); }
        static std::pair<node_pointer, value_type> pop_back(node_pointer t) { return erase(t, safe_size(t) - 1); }

        static node_pointer rotate(node_pointer t, size_type k) {
            auto [tl, tr] = split(t, k);
            return merge(tr, tl);
        }
        static node_pointer rotate(node_pointer t, size_type l, size_type m, size_type r) {
            auto [tl, tm, tr] = split(t, l, r);
            return merge(tl, rotate(tm, m - l), tr);
        }

        static value_type& get(node_pointer t, size_type k) {
            while (true) {
                node_type::push(t);
                if (const size_type lsiz = safe_size(child0(t)); k == lsiz) {
                    return value(t);
                } else if (k < lsiz) {
                    t = child0(t);
                } else {
                    k -= lsiz + 1;
                    t = child1(t);
                }
            }
        }

        template <typename Func>
        static node_pointer set_update(node_pointer t, size_type k, const Func& f) {
            node_type::push(t);
            if (const size_type lsiz = safe_size(child0(t)); k == lsiz) {
                value_type& val = value(t);
                val = f(const_cast<const value_type&>(val));
            } else if (k < lsiz) {
                set_child0(t, set_update(child0(t), k, f));
            } else {
                set_child1(t, set_update(child1(t), k - (lsiz + 1), f));
            }
            return node_type::update(t);
        }

        static std::vector<value_type> dump(node_pointer t) {
            std::vector<value_type> res;
            res.reserve(safe_size(t));
            auto rec = [&](auto rec, node_pointer t) -> void {
                if (is_null(t)) return;
                node_type::push(t);
                rec(rec, child0(t));
                res.push_back(value(t));
                rec(rec, child1(t));
            };
            rec(rec, t);
            return res;
        }

        template <bool reversed_, bool constant_>
        struct NodeIterator {
            static constexpr bool constant = constant_;
            static constexpr bool reversed = reversed_;

            friend Node;
            friend NodeIterator<false, false>;
            friend NodeIterator<false, true>;
            friend NodeIterator<true, false>;
            friend NodeIterator<true, true>;

            using difference_type = Node::difference_type;
            using value_type = Node::value_type;
            using pointer = std::conditional_t<constant, Node::const_pointer, Node::pointer>;
            using reference = std::conditional_t<constant, Node::const_reference, Node::reference>;
            using iterator_cateogory = std::random_access_iterator_tag;

            NodeIterator(node_pointer root = null): NodeIterator(root, ~size_type(0)) {}

            reference operator*() {
                if (is_null(cur) and index != safe_size(root)) {
                    cur = root;
                    for (size_type k = index;;) {
                        node_type::push(cur);
                        if (size_type siz = safe_size(child(cur, reversed)); k == siz) {
                            break;
                        } else if (k < siz) {
                            cur = child(cur, reversed);
                        } else {
                            cur = child(cur, not reversed);
                            k -= siz + 1;
                        }
                    }
                }
                return value(cur);
            }
            reference operator[](difference_type k) const { return *((*this) + k); }

            NodeIterator& operator++() { return *this += 1; }
            NodeIterator& operator--() { return *this -= 1; }
            NodeIterator& operator+=(difference_type k) { return suc(+k), * this; }
            NodeIterator& operator-=(difference_type k) { return suc(-k), * this; }
            NodeIterator operator++(int) { NodeIterator res = *this; ++(*this); return res; }
            NodeIterator operator--(int) { NodeIterator res = *this; --(*this); return res; }
            friend NodeIterator operator+(NodeIterator it, difference_type k) { return it += k; }
            friend NodeIterator operator+(difference_type k, NodeIterator it) { return it += k; }
            friend NodeIterator operator-(NodeIterator it, difference_type k) { return it -= k; }

            friend difference_type operator-(const NodeIterator& lhs, const NodeIterator& rhs) { return lhs.index - rhs.index; }

            friend bool operator==(const NodeIterator& lhs, const NodeIterator& rhs) { return lhs.index == rhs.index; }
            friend bool operator!=(const NodeIterator& lhs, const NodeIterator& rhs) { return lhs.index != rhs.index; }
            friend bool operator<(const NodeIterator& lhs, const NodeIterator& rhs) { return lhs.index < rhs.index; }
            friend bool operator>(const NodeIterator& lhs, const NodeIterator& rhs) { return lhs.index > rhs.index; }
            friend bool operator<=(const NodeIterator& lhs, const NodeIterator& rhs) { return lhs.index <= rhs.index; }
            friend bool operator>=(const NodeIterator& lhs, const NodeIterator& rhs) { return lhs.index >= rhs.index; }

            static NodeIterator begin(node_pointer root) { return NodeIterator(root, 0); }
            static NodeIterator end(node_pointer root) { return NodeIterator(root, safe_size(root)); }

            int get_index() const {
                return index;
            }
            int size() const {
                return safe_size(root);
            }
            NodeIterator<reversed, false> remove_const() const {
                return NodeIterator<reversed, false>(root, index, cur);
            }
        private:
            node_pointer root;
            size_type index;
            node_pointer cur = null; // it==end() or uninitialized (updates only index)

            NodeIterator(node_pointer root, size_type index, node_pointer cur = null): root(root), index(index), cur(cur) {}

            void suc(difference_type k) {
                index += k;
                if (index == safe_size(root) or std::abs(k) >= 10) cur = null;
                if (is_null(cur)) return;

                const bool positive = k < 0 ? (k = -k, reversed) : not reversed;

                if (positive) {
                    while (k-- > 0) cur = next(cur);
                } else {
                    while (k-- > 0) cur = prev(cur);
                }
            }
            node_pointer get_root() const {
                return root;
            }
            node_pointer node() const {
                return cur;
            }
            bool is_default() const {
                return index == ~size_type(0);
            }
            void init_binary_search() {
                cur = root;
                node_type::push(cur);
                index = safe_size(child0(cur));
            }
            void go_left() {
                cur = child0(cur);
                index -= is_null(cur) ? 1 : safe_size(child1(cur)) + 1;
            }
            void go_right() {
                cur = child1(cur);
                index += is_null(cur) ? 1 : safe_size(child0(cur)) + 1;
            }
        };
        using iterator = NodeIterator<false, false>;
        using reverse_iterator = NodeIterator<true, false>;
        using const_iterator = NodeIterator<false, true>;
        using const_reverse_iterator = NodeIterator<true, true>;

        static iterator begin(node_pointer t) { return iterator::begin(t); }
        static iterator end(node_pointer t) { return iterator::end(t); }
        static reverse_iterator rbegin(node_pointer t) { return reverse_iterator::begin(t); }
        static reverse_iterator rend(node_pointer t) { return reverse_iterator::end(t); }
        static const_iterator cbegin(node_pointer t) { return const_iterator::begin(t); }
        static const_iterator cend(node_pointer t) { return const_iterator::end(t); }
        static const_reverse_iterator crbegin(node_pointer t) { return const_reverse_iterator::begin(t); }
        static const_reverse_iterator crend(node_pointer t) { return const_reverse_iterator::end(t); }

        // Find the first element that satisfies the condition f : const_iterator -> { false, true }.
        // Returns const_iterator
        template <typename Predicate>
        static const_iterator binary_search(node_pointer t, const Predicate& f) {
            if (is_null(t)) return cend(t);
            const_iterator it(t), res = cend(t);
            it.init_binary_search();
            while (is_not_null(it.node())) {
                node_type::push(it.node());
                if (f(it)) {
                    res = it;
                    it.go_left();
                } else {
                    it.go_right();
                }
            }
            return res;
        }
        // comp(T t, U u) = (t < u)
        template <typename U, typename Compare = std::less<>>
        static const_iterator lower_bound(node_pointer t, const U& target, Compare comp) {
            return binary_search(t, [&](const_iterator it) { return not comp(*it, target); });
        }
        // comp(T u, U t) = (u < t)
        template <typename U, typename Compare = std::less<>>
        static const_iterator upper_bound(node_pointer t, const U& target, Compare comp) {
            return binary_search(t, [&](const_iterator it) { return comp(target, *it); });
        }

        static node_pointer insert(const_iterator it, const value_type &val) {
            return insert(it.root, it.index, val);
        }
        static std::pair<node_pointer, value_type> erase(const_iterator it) {
            return erase(it.root, it.index);
        }
        static std::pair<node_pointer, node_pointer> split(const_iterator it) {
            return split(it.root, it.index);
        }
    };
} // namespace suisen::internal::implicit_treap

namespace suisen {
    namespace internal::implicit_treap {
        template <typename T>
        struct DefaultNode: Node<T, DefaultNode<T>> {
            using base = Node<T, DefaultNode<T>>;
            using base::base;
        };
    }

    template <typename T>
    class DynamicArray {
        using node_type = internal::implicit_treap::DefaultNode<T>;
        using node_pointer = typename node_type::node_pointer;

        node_pointer _root;

        struct node_pointer_construct {};
        DynamicArray(node_pointer root, node_pointer_construct): _root(root) {}

    public:
        using value_type = typename node_type::value_type;

        DynamicArray(): _root(node_type::empty_node()) {}
        explicit DynamicArray(size_t n, const value_type& fill_value = {}): _root(node_type::build(n, fill_value)) {}
        template <typename U>
        DynamicArray(const std::vector<U>& dat) : _root(node_type::build(dat.begin(), dat.end())) {}

        void free() {
            node_type::delete_tree(_root);
            _root = node_type::empty_node();
        }
        void clear() { free(); }

        static void reserve(size_t capacity) { node_type::reserve(capacity); }

        bool empty() const { return node_type::empty(_root); }
        int size() const { return node_type::safe_size(_root); }

        value_type& operator[](size_t k) {
            assert(k < size_t(size()));
            return node_type::get(_root, k);
        }
        const value_type& operator[](size_t k) const {
            assert(k < size_t(size()));
            return node_type::get(_root, k);
        }
        value_type& front() { return (*this)[0]; }
        value_type& back() { return (*this)[size() - 1]; }
        const value_type& front() const { return (*this)[0]; }
        const value_type& back() const { return (*this)[size() - 1]; }

        void insert(size_t k, const value_type& val) {
            assert(k <= size_t(size()));
            _root = node_type::insert(_root, k, val);
        }
        void push_front(const value_type& val) { _root = node_type::push_front(_root, val); }
        void push_back(const value_type& val) { _root = node_type::push_back(_root, val); }

        value_type erase(size_t k) {
            assert(k <= size_t(size()));
            value_type v;
            std::tie(_root, v) = node_type::erase(_root, k);
            return v;
        }
        value_type pop_front() { return erase(0); }
        value_type pop_back() { return erase(size() - 1); }

        // Split immediately before the k-th element.
        DynamicArray split(size_t k) {
            assert(k <= size_t(size()));
            node_pointer root_r;
            std::tie(_root, root_r) = node_type::split(_root, k);
            return DynamicArray(root_r, node_pointer_construct{});
        }

        void merge(DynamicArray r) { _root = node_type::merge(_root, r._root); }

        void rotate(size_t k) {
            assert(k <= size_t(size()));
            _root = node_type::rotate(_root, k);
        }
        void rotate(size_t l, size_t m, size_t r) {
            assert(l <= m and m <= r and r <= size_t(size()));
            _root = node_type::rotate(_root, l, m, r);
        }

        std::vector<value_type> dump() const { return node_type::dump(_root); }

        using iterator = typename node_type::iterator;
        using reverse_iterator = typename node_type::reverse_iterator;
        using const_iterator = typename node_type::const_iterator;
        using const_reverse_iterator = typename node_type::const_reverse_iterator;

        iterator begin() { return node_type::begin(_root); }
        iterator end() { return node_type::end(_root); }
        reverse_iterator rbegin() { return node_type::rbegin(_root); }
        reverse_iterator rend() { return node_type::rend(_root); }

        const_iterator begin() const { return cbegin(); }
        const_iterator end() const { return cend(); }
        const_reverse_iterator rbegin() const { return crbegin(); }
        const_reverse_iterator rend() const { return crend(); }
        const_iterator cbegin() const { return node_type::cbegin(_root); }
        const_iterator cend() const { return node_type::cend(_root); }
        const_reverse_iterator crbegin() const { return node_type::crbegin(_root); }
        const_reverse_iterator crend() const { return node_type::crend(_root); }

        // Find the first element that satisfies the condition f.
        // Returns { position, optional(value) }
        // Requirements: f(A[i]) must be monotonic
        template <typename Predicate>
        const_iterator binary_search(const Predicate& f) {
            return node_type::binary_search(_root, f);
        }
        // comp(T t, U u) = (t < u)
        // Requirements: sequence is sorted
        template <typename U, typename Compare = std::less<>>
        const_iterator lower_bound(const U& target, Compare comp = {}) const {
            return node_type::lower_bound(_root, target, comp);
        }
        // comp(T u, U t) = (u < t)
        // Requirements: sequence is sorted
        template <typename U, typename Compare = std::less<>>
        const_iterator upper_bound(const U& target, Compare comp = {}) const {
            return node_type::upper_bound(_root, target, comp);
        }
 
        void insert(const_iterator it, const value_type &val) {
            _root = node_type::insert(it, val);
        }
        value_type erase(const_iterator it) {
            value_type erased;
            std::tie(_root, erased) = node_type::erase(it);
            return erased;
        }
        DynamicArray split(const_iterator it) {
            node_pointer root_r;
            std::tie(_root, root_r) = node_type::split(it);
            return DynamicArray(root_r, node_pointer_construct{});
        }
    };
} // namespace suisen

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int q, k;
    std::cin >> q >> k;

    using suisen::DynamicArray;

    DynamicArray<long long> a;
    while (q-- > 0) {
        int t;
        std::cin >> t;
        if (t == 1) {
            long long v;
            std::cin >> v;
            a.insert(a.lower_bound(v), v);
        } else {
            if (a.size() < k) {
                std::cout << -1 << '\n';
            } else {
                std::cout << a.erase(k - 1) << '\n';
            }
        }
    }
}

0