#line 1 "playspace/main.cpp" #include #line 1 "library/gandalfr/other/io_supporter.hpp" #line 7 "library/gandalfr/other/io_supporter.hpp" std::ostream &operator<<(std::ostream &dest, __uint128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); int len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __int128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } int len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } template std::ostream &operator<<(std::ostream &os, const std::vector &v) { for(int i=0; i<(int)v.size(); i++) os << v[i] << (i+1 != (int)v.size() ? " " : ""); return os; } template std::istream &operator>>(std::istream &is, std::vector &v){ for(T &in : v) is >> in; return is; } template std::ostream &operator<<(std::ostream &os, const std::pair& p) { os << p.first << " " << p.second; return os; } template std::istream &operator>>(std::istream &is, std::pair &p) { is >> p.first >> p.second; return is; } template std::ostream &operator<<(std::ostream &os, std::queue v) { int N = v.size(); for(int i=0; i std::istream &operator>>(std::istream &is, std::queue &v) { for(T &in : is) v.push(is); return is; } template std::ostream &operator<<(std::ostream &os, const std::set &st) { for(const T &x : st){ std::cout << x << " "; } return os; } template std::ostream &operator<<(std::ostream &os, const std::multiset &st) { for(const T &x : st){ std::cout << x << " "; } return os; } #line 3 "playspace/main.cpp" using namespace std; using ll = long long; const int INF = 1001001001; const int MAXINT = std::numeric_limits::max(); const int MININT = std::numeric_limits::min(); const ll INFLL = 1001001001001001001; const ll MAXLL = std::numeric_limits::max(); const ll MINLL = std::numeric_limits::min(); const ll MOD = 1000000007; const ll _MOD = 998244353; #define rep(i, j, n) for(ll i = (ll)(j); i < (ll)(n); i++) #define rrep(i, j, n) for(ll i = (ll)(n-1); i >= (ll)(j); i--) #define fore(i,a) for(auto &i : a) #define all(a) (a).begin(),(a).end() #define lr(a, l, r) (a).begin()+(l),(a).begin()+(r) #define LF cout << endl template inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } void Yes(bool ok){ std::cout << (ok ? "Yes" : "No") << std::endl; } /*using mint = mod_integer; using _mint = mod_integer<_MOD>; using binom = binomial_coefficients; using _binom = binomial_coefficients<_mint>;*/ template auto make_vec(const size_t (&d)[n], const T& init) noexcept { if constexpr (idx < n) return std::vector(d[idx], make_vec(d, init)); else return init; } template auto make_vec(const size_t (&d)[n]) noexcept { return make_vec(d, T{}); } template class Kth_hold_multiset{ private: std::multiset low, high;// low のサイズを K に保つ const int k; void refill(){ while((int)low.size() < k) { auto it = high.begin(); low.insert(*it); high.erase(it); } while((int)low.size() > k) { auto it = prev(low.end()); high.insert(*it); low.erase(it); } } class Iterator { public: Iterator(typename std::multiset::iterator it, std::multiset& low, std::multiset& high) : it_(it), low_(low), high_(high) {} bool operator==(const Iterator& other) const { return it_ == other.it_; } bool operator!=(const Iterator& other) const { return it_ != other.it_; } const T& operator*() const { return *it_; } Iterator& operator++() { ++it_; if(it_ == low_.end()) it_ = high_.begin(); return *this; } Iterator operator++(int) { Iterator tmp = *this; ++(*this); return tmp; } private: typename std::multiset::iterator it_; std::multiset& low_; std::multiset& high_; }; public: Kth_hold_multiset(int K) : k(K) {} void insert(T x){ if((int)low.size() < k) { low.insert(x); return; } T Kth = *prev(low.end()); if(x >= Kth) high.insert(x); else low.insert(x); refill(); } void erase_a_element(T x){ if(high.empty()) { auto low_it = low.find(x); if(low_it != low.end()) low.erase(low_it); return; } auto high_it = high.find(x); auto low_it = low.find(x); if(high_it != high.end()) high.erase(high_it); else if(low_it != low.end()) low.erase(low_it); refill(); } void erase(T x){ low.erase(x); high.erase(x); refill(); } int size(){ return low.size() + high.size(); } T get_Kth_value(){ return *prev(low.end()); } int count(T x){ return low.count(x) + high.count(x); } void dump(){ int loop = low.size() + high.size(); for(auto x : *this){ loop--; std::cout << x << (loop ? " " : ""); } } Iterator begin() { return low.empty() ? Iterator(high.end(), low, high) : Iterator(low.begin(), low, high); } Iterator end() { return Iterator(high.end(), low, high); } }; int main(void){ /*ifstream in("input.txt"); cin.rdbuf(in.rdbuf()); ofstream fout("output.txt");*/ //input int Q, K; cin >> Q >> K; //calculate vector ans; Kth_hold_multiset st(K); rep(i,0,Q){ int q; cin >> q; if(q == 1){ ll x; cin >> x; st.insert(x); } else{ if(st.size() < K){ ans.push_back(-1); } else{ ans.push_back(st.get_Kth_value()); st.erase_a_element(st.get_Kth_value()); } } //st.dump(); } for(auto x : ans) cout << x << endl; }