#ifdef BODY ================================================================================================================================ +library{ name:splay_tree_set, url:https://tqkoh.github.io/library/library/lib/data-structure/splay-tree-set.hpp, version:2020-08-07 } int solve(){ lin(q,k); Set s; while(q--){ lint t,v; in(t); if(t==1){ in(v); s.insert({v, q}); } else{ if(s.size()>=k){ auto p = s.nth(k-1); out(p->val.first); s.erase(p->val); } else out(-1); } } return 0; } #endif BODY ================================================================================================================================ //sub-BOF // desktop // author: Tqk //#define _AOJ_ //#define _C_INPUT_ #pragma region template #pragma region basic #define IN_FILE "./in.txt" //#pragma GCC optimize ("O3") #pragma warning(disable: 4455 4244 4067 4068 4996) #pragma GCC target ("avx") #pragma GCC diagnostic ignored "-Wliteral-suffix" #define NOMINMAX #include "bits/stdc++.h" using namespace std; typedef int64_t lint; typedef long double ld; typedef string cs; #define linf 1152921504606846976 inline lint operator""z(const unsigned long long n){ return lint(n); } #pragma endregion #pragma region rep #define _vcppunko4(tuple) _getname4 tuple #define _getname4(_1,_2,_3,_4,name,...) name #define _getname3(_1,_2,_3,name,...) name #define _trep2(tuple) _rep2 tuple #define _trep3(tuple) _rep3 tuple #define _trep4(tuple) _rep4 tuple #define _rep1(n) for(lint i=0;i=0;--i) #define _rrep2(i,n) for(lint i=n-1;i>=0;--i) #define _rrep3(i,a,b) for(lint i=b-1;i>=a;--i) #define _rrep4(i,a,b,c) for(lint i=a+(b-a-1)/c*c;i>=a;i-=c) #define rep(...) _vcppunko4((__VA_ARGS__,_trep4,_trep3,_trep2,_rep1))((__VA_ARGS__)) #define per(...) _vcppunko4((__VA_ARGS__,_trrep4,_trrep3,_trrep2,_rrep1))((__VA_ARGS__)) #define each(c) for(auto &e:c) #pragma endregion #pragma region io template istream& operator>>(istream& is,vector& vec); template istream& operator>>(istream& is,array& vec); template istream& operator>>(istream& is,pair& p); template ostream& operator<<(ostream& os,vector& vec); template ostream& operator<<(ostream& os,pair& p); template istream& operator>>(istream& is,vector& vec){ for(T& x: vec) is>>x;return is; } template istream& operator>>(istream& is,pair& p){ is>>p.first;is>>p.second;return is; } template ostream& operator<<(ostream& os,pair& p){ os< ostream& operator<<(ostream& os,vector& vec){ os< ostream& operator<<(ostream& os,deque& deq){ os< ostream& operator<<(ostream& os,set& s){ each(s)os< ifstream& operator>>(ifstream& is,vector& vec); template ifstream& operator>>(ifstream& is,array& vec); template ifstream& operator>>(ifstream& is,pair& p); template ifstream& operator>>(ifstream& is,vector& vec){ for(T& x: vec) is>>x;return is; } template ifstream& operator>>(ifstream& is,pair& p){ is>>p.first;is>>p.second;return is; } inline void fin(){} template inline void fin(Head&& head,Tail&&... tail){ infile>>head;fin(move(tail)...); } */ #include HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); inline void in(){ SetConsoleTextAttribute(hConsole,10); } template inline void in(Head&& head,Tail&&... tail){ SetConsoleTextAttribute(hConsole,15); cin>>head;in(move(tail)...); } #else inline void in(){} template inline void in(Head&& head,Tail&&... tail){ cin>>head;in(move(tail)...); } #endif inline bool out(){ return(cout<<'\n',0); } template inline bool out(T t){ return(cout< inline bool out(Head head,Tail... tail){ cout< inline void alloc(T &c,lint s){ rep(c.size())c[i].resize(s); } #define alc alloc #ifdef __ENV_TQK__ inline bool deb(){ SetConsoleTextAttribute(hConsole,10); return(cout<<'\n',0); } template inline bool deb(T t){ return(SetConsoleTextAttribute(hConsole,12),cout< inline bool deb(Head head,Tail... tail){ SetConsoleTextAttribute(hConsole,12); cout< name(size);in(name) #define all(v) v.begin(),v.end() #define pb push_back #define fi e.first #define se e.second #define YES(c) cout<<((c)?"YES\n":"NO\n"),0 #define Yes(c) cout<<((c)?"Yes\n":"No\n"),0 #define POSSIBLE(c) cout<<((c)?"POSSIBLE\n":"IMPOSSIBLE\n"),0 #define Possible(c) cout<<((c)?"Possible\n":"Impossible\n"),0 #define o(p) cout<r)swap(l,r); inline constexpr lint gcd(lint a,lint b){ while(b){ lint c=b;b=a%b;a=c; }return a; } inline constexpr lint lcm(lint a,lint b){ return a/gcd(a,b)*b; } template inline constexpr bool chmin(T &mn,const T &cnt){ if(mn>cnt){ mn=cnt;return 1; } else return 0; } template inline constexpr bool chmax(T &mx,const T &cnt){ if(mx>(istream& os,P& p){ os>>p.f>>p.s;return os; } constexpr bool operator<(const P& l,const P& r){ return(l.f-r.f?l.f(const P& l,const P& r){ return(l.f-r.f?l.f>r.f:l.s>r.s); } constexpr bool operator<=(const P& l,const P& r){ return!(l.f-r.f?l.f>r.f:l.s>r.s); } constexpr bool operator>=(const P& l,const P& r){ return!(l.f-r.f?l.f; class Set{ public: struct Node{ Node* l,* r,* p; int size; T val; Node():l(nullptr),r(nullptr),p(nullptr),size(1),val(T()){} void rotate(){ Node* c; if(p->l==this){ c = r; r = p; p->l = c; } else{ c = l; l = p; p->r = c; } if(p->p && p->p->l == p)p->p->l = this; if(p->p && p->p->r == p)p->p->r = this; Node* prv_p = p; p = p->p; prv_p->p = this; if(c)c->p = prv_p; prv_p->update(); update(); } int pdirection(){ if(!p)return 0; return p->l == this ? 1 : -1; } Node* splay(){ while(pdirection()){ if(!p->pdirection()){ rotate(); } else if(p->pdirection() == pdirection()){ p->rotate(); rotate(); } else{ rotate(); rotate(); } } return this; } void update(){ size = 1; if(l)size += l->size; if(r)size += r->size; } }; private: static Node node[MAX_SIZE]; static int _size; Node* _root; public: Set(): _root(nullptr){}; Node* root(){ return _root; } int size(){ if(!root()) return 0; return root()->size; } Node* nth(int n){ if(n >= size())return nullptr; Node* cur = root(); while(cur){ int lsize = cur->l ? cur->l->size : 0; if(n < lsize){ cur = cur->l; } else if(n == lsize){ return _root = cur->splay(); } else{ cur = cur->r; n -= lsize + 1; } } return nullptr; } bool find(T x){ Node* cur = root(),* prv = nullptr; while(cur){ prv = cur; if(x < cur->val){ cur = cur->l; } else if(x == cur->val){ _root = cur->splay(); return 1; } else{ cur = cur->r; } } prv ? _root = prv->splay() : nullptr; return 0; } void merge(Node* r_root){ if(!r_root)return; nth(size() - 1); root()->r = r_root; r_root->p = root(); root()->update(); } void merge(Set& s){ merge(s.root()); } void insert(T x){ if(!find(x)){ node[_size].val = x; if(!root()){ _root = node + _size++; return; } if(x < root()->val){ node[_size].r = root(); root()->p = node + _size; if(root()->l){ node[_size].l = root()->l; root()->l->p = node + _size; root()->l = nullptr; } } else{ node[_size].l = root(); root()->p = node + _size; if(root()->r){ node[_size].r = root()->r; root()->r->p = node + _size; root()->r = nullptr; } } root()->update(); node[_size].update(); _root = node + _size++; } } void erase(T x){ if(find(x)){ Node* prv_root = root(),* prv_l = root()->l,* prv_r = root()->r; if(!prv_root->l){ _root = prv_root->r; } else{ prv_l->p = nullptr; prv_root->l = nullptr; } if(!prv_root->r){ _root = prv_l; } else{ prv_r->p = nullptr; prv_root->r = nullptr; } if(prv_l && prv_r){ _root = prv_l; merge(prv_r); } } } template void tour(Node* v,F f,int d = 0,int dir = 0){ if(v){ tour(v->l,f,d+1,-1); f(v,d,dir); tour(v->r,f,d+1,1); } } void print_tree(){ tour(root(),[&](Node* v,int d,int dir){ std::cout << std::string(2*d,' '); if(dir == -1)std::cout << "/ "; if(dir == 0)std::cout << "- "; if(dir == 1)std::cout << "\\ "; std::cout << v->val << '\n'; }); } void print(){ std::cout << "{ "; tour(root(),[&](Node* v,int,int){ std::cout << v->val << ", "; }); std::cout << "}"; } }; std::ostream& operator<<(std::ostream& os,Set& rhs){ rhs.print(); return os; } Set operator+=(Set& l,Set& r){ r.tour(r.root(),[&](Set::Node* v,int,int){ l.insert(v->val); }); return l; } Set operator+(Set& l,Set& r){ Set ret; l.tour(l.root(),[&](Set::Node* v,int,int){ ret.insert(v->val); }); r.tour(r.root(),[&](Set::Node* v,int,int){ ret.insert(v->val); }); return ret; } Set operator*=(Set& l,Set& r){ r.tour(r.root(),[&](Set::Node* v,int,int){ if(!l.find(v->val))l.erase(v->val); }); return l; } Set operator*(Set& l,Set& r){ Set ret; l.tour(l.root(),[&](Set::Node* v,int,int){ if(r.find(v->val))ret.insert(v->val); }); return ret; } Set::Node Set::node[MAX_SIZE]; int Set::_size = 0; /** * @title Splay Tree(set) */ #pragma endregion int solve(){ lin(q,k); Set s; while(q--){ lint t,v; in(t); if(t==1){ in(v); s.insert({v, q}); } else{ if(s.size()>=k){ auto p = s.nth(k-1); out(p->val.first); s.erase(p->val); } else out(-1); } } return 0; } #pragma endregion #pragma region main int main(){ solve(); } #pragma endregion //sub-EOF