#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long VAL; random_device rd; mt19937 gen(rd()); uniform_int_distribution<> dis(1, 10000000); struct node_t{ VAL val; node_t* lch; node_t* rch; int cnt; VAL sum; node_t() : val(0), cnt(1), sum(0) {lch = rch = NULL;} node_t(VAL v) : val(v), cnt(1), sum(v) {lch = rch = NULL;} }; struct Tree { node_t* root; Tree() : root(NULL) { } Tree(node_t* t) : root(t) { } int cnt(node_t* t) {return !t ?0 :t->cnt;} int cnt() {return this->cnt(this->root);} VAL sum(node_t* t) {return !t ?0 :t->sum;} VAL sum() {return this->sum(this->root);} node_t* update(node_t* t) { t->cnt = cnt(t->lch) + cnt(t->rch) + 1; t->sum = sum(t->lch) + sum(t->rch) + t->val; return t; } int lowerBound(node_t* t, VAL val) { if (!t) return 0; if (val <= t->val) return lowerBound(t->lch, val); else return cnt(t->lch) + lowerBound(t->rch, val) + 1; } int lowerBound(VAL val) {return this->lowerBound(this->root, val);} int upperBound(node_t* t, VAL val) { return lowerBound(t, val+1); } int upperBound(VAL val) {return this->lowerBound(this->root, val+1);} VAL get(node_t* t, int k) { if (!t) return -1; if (k == cnt(t->lch)) return t->val; if (k < cnt(t->lch)) return get(t->lch, k); else return get(t->rch, k - cnt(t->lch) - 1); } VAL get(int k) {return get(this->root, k);} node_t* merge(node_t* l, node_t* r) { if (!l || !r) return !l ?r :l; if (dis(gen) % (l->cnt + r->cnt) < l->cnt) { l->rch = merge(l->rch, r); return update(l); } else { r->lch = merge(l, r->lch); return update(r); } } void merge(Tree add) {this->root = this->merge(this->root, add.root);} pair split(node_t* t, int k) { // [0, k), [k, n) if (!t) return make_pair(t, t); if (k <= cnt(t->lch)) { pair s = split(t->lch, k); t->lch = s.second; return make_pair(s.first, update(t)); } else { pair s = split(t->rch, k - cnt(t->lch) - 1); t->rch = s.first; return make_pair(update(t), s.second); } } Tree split(int k) { pair s = split(this->root, k); this->root = s.first; return Tree(s.second); } void insert(VAL val) { pair s = this->split(this->root, this->lowerBound(val)); this->root = this->merge(this->merge(s.first, new node_t(val)), s.second); } void erase(VAL val) { if (!(this->upperBound(val) - this->lowerBound(val))) return; pair s = this->split(this->root, this->lowerBound(val)); this->root = this->merge(s.first, this->split(s.second, 1).second); } }; int main() { int q, k; cin >> q >> k; Tree tree; for (int i = 0; i < q; i++) { int t; cin >> t; if (t == 1) { long long val; cin >> val; tree.insert(val); } else { if (tree.cnt() < k) { cout << -1 << endl; continue; } long long val = tree.get(k-1); cout << val << endl; tree.erase(val); } } }