import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { import std.typecons : Tuple; alias Query = Tuple!(int, "type", long, "value"); int q, k; scan(q, k); auto qs = new Query[](q); auto a = new long[](0); foreach (i ; 0 .. q) { auto line = readln.split.to!(long[]); if (line.length == 1) { qs[i] = Query(2, -1); } else { qs[i] = Query(1, line[1]); a ~= line[1]; } } a = a.sort().uniq.array; int[long] map; foreach (i, ai; a) { map[ai] = i.to!int; } auto ft = FenwickTree!(int)(q); foreach (i ; 0 .. q) { if (qs[i].type == 2) { if (ft.sum(q) < k) { writeln(-1); } else { int btm = 0, top = q; while (top - btm > 1) { int mid = (top + btm) / 2; if (ft.sum(mid) >= k) { top = mid; } else { btm = mid; } } writeln(a[top-1]); ft.add(top-1, -1); } } else { ft.add(map[qs[i].value], 1); } } } struct FenwickTree(T) { private { int size; T[] data; } this (int N) { size = N + 1; data = new T[](size); } void add(int i, int x) { i++; while (i < size) { data[i] += x; i += i & (-i); } } T sum(int r) { T res = 0; while (r > 0) { res += data[r]; r -= r & (-r); } return res; } } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }