#define PROBLEM "https://judge.yosupo.jp/problem/double_ended_priority_queue"
#include"data_structures/interval_heap.hpp"
#include<bits/stdc++.h>intmain(){std::cin.tie(0)->sync_with_stdio(0);intN,Q;std::cin>>N>>Q;interval_heap<int>pq;for(autoi=0;i<N;++i){intx;std::cin>>x;pq.push(x);}for(autoi=0;i<Q;++i){intt;std::cin>>t;if(t==0){intx;std::cin>>x;pq.push(x);}elseif(t==1){std::cout<<pq.min()<<"\n";pq.pop_min();}else{std::cout<<pq.max()<<"\n";pq.pop_max();}}}
#line 1 "test/data_structures/interval_heap.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/double_ended_priority_queue"
#line 1 "data_structures/interval_heap.hpp"
#include<cassert>
#include<utility>
#include<vector>// source: https://natsugiri.hatenablog.com/entry/2016/10/10/035445template<typenameT>structinterval_heap{interval_heap()=default;voidpush(constT&x){autok=int(data_.size());data_.push_back(x);up(k);}voidpop_min(){assert(!data_.empty());if(int(data_.size())<3){data_.pop_back();}else{std::swap(data_[1],data_.back());data_.pop_back();autok=down(1);up(k);}}voidpop_max(){assert(!data_.empty());if(int(data_.size())<2){data_.pop_back();}else{std::swap(data_[0],data_.back());data_.pop_back();autok=down(0);up(k);}}constT&min()const{assert(!data_.empty());returnint(data_.size())<2?data_[0]:data_[1];}constT&max()const{assert(!data_.empty());returndata_[0];}intsize()const{returnint(data_.size());}boolempty()const{returndata_.empty();}private:intparent(intk)const{return(((k>>1)-1)&~1);}intdown(intk){auton=int(data_.size());if(k&1){// min heapwhile(2*k+1<n){autoc=2*k+3;if(n<=c||data_[c-2]<data_[c]){c-=2;}if(c<n&&data_[c]<data_[k]){std::swap(data_[k],data_[c]);k=c;}else{break;}}}else{// max heapwhile(2*k+2<n){autoc=2*k+4;if(n<=c||data_[c]<data_[c-2]){c-=2;}if(c<n&&data_[k]<data_[c]){std::swap(data_[k],data_[c]);k=c;}else{break;}}}returnk;}intup(intk,introot=1){if((k|1)<int(data_.size())&&data_[k&~1]<data_[k|1]){std::swap(data_[k&~1],data_[k|1]);k^=1;}intp;while(root<k&&data_[p=parent(k)]<data_[k]){// max heapstd::swap(data_[p],data_[k]);k=p;}while(root<k&&data_[k]<data_[p=parent(k)|1]){// min heapstd::swap(data_[p],data_[k]);k=p;}returnk;}std::vector<T>data_;};#line 4 "test/data_structures/interval_heap.test.cpp"
#include<bits/stdc++.h>intmain(){std::cin.tie(0)->sync_with_stdio(0);intN,Q;std::cin>>N>>Q;interval_heap<int>pq;for(autoi=0;i<N;++i){intx;std::cin>>x;pq.push(x);}for(autoi=0;i<Q;++i){intt;std::cin>>t;if(t==0){intx;std::cin>>x;pq.push(x);}elseif(t==1){std::cout<<pq.min()<<"\n";pq.pop_min();}else{std::cout<<pq.max()<<"\n";pq.pop_max();}}}