// https://github.com/yuki-4869/Algorithm/blob/main/Templates/multiple_test_cases.cpp #include #include using namespace std; using namespace atcoder; using ll=long long; using LL=__int128; using mint=modint998244353; using ld = long double; using pll=pair; using tu=tuple; using tu4 = tuple;using vl=vector; using vb=vector; using vvb=vector; using vvl=vector; using vs=vector; using vld=vector; using vc=vector; using vmi=vector; using vp = vector; using vt = vector;using graph = vvl; using Graph = vector; template auto vec2(ll n,ll m,T x=T()){return vector(n,vector(m,x));} template auto vec3(ll n,ll m,ll l,T x=T()){return vector(n,vector(m,vector(l,x)));} template auto vec4(ll a,ll b,ll c,ll d,T x=T()){return vector(a,vector(b,vector(c,vector(d,x))));} template using rpq=priority_queue,greater>; // 小さい順priority_queue using inverse_priority_queue=rpq; const ll Inf=2147483647LL, Inf9=1000000000LL, inf=9223372036854775807LL, inf18=1000000000000000000LL, mod=998244353LL; #define all(v) (v).begin(),(v).end() #define rall(v) (v).rbegin(),(v).rend() #define Re(n) for(ll i = 0; i<(ll)(n);i++) #define rep(i,n) for(ll i=0;i<(ll)(n);i++) #define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--) #define rep1(i,n) for(ll i=1;i<=(ll)(n);i++) #define rep3(i,s,t) for(ll i=(ll)(s);i<(ll)(t);i++) #define pb push_back #define p(s) cout<<(s)<<'\n' #define p2(s,t) cout<<(s)<<" "<<(t)<<'\n' #define p3(s,t,u) cout<<(s)<<" "<<(t)<<" "<<(u)<<'\n' #define p4(s,t,u,v) cout<<(s)<<" "<<(t)<<" "<<(u)<<" "<<(v)<<'\n' #define pe(s) cout<<(s)<std::ostream&{auto flags=std::cout.flags();auto precision=std::cout.precision();std::cout<(values)),...);std::cout<<'\n';std::cout.flags(flags);std::cout.precision(precision);return std::cout;}(__VA_ARGS__)) #define pld2(s,t) pld((s),(t)) #define pld3(s,t,u) pld((s),(t),(u)) #define pld4(s,t,u,v) pld((s),(t),(u),(v)) #define pe2(s,t) cout<<(s)<<" "<<(t)< inline ll sz(const T& x){return (ll)x.size();} // サイズ取得 ostream& operator<<(ostream& os,const mint& x){return os<>(istream& is,mint& x){ll v; is>>v; x=mint(v); return is;} // mint入力 template inline bool chmin(T& a,const T& b){if(a>b){a=b; return true;} return false;} // 最小値更新 template inline bool chmax(T& a,const T& b){if(a ll LB(const vector& v,const U& a){return lower_bound(all(v),a)-v.begin();} // lower_boundのindex template ll UB(const vector& v,const U& a){return upper_bound(all(v),a)-v.begin();} // upper_boundのindex template T vec_min(const vector& v){do { if (!(!v.empty())) std::abort(); } while (false); return *min_element(all(v));} // vectorの最小値 template T vec_max(const vector& v){do { if (!(!v.empty())) std::abort(); } while (false); return *max_element(all(v));} // vectorの最大値 template T vec_sum(const vector& v){T res=T(0); for(const auto& x:v) res+=x; return res;} // vectorの総和 template vector cin_vl(ll n){vector ret(n); rep(i,n) cin>>ret[i]; return ret;} // vector入力、型省略時ll template vector> cin_vvl(ll h,ll w){vector> ret(h,vector(w)); rep(i,h) rep(j,w) cin>>ret[i][j]; return ret;} // h*w入力、型省略時ll template pair,vector> cin_xy(ll n){vector x(n),y(n); rep(i,n) cin>>x[i]>>y[i]; return {x,y};} // x,y入力、型省略時ll template tuple,vector,vector> cin_xyz(ll n){vector x(n),y(n),z(n); rep(i,n) cin>>x[i]>>y[i]>>z[i]; return {x,y,z};} // x,y,z入力、型省略時ll template vector> Cin_vl(ll n,ll k){vector> ret(k,vector(n)); rep(i,n) rep(j,k) cin>>ret[j][i]; return ret;} // n行k列入力を列ごと保持、型省略時ll template vector> cin_pairs(ll n){vector> ret(n); rep(i,n) cin>>ret[i].first>>ret[i].second; return ret;} // pair列入力、型省略時ll template vector> cin_tuples(ll n){vector> ret(n); rep(i,n){T a,b,c; cin>>a>>b>>c; ret[i]={a,b,c};} return ret;} // 3要素tuple列入力、型省略時ll template void cout_vl(const vector& a,char sep=' ',char end='\n'){rep(i,sz(a)){if(i) cout< void cout_ver(const vector& a){for(const auto& value:a) cout< void Cout_vl(const vector& a,const vector& b){rep(i,sz(a)) cout< void cout_vvl(const vector>& a){for(const auto& e:a) cout_vl(e);} // 2次元vector出力 template void Swap(vector>& v,ll& h,ll& w){vector> ret(w,vector(h));rep(i,h) rep(j,w) ret[j][i]=v[i][j];v.swap(ret);swap(h,w);} // h*w行列をw*h行列に変換 hとwを自動でswap ll ceil_div(ll a,ll b){do { if (!(b!=0)) std::abort(); } while (false); if(b<0) a=-a,b=-b; if(a>=0) return (a+b-1)/b; return -((-a)/b);} // 切り上げ除算 ll floor_div(ll a,ll b){do { if (!(b!=0)) std::abort(); } while (false); if(b<0) a=-a,b=-b; if(a>=0) return a/b; return -((-a+b-1)/b);} // 切り下げ除算 ll isqrt(ll n){ll x=sqrtl(n); while((LL)(x+1)*(x+1)<=n) x++; while((LL)x*x>n) x--; return x;} // floor(sqrt(n)) template vector sort_unique_vec(vector v){sort(all(v)); v.erase(unique(all(v)),v.end()); return v;} // sortして重複削除した新vector template vector compress(const vector& v){auto xs=sort_unique_vec(v); vector ret; for(auto& x:v) ret.pb(LB(xs,x)); return ret;} // 座標圧縮後のindex列 template vector prefix_sum(const vector& v){vector s(sz(v)+1,0); rep(i,sz(v)) s[i+1]=s[i]+v[i]; return s;} // 1次元累積和 template vector> prefix_sum2(const vector>& a){int h=sz(a),w=sz(a[0]); vector> s(h+1,vector(w+1,0)); rep(i,h)rep(j,w)s[i+1][j+1]=s[i+1][j]+s[i][j+1]-s[i][j]+a[i][j]; return s;} // 2次元累積和 template T range_sum(const vector& s,ll l,ll r){return s[r]-s[l];} // 累積和から[l,r)の和 template vector> run_length(const vector& v){vector> ret; for(auto& x:v){if(ret.empty()||ret.back().first!=x) ret.pb({x,1}); else ret.back().second++;} return ret;} // ランレングス圧縮 vector> run_length(const string& s){vector> ret; for(char c:s){if(ret.empty()||ret.back().first!=c) ret.pb({c,1}); else ret.back().second++;} return ret;} // 文字列ランレングス圧縮 template ll argmin(const vector& v){do { if (!(!v.empty())) std::abort(); } while (false); return min_element(all(v))-v.begin();} // 最小値のindex template ll argmax(const vector& v){do { if (!(!v.empty())) std::abort(); } while (false); return max_element(all(v))-v.begin();} // 最大値のindex template bool in_vec(const vector& v,const T& x){return binary_search(all(v),x);} // sort済vectorに存在するか bool even(ll x){return x%2==0;} bool odd(ll x){return x%2!=0;} ll grid_id(ll x,ll y,ll w){return x*w+y;} // 座標からGridのID pll grid_xy(ll id,ll w){return {id/w,id%w};} // GridのIDから座標 ll gcd(ll a,ll b){if(b==0) return abs(a); return gcd(b,a%b);} // 最大公約数 ll lcm(ll a,ll b){return a/gcd(a,b)*b;} // 最小公倍数 ll Exp(ll x,ll y){ll cnt=-1; while(x>0) x/=y,cnt++; return cnt;} // y進桁数-1 ll powmod(ll x,ll k,ll m){if(k==0) return 1%m; ll r=powmod(x,k/2,m); r=(LL)r*r%m; if(k&1) r=(LL)r*x%m; return r;} // mod累乗 template T square(T x){return x*x;} // 二乗 template void Uni_erase(vector& v){sort(all(v)); v.erase(unique(all(v)),v.end());} // sortして重複削除 inline bool OutIn(ll x,ll n){return 0<=x&&x dist(l,r-1); return dist(rng);} // [l,r)乱数 void YesNo(bool x){x?p_yes():p_no();} // Yes/No出力 vl vl_n(ll n){vl ret(n); rep(i,n) ret[i]=i; return ret;} // 0..n-1のvector生成 vl vl_N(ll n){vl ret(n); rep(i,n) ret[i]=i+1; return ret;} // 1..nのvector生成 vl dijkstra(const graph& g,const vl& starts){ vl dist(g.size(),inf); queue q; for(ll s:starts) if(dist[s]==inf){dist[s]=0; q.push(s);} while(!q.empty()){ ll v=q.front(); q.pop(); for(ll to:g[v]) if(dist[to]==inf){dist[to]=dist[v]+1; q.push(to);} } return dist; } vl dijkstra(const graph& g,ll s){return dijkstra(g,vl{s});} vl dijkstra(const graph& g,initializer_list starts){return dijkstra(g,vl(starts));} vl dijkstra(const Graph& g,const vl& starts){ vl dist(g.size(),inf); rpq q; for(ll s:starts) if(dist[s]==inf){dist[s]=0; q.push({0,s});} while(!q.empty()){ auto [d,v]=q.top(); q.pop(); if(d!=dist[v]) continue; for(auto [to,w]:g[v]){ if(w>=inf-d) continue; ll nd=d+w; if(nd starts){return dijkstra(g,vl(starts));} const vl Pow2={1LL,2LL,4LL,8LL,16LL,32LL,64LL,128LL,256LL,512LL,1024LL,2048LL,4096LL,8192LL,16384LL,32768LL,65536LL,131072LL,262144LL,524288LL,1048576LL,2097152LL,4194304LL,8388608LL,16777216LL,33554432LL,67108864LL,134217728LL,268435456LL,536870912LL,1073741824LL,2147483648LL,4294967296LL,8589934592LL,17179869184LL,34359738368LL,68719476736LL,137438953472LL,274877906944LL,549755813888LL,1099511627776LL,2199023255552LL,4398046511104LL,8796093022208LL,17592186044416LL,35184372088832LL,70368744177664LL,140737488355328LL,281474976710656LL,562949953421312LL,1125899906842624LL,2251799813685248LL,4503599627370496LL,9007199254740992LL,18014398509481984LL,36028797018963968LL,72057594037927936LL,144115188075855872LL,288230376151711744LL,576460752303423488LL,1152921504606846976LL}; vl dx={0,1,0,-1}, dy={1,0,-1,0}; vl dx8={1,1,0,-1,-1,-1,0,1}, dy8={0,1,1,1,0,-1,-1,-1}; // 8近傍 vector fac,finv,pow2; void factorial(ll N=1000000LL){ll M=N+100; fac.assign(M+1,1); finv.assign(M+1,1); pow2.assign(M+1,1); rep1(i,M) fac[i]=fac[i-1]*i; finv[M]=fac[M].inv(); for(ll i=M;i>=1;i--) finv[i-1]=finv[i]*i; rep1(i,M) pow2[i]=pow2[i-1]*2;} // N+100まで階乗・逆階乗・2の累乗前計算 void ensure_factorial(ll N){if((ll)fac.size()>N) return; factorial(N);} // 階乗表の不足分確保 mint nCr(ll N,ll r){if(N<0||r<0||N(1u<<29)) std::abort(); return static_cast(n); } inline int checked_n(int n) { if(n<0) std::abort(); return checked_size(static_cast(n)); } inline std::vector zeros(int n) { return std::vector(static_cast(checked_n(n)),0); } } template class LazySegBase : private atcoder::lazy_segtree { using Tree=atcoder::lazy_segtree; int n_=0; static const std::vector& checked(const std::vector& a) { lazy_wrapper_detail::checked_size(a.size()); return a; } void check_point(int p) const { if(p<0 || p>=n_) std::abort(); } void check_range(int l,int r) const { if(l<0 || l>r || r>n_) std::abort(); } public: LazySegBase()=default; explicit LazySegBase(int n):Tree(lazy_wrapper_detail::checked_n(n)),n_(n){} explicit LazySegBase(const std::vector& a):Tree(checked(a)),n_(static_cast(a.size())){} LazySegBase(const LazySegBase&)=default; LazySegBase& operator=(const LazySegBase&)=default; LazySegBase(LazySegBase&& other) noexcept :Tree(std::move(static_cast(other))),n_(std::exchange(other.n_,0)){} LazySegBase& operator=(LazySegBase&& other) noexcept { if(this!=&other){Tree::operator=(std::move(static_cast(other)));n_=std::exchange(other.n_,0);} return *this; } int size() const {return n_;} bool empty() const {return n_==0;} void set(int p,S x){check_point(p);Tree::set(p,std::move(x));} S get(int p){check_point(p);return Tree::get(p);} S prod(int l,int r){check_range(l,r);return l==r?e():Tree::prod(l,r);} S all_prod() const {return n_?const_cast(this)->Tree::all_prod():e();} void apply(int p,F f){check_point(p);Tree::apply(p,std::move(f));} void apply(int l,int r,F f){check_range(l,r);if(l int max_right(int l){return max_right(l,g);} template int max_right(int l,G g){ check_range(l,l); if(!g(e()))std::abort(); return l==n_?n_:Tree::max_right(l,std::move(g)); } template int min_left(int r){return min_left(r,g);} template int min_left(int r,G g){ check_range(r,r); if(!g(e()))std::abort(); return r==0?0:Tree::min_left(r,std::move(g)); } }; struct LazyAssignF{long long x=0;bool set=false;}; constexpr long long LSEG_INF=(1LL<<62); namespace LazyAddMin_impl{ long long op(long long a,long long b){return std::min(a,b);} long long e(){return LSEG_INF;} long long mp(long long f,long long x){return x==e()?x:x+f;} long long cp(long long f,long long g){return f+g;} long long id(){return 0;} } struct LazyAddMin:LazySegBase{ using B=LazySegBase; using B::B; LazyAddMin(int n):B(lazy_wrapper_detail::zeros(n)){} LazyAddMin()=default; void add(int l,int r,long long x){B::apply(l,r,x);} long long min(int l,int r){return B::prod(l,r);} }; namespace LazyAddMax_impl{ long long op(long long a,long long b){return std::max(a,b);} long long e(){return -LSEG_INF;} long long mp(long long f,long long x){return x==e()?x:x+f;} long long cp(long long f,long long g){return f+g;} long long id(){return 0;} } struct LazyAddMax:LazySegBase{ using B=LazySegBase; using B::B; LazyAddMax(int n):B(lazy_wrapper_detail::zeros(n)){} LazyAddMax()=default; void add(int l,int r,long long x){B::apply(l,r,x);} long long max(int l,int r){return B::prod(l,r);} }; namespace LazySum_impl{ struct S{long long val,size;}; S op(S a,S b){return {a.val+b.val,a.size+b.size};} S e(){return {0,0};} std::vector cv(const std::vector&a){std::vectorv;for(long long x:a)v.push_back({x,1});return v;} } namespace LazyAddSum_impl{ using S=LazySum_impl::S; S mp(long long f,S x){return {x.val+f*x.size,x.size};} long long cp(long long f,long long g){return f+g;} long long id(){return 0;} } struct LazyAddSum:LazySegBase{ using S=LazySum_impl::S; using B=LazySegBase; using B::B;using B::set; LazyAddSum(int n):B(LazySum_impl::cv(lazy_wrapper_detail::zeros(n))){} LazyAddSum(const std::vector&a):B(LazySum_impl::cv(a)){} void set(int i,long long x){B::set(i,{x,1});} long long sum(int l,int r){return B::prod(l,r).val;} LazyAddSum()=default; using B::apply; void add(int l,int r,long long x){B::apply(l,r,x);} }; namespace LazyAssignMin_impl{ long long op(long long a,long long b){return std::min(a,b);} long long e(){return LSEG_INF;} long long mp(LazyAssignF f,long long x){return x==e()?x:(f.set?f.x:x);} LazyAssignF cp(LazyAssignF f,LazyAssignF g){return f.set?f:g;} LazyAssignF id(){return {};} } struct LazyAssignMin:LazySegBase{ using B=LazySegBase; using B::B;using B::apply; LazyAssignMin(int n):B(lazy_wrapper_detail::zeros(n)){} void apply(int l,int r,long long x){B::apply(l,r,{x,true});} LazyAssignMin()=default; void apply(int i,long long x){B::apply(i,LazyAssignF{x,true});} void assign(int l,int r,long long x){apply(l,r,x);} long long min(int l,int r){return B::prod(l,r);} }; namespace LazyAssignMax_impl{ long long op(long long a,long long b){return std::max(a,b);} long long e(){return -LSEG_INF;} long long mp(LazyAssignF f,long long x){return x==e()?x:(f.set?f.x:x);} LazyAssignF cp(LazyAssignF f,LazyAssignF g){return f.set?f:g;} LazyAssignF id(){return {};} } struct LazyAssignMax:LazySegBase{ using B=LazySegBase; using B::B;using B::apply; LazyAssignMax(int n):B(lazy_wrapper_detail::zeros(n)){} void apply(int l,int r,long long x){B::apply(l,r,{x,true});} LazyAssignMax()=default; void apply(int i,long long x){B::apply(i,LazyAssignF{x,true});} void assign(int l,int r,long long x){apply(l,r,x);} long long max(int l,int r){return B::prod(l,r);} }; namespace LazyAssignSum_impl{ using S=LazySum_impl::S; S mp(LazyAssignF f,S x){return f.set?S{f.x*x.size,x.size}:x;} LazyAssignF cp(LazyAssignF f,LazyAssignF g){return f.set?f:g;} LazyAssignF id(){return {};} } struct LazyAssignSum:LazySegBase{ using S=LazySum_impl::S; using B=LazySegBase; using B::B;using B::apply;using B::set; LazyAssignSum(int n):B(LazySum_impl::cv(lazy_wrapper_detail::zeros(n))){} LazyAssignSum(const std::vector&a):B(LazySum_impl::cv(a)){} void apply(int l,int r,long long x){B::apply(l,r,{x,true});} void set(int i,long long x){B::set(i,{x,1});} long long sum(int l,int r){return B::prod(l,r).val;} LazyAssignSum()=default; void apply(int i,long long x){B::apply(i,LazyAssignF{x,true});} void assign(int l,int r,long long x){apply(l,r,x);} }; namespace LazyMinMax_impl{ struct S{long long mx,mn;}; S op(S a,S b){return {std::max(a.mx,b.mx),std::min(a.mn,b.mn)};} S e(){return {-LSEG_INF,LSEG_INF};} std::vector cv(const std::vector&a){std::vectorv;for(long long x:a)v.push_back({x,x});return v;} } namespace LazyAddMinMax_impl{ using S=LazyMinMax_impl::S; S mp(long long f,S x){return x.mn==LSEG_INF?x:S{x.mx+f,x.mn+f};} long long cp(long long f,long long g){return f+g;} long long id(){return 0;} } struct LazyAddMinMax:LazySegBase{ using S=LazyMinMax_impl::S; using B=LazySegBase; using B::B;using B::set; LazyAddMinMax(int n):B(LazyMinMax_impl::cv(lazy_wrapper_detail::zeros(n))){} LazyAddMinMax(const std::vector&a):B(LazyMinMax_impl::cv(a)){} void set(int i,long long x){B::set(i,{x,x});} LazyAddMinMax()=default; using B::apply; void add(int l,int r,long long x){B::apply(l,r,x);} long long min(int l,int r){return B::prod(l,r).mn;} long long max(int l,int r){return B::prod(l,r).mx;} }; namespace LazyAssignMinMax_impl{ using S=LazyMinMax_impl::S; S mp(LazyAssignF f,S x){return x.mn==LSEG_INF?x:(f.set?S{f.x,f.x}:x);} LazyAssignF cp(LazyAssignF f,LazyAssignF g){return f.set?f:g;} LazyAssignF id(){return {};} } struct LazyAssignMinMax:LazySegBase{ using S=LazyMinMax_impl::S; using B=LazySegBase; using B::B;using B::apply;using B::set; LazyAssignMinMax(const std::vector&a):B(LazyMinMax_impl::cv(a)){} void apply(int l,int r,long long x){B::apply(l,r,{x,true});} void set(int i,long long x){B::set(i,{x,x});} LazyAssignMinMax()=default; explicit LazyAssignMinMax(int n):LazyAssignMinMax(lazy_wrapper_detail::zeros(n)){} void apply(int i,long long x){B::apply(i,LazyAssignF{x,true});} void assign(int l,int r,long long x){apply(l,r,x);} long long min(int l,int r){return B::prod(l,r).mn;} long long max(int l,int r){return B::prod(l,r).mx;} }; namespace LazyAddArgMax_impl{ struct S{long long val,id;}; S op(S a,S b){return a.val!=b.val?(a.val>b.val?a:b):(a.id{ using S=LazyAddArgMax_impl::S; using B=LazySegBase; static std::vector cv(const std::vector&a){std::vectorv;for(std::size_t i=0;i(i)});return v;} LazyAddArgMax(const std::vector&a):B(cv(a)){} void set(int i,long long x){B::set(i,{x,i});} LazyAddArgMax()=default; explicit LazyAddArgMax(int n):LazyAddArgMax(lazy_wrapper_detail::zeros(n)){} using B::apply; using B::set; void add(int l,int r,long long x){B::apply(l,r,x);} }; namespace LazyAddArgMin_impl{ struct S{long long val,id;}; S op(S a,S b){return a.val!=b.val?(a.val{ using S=LazyAddArgMin_impl::S; using B=LazySegBase; static std::vector cv(const std::vector&a){std::vectorv;for(std::size_t i=0;i(i)});return v;} LazyAddArgMin(const std::vector&a):B(cv(a)){} void set(int i,long long x){B::set(i,{x,i});} LazyAddArgMin()=default; explicit LazyAddArgMin(int n):LazyAddArgMin(lazy_wrapper_detail::zeros(n)){} using B::apply; using B::set; void add(int l,int r,long long x){B::apply(l,r,x);} }; namespace LazyChminMax_impl{ long long op(long long a,long long b){return std::max(a,b);} long long e(){return -LSEG_INF;} long long mp(long long f,long long x){return std::min(f,x);} long long cp(long long f,long long g){return std::min(f,g);} long long id(){return LSEG_INF;} } struct LazyChminMax:LazySegBase{ using B=LazySegBase; using B::B; LazyChminMax(int n):B(lazy_wrapper_detail::zeros(n)){} LazyChminMax()=default; long long max(int l,int r){return B::prod(l,r);} void chmin(int l,int r,long long x){B::apply(l,r,x);} }; namespace LazyChmaxMin_impl{ long long op(long long a,long long b){return std::min(a,b);} long long e(){return LSEG_INF;} long long mp(long long f,long long x){return std::max(f,x);} long long cp(long long f,long long g){return std::max(f,g);} long long id(){return -LSEG_INF;} } struct LazyChmaxMin:LazySegBase{ using B=LazySegBase; using B::B; LazyChmaxMin(int n):B(lazy_wrapper_detail::zeros(n)){} LazyChmaxMin()=default; long long min(int l,int r){return B::prod(l,r);} void chmax(int l,int r,long long x){B::apply(l,r,x);} }; namespace LazyAffineStats_impl{ struct S{long long sum,mx,mn,size;}; struct F{long long a=1,b=0;}; S op(S x,S y){ return {x.sum+y.sum,std::max(x.mx,y.mx),std::min(x.mn,y.mn),x.size+y.size}; } S e(){return {0,-LSEG_INF,LSEG_INF,0};} S mp(F f,S x){ if(!x.size)return x; long long sum=f.a*x.sum+f.b*x.size; if(f.a>=0)return {sum,f.a*x.mx+f.b,f.a*x.mn+f.b,x.size}; return {sum,f.a*x.mn+f.b,f.a*x.mx+f.b,x.size}; } F cp(F f,F g){return {f.a*g.a,f.a*g.b+f.b};} F id(){return {};} std::vector cv(const std::vector&a){ std::vectorv; for(long long x:a)v.push_back({x,x,x,1}); return v; } } struct LazyAffineStats:LazySegBase{ using S=LazyAffineStats_impl::S; using F=LazyAffineStats_impl::F; using B=LazySegBase; LazyAffineStats(int n):B(LazyAffineStats_impl::cv(lazy_wrapper_detail::zeros(n))){} LazyAffineStats(const std::vector&a):B(LazyAffineStats_impl::cv(a)){} void affine(int l,int r,long long a,long long b){B::apply(l,r,{a,b});} void add(int l,int r,long long x){affine(l,r,1,x);} void mul(int l,int r,long long x){affine(l,r,x,0);} void assign(int l,int r,long long x){affine(l,r,0,x);} void set(int i,long long x){B::set(i,{x,x,x,1});} long long sum(int l,int r){return B::prod(l,r).sum;} long long max(int l,int r){return B::prod(l,r).mx;} long long min(int l,int r){return B::prod(l,r).mn;} LazyAffineStats()=default; using B::apply; using B::set; }; template struct LazyAffineSumOps { struct S{Mint val;long long size;}; struct F{Mint a=1,b=0;}; static S op(S x,S y){return {x.val+y.val,x.size+y.size};} static S e(){return {0,0};} static S mp(F f,S x){return {f.a*x.val+f.b*x.size,x.size};} static F cp(F f,F g){return {f.a*g.a,f.a*g.b+f.b};} static F id(){return {};} template static std::vector cv(const std::vector& a){ std::vector result;result.reserve(a.size()); for(const auto& x:a)result.push_back({Mint(x),1}); return result; } }; template struct LazyAffineSumT:LazySegBase::S,LazyAffineSumOps::op,LazyAffineSumOps::e,typename LazyAffineSumOps::F,LazyAffineSumOps::mp,LazyAffineSumOps::cp,LazyAffineSumOps::id>{ using Ops=LazyAffineSumOps; using S=typename Ops::S; using F=typename Ops::F; using B=LazySegBase; using B::apply; using B::set; LazyAffineSumT()=default; explicit LazyAffineSumT(int n):B(Ops::cv(lazy_wrapper_detail::zeros(n))){} template explicit LazyAffineSumT(const std::vector& a):B(Ops::cv(a)){} void affine(int l,int r,Mint a,Mint b){B::apply(l,r,{a,b});} void add(int l,int r,Mint x){affine(l,r,1,x);} void mul(int l,int r,Mint x){affine(l,r,x,0);} void assign(int l,int r,Mint x){affine(l,r,0,x);} void set(int i,Mint x){B::set(i,{x,1});} Mint sum(int l,int r){return B::prod(l,r).val;} }; using LazyAffineSum=LazyAffineSumT; namespace LazyAffineSum_impl { using Ops=LazyAffineSumOps; using S=Ops::S;using F=Ops::F; inline S op(S a,S b){return Ops::op(a,b);} inline S e(){return Ops::e();} inline S mp(F f,S x){return Ops::mp(f,x);} inline F cp(F f,F g){return Ops::cp(f,g);} inline F id(){return Ops::id();} } namespace LazyFlipSum_impl{ struct S{long long one,size;}; S op(S a,S b){return {a.one+b.one,a.size+b.size};} S e(){return {0,0};} S mp(bool f,S x){return f?S{x.size-x.one,x.size}:x;} bool cp(bool f,bool g){return f^g;} bool id(){return false;} } struct LazyFlipSum:LazySegBase{ using S=LazyFlipSum_impl::S; using B=LazySegBase; static std::vector cv(const std::vector&a){std::vectorv;for(long long x:a)v.push_back({!!x,1});return v;} LazyFlipSum(int n):B(cv(lazy_wrapper_detail::zeros(n))){} LazyFlipSum(const std::vector&a):B(cv(a)){} void flip(int l,int r){B::apply(l,r,true);} void set(int i,long long x){B::set(i,{!!x,1});} long long ones(int l,int r){return B::prod(l,r).one;} LazyFlipSum()=default; using B::apply; using B::set; }; namespace LazyFlipInv_impl{ struct S{long long zero,one,inv;}; S op(S a,S b){ return {a.zero+b.zero,a.one+b.one,a.inv+b.inv+a.one*b.zero}; } S e(){return {0,0,0};} S mp(bool f,S x){ if(!f)return x; return {x.one,x.zero,x.zero*x.one-x.inv}; } bool cp(bool f,bool g){return f^g;} bool id(){return false;} } struct LazyFlipInv:LazySegBase{ using S=LazyFlipInv_impl::S; using B=LazySegBase; static std::vector cv(const std::vector&a){ std::vectorv; for(long long x:a)v.push_back(x?S{0,1,0}:S{1,0,0}); return v; } LazyFlipInv(int n):B(cv(lazy_wrapper_detail::zeros(n))){} LazyFlipInv(const std::vector&a):B(cv(a)){} void flip(int l,int r){B::apply(l,r,true);} void set(int i,long long x){B::set(i,x?S{0,1,0}:S{1,0,0});} long long inversions(int l,int r){return B::prod(l,r).inv;} LazyFlipInv()=default; using B::apply; using B::set; }; namespace LazyAPAddSum_impl{ struct S{long long sum,size,idxsum;}; struct F{long long a=0,b=0;}; S op(S x,S y){return {x.sum+y.sum,x.size+y.size,x.idxsum+y.idxsum};} S e(){return {0,0,0};} S mp(F f,S x){return {x.sum+f.a*x.idxsum+f.b*x.size,x.size,x.idxsum};} F cp(F f,F g){return {f.a+g.a,f.b+g.b};} F id(){return {};} } struct LazyAPAddSum:LazySegBase{ using S=LazyAPAddSum_impl::S; using F=LazyAPAddSum_impl::F; using B=LazySegBase; static std::vector cv(const std::vector&a){ std::vectorv; for(std::size_t i=0;i(i)}); return v; } LazyAPAddSum(int n):B(cv(lazy_wrapper_detail::zeros(n))){} LazyAPAddSum(const std::vector&a):B(cv(a)){} void add_linear(int l,int r,long long a,long long b){B::apply(l,r,{a,b});} void add_ap(int l,int r,long long a,long long step){add_linear(l,r,step,a-step*l);} void set(int i,long long x){B::set(i,{x,1,i});} long long sum(int l,int r){return B::prod(l,r).sum;} LazyAPAddSum()=default; using B::apply; using B::set; }; /* 細かい注意点 0-indexed、区間は[l,r)。0<=i seg(v); // 指定したACL modint型の一次変換 + 和。S={val,size}、F={a,b} LazyFlipSum seg(v); // 0/1反転 + 1の個数。S={one,size}、F=bool LazyFlipInv seg(v); // 0/1反転 + 転倒数。S={zero,one,inv}、F=bool LazyAPAddSum seg(v); // 等差数列加算 + 和。S={sum,size,idxsum}、F={a,b} // 構築・コピー・ムーブ(Typeは上記のクラス。otherも同じ型) Type seg; // 返り値なし: 長さ0の木を構築 O(1) Type seg(n); // 返り値なし: 長さn、全要素0で構築 O(N) Type seg(v); // 返り値なし: vから構築 O(N) Type seg(other); // 返り値なし: 独立なコピーを構築 O(N_other) Type seg(std::move(other)); // 返り値なし: 木を移し、otherを長さ0にする O(1) seg = other; // Type&: segへの参照。独立なコピーで上書き O(N+N_other) seg = std::move(other); // Type&: segへの参照。木を移し、otherを長さ0にする O(N+1) // 全型共通API(SとFの内容は上表。Sを直接setする場合は葉のメタデータも正しく設定) seg.size(); // int: 配列長Nを返す O(1) seg.empty(); // bool: 配列長0ならtrue O(1) seg.set(i,s); // void: 点iの集約情報をS=sに変更 O(log(N+1)) seg.get(i); // S: 点iの情報を返す(必ずしも整数そのものではない) O(log(N+1)) seg.prod(l,r); // S: [l,r)の集約情報を返す O(log(N+1)) seg.all_prod(); // S: 配列全体の集約情報を返す O(1) seg.apply(i,f); // void: 点iに更新F=fを作用 O(log(N+1)) seg.apply(l,r,f); // void: [l,r)に更新F=fを作用 O(log(N+1)) seg.max_right(l,g); // int: g(prod(l,r))を保つ最大rを返す O(log(N+1)) seg.min_left(r,g); // int: g(prod(l,r))を保つ最小lを返す O(log(N+1)) seg.max_right(l); // int: 関数ポインタ版のmax_right O(log(N+1)) seg.min_left(r); // int: 関数ポインタ版のmin_left O(log(N+1)) // 値だけを渡すset(集約型Sのメタデータを自動設定) seg.set(i,x); // void: 点iを値xに変更 O(log(N+1)) // 全ての具体型で利用可能。法付き型はMint、その他はlong longを渡す。 // 加算・代入型の便利API(型が対応する操作だけを利用) seg.add(l,r,x); // void: Add系・Affine系の[l,r)にx加算 O(log(N+1)) seg.assign(l,r,x); // void: Assign系・Affine系の[l,r)をxに代入 O(log(N+1)) seg.apply(i,x); // void: Assign系の点iを値xに代入 O(log(N+1)) seg.apply(l,r,x); // void: Assign系の[l,r)を値xに代入 O(log(N+1)) // 生の代入写像はLazyAssignF{x,true}。LazyAssignF{}は何もしない写像。 seg.chmin(l,r,x); // void: LazyChminMaxでA[i]=min(A[i],x) O(log(N+1)) seg.chmax(l,r,x); // void: LazyChmaxMinでA[i]=max(A[i],x) O(log(N+1)) seg.min(l,r); // long long: Min/MinMax/Stats型の区間最小値 O(log(N+1)) seg.max(l,r); // long long: Max/MinMax/Stats型の区間最大値 O(log(N+1)) seg.sum(l,r); // long longまたはMint: AddSum/AssignSum/Affine/Stats/AP型の区間和 O(log(N+1)) // 一次変換(LazyAffineStats / LazyAffineSum / LazyAffineSumT) seg.affine(l,r,a,b); // void: A[i]=a*A[i]+bを適用 O(log(N+1)) seg.mul(l,r,x); // void: A[i]をx倍 O(log(N+1)) // addはa=1、assignはa=0の一次変換。整数Stats型は負のaにも対応。 // 二進列(LazyFlipSum / LazyFlipInv。初期値・setの値は0/非0で0/1に正規化) seg.flip(l,r); // void: [l,r)の0/1を反転 O(log(N+1)) seg.ones(l,r); // long long: LazyFlipSumで区間の1の個数を返す O(log(N+1)) seg.inversions(l,r); // long long: LazyFlipInvでi: {値,元の添字}へ変換 O(N) LazyAddArgMax::cv(v); // vector: {値,元の添字}へ変換 O(N) LazyFlipSum::cv(v); // vector: {0/1の値,1}へ変換 O(N) LazyFlipInv::cv(v); // vector: {zero,one,0}へ変換 O(N) LazyAPAddSum::cv(v); // vector: {値,1,元の添字}へ変換 O(N) 基本の関数 LazyAddSum seg(vector{1,2,3}); // 構築 O(N) seg.apply(0,2,5); // [0,2)へ5加算 O(log N) auto result = seg.prod(0,3); // S={val=16,size=3} O(log N) seg.set(1,10); // 点代入 O(log N) auto value = seg.get(1); // S={val=10,size=1} O(log N) */ void solve(); signed main(){ ios::sync_with_stdio(false); cin.tie(nullptr); ll t; cin >> t; rep(i,t) solve(); } void solve(){ ll n; cin >> n; auto a = cin_vl(n); Sort(a); if(n == 1){ p(a[0]); return; } auto f = [&](ll val){ if(val == 0) return true; LazyAddMax seg(val+100); ll cnt = n; rrep(i,val){ seg.apply(i,1); ll k = (UB(a,i)-LB(a,i)); cnt -= min(k,seg.get(i)); seg.set(i,max(0LL,seg.get(i)-k)); if(i > 0){ if(i > 1) seg.apply(0,i,seg.get(i)); else seg.apply(0,seg.get(i)); if(seg.prod(0,i) > n) return false; } } return seg.get(0) <= cnt; }; ll ok = n+10, ng = 0; while(ok-ng>1){ ll mid = (ok+ng)/2; if(f(mid)) ng = mid; else ok = mid; } p(ng); }