#pragma once
template<typenameT>structSegmentSet{map<T,T>st;SegmentSet()=default;usingconst_iterator=typenamemap<T,T>::const_iterator;const_iteratorbegin()const{returnst.begin();}const_iteratorend()const{returnst.end();}const_iteratorfind(Tx)const{autoit=st.upper_bound(x);if(it==st.begin()||(--it)->second<=x)returnst.end();returnit;}const_iteratorlower_bound(Tx)const{autoit=st.lower_bound(x);if(it==st.begin()||prev(it)->second<=x)returnit;returnprev(it);}voidinsert(Tl,Tr){autoL=st.upper_bound(l),R=st.upper_bound(r);if(L!=st.begin()&&l<=prev(L)->second)--L;if(L!=R){l=min(l,L->first);r=max(r,prev(R)->second);st.erase(L,R);}st[l]=r;}voiderase(Tl,Tr){autoL=st.upper_bound(l),R=st.upper_bound(r);if(L!=st.begin()&&l<=prev(L)->second)--L;if(L==R)return;Tnl=min(l,L->first),nr=max(r,prev(R)->second);st.erase(L,R);if(nl<l)st[nl]=l;if(r<nr)st[r]=nr;}// x 以上の値を返す // 存在しない場合は numeric_limits<T>::max() を返すTnext(Tx)const{autoit=this->lower_bound(x);if(it==this->end())returnnumeric_limits<T>::max();returnmax<T>(x,it->first);}intsize(){returnst.size();}};
#line 2 "data-structure/segment-set.hpp"
template<typenameT>structSegmentSet{map<T,T>st;SegmentSet()=default;usingconst_iterator=typenamemap<T,T>::const_iterator;const_iteratorbegin()const{returnst.begin();}const_iteratorend()const{returnst.end();}const_iteratorfind(Tx)const{autoit=st.upper_bound(x);if(it==st.begin()||(--it)->second<=x)returnst.end();returnit;}const_iteratorlower_bound(Tx)const{autoit=st.lower_bound(x);if(it==st.begin()||prev(it)->second<=x)returnit;returnprev(it);}voidinsert(Tl,Tr){autoL=st.upper_bound(l),R=st.upper_bound(r);if(L!=st.begin()&&l<=prev(L)->second)--L;if(L!=R){l=min(l,L->first);r=max(r,prev(R)->second);st.erase(L,R);}st[l]=r;}voiderase(Tl,Tr){autoL=st.upper_bound(l),R=st.upper_bound(r);if(L!=st.begin()&&l<=prev(L)->second)--L;if(L==R)return;Tnl=min(l,L->first),nr=max(r,prev(R)->second);st.erase(L,R);if(nl<l)st[nl]=l;if(r<nr)st[r]=nr;}// x 以上の値を返す // 存在しない場合は numeric_limits<T>::max() を返すTnext(Tx)const{autoit=this->lower_bound(x);if(it==this->end())returnnumeric_limits<T>::max();returnmax<T>(x,it->first);}intsize(){returnst.size();}};