#define PROBLEM "https://judge.yosupo.jp/problem/ordered_set"
#include"../../../ds/bst/ordered_multiset.hpp"#include<cassert>
#include<cstdint>
#include"../../../utilities/fast_io.hpp"
#include<iterator>
#include<set>
#include<utility>
#include<vector>voidsplit_merge_test(){m1une::ds::OrderedMultiset<int>tree={1,2,2,4,7};autocopy=tree;auto[left,right]=std::move(tree).split(4);std::vector<int>expected_left={1,2,2};std::vector<int>expected_right={4,7};std::vector<int>expected_all={1,2,2,4,7};assert(left.to_vector()==expected_left);assert(right.to_vector()==expected_right);tree=std::move(left).merge(std::move(right));assert(tree.to_vector()==expected_all);assert(copy.to_vector()==expected_all);}voidrandomized_test(){m1une::ds::OrderedMultiset<int>tree;std::multiset<int>expected;std::uint64_tstate=123456789;for(intquery=0;query<10000;++query){state=state*6364136223846793005ULL+1442695040888963407ULL;constintkey=int(state>>32)%500;constinttype=int(state%6);if(type<=1){constintmultiplicity=int(state%3)+1;tree.insert(key,multiplicity);for(inti=0;i<multiplicity;++i)expected.insert(key);}elseif(type==2){constboolerased=tree.erase_one(key);autoit=expected.find(key);assert(erased==(it!=expected.end()));if(it!=expected.end())expected.erase(it);}elseif(type==3){assert(tree.erase_all(key)==int(expected.count(key)));expected.erase(key);}elseif(type==4){assert(tree.count(key)==int(expected.count(key)));assert(tree.order_of_key(key)==int(std::distance(expected.begin(),expected.lower_bound(key))));}else{autocopy=tree;auto[left,right]=std::move(tree).split(key);std::vector<int>expected_left(expected.begin(),expected.lower_bound(key));std::vector<int>expected_right(expected.lower_bound(key),expected.end());assert(left.to_vector()==expected_left);assert(right.to_vector()==expected_right);tree=std::move(left).merge(std::move(right));assert(copy.to_vector()==tree.to_vector());}assert(tree.size()==int(expected.size()));assert(tree.to_vector()==std::vector<int>(expected.begin(),expected.end()));}}intmain(){m1une::utilities::FastInputfast_input;m1une::utilities::FastOutputfast_output;split_merge_test();randomized_test();intN,Q;fast_input>>N>>Q;m1une::ds::OrderedMultiset<int>ms;for(inti=0;i<N;i++){inta;fast_input>>a;ms.insert(a);}while(Q--){inttype,x;fast_input>>type>>x;if(type==0){if(!ms.contains(x))ms.insert(x);}elseif(type==1){if(ms.contains(x))ms.erase_one(x);}elseif(type==2){if(ms.size()<x){fast_output<<-1<<'\n';}else{fast_output<<ms.kth(x-1)<<'\n';}}elseif(type==3){fast_output<<ms.count_less_equal(x)<<'\n';}elseif(type==4){constint*ans=ms.max_le(x);fast_output<<(ans?*ans:-1)<<'\n';}else{constint*ans=ms.min_ge(x);fast_output<<(ans?*ans:-1)<<'\n';}}}
#line 1 "verify/ds/bst/ordered_multiset.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/ordered_set"
#line 1 "ds/bst/ordered_multiset.hpp"
#include<cassert>
#include<functional>
#include<initializer_list>
#include<memory>
#include<utility>
#include<vector>namespacem1une{namespaceds{template<typenameT,typenameCompare=std::less<T>>structOrderedMultiset{private:structNode{Tkey;intcount;intsize;intdistinct_size;Node*l;Node*r;Node(Tvalue,intmultiplicity):key(std::move(value)),count(multiplicity),size(multiplicity),distinct_size(1),l(nullptr),r(nullptr){}};staticconstexprintpool_block_size=1<<14;structPool{std::vector<std::vector<Node>>blocks;std::vector<Node*>free_nodes;template<class...Args>Node*emplace(Args&&...args){if(!free_nodes.empty()){Node*result=free_nodes.back();free_nodes.pop_back();std::destroy_at(result);std::construct_at(result,std::forward<Args>(args)...);returnresult;}if(blocks.empty()||int(blocks.back().size())==pool_block_size){blocks.emplace_back();blocks.back().reserve(pool_block_size);}blocks.back().emplace_back(std::forward<Args>(args)...);return&blocks.back().back();}voidrecycle(Node*node){free_nodes.push_back(node);}};inlinestaticPoolpool;Node*root;Comparecomp;staticintsubtree_size(constNode*t){returnt==nullptr?0:t->size;}staticintsubtree_distinct_size(constNode*t){returnt==nullptr?0:t->distinct_size;}boolequal(constT&a,constT&b)const{return!comp(a,b)&&!comp(b,a);}Node*new_node(Tkey,intmultiplicity){returnpool.emplace(std::move(key),multiplicity);}staticvoidupdate(Node*t){t->size=t->count+subtree_size(t->l)+subtree_size(t->r);t->distinct_size=1+subtree_distinct_size(t->l)+subtree_distinct_size(t->r);}staticNode*rotate_right(Node*t){Node*s=t->l;t->l=s->r;s->r=t;update(t);update(s);returns;}staticNode*rotate_left(Node*t){Node*s=t->r;t->r=s->l;s->l=t;update(t);update(s);returns;}staticNode*balance(Node*t){if(t==nullptr)returnnullptr;constintleft_size=subtree_distinct_size(t->l);constintright_size=subtree_distinct_size(t->r);if(left_size+right_size>1&&left_size>3LL*right_size){if(subtree_distinct_size(t->l->r)>=2LL*subtree_distinct_size(t->l->l)){t->l=rotate_left(t->l);}returnrotate_right(t);}if(left_size+right_size>1&&right_size>3LL*left_size){if(subtree_distinct_size(t->r->l)>=2LL*subtree_distinct_size(t->r->r)){t->r=rotate_right(t->r);}returnrotate_left(t);}update(t);returnt;}staticNode*join_with_root(Node*l,Node*middle,Node*r){constintleft_size=subtree_distinct_size(l);constintright_size=subtree_distinct_size(r);if(left_size>3LL*(right_size+1)){l->r=join_with_root(l->r,middle,r);returnbalance(l);}if(right_size>3LL*(left_size+1)){r->l=join_with_root(l,middle,r->l);returnbalance(r);}middle->l=l;middle->r=r;returnbalance(middle);}staticNode*detach_max(Node*t,Node*&maximum){if(t->r==nullptr){maximum=t;returnt->l;}t->r=detach_max(t->r,maximum);returnbalance(t);}staticNode*merge_nodes(Node*l,Node*r){if(l==nullptr||r==nullptr)returnl==nullptr?r:l;Node*middle;l=detach_max(l,middle);returnjoin_with_root(l,middle,r);}std::pair<Node*,Node*>split_nodes(Node*t,constT&key){if(t==nullptr)return{nullptr,nullptr};Node*left=t->l;Node*right=t->r;t->l=nullptr;t->r=nullptr;if(comp(t->key,key)){auto[l,r]=split_nodes(right,key);return{join_with_root(left,t,l),r};}auto[l,r]=split_nodes(left,key);return{l,join_with_root(r,t,right)};}Node*insert_impl(Node*t,T&key,intmultiplicity,bool&new_key){if(t==nullptr){new_key=true;returnnew_node(std::move(key),multiplicity);}if(comp(key,t->key)){t->l=insert_impl(t->l,key,multiplicity,new_key);}elseif(comp(t->key,key)){t->r=insert_impl(t->r,key,multiplicity,new_key);}else{t->count+=multiplicity;t->size+=multiplicity;new_key=false;returnt;}if(!new_key){t->size+=multiplicity;returnt;}returnbalance(t);}Node*erase_impl(Node*t,constT&key,boolerase_all,int&erased,bool&removed_key){if(t==nullptr)returnnullptr;if(comp(key,t->key)){t->l=erase_impl(t->l,key,erase_all,erased,removed_key);}elseif(comp(t->key,key)){t->r=erase_impl(t->r,key,erase_all,erased,removed_key);}elseif(!erase_all&&t->count>1){--t->count;--t->size;erased=1;removed_key=false;returnt;}else{erased=t->count;removed_key=true;Node*l=t->l;Node*r=t->r;pool.recycle(t);returnmerge_nodes(l,r);}if(erased==0)returnt;if(!removed_key){t->size-=erased;returnt;}returnbalance(t);}staticconstT*kth_impl(constNode*t,intk){while(t!=nullptr){constintleft_size=subtree_size(t->l);if(k<left_size){t=t->l;}elseif(k<left_size+t->count){return&t->key;}else{k-=left_size+t->count;t=t->r;}}returnnullptr;}intcount_impl(constNode*t,constT&key)const{while(t!=nullptr){if(comp(key,t->key)){t=t->l;}elseif(comp(t->key,key)){t=t->r;}else{returnt->count;}}return0;}intorder_of_key_impl(constNode*t,constT&key,boolupper)const{intresult=0;while(t!=nullptr){constbooltake=upper?!comp(key,t->key):comp(t->key,key);if(take){result+=subtree_size(t->l)+t->count;t=t->r;}else{t=t->l;}}returnresult;}constT*lower_bound_impl(constNode*t,constT&key,boolstrict)const{constT*result=nullptr;while(t!=nullptr){constboolcandidate=strict?comp(key,t->key):!comp(t->key,key);if(candidate){result=&t->key;t=t->l;}else{t=t->r;}}returnresult;}constT*max_less_impl(constNode*t,constT&key,boolstrict)const{constT*result=nullptr;while(t!=nullptr){constboolcandidate=strict?comp(t->key,key):!comp(key,t->key);if(candidate){result=&t->key;t=t->r;}else{t=t->l;}}returnresult;}staticvoiddump_impl(constNode*t,std::vector<T>&result){if(t==nullptr)return;dump_impl(t->l,result);for(inti=0;i<t->count;++i)result.push_back(t->key);dump_impl(t->r,result);}staticvoidrecycle_impl(Node*t){if(t==nullptr)return;recycle_impl(t->l);recycle_impl(t->r);pool.recycle(t);}Node*clone_impl(constNode*t){if(t==nullptr)returnnullptr;Node*result=new_node(t->key,t->count);result->l=clone_impl(t->l);result->r=clone_impl(t->r);update(result);returnresult;}OrderedMultiset(Node*node,Comparecompare):root(node),comp(std::move(compare)){}public:explicitOrderedMultiset(Comparecompare):root(nullptr),comp(std::move(compare)){}OrderedMultiset():OrderedMultiset(Compare()){}OrderedMultiset(std::initializer_list<T>init,Comparecompare=Compare()):OrderedMultiset(std::move(compare)){for(constT&x:init)insert(x);}template<typenameIterator>OrderedMultiset(Iteratorfirst,Iteratorlast,Comparecompare=Compare()):OrderedMultiset(std::move(compare)){while(first!=last)insert(*first++);}OrderedMultiset(constOrderedMultiset&other):root(nullptr),comp(other.comp){root=clone_impl(other.root);}OrderedMultiset(OrderedMultiset&&other)noexcept:root(std::exchange(other.root,nullptr)),comp(std::move(other.comp)){}~OrderedMultiset(){recycle_impl(root);}OrderedMultiset&operator=(OrderedMultisetother){swap(other);return*this;}voidswap(OrderedMultiset&other)noexcept{usingstd::swap;swap(root,other.root);swap(comp,other.comp);}intsize()const{returnsubtree_size(root);}intunique_size()const{returnsubtree_distinct_size(root);}boolempty()const{returnroot==nullptr;}voidclear(){recycle_impl(root);root=nullptr;}voidinsert(Tkey,intmultiplicity=1){assert(multiplicity>0);boolnew_key=false;root=insert_impl(root,key,multiplicity,new_key);}boolerase_one(constT&key){interased=0;boolremoved_key=false;root=erase_impl(root,key,false,erased,removed_key);returnerased!=0;}boolerase(constT&key){returnerase_one(key);}interase_all(constT&key){interased=0;boolremoved_key=false;root=erase_impl(root,key,true,erased,removed_key);returnerased;}boolcontains(constT&key)const{returncount(key)>0;}intcount(constT&key)const{returncount_impl(root,key);}constT*find_by_order(intk)const{assert(0<=k&&k<size());returnkth_impl(root,k);}Tkth(intk)const{return*find_by_order(k);}intorder_of_key(constT&key)const{returnorder_of_key_impl(root,key,false);}intcount_less(constT&key)const{returnorder_of_key(key);}intcount_less_equal(constT&key)const{returnorder_of_key_impl(root,key,true);}intcount_greater(constT&key)const{returnsize()-count_less_equal(key);}intcount_greater_equal(constT&key)const{returnsize()-count_less(key);}constT*lower_bound(constT&key)const{returnlower_bound_impl(root,key,false);}constT*upper_bound(constT&key)const{returnlower_bound_impl(root,key,true);}constT*min_ge(constT&key)const{returnlower_bound(key);}constT*min_gt(constT&key)const{returnupper_bound(key);}constT*max_le(constT&key)const{returnmax_less_impl(root,key,false);}constT*max_lt(constT&key)const{returnmax_less_impl(root,key,true);}constT*min()const{returnempty()?nullptr:kth_impl(root,0);}constT*max()const{returnempty()?nullptr:kth_impl(root,size()-1);}std::pair<OrderedMultiset,OrderedMultiset>split(constT&key)&&{auto[l,r]=split_nodes(root,key);root=nullptr;return{OrderedMultiset(l,comp),OrderedMultiset(r,std::move(comp))};}OrderedMultisetmerge(OrderedMultisetother)&&{assert(empty()||other.empty()||comp(*max(),*other.min()));root=merge_nodes(root,other.root);other.root=nullptr;returnstd::move(*this);}std::vector<T>to_vector()const{std::vector<T>result;result.reserve(size());dump_impl(root,result);returnresult;}};}// namespace ds}// namespace m1une#line 4 "verify/ds/bst/ordered_multiset.test.cpp"
#line 6 "verify/ds/bst/ordered_multiset.test.cpp"
#include<cstdint>
#line 1 "utilities/fast_io.hpp"
#include<algorithm>
#include<array>
#include<cerrno>
#include<charconv>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#line 12 "utilities/fast_io.hpp"
#include<cstring>
#include<iterator>
#include<string>
#include<sys/stat.h>
#include<type_traits>
#line 18 "utilities/fast_io.hpp"
#include<unistd.h>
#line 20 "utilities/fast_io.hpp"
namespacem1une{namespaceutilities{structFastOutput;namespaceinternal{// Shared with the convenience helpers in template.hpp.inlineFastOutput*standard_output_instance=nullptr;// Detect std::begin(x), std::end(x).template<classT,class=void>structis_range:std::false_type{};template<classT>structis_range<T,std::void_t<decltype(std::begin(std::declval<T&>())),decltype(std::end(std::declval<T&>()))>>:std::true_type{};template<classT>inlineconstexprboolis_range_v=is_range<T>::value;template<classT>usingrange_reference_t=decltype(*std::begin(std::declval<T&>()));template<classT>usingrange_value_t=std::remove_cv_t<std::remove_reference_t<range_reference_t<T>>>;template<classT,class=void>structrange_stored_value{usingtype=range_value_t<T>;};template<classT>structrange_stored_value<T,std::void_t<typenamestd::remove_cv_t<std::remove_reference_t<T>>::value_type>>{usingtype=typenamestd::remove_cv_t<std::remove_reference_t<T>>::value_type;};template<classT>usingrange_stored_value_t=typenamerange_stored_value<T>::type;// Treat strings and C strings as scalar output objects, not as ranges.template<classT>structis_char_array:std::false_type{};template<classT,std::size_tN>structis_char_array<T[N]>:std::bool_constant<std::is_same_v<std::remove_cv_t<T>,char>>{};template<classT>structis_string_like:std::bool_constant<std::is_same_v<std::decay_t<T>,std::string>||std::is_same_v<std::decay_t<T>,constchar*>||std::is_same_v<std::decay_t<T>,char*>||is_char_array<std::remove_reference_t<T>>::value>{};template<classT>inlineconstexprboolis_string_like_v=is_string_like<T>::value;// ModInt-like type: x.val() is printable, and x can be assigned from long long.template<classT,class=void>structhas_val_method:std::false_type{};template<classT>structhas_val_method<T,std::void_t<decltype(std::declval<constT&>().val())>>:std::true_type{};template<classT>inlineconstexprboolhas_val_method_v=has_val_method<T>::value;template<classT,class=void>structhas_static_mod_raw:std::false_type{};template<classT>structhas_static_mod_raw<T,std::void_t<decltype(T::mod()),decltype(T::raw(std::declval<uint32_t>()))>>:std::true_type{};template<classT>inlineconstexprboolhas_static_mod_raw_v=has_static_mod_raw<T>::value;// libstdc++ before GCC 16 does not classify __int128 as an integral type in// strict ISO modes such as -std=c++23. Keep the fast-I/O interface independent// of that implementation detail.template<classT>inlineconstexprboolis_integral_v=std::is_integral_v<T>||std::is_same_v<std::remove_cv_t<T>,__int128_t>||std::is_same_v<std::remove_cv_t<T>,__uint128_t>;template<classT>inlineconstexprboolis_signed_v=std::is_signed_v<T>||std::is_same_v<std::remove_cv_t<T>,__int128_t>;template<classT>structmake_unsigned{usingtype=std::make_unsigned_t<T>;};template<>structmake_unsigned<__int128_t>{usingtype=__uint128_t;};template<>structmake_unsigned<__uint128_t>{usingtype=__uint128_t;};template<classT>usingmake_unsigned_t=typenamemake_unsigned<std::remove_cv_t<T>>::type;}// namespace internalstructFastInput{staticconstexprintbuffer_size=1<<20;private:std::FILE*_stream;char_buffer[buffer_size];int_position;int_length;int_file_descriptor;bool_streaming;boolrefill(){_position=0;if(_streaming){ssize_tlength;do{length=::read(_file_descriptor,_buffer,buffer_size);}while(length<0&&errno==EINTR);if(length<=0){_length=0;returnfalse;}_length=int(length);}else{_length=int(std::fread(_buffer,1,buffer_size,_stream));}return_length!=0;}template<classT>boolread_integer_from_stream(T&value){if(!skip_spaces())returnfalse;intc=read_char_raw();boolnegative=false;if(c=='-'){negative=true;c=read_char_raw();}ifconstexpr(internal::is_signed_v<T>){Tresult=0;while('0'<=c&&c<='9'){result=negative?result*10-(c-'0'):result*10+(c-'0');c=read_char_raw();}value=result;}else{Tresult=0;while('0'<=c&&c<='9'){result=result*10+T(c-'0');c=read_char_raw();}value=negative?T(0)-result:result;}returntrue;}boolprepare_number(){if(_length-_position>=64)returntrue;constintremaining=_length-_position;if(remaining>0)std::memmove(_buffer,_buffer+_position,remaining);constintadded=int(std::fread(_buffer+remaining,1,buffer_size-remaining,_stream));_position=0;_length=remaining+added;if(_length<buffer_size)_buffer[_length]='\0';return_length!=0;}public:explicitFastInput(std::FILE*stream=stdin):_stream(stream),_position(0),_length(0),_file_descriptor(::fileno(stream)),_streaming([&]{structstatstatus;return_file_descriptor>=0&&::fstat(_file_descriptor,&status)==0&&!S_ISREG(status.st_mode);}()){}FastInput(constFastInput&)=delete;FastInput&operator=(constFastInput&)=delete;intread_char_raw(){if(_position==_length&&!refill())returnEOF;return_buffer[_position++];}boolskip_spaces(){intc=read_char_raw();while(c!=EOF&&c<=' ')c=read_char_raw();if(c==EOF)returnfalse;--_position;returntrue;}boolread(char&value){if(!skip_spaces())returnfalse;value=char(read_char_raw());returntrue;}boolread(std::string&value){if(!skip_spaces())returnfalse;value.clear();while(true){constintbegin=_position;while(_position<_length&&static_cast<unsignedchar>(_buffer[_position])>' '){++_position;}value.append(_buffer+begin,_position-begin);if(_position<_length){++_position;returntrue;}if(!refill())returntrue;}}boolread(bool&value){intx;if(!read(x))returnfalse;value=x!=0;returntrue;}template<classT>std::enable_if_t<internal::is_integral_v<T>&&!std::is_same_v<std::remove_cv_t<T>,bool>&&!std::is_same_v<std::remove_cv_t<T>,char>,bool>read(T&value){if(_streaming)returnread_integer_from_stream(value);if(!prepare_number())returnfalse;intc=static_cast<unsignedchar>(_buffer[_position++]);while(c<=' ')c=static_cast<unsignedchar>(_buffer[_position++]);boolnegative=false;if(c=='-'){negative=true;c=static_cast<unsignedchar>(_buffer[_position++]);}ifconstexpr(internal::is_signed_v<T>){Tresult=0;while('0'<=c&&c<='9'){constintfirst=c-'0';constintsecond=static_cast<unsignedchar>(_buffer[_position])-'0';if(0<=second&&second<=9){result=negative?result*100-(first*10+second):result*100+(first*10+second);++_position;}else{result=negative?result*10-first:result*10+first;}c=static_cast<unsignedchar>(_buffer[_position++]);}value=result;}else{Tresult=0;while('0'<=c&&c<='9'){constunsignedfirst=unsigned(c-'0');constintsecond=static_cast<unsignedchar>(_buffer[_position])-'0';if(0<=second&&second<=9){result=result*100+T(first*10+unsigned(second));++_position;}else{result=result*10+T(first);}c=static_cast<unsignedchar>(_buffer[_position++]);}value=negative?T(0)-result:result;}if(_position>_length)_position=_length;returntrue;}template<classT>std::enable_if_t<std::is_floating_point_v<T>,bool>read(T&value){if(!skip_spaces())returnfalse;intc=read_char_raw();boolnegative=false;if(c=='-'||c=='+'){negative=c=='-';c=read_char_raw();}longdoubleresult=0;while('0'<=c&&c<='9'){result=result*10+(c-'0');c=read_char_raw();}if(c=='.'){longdoubleplace=0.1L;c=read_char_raw();while('0'<=c&&c<='9'){result+=(c-'0')*place;place*=0.1L;c=read_char_raw();}}if(c=='e'||c=='E'){c=read_char_raw();boolexponent_negative=false;if(c=='-'||c=='+'){exponent_negative=c=='-';c=read_char_raw();}intexponent=0;while('0'<=c&&c<='9'){exponent=exponent*10+(c-'0');c=read_char_raw();}longdoublescale=1;longdoublepower=10;while(exponent>0){if(exponent&1)scale*=power;power*=power;exponent>>=1;}result=exponent_negative?result/scale:result*scale;}value=static_cast<T>(negative?-result:result);returntrue;}template<classT>std::enable_if_t<internal::has_val_method_v<T>&&!internal::is_integral_v<T>&&!internal::is_range_v<T>,bool>read(T&value){longlongx;if(!read(x))returnfalse;ifconstexpr(internal::has_static_mod_raw_v<T>){if(x>=0&&uint64_t(x)<uint64_t(T::mod())){value=T::raw(uint32_t(x));}else{value=T(x);}}else{value=T(x);}returntrue;}template<classFirst,classSecond>boolread(std::pair<First,Second>&value){if(!read(value.first))returnfalse;returnread(value.second);}template<classRange>std::enable_if_t<internal::is_range_v<Range>&&!internal::is_string_like_v<Range>,bool>read(Range&range){usingStoredValue=internal::range_stored_value_t<Range>;constexprboolnested=internal::is_range_v<StoredValue>&&!internal::is_string_like_v<StoredValue>;for(auto&&value:range){ifconstexpr(std::is_same_v<StoredValue,bool>&&!nested){boolx;if(!read(x))returnfalse;value=x;}else{if(!read(value))returnfalse;}}returntrue;}template<classFirst,classSecond,class...Rest>boolread(First&first,Second&second,Rest&...rest){if(!read(first))returnfalse;returnread(second,rest...);}template<classT>FastInput&operator>>(T&value){if(!read(value))std::abort();return*this;}};structFastOutput{staticconstexprintbuffer_size=1<<20;private:inlinestaticconstautodigit_quads=[]{std::array<char,40000>result{};for(inti=0;i<10000;i++){intvalue=i;for(intj=3;j>=0;j--){result[4*i+j]=char('0'+value%10);value/=10;}}returnresult;}();std::FILE*_stream;char_buffer[buffer_size];int_position;int_precision;std::chars_format_float_format;char_range_separator;std::string*_capture=nullptr;template<classT>std::stringformat_cell(constT&value){std::stringresult;structCaptureGuard{std::string*⌖std::string*previous;~CaptureGuard(){target=previous;}}guard{_capture,_capture};_capture=&result;write(value);returnresult;}template<classMatrix>voidwrite_aligned_matrix(constMatrix&matrix){std::vector<std::vector<std::string>>rows;std::vector<std::size_t>widths;for(constauto&row:matrix){auto&cells=rows.emplace_back();std::size_tcolumn=0;for(constauto&value:row){cells.push_back(format_cell(value));if(column==widths.size())widths.push_back(0);widths[column]=std::max(widths[column],cells.back().size());++column;}}boolfirst=true;for(constauto&row:rows){if(!first)write_char('\n');first=false;for(std::size_tcolumn=0;column<row.size();++column){if(column!=0)write_char(_range_separator);for(std::size_tpadding=row[column].size();padding<widths[column];++padding){write_char(' ');}write(row[column]);}}}public:explicitFastOutput(std::FILE*stream=stdout):_stream(stream),_position(0),_precision(6),_float_format(std::chars_format::general),_range_separator(' '){if(_stream==stdout&&internal::standard_output_instance==nullptr){internal::standard_output_instance=this;}}FastOutput(constFastOutput&)=delete;FastOutput&operator=(constFastOutput&)=delete;~FastOutput(){flush();if(internal::standard_output_instance==this){internal::standard_output_instance=nullptr;}}voidflush(){if(_position!=0){std::fwrite(_buffer,1,_position,_stream);_position=0;}std::fflush(_stream);}voidwrite_char(charc){if(_capture!=nullptr){_capture->push_back(c);return;}if(_position==buffer_size)flush();_buffer[_position++]=c;}voidwrite(constchar*s){while(*s!='\0')write_char(*s++);}voidwrite(conststd::string&s){if(_capture!=nullptr){_capture->append(s);return;}std::size_tposition=0;while(position<s.size()){if(_position==buffer_size)flush();conststd::size_tcopied=std::min<std::size_t>(buffer_size-_position,s.size()-position);std::memcpy(_buffer+_position,s.data()+position,copied);_position+=int(copied);position+=copied;}}voidwrite(charc){write_char(c);}voidwrite(boolvalue){write_char(value?'1':'0');}template<classT>std::enable_if_t<std::is_floating_point_v<T>>write(Tvalue){chardigits[128];auto[end,error]=std::to_chars(digits,digits+sizeof(digits),value,_float_format,_precision);if(error!=std::errc())std::abort();for(constchar*pointer=digits;pointer!=end;pointer++){write_char(*pointer);}}template<classT>std::enable_if_t<internal::is_integral_v<T>&&!std::is_same_v<std::remove_cv_t<T>,bool>&&!std::is_same_v<std::remove_cv_t<T>,char>>write(Tvalue){usingRaw=std::remove_cv_t<T>;usingUnsigned=internal::make_unsigned_t<Raw>;Unsignedmagnitude;ifconstexpr(internal::is_signed_v<Raw>){if(value<0){write_char('-');magnitude=Unsigned(0)-Unsigned(value);}else{magnitude=Unsigned(value);}}else{magnitude=value;}if(magnitude==0){write_char('0');return;}unsignedchunks[16];intcount=0;while(magnitude>=10000){constUnsignedquotient=magnitude/10000;chunks[count++]=unsigned(magnitude-quotient*10000);magnitude=quotient;}if(_capture==nullptr&&_position>buffer_size-64)flush();charcaptured[64];char*constbegin=_capture!=nullptr?captured:_buffer+_position;char*destination=begin;constunsignedleading=unsigned(magnitude);constchar*first=digit_quads.data()+4*leading;intskip=leading<10?3:leading<100?2:leading<1000?1:0;for(;skip<4;skip++)*destination++=first[skip];while(count--){constchar*digits=digit_quads.data()+4*chunks[count];std::memcpy(destination,digits,4);destination+=4;}if(_capture!=nullptr){_capture->append(begin,destination-begin);}else{_position+=int(destination-begin);}}template<classT>std::enable_if_t<internal::has_val_method_v<T>&&!internal::is_integral_v<T>&&!internal::is_range_v<T>>write(constT&value){write(value.val());}template<classFirst,classSecond>voidwrite(conststd::pair<First,Second>&value){write(value.first);write_char(' ');write(value.second);}template<classRange>std::enable_if_t<internal::is_range_v<Range>&&!internal::is_string_like_v<Range>>write(constRange&range){usingStoredValue=internal::range_stored_value_t<constRange>;constexprboolnested=internal::is_range_v<StoredValue>&&!internal::is_string_like_v<StoredValue>;boolfirst=true;for(constauto&value:range){if(!first)write_char(nested?'\n':_range_separator);first=false;ifconstexpr(std::is_same_v<StoredValue,bool>&&!nested){write(static_cast<bool>(value));}else{write(value);}}}template<classFirst,class...Rest>voidprint(constFirst&first,constRest&...rest){write(first);((write_char(' '),write(rest)),...);}voidprintln(){write_char('\n');}voidset_precision(intprecision){_precision=precision;}voidset_fixed(intprecision=6){_float_format=std::chars_format::fixed;_precision=precision;}voidset_general(intprecision=6){_float_format=std::chars_format::general;_precision=precision;}voidset_range_separator(charseparator){_range_separator=separator;}template<classMatrix>voidwrite_aligned(constMatrix&matrix){usingRow=internal::range_stored_value_t<constMatrix>;usingCell=internal::range_stored_value_t<constRow>;static_assert(internal::is_range_v<Row>&&!internal::is_string_like_v<Row>,"write_aligned requires a two-dimensional range");static_assert(!internal::is_range_v<Cell>||internal::is_string_like_v<Cell>,"write_aligned requires scalar cells");write_aligned_matrix(matrix);}template<classMatrix>voidprintln_aligned(constMatrix&matrix){write_aligned(matrix);write_char('\n');}template<class...Args>voidprintln(constArgs&...args){print(args...);write_char('\n');}template<classT>FastOutput&operator<<(constT&value){write(value);return*this;}};}// namespace utilities}// namespace m1une#line 9 "verify/ds/bst/ordered_multiset.test.cpp"
#include<set>
#line 12 "verify/ds/bst/ordered_multiset.test.cpp"
voidsplit_merge_test(){m1une::ds::OrderedMultiset<int>tree={1,2,2,4,7};autocopy=tree;auto[left,right]=std::move(tree).split(4);std::vector<int>expected_left={1,2,2};std::vector<int>expected_right={4,7};std::vector<int>expected_all={1,2,2,4,7};assert(left.to_vector()==expected_left);assert(right.to_vector()==expected_right);tree=std::move(left).merge(std::move(right));assert(tree.to_vector()==expected_all);assert(copy.to_vector()==expected_all);}voidrandomized_test(){m1une::ds::OrderedMultiset<int>tree;std::multiset<int>expected;std::uint64_tstate=123456789;for(intquery=0;query<10000;++query){state=state*6364136223846793005ULL+1442695040888963407ULL;constintkey=int(state>>32)%500;constinttype=int(state%6);if(type<=1){constintmultiplicity=int(state%3)+1;tree.insert(key,multiplicity);for(inti=0;i<multiplicity;++i)expected.insert(key);}elseif(type==2){constboolerased=tree.erase_one(key);autoit=expected.find(key);assert(erased==(it!=expected.end()));if(it!=expected.end())expected.erase(it);}elseif(type==3){assert(tree.erase_all(key)==int(expected.count(key)));expected.erase(key);}elseif(type==4){assert(tree.count(key)==int(expected.count(key)));assert(tree.order_of_key(key)==int(std::distance(expected.begin(),expected.lower_bound(key))));}else{autocopy=tree;auto[left,right]=std::move(tree).split(key);std::vector<int>expected_left(expected.begin(),expected.lower_bound(key));std::vector<int>expected_right(expected.lower_bound(key),expected.end());assert(left.to_vector()==expected_left);assert(right.to_vector()==expected_right);tree=std::move(left).merge(std::move(right));assert(copy.to_vector()==tree.to_vector());}assert(tree.size()==int(expected.size()));assert(tree.to_vector()==std::vector<int>(expected.begin(),expected.end()));}}intmain(){m1une::utilities::FastInputfast_input;m1une::utilities::FastOutputfast_output;split_merge_test();randomized_test();intN,Q;fast_input>>N>>Q;m1une::ds::OrderedMultiset<int>ms;for(inti=0;i<N;i++){inta;fast_input>>a;ms.insert(a);}while(Q--){inttype,x;fast_input>>type>>x;if(type==0){if(!ms.contains(x))ms.insert(x);}elseif(type==1){if(ms.contains(x))ms.erase_one(x);}elseif(type==2){if(ms.size()<x){fast_output<<-1<<'\n';}else{fast_output<<ms.kth(x-1)<<'\n';}}elseif(type==3){fast_output<<ms.count_less_equal(x)<<'\n';}elseif(type==4){constint*ans=ms.max_le(x);fast_output<<(ans?*ans:-1)<<'\n';}else{constint*ans=ms.min_ge(x);fast_output<<(ans?*ans:-1)<<'\n';}}}