#ifndef MYLOCAL //# pragma GCC target("avx2")//yukiではNG # pragma GCC optimize("O3") # pragma GCC optimize("unroll-loops") #endif #if defined(NDEBUG) #undef NDEBUG #endif #include "bits/stdc++.h" using namespace std; using ll=long long; using dd=long double; using pll=pair; using tll=tuple; using qll=tuple; using namespace chrono; constexpr ll INF = 1201001001001001001; struct Fast{ Fast(){ cin.tie(0); ios::sync_with_stdio(false); cout<::max_digits10); } } fast; #define EXPAND( x ) x//VS用おまじない #define overload3(_1,_2,_3,name,...) name #define overload4(_1,_2,_3,_4,name,...) name #define overload5(_1,_2,_3,_4,_5,name,...) name #define rep1(N) for (ll dmyi = 0; dmyi < (N); dmyi++) #define rep2(i, N) for (ll i = 0; i < (N); i++) #define rep3(i, S, E) for (ll i = (S); i <= (E); i++) #define rep4(i, S, E, t) for (ll i = (S); i <= (E); i+=(t)) #define rep(...) EXPAND(overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)) #define dep3(i, E, S) for (ll i = (E); i >= (S); i--) #define dep4(i, E, S, t) for (ll i = (E); i >= (S); i-=(t)) #define dep(...) EXPAND(overload4(__VA_ARGS__, dep4, dep3,_,_)(__VA_ARGS__)) #define ALL1(v) (v).begin(), (v).end() #define ALL2(v,E) (v).begin(), (v).begin()+((E)+1) #define ALL3(v,S,E) (v).begin()+(S), (v).begin()+((E)+1) #define all(...) EXPAND(overload3(__VA_ARGS__, ALL3, ALL2, ALL1)(__VA_ARGS__)) template inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; }return false; } template inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; }return false; } template [[nodiscard]] inline T limithi(T a,T b){ return min(a,b); } template [[nodiscard]] inline T limitlo(T a,T b){ return max(a,b); } template inline bool chlimithi(T &a,T b){ return chmin(a,b); } template inline bool chlimitlo(T &a,T b){ return chmax(a,b); } template inline auto maxe(T &&v,ll S,ll E){ return *max_element(all(v,S,E)); } template inline auto maxe(T &&v){ return *max_element(all(v)); } template inline auto mine(T &&v,ll S,ll E){ return *min_element(all(v,S,E)); } template inline auto mine(T &&v){ return *min_element(all(v)); } template::type::value_type> inline U sum(T &&v,ll S,ll E) {return accumulate(all(v,S,E),U());} template inline auto sum(T &&v) {return sum(v,0,v.end()-v.begin()-1);} template inline ll sz(T &&v){ return (ll)v.size(); } //cin struct cinutil{ template static void cin1core(T &a){ cin>>a; } template static void cin1core(pair &a){ cin1core(a.first),cin1core(a.second); } template static void cin1core(tuple &a){ cinTplRec,sizeof...(Args)-1>()(a); } template static void cin1core(array &a){ for (int i=0; i<(int)N; ++i) cin>>a[i]; } private: template struct cinTplRec{ void operator()(Tpl &a){ cinTplRec()(a); cin1core(get(a)); } }; template struct cinTplRec{ void operator()(Tpl &a){ cin1core(get<0>(a)); } }; }; template T cin1(){ T a; cinutil::cin1core(a); return a; } template tuple cins(){ return cin1>(); } //cout template inline ostream &operator<<(ostream &os,const pair &a){ return os << a.first << ' ' << a.second; } template inline ostream &operator<<(ostream &os,const tuple &a){ return os << get<0>(a) << ' ' << get<1>(a) << ' ' << get<2>(a); } template inline ostream &operator<<(ostream &os,const tuple &a){ return os << get<0>(a) << ' ' << get<1>(a) << ' ' << get<2>(a) << ' ' << get<3>(a); } template inline ostream &operator<<(ostream &os,const vector &a){ for (ll i=0; i<(ll)a.size(); i++) os<<(i>0?" ":"")<(system_clock::now()-st).count()/1000;} } timeget; template struct mll_{ using Int = long long; using ll = long long; ll val_=0; /*---- utility ----*/ mll_ &norm(){ return normR().normS(); }//正規化 mll_ &normR(){ val_%=MOD; return *this; }//剰余正規化のみ mll_ &normS(){ if (val_<0) val_+=MOD; return *this; }//正負正規化のみ mll_ &normP(){ if (val_>=MOD) val_-=MOD; return *this; }//加算時正規化 mll_ &invsg(){ val_=-val_; return normS(); }//正負反転 ll modinv(int a){//a^-1 mod MOD int ypre=0,y=1,apre=MOD; while (a>1){ int t=apre/a; apre-=a*t,swap(a,apre); ypre-=y*t,swap(y,ypre); } return y<0 ? y+MOD : y; } /*---- I/F ----*/ mll_(){} mll_(ll v): val_(v){ norm(); } mll_(ll v,bool b): val_(v){} //正規化無のコンストラクタ Int val()const{ return (Int)val_; } bool isnone() const { return val_==-1; } //true:値なし mll_ &none() { val_=-1; return *this; } //値なしにする mll_ &inv(){ val_=modinv((int)val_); return *this; } mll_ &operator+=(mll_ b){ val_+=b.val_; return normP(); } mll_ &operator-=(mll_ b){ val_-=b.val_; return normS(); } mll_ &operator*=(mll_ b){ val_*=b.val_; return normR(); } mll_ &operator/=(mll_ b){ return *this*=b.inv(); } mll_ &operator+=(ll b){ return *this+=mll_(b); } mll_ &operator-=(ll b){ return *this-=mll_(b); } mll_ &operator*=(ll b){ return *this*=mll_(b); } mll_ &operator/=(ll b){ return *this/=mll_(b); } mll_ operator-()const{ return mll_(*this).invsg(); } mll_ operator+(mll_ b)const{ return mll_(*this)+=b; } mll_ operator-(mll_ b)const{ return mll_(*this)-=b; } mll_ operator*(mll_ b)const{ return mll_(*this)*=b; } mll_ operator/(mll_ b)const{ return mll_(*this)/=b; } mll_ operator+(ll b)const{ return mll_(*this)+=b; } mll_ operator-(ll b)const{ return mll_(*this)-=b; } mll_ operator*(ll b)const{ return mll_(*this)*=b; } mll_ operator/(ll b)const{ return mll_(*this)/=b; } friend mll_ operator+(ll a,mll_ b){ return b+a; } friend mll_ operator-(ll a,mll_ b){ return -b+a; } friend mll_ operator*(ll a,mll_ b){ return b*a; } friend mll_ operator/(ll a,mll_ b){ return mll_(a)/b; } bool operator==(mll_ b)const{ return val_==b.val_; } bool operator!=(mll_ b)const{ return val_!=b.val_; } bool operator==(ll b)const{ return *this==mll_(b); } bool operator!=(ll b)const{ return *this!=mll_(b); } friend bool operator==(ll a,mll_ b){ return mll_(a)==b; } friend bool operator!=(ll a,mll_ b){ return mll_(a)!=b; } friend ostream &operator<<(ostream &os,mll_ a){ return os << a.val_; } friend istream &operator>>(istream &is,mll_ &a){ return is >> a.val_; } mll_ pow(ll k)const{ mll_ ret(1,false),a(*this); for (; k>0; k>>=1,a*=a) if (k&1)ret*=a; return ret; } static constexpr int mod() { return MOD; } //enum{ modll=MOD }; }; template struct Vector: vector{ using Int = long long; using vT=vector; using cvT=const vector; using cT=const T; using vT::vT; //親クラスのコンストラクタの隠蔽を回避 using vT::begin,vT::end,vT::insert,vT::erase; auto it(Int i){ return begin()+i; } auto it(Int i)const{ return begin()+i; } Vector(cvT& b):vT(b){} Vector(vT&& b):vT(move(b)){} Vector(int n,cT& x):vT(n,x){}// ┬ 型推論のためラッパー Vector(long long n,cT& x):vT(n,x){} template Vector(const Vector& b):vT(b.begin(),b.end()){} template Vector(const vector& b):vT(b.begin(),b.end()){} Vector(Int n,T s,T d){ iota(n,s,d); } Vector(Int n,function g):vT(n){ for(Int i=0;i &b){ return *this+=(cvT&)b; } Vector &operator-=(const Vector &b){ return *this-=(cvT&)b; } Vector &operator*=(const Vector &b){ return *this*=(cvT&)b; } Vector &operator/=(const Vector &b){ return *this/=(cvT&)b; } Vector &operator%=(const Vector &b){ return *this%=(cvT&)b; } Vector operator+(cvT &b){ return Vector(*this)+=b; } Vector operator-(cvT &b){ return Vector(*this)-=b; } Vector operator*(cvT &b){ return Vector(*this)*=b; } Vector operator/(cvT &b){ return Vector(*this)/=b; } Vector operator%(cvT &b){ return Vector(*this)%=b; } Vector operator+(const Vector &b){ return Vector(*this)+=b; } Vector operator-(const Vector &b){ return Vector(*this)-=b; } Vector operator*(const Vector &b){ return Vector(*this)*=b; } Vector operator/(const Vector &b){ return Vector(*this)/=b; } Vector operator%(const Vector &b){ return Vector(*this)%=b; } template Vector &operator+=(S x){ for(T &e: *this) e+=x; return *this; } template Vector &operator-=(S x){ for(T &e: *this) e-=x; return *this; } template Vector &operator*=(S x){ for(T &e: *this) e*=x; return *this; } template Vector &operator/=(S x){ for(T &e: *this) e/=x; return *this; } template Vector &operator%=(S x){ for(T &e: *this) e%=x; return *this; } template Vector operator+(S x)const{ return Vector(*this)+=x; } template Vector operator-(S x)const{ return Vector(*this)-=x; } template Vector operator*(S x)const{ return Vector(*this)*=x; } template Vector operator/(S x)const{ return Vector(*this)/=x; } template Vector operator%(S x)const{ return Vector(*this)%=x; } Vector &operator--(int){ return *this-=1; } Vector &operator++(int){ return *this+=1; } Vector operator-()const{ return Vector(*this)*=-1; } template friend Vector operator-(S x,const Vector &a){ return -a+=x; } T& at(Int i){ assert(i>=0); if(n()<=i)vT::resize(i+1); return vT::operator[](i); } Vector slice(Int l,Int r,Int d=1)const{ Vector ret; for(Int i=l;(d>0&&i<=r)||(d<0&&r<=i);i+=d) ret.push_back((*this)[i]); return ret; } Int size()const{ return (Int)vT::size(); } Int n()const{ return size(); } Vector &push_back(cT& x,Int n=1){ for(Int i=0;iinsert(0,x,n); return *this; } Vector &pop_front(Int n=1){ erase(0,n-1); return *this; } T pull_back(){ T x=move(vT::back()); vT::pop_back(); return x; } T pull_front(){ T x=move(vT::front()); erase(0); return x; } Vector &insert(Int i,cT& x,Int n=1){ insert(it(i),n,x); return *this; } Vector &insert(Int i,cvT& b){ insert(it(i),b.begin(),b.end()); return *this; } Vector &erase(Int i){ erase(it(i)); return *this; } Vector &erase(Int l,Int r){ erase(it(l),it(r+1)); return *this; } Vector &erase(const Vector &idxs){ for (Int I=0; In(); copy(it(l),it(r),it(l-I-1));//[l,r)を前にI+1個ずらす } vT::resize(this->n()-idxs.n()); return *this; } Vector &eraseall(cT& x){ return eraseall(0,size()-1,x); } Vector &eraseall(Int l,Int r,cT& x){ erase(remove(it(l),it(r+1),x),it(r+1)); return *this; } template Vector &eraseif(Pr pr){ return eraseif(0,size()-1,pr); } template Vector &eraseif(Int l,Int r,Pr pr){ erase(remove_if(it(l),it(r+1),pr),it(r+1)); return *this; } Vector &concat(cvT &b,Int n=1){ cvT B = (&b==this) ? *this : vT{}; for(int i=0;iinsert(size(),(&b==this)?B:b); return *this; } Vector repeat(Int n){ return Vector{}.concat(*this,n); } Vector &reverse(Int l=0,Int r=-1){ r+=r<0?size():0; std::reverse(it(l),it(r+1)); return *this; } Vector &rotate(Int m){ return rotate(0,size()-1,m); } Vector &rotate(Int l,Int r,Int m){ std::rotate(it(l),it(m),it(r+1)); return *this; } Vector &sort(Int l=0,Int r=-1){ r+=r<0?size():0; std::sort(it(l),it(r+1)); return *this; } Vector &rsort(Int l=0,Int r=-1){ return sort(l,r).reverse(l,r); } template Vector &sort(Pr pr){ return sort(0,size()-1,pr); } template Vector &sort(Int l,Int r,Pr pr){ std::sort(it(l),it(r+1),pr); return *this; } template Vector &sortbykey(Int l=0,Int r=-1){ r+=r<0?size():0; sort(l,r,[](cT &x,cT &y){return get(x)(y);}); return *this; } Vector &uniq(){ erase(unique(begin(),end()),end()); return *this; } Vector &sortq(){ return sort().uniq(); } Vector &fill(cT& x){ return fill(0,size()-1,x); } Vector &fill(Int l,Int r,cT& x){ std::fill(it(l),it(r+1),x); return *this; } Vector ©(Int i,cvT &b,Int n=1){//A[i]スタートでbをn回分コピー for (int t=0; t=size()) return *this; if (i>=0) (*this)[i]=b[j]; i++; } return *this; } template Vector &iota(Int n,T s=0,S d=1){ vT::resize(n); if(n==0) return *this; (*this)[0]=s; for(int i=1;i Int countif(Pr pr)const{ return countif(0,size()-1,pr); } template Int countif(Int l,Int r,Pr pr)const{ return Int(count_if(it(l),it(r+1),pr)); } Int find(cT& x)const{ return find(0,size()-1,x); } Int find(Int l,Int r,cT& x)const{ return Int(std::find(it(l),it(r+1),x)-begin()); } Int rfind(cT& x)const{ return rfind(0,size()-1,x); } Int rfind(Int l,Int r,cT& x)const{ for (int i=r;i>=l;--i) if ((*this)[i]==x) return i; return l-1; } template Int findif(Pr pr)const{ return findif(0,size()-1,pr); } template Int findif(Int l,Int r,Pr pr)const{ return Int(find_if(it(l),it(r+1),pr)-begin()); } Vector findall(cT& x)const{ return findall(0,size()-1,x); } Vector findall(Int l,Int r,cT& x)const{ return findallif(l,r,[&](cT& y){return y==x;}); } template Vector findallif(Pr pr)const{ return findallif(0,size()-1,pr); } template Vector findallif(Int l,Int r,Pr pr)const{ Vector ret; for(Int i=l;i<=r;++i) if(pr((*this)[i])) ret.push_back(i); return ret; } Int flooridx(cT& x)const{ return Int(upper_bound(begin(),end(),x)-begin()-1); } Int ceilidx(cT& x)const{ return Int(lower_bound(begin(),end(),x)-begin()); } Int leftnmof(cT& x)const{ return flooridx(x)+1; } Int rightnmof(cT& x)const{ return size()-ceilidx(x); } bool contains(cT& x)const{ Int i=flooridx(x); return i>=0 && (*this)[i]==x; } template Int flooridx(cT& x,Pr pr)const{ return Int(upper_bound(begin(),end(),x,pr)-begin()-1); } template Int ceilidx(cT& x,Pr pr)const{ return Int(lower_bound(begin(),end(),x,pr)-begin()); } template Int leftnmof(cT& x,Pr pr)const{ return flooridx(x,pr)+1; } template Int rightnmof(cT& x,Pr pr)const{ return size()-ceilidx(x,pr); } template bool contains(cT& x,Pr pr)const{ Int i=flooridx(x,pr); return i>=0 && (*this)[i]==x; } template using VV = Vector>; template using sVV = vector>; template using VVV = Vector>; template using sVVV = vector>; template using VVVV = Vector>; template using sVVVV = vector>; template using VVVVV = Vector>; template using sVVVVV = vector>; auto tostd()const{ return tov(*this); } template static vector tov(const Vector&v){ return v; } template static sVV tov(const VV &v){ sVV ret; for(auto&& e:v) ret.push_back(e); return ret; } template static sVVV tov(const VVV &v){ sVVV ret; for(auto&& e:v) ret.push_back(e.tostd()); return ret; } template static sVVVV tov(const VVVV &v){ sVVVV ret; for(auto&& e:v) ret.push_back(e.tostd()); return ret; } template static sVVVVV tov(const VVVVV &v){ sVVVVV ret; for(auto&& e:v) ret.push_back(e.tostd()); return ret; } }; #if 0 #define MODLL (1000000007LL) #else #define MODLL (998244353LL) #endif using mll = mll_; //using mll = fraction; namespace SolvingSpace{ template using vector = Vector; using vll=vector< ll>; using vmll=vector< mll>; using vdd=vector< dd>; using vvll=vector< vll>; using vvmll=vector< vmll>; using vvdd=vector< vdd>; using vvvll=vector< vvll>; using vvvmll=vector< vvmll>; using vvvdd=vector< vvdd>; using vvvvll=vector; using vvvvmll=vector; using vvvvdd=vector; using vpll=vector< pll>; using vtll=vector< tll>; using vqll=vector< qll>; using vvpll=vector< vpll>; using vvtll=vector< vtll>; using vvqll=vector< vqll>; using vss=vector; template vector cinv(ll nm){ return vector(nm,[](ll i){ (void)i; return cin1(); }); } template vector> cinvv(ll H,ll W){ return vector>(H,[&](ll i){ (void)i; return cinv(W); }); } /*■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■*/ template struct combination{ vector f,g; ll mxN=0; combination(){} combination(ll maxN): f(maxN+1,1),g(maxN+1),mxN(maxN) { for (ll i=1; i<=mxN; ++i) { f[i]=f[i-1]*i; } g[mxN]=1/f[mxN]; for (ll i=mxN; i>=1; --i) { g[i-1]=g[i]*i; } } T P(ll n,ll r){ return (n<0 || r<0 || n vector CnLnR(long long nL,long long nR,long long r,SP sp){ if (nR-nL+1<=0) return vector(); if (r<0) return vector(nR-nL+1,0); vector v=sp(nL-r+1,nR-r+1,r); for (T& e: v) e*=finv(r); return v; } template vector HrLrR(long long n,long long rL,long long rR,SP sp){//r<0不可 return CnLnR(n-1+rL,n-1+rR,n-1,sp); } }; namespace atcoder { //======== from internal_bit.hpp namespace internal { using std::bit_ceil; inline int countr_zero(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index,n); return index; #else return __builtin_ctz(n); #endif } } // namespace internal //======== from segtree.hpp #if __cplusplus >= 201703L template struct segtree { static_assert(std::is_convertible_v>, "op must work as S(S, S)"); static_assert(std::is_convertible_v>, "e must work as S()"); #else template struct segtree { #endif public: segtree(): segtree(0) {} explicit segtree(int n): segtree(std::vector(n,e())) {} explicit segtree(const std::vector& v): _n(int(v.size())) { size = (int)internal::bit_ceil((unsigned int)(_n)); log = internal::countr_zero((unsigned int)size); d = std::vector(2 * size,e()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p,S x) { assert(0 <= p && p < _n); p += size; d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) const { assert(0 <= p && p < _n); return d[p + size]; } S prod(int l,int r) const { assert(0 <= l && l <= r && r <= _n); S sml = e(),smr = e(); l += size; r += size; while (l < r) { if (l & 1) sml = op(sml,d[l++]); if (r & 1) smr = op(d[--r],smr); l >>= 1; r >>= 1; } return op(sml,smr); } S all_prod() const { return d[1]; } template int max_right(int l) const { return max_right(l,[](S x) { return f(x); }); } template int max_right(int l,F f) const { assert(0 <= l && l <= _n); assert(f(e())); if (l == _n) return _n; l += size; S sm = e(); do { while (l % 2 == 0) l >>= 1; if (!f(op(sm,d[l]))) { while (l < size) { l = (2 * l); if (f(op(sm,d[l]))) { sm = op(sm,d[l]); l++; } } return l - size; } sm = op(sm,d[l]); l++; } while ((l & -l) != l); return _n; } template int min_left(int r) const { return min_left(r,[](S x) { return f(x); }); } template int min_left(int r,F f) const { assert(0 <= r && r <= _n); assert(f(e())); if (r == 0) return 0; r += size; S sm = e(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!f(op(d[r],sm))) { while (r < size) { r = (2 * r + 1); if (f(op(d[r],sm))) { sm = op(d[r],sm); r--; } } return r + 1 - size; } sm = op(d[r],sm); } while ((r & -r) != r); return 0; } private: int _n,size,log; std::vector d; void update(int k) { d[k] = op(d[2 * k],d[2 * k + 1]); } }; } // namespace atcoder using atcoder::segtree; namespace segtreeUtils{ using Int=long long; using ll=long long; template T assign(T x,T y){ (void)x; return y; }; template T add(T x,T y){ return x+y; }; /*---- ACLセグ木のwrap ----*/ template> struct segtreeWrap{ atcoder::segtree sgt; ll N; ll offset=0; //位置iの値をi-offsetに格納 segtreeWrap(Int N):sgt(int(N)),N(N){} segtreeWrap(ll l,ll r):sgt(int(r-l+1)),N(r-l+1),offset(l){} //i=l~r segtreeWrap(const vector &v,ll l=0):sgt(v),N((ll)v.size()),offset(l){} ll imin()const{ return offset; } ll imax()const{ return N-1+offset; } void set(Int i,const T &x){ sgt.set(int(i-offset),x); } template void apply(Int i,const E &y){ set(i,f(get(i),y)); } T get(Int i)const{ return sgt.get(int(i-offset)); } T operator[](Int i)const{ return get(i); } T prod(Int l,Int r)const{ return sgt.prod(int(l-offset),int(r+1-offset)); } T get(Int l,Int r)const{ return prod(l,r); } T allprod()const{ return sgt.all_prod(); } template Int maxRight(Int l,Int R,IsOK isOK)const{ ll pos=sgt.max_right((int)normalizeL(l),isOK)-1; return normalizeR(R) ll minRight(Int l,Int R,IsOK isOK)const{ ll pos=maxRight(l,R,[&isOK](const T &prd){return !isOK(prd); })+1; return min(pos,R+1); } template ll minLeft(Int L,Int r,IsOK isOK)const{ ll pos=sgt.min_left((int)normalizeR(r)+1,isOK); return pos ll maxLeft(Int L,Int r,IsOK isOK)const{ ll pos=minLeft(L,r,[&isOK](const T &prd){return !isOK(prd); })-1; return max(pos,L-1); } #if 0 void dump()const{ vector v; for (ll i=imin(); i<=imax(); ++i) v.push_back(get(i)); dumpstring::dumpNd("",v,args().sx(imin()).labels({"i"})); } #endif private: ll normalizeL(ll l)const{ return clamp(l-offset,0LL,N); } ll normalizeR(ll r)const{ return clamp(r-offset,-1LL,N-1); } }; /*---- max/min特化セグ木 ----*/ template, auto op=[](T x,T y){return IsMAX ? max(x,y) : min(x,y); }, auto e=[]{return E; }> struct SegtreeMaxOrMin: segtreeWrap{ using Base = segtreeWrap; using Base::Base; Int findNextGE(Int i,T x)const{ return Base::minRight(i,Base::imax(),[&](T va){return IsMAX ? va>=x : va<=x; }); } Int findPrevGE(Int i,T x)const{ return Base::maxLeft(Base::imin(),i,[&](T va){return IsMAX ? va>=x : va<=x; }); } Int findPeakL(Int l,Int r)const{ return findNextGE(l,Base::prod(l,r)); } Int findPeakR(Int l,Int r)const{ return findPrevGE(r,Base::prod(l,r)); } }; using segtreemax=SegtreeMaxOrMin; using segtreemin=SegtreeMaxOrMin; }//namespace using segtreeUtils::segtreeWrap; using segtreeUtils::SegtreeMaxOrMin; using segtreeUtils::segtreemax; using segtreeUtils::segtreemin; /* ACLのsegtreeにoffset機能と各種I/Fを追加 適用関数fは不使用時省略可(set使えばよい) - ---- 定義 一般のセグ木 ---- using VAL=ll; constexpr auto opfunc=[](const VAL& x,const VAL& y){return x^y;}; //合成関数 constexpr auto efunc=[]{ return 0ll; }; //単位元 constexpr auto ffunc=[](ll x,pll y){auto[a,b]=y; return x*a+b;}; //適用関数 省略可 segtreeWrap sgt(N); //i=0~N-1 全て単位元 segtreeWrap sgt(l,r); //i=l~r 全て単位元 segtreeWrap sgt(ini); //i=0~N-1 v[0]~v[N-1]で初期化 segtreeWrap sgt(ini,l); //i=l~l+N-1 v[0]~v[N-1]で初期化 segtreeWrap sgt(ini); //f不使用(代入しかしないとき) - ---- 定義 一般のmax/minセグ木 ---- ・引数は一般のセグ木と同様 ・適用関数のデフォルトは加算 using VAL=ll; constexpr auto ffunc=[](ll x,pll y){auto[a,b]=y; return x*a+b;}; //適用関数 省略可 SegtreeMaxOrMin sgt(v); . 型┘ ↑ └単位元 . true:max,false:min SegtreeMaxOrMin sgt(v); //一点加算maxセグ木 単位元0 SegtreeMaxOrMin sgt(v); //double型一点加算minセグ木 - ---- 定義 ll型一点加算maxセグ木 ---- segtreemax sgt(N); //i=0~N-1 全て単位元 segtreemax sgt(l,r); //i=l~r 全て単位元 segtreemax sgt(v); //i=0~N-1 v[0]~v[N-1]で初期化 segtreemax sgt(v,l); //i=l~l+N-1 v[0]~v[N-1]で初期化 - ---- 定義 ll型一点加算minセグ木 ---- segtreemin sgt(N); // segtreemin sgt(l,r); //引数はmaxセグ木と同様 segtreemin sgt(v); // segtreemin sgt(v,l); // - ---- 操作 ---- ll i=sgt.imin(); //iの下限 ll i=sgt.imax(); //iの上限 sgt.offset+=d; //全iについて位置iの要素をi+dにシフト sgt.set(i,x); //a[i]←x 位置iにx代入 sgt.apply(i,y); //a[i]←f(a[i],y) 適用関数使用 ll x=sgt.get(i); //a[i] 位置iの値 ll x=sgt[i]; //a[i] 位置iの値 ll x=sgt.prod(l,r); //[l,r]の値 ll x=sgt.get(l,r); // 〃 ll x=sgt.allprod(); //全区間の値 - ---- max(min)セグ木のみの操作 ---- ll j=sgt.findNextGE(i,x); //i以降で初めてx以上(以下)になる位置 ll j=sgt.findPrevGE(i,x); //i以前で 〃 ll i=sgt.findPeakL(l,r); //区間[l,r]内max(min)になる最左位置 ll i=sgt.findPeakR(l,r); //区間[l,r]内max(min)になる最右位置 - ---- 二分探索 ---- [L,R]内でラムダ式isOKが○(true)になる位置 L,Rは範囲外や逆転も可 - 使用例 ll i=sgt.minRight(L,R,[&](ll x){return x>=th;}); // L×→×→×ⓡ○R ll i=sgt.maxRight(L,R,[&](ll x){return x<=th;}); // L○→○→ⓡ××R ll i=sgt.maxLeft (L,R,[&](ll x){return x>=th;}); //L○ⓛ×←×←×R ll i=sgt.minLeft (L,R,[&](ll x){return x<=th;}); //L××ⓛ←○←○R - 要件・挙動 minRight: isOK([L,r])=○(l≦r≦R)となる最左のr、ないときR+1 要件:isOK(e)=× maxRight: isOK([L,r])=○(l≦r≦R)となる最右のr、ないときL-1 要件:isOK(e)=○ maxLeft: isOK([l,R])=○(L≦l≦r)となる最右のl、ないときL-1 要件:isOK(e)=× minLeft: isOK([l,R])=○(L≦l≦r)となる最左のl、ないときR+1 要件:isOK(e)=○ */ namespace BITSpace{ //generated by GPT-5.6 Sol modified by hamamu using ll = long long; using Int = long long; // デフォルトの加算・減算 template struct BITAdd{ T operator()(const T &a,const T &b)const{ return a+b; } }; template struct BITSub{ T operator()(const T &a,const T &b)const{ return a-b; } }; template,class Sub=BITSub> struct BIT{ // sub=nullptr_t のとき、逆演算が必要な関数を requires で制限する。 static constexpr bool HAS_SUB=!is_same_v; ll n=0; T e; Ope op; Sub sub; // bit[i]は1-origin Fenwick Tree のノード // 公開添字iはbit[i+1]側に対応 vector bit; // デフォルトは加算・減算。iniが空なら全要素eとして構築を省略する。 BIT(T,ll n_,const vector &ini={},const T &e_=T()):n(n_),e(e_),op(Ope()),sub(Sub()){ init(ini); } // op指定。sub省略時(nullptr)は逆演算なし。 BIT(T,ll n_,const vector &ini,const T &e_,Ope op_,Sub sub_=nullptr):n(n_),e(e_),op(move(op_)),sub(move(sub_)){ init(ini); } void apply(ll i,const T &x){ assert(0<=i && i0; j-=j&-j) ret=op(ret,bit[j]); return ret; } pair floorGroup(ll l,T x)const requires(HAS_SUB){ assert(0<=l && l<=n); //prefix(l-1)+x以下となる最大prefixをBIT上で二分探索する T base=prefix(l-1),th=op(base,x); auto [p,pref]=maxPrefix([&](const T &v){ return !(th ceilGroup(ll l,T x)const requires(HAS_SUB){ assert(0<=l && l<=n); if (x==e) return {(Int)(l-1),e}; //x=0なら空区間r=l-1が最小の答 //閾値prefix(l-1)+x未満となる最大prefixを二分探索で得る //その次の要素(=p)が初めて閾値以上になる位置 T base=prefix(l-1),th=op(base,x); auto [p,pref]=maxPrefix([&](const T &v){ return v floorGroupRev(ll r,T x)const requires(HAS_SUB){ assert(-1<=r && r=prefix(r)-xを探す T total=prefix(r),th=sub(total,x); ll l=maxPrefix([&](const T &v){ return v ceilGroupRev(ll r,T x)const requires(HAS_SUB){ assert(-1<=r && r &ini){ assert(n>=0); bit.assign(n+1,e); if (ini.empty()) return; assert((ll)ini.size()==n); //iniを葉として入れ、各要素を一度だけ親へ集約 for (ll i=0; i pair maxPrefix(F ok)const{ if (!ok(e)) return {-1,e}; ll p=0; T cur=e; for (ll step=(ll)bit_floor((unsigned long long)n); step; step>>=1){ ll np=p+step; if (np<=n){ T nxt=op(cur,bit[np]); if (ok(nxt)) p=np,cur=move(nxt); } } return {p,cur}; } }; // custom opでsubを省略したときは逆演算なし。 template BIT(T,ll,const vector&,const T&,Ope) -> BIT; } using BITSpace::BIT; /* - ---- 定義 ---- BIT bit(ll(),N); //データ型ll、[0,N-1]、初期値0で構築省略 BIT bit(ll(),N,ini); //iniからO(N)構築 BIT bit(ll(),N,ini,e); //単位元e指定 BIT bit(ll(),N,ini,e,op); //op指定,逆演算sub無 一部操作不可となる BIT bit(ll(),N,ini,e,op,sub); //op指定,逆演算sub有 BIT bit(ll(),N,vll(),e); //iniを空vectorにすると全要素eで構築省略 //op=maxの例 BIT bit(ll(),N,ini,-INF,[](ll a,ll b){return max(a,b);}); //op=XORの例 BIT bit(ll(),N,ini, 0, [](ll a,ll b){return a^b;}, //op [](ll a,ll b){return a^b;}); //sub - ---- 操作 ---- bit.apply(i,x); //A[i]<-op(A[i],x) bit.set(i,x); //A[i]<-x sub有時のみ - ---- 取得 ---- [0,N)外は単位元として扱う T x=bit.get(i); //A[i] sub有時のみ T x=bit[i]; //A[i] sub有時のみ T x=bit.get(l,r); //[l,r] sub有時のみ T x=bit.prefix(r);//[0,r] - ---- BIT上二分探索 ---- op,subが加減算、x>=0、全Ai>=0が前提 auto [r,rem]=bit.floorGroup(l,x); //Al+A_{l+1}+…+Ar+rem=xなる最大のr auto [r,rem]=bit. ceilGroup(l,x); //Al+A_{l+1}+…+Ar=x+remなる最小のr ない時N auto [l,rem]=bit.floorGroupRev(r,x); //Ar+A_{r-1}+…+Al+rem=xなる最小のl auto [l,rem]=bit. ceilGroupRev(r,x); //Ar+A_{r-1}+…+Al=x+remなる最大のl ない時-1 */ namespace GridEnumerate{ using ll = long long; template MLL CutL(ll N,ll M,ll L,CMB &cmb){ if (L>min(N,M)) return MLL(0); return cmb(N+M,N)-cmb(N+M,L-1); } template MLL CutLK(ll N,ll M,ll L,ll K,CMB &cmb){ MLL ret=0; if (L>min(N,M) || K>min(N,M)) return ret; ll P=N+M-K-L+2; for (ll i= N%P; i<=N+M; i+=P) ret+=cmb(N+M,i); for (ll i=(K-1)%P; i<=N+M; i+=P) ret-=cmb(N+M,i); return ret; } template MLL turnKtimes___UpStart(ll N,ll M,ll K,CMB &cmb){ if (K==0) return M==0 ? 1 : 0; //上startでK=0のとき 横幅0なら1経路、1以上なら0経路 else {//K>=1のとき if (N==0 || M==0) return MLL(0); //幅0だと曲がれない→0 return cmb(N-1,K/2)*cmb(M-1,(K-1)/2); //右折K/2回、左折(K-1)/2回 } } template MLL turnKtimesRightStart(ll N,ll M,ll K,CMB &cmb){ if (K==0) return N==0 ? 1 : 0; //右startでK=0のとき 縦幅0なら1経路、1以上なら0経路 else{//K>=1のとき if (N==0 || M==0) return MLL(0); //幅0だと曲がれない→0 return cmb(N-1,(K-1)/2)*cmb(M-1,K/2); //右折(K-1)/2回、左折K/2回 } } template MLL turnKtimes(ll N,ll M,ll K,CMB &cmb){ MLL ret(0); if (N==0 && M==0 && K==0) return MLL(1); //始点終点一致の時の上/右start二重count回避 ret+=turnKtimes___UpStart(N,M,K,cmb); ret+=turnKtimesRightStart(N,M,K,cmb); return ret; } template MLL turnKtimesRightStartRightEndCutL(ll N,ll M,ll L,ll K,CMB &cmb){ if (K%2==1) return MLL(0); ll KK=(K+2)/2; return cmb(N-1,KK-2)*cmb(M-1,KK-1)-cmb(M+N-L-1,KK-2)*cmb(L-1,KK-1); } template MLL turnKtimesRightStart___UpEndCutL(ll N,ll M,ll L,ll K,CMB &cmb){ if (K%2==0) return MLL(0); ll KK=(K+1)/2; return cmb(N-1,KK-1)*cmb(M-1,KK-1)-cmb(M+N-L-1,KK-2)*cmb(L-1,KK); } template MLL turnKtimes___UpStartRightEndCutL(ll N,ll M,ll L,ll K,CMB &cmb){ if (K%2==0) return MLL(0); ll KK=(K+3)/2; return cmb(N-1,KK-2)*cmb(M-1,KK-2)-cmb(M+N-L-1,KK-2)*cmb(L-1,KK-2); } template MLL turnKtimes___UpStart___UpEndCutL(ll N,ll M,ll L,ll K,CMB &cmb){ if (K%2==1) return MLL(0); ll KK=(K+2)/2; return cmb(N-1,KK-1)*cmb(M-1,KK-2)-cmb(M+N-L-1,KK-2)*cmb(L-1,KK-1); } template MLL turnKtimesCutL(ll N,ll M,ll L,ll K,CMB &cmb){ MLL ret(0); if (L>min(N,M)) return ret; if (L<=0) return turnKtimes(N,M,K,cmb);//L=0だと合わないため別計算 ret+=turnKtimesRightStartRightEndCutL(N,M,L,K,cmb); ret+=turnKtimesRightStart___UpEndCutL(N,M,L,K,cmb); ret+=turnKtimes___UpStartRightEndCutL(N,M,L,K,cmb); ret+=turnKtimes___UpStart___UpEndCutL(N,M,L,K,cmb); return ret; } }//namespace using GridEnumerate::CutL; using GridEnumerate::turnKtimes; using GridEnumerate::CutLK; using GridEnumerate::turnKtimesCutL; /* N*Mグリッド辺上でのs→t経路数 -- 要件 -- - cmb(ll n,ll r)がbinom{n}{r}をMLL型で返すこと - cmb(ll n,ll r)がn≦M+N-1で動作すること - ------------------------- 欠損なし ------------------------- ┌┬┬┬┬┬t -- ├┼┼┼┼┼┤ ↑ ├┼┼┼┼┼┤ N ├┼┼┼┼┼┤ ↓ s┴┴┴┴┴┘ -- .←---M---→ combination cmb(M+N+10); mll x=turnKtimes(N,M,K,cmb); //K回曲がる - ----------------------- 左上斜め欠損 ----------------------- . N=4,M=7,L=3 || N=M=L=4 =====================||=============== .←L→┌┬┬┬t -- || ←-L-→t -- . ┌┼┼┼┼┤ ↑ || ┌┤ ↑ . ┌┼┼┼┼┼┤ N || ┌┼┤ N ┌┼┼┼┼┼┼┤ ↓ || ┌┼┼┤ ↓ s┴┴┴┴┴┴┘ -- || s┴┴┴┘ -- .←----M----→ || ←-M-→ combination cmb(M+N+10); mll x=CutL(N,M,L,cmb); //任意回曲がる mll x=turnKtimesCutL(N,M,L,K,cmb); //K回曲がる - --------------------- 左上&右下斜め欠損 -------------------- .←L→┌┬┬┬t -- . ┌┼┼┼┼┤ ↑ N=4,M=7,L=3,K=2 . ┌┼┼┼┼┼┘ N ┌┼┼┼┼┼┘ ↓ s┴┴┴┴┘-K- -- .←----M----→ combination cmb(M+N+10); mll x=CutLK(N,M,L,K,cmb); //任意回曲がる */ void cin2solve() { auto [N,Q]=cins(); auto S=cin1(); auto query=cinv>(Q); combination cmb(N+10); ;/* mll v=cmb(n,r); // nCr 負も可、n sgt(N); //i=0~N-1 v[0]~v[N-1]で初期化 auto f=[&](ll len)->mll{//カタラン数(len/2) if (len%2==1) return 0; len/=2; return cmb(len*2,len) * cmb.inv(len+1); }; { ll i=0; while (i<=N-1){ ll r=rpos(i); ll len=r-i+1; sgt.set(i,f(len)); i=r+1; } } auto Q1=[&](ll qi){ auto [ki,i]=query[qi]; i--; //---- セグ木から元を削除 //左 不要 //中 if (1<=i and S[i-1]!=S[i]){ sgt.set(i,1); } //右 if (i+1<=N-1 and S[i]!=S[i+1]){ sgt.set(i+1,1); } //---- セグ木に新値セット //左 if (1<=i){ ll l=lpos(i-1); if (S[i-1]==S[i]){//中左で止まるとき ll len=i-l; sgt.set(l,f(len)); } else if(i==N-1 or S[i]==S[i+1]){//中右で止まるとき ll len=i-l+1; sgt.set(l,f(len)); } else{//右までいくとき ll r=rpos(i+1); ll len=r-l+1; sgt.set(l,f(len)); } } //中 if (i==0 or S[i-1]==S[i]){ if (i+1<=N-1 and S[i]!=S[i+1]){//右までいくとき ll r=rpos(i+1); ll len=r-i+1; sgt.set(i,f(len)); } else{//中右で止まるとき sgt.set(i,f(1)); } } //右 if (i+1<=N-1 and S[i]==S[i+1]){ ll r=rpos(i+1); ll len=r-i; sgt.set(i+1,f(len)); } //---- S,bit if (S[i]=='Y'){ S[i]='N'; bitP.apply(i,-1); bitN.apply(i,1); } else{ S[i]='Y'; bitP.apply(i,1); bitN.apply(i,-1); } }; auto Q2=[&](ll qi){ auto [ki,K]=query[qi]; //右端だけ修正 ll l=lpos(N-1); ll len=N-l; ll h=abs(N-2*K); if ((len+h)%2==1){ cout << 0 << '\n'; return; } ll a=(len+h)/2; ll b=len-a; mll x=CutL(b/*N*/,a/*M*/,b/*L*/,cmb);//任意回曲がる sgt.set(l,x); mll ans=sgt.allprod(); cout << ans << '\n'; }; rep(qi,0,Q-1){ ll ki=get<0>(query[qi]); if (ki==1) Q1(qi); if (ki==2) Q2(qi); } return; } }//SolvingSpace ////////////////////////////////////////// int main(){ #if defined(RANDOM_TEST) SolvingSpace::cin2solve(); SolvingSpace::generand(); #else #if 1 //SolvingSpace::labo(); SolvingSpace::cin2solve(); #else ll t; cin >> t; rep(i,0,t-1){ SolvingSpace::cin2solve(); } #endif #endif cerr << timeget() <<"ms"<< '\n'; return 0; }