// 実験(他の人のコード) #include using namespace std; typedef long long VAL; struct RBST { unsigned int randInt() { static unsigned int tx = 123456789, ty=362436069, tz=521288629, tw=88675123; unsigned int tt = (tx^(tx<<11)); tx = ty; ty = tz; tz = tw; return ( tw=(tw^(tw>>19))^(tt^(tt>>8)) ); } struct NODE { NODE *left, *right; VAL val; // the value of the node int size; // the size of the subtree VAL sum; // the value-sum of the subtree NODE() : val(0), size(1), sum(0) { left = right = NULL; } NODE(VAL v) : val(v), size(1), sum(v) { left = right = NULL; } }; /////////////////////// // root /////////////////////// NODE* root; RBST() : root(NULL) { } RBST(NODE* node) : root(node) { } /////////////////////// // basic operations /////////////////////// /* size */ inline int size(NODE *node) { return !node ? 0 : node->size; } inline int size() { return this->size(this->root); } /* sum */ inline VAL sum(NODE *node) { return !node ? 0 : node->sum; } inline VAL sum() { return this->sum(this->root); } /* update, push */ inline NODE* update(NODE *node) { node->size = size(node->left) + size(node->right) + 1; node->sum = sum(node->left) + sum(node->right) + node->val; return node; } /* lower_bound */ inline int lowerBound(NODE *node, VAL val) { if (!node) return 0; if (val <= node->val) return lowerBound(node->left, val); else return size(node->left) + lowerBound(node->right, val) + 1; } inline int lowerBound(VAL val) { return this->lowerBound(this->root, val); } /* upper_bound */ inline int upperBound(NODE *node, VAL val) { if (!node) return 0; if (val >= node->val) return size(node->left) + upperBound(node->right, val) + 1; else return upperBound(node->left, val); } inline int upperBound(VAL val) { return this->upperBound(this->root, val); } /* count */ inline int count(VAL val) { return upperBound(val) - lowerBound(val); } /* get --- k: 0-index */ inline VAL get(NODE *node, int k) { if (!node) return -1; if (k == size(node->left)) return node->val; if (k < size(node->left)) return get(node->left, k); else return get(node->right, k - size(node->left) - 1); } inline VAL get(int k) { return get(this->root, k); } /////////////////////// // merge-split /////////////////////// NODE* merge(NODE *left, NODE *right) { if (!left || !right) { if (left) return left; else return right; } if (randInt() % (left->size + right->size) < left->size) { left->right = merge(left->right, right); return update(left); } else { right->left = merge(left, right->left); return update(right); } } void merge(RBST add) { this->root = this->merge(this->root, add.root); } pair split(NODE* node, int k) { // [0, k), [k, n) if (!node) return make_pair(node, node); if (k <= size(node->left)) { pair sub = split(node->left, k); node->left = sub.second; return make_pair(sub.first, update(node)); } else { pair sub = split(node->right, k - size(node->left) - 1); node->right = sub.first; return make_pair(update(node), sub.second); } } RBST split(int k) { pair sub = split(this->root, k); this->root = sub.first; return RBST(sub.second); } /////////////////////// // insert-erase /////////////////////// void insert(const VAL val) { pair sub = this->split(this->root, this->lowerBound(val)); this->root = this->merge(this->merge(sub.first, new NODE(val)), sub.second); } void erase(const VAL val) { if (!this->count(val)) return; pair sub = this->split(this->root, this->lowerBound(val)); this->root = this->merge(sub.first, this->split(sub.second, 1).second); } }; int main() { RBST S; int Q, K; cin >> Q >> K; for (int i = 0; i < Q; ++i) { int T; cin >> T; if (T == 1) { long long val; cin >> val; S.insert(val); } else { if (S.size() < K) { cout << -1 << endl; continue; } long long res = S.get(K-1); cout << res << endl; S.erase(res); } } }