#ifndef INCLUDE_MODE #define INCLUDE_MODE /* #define SUBMIT_ONLY */ #define DEBUG_OUTPUT #endif #ifdef INCLUDE_MAIN VO Solve() { CIN( int , N ); CIN_A( int , 0 , N , A ); --A; CIN( int , Q ); Trop<>::SetZero( -1e5 ); using U = ShapedMatrix>; U::SetShape( id(4) , vector(4,3) ); U::SetZero( Trop<>{} ); WHAT( U::size() ); U E{}; vector M( 4 , E ); FOR( i , 0 , 4 ){ E( i , i ) = 0; } FOR( i , 0 , 4 ){ FOR( j , 0 , 4 ){ FOR( k , j , 4 ){ M[i]( j , k ) = j <= i && i <= k ? 1 : 0; } } } auto UPower = [&]( const int& i , const U& a ){ return i == 0 ? E : Power( a , i - 1 , a ); }; vector X_pre( N ); FOR( i , 0 , N ){ X_pre[i] = M[A[i]]; } IntervalSetMonoidSqrtDecomposition X{ AbstractModule( 0 , UPower , MultiplicativeMonoid( E ) ) , move( X_pre ) }; FOR( q , 0 , Q ){ CIN( int , type ); if( type == 1 ){ CIN( ll , x , y ); x--; y--; auto a = X.IntervalProduct( x , y ); COUT( a( 0 , 3 ).Get() ); } else if( type == 2 ){ CIN( ll , x , y , n ); x--; y--; n--; X.IntervalSet( x , y , M[n] ); } } } REPEAT_MAIN(1); #else /* INCLUDE_MAIN */ #ifdef INCLUDE_SUB /* 圧縮時は中身だけ削除する。*/ IN VO Experiment() { } /* 圧縮時は中身だけ削除する。*/ IN VO SmallTest() { CERR( "全てのケースを確認しました。" ); } /* 圧縮時は中身だけ削除する。*/ IN VO RandomTest( const int& test_case_num ) { REPEAT( test_case_num ){ } CERR( "全てのケースを確認しました。" ); } #define INCLUDE_MAIN #include __FILE__ #else /* INCLUDE_SUB */ #ifdef INCLUDE_LIBRARY /* VVV 常設でないライブラリは以下に挿入する。*/ #ifdef DEBUG #include "c:/Users/user/Documents/Programming/Mathematics/Algebra/Monoid/Semirng/MeetSemirng/Implement/a_Body.hpp" #else TE CL Trop;TE CL ZeroForTrop{PU:ZeroForTrop()= delete;ST U g_zero;};TE CL Trop{PU:U m_n;IN Trop(CO U& n = ZeroForTrop::g_zero);IN Trop& OP+=(CO Trop& n);IN Trop& OP*=(CO Trop& n);IN Trop OP+(CO Trop& n)CO;IN Trop OP*(CO Trop& n)CO;IN VO Set(U n)NE;IN CO U& Get()CO NE;ST IN VO SetZero(U zero)NE;;}; TE U ZeroForTrop::g_zero{};TE IN Trop::Trop(CO U& n):m_n(n){}TE IN Trop& Trop::OP+=(CO Trop& n){n.m_n == ZeroForTrop::g_zero?m_n:m_n =(m_n == ZeroForTrop::g_zero?n.m_n:max(m_n,n.m_n));RE *TH;}TE IN Trop& Trop::OP*=(CO Trop& n){m_n == ZeroForTrop::g_zero?m_n:m_n =(n.m_n == ZeroForTrop::g_zero?ZeroForTrop::g_zero:m_n + n.m_n);RE *TH;}TE IN Trop Trop::OP+(CO Trop& n)CO{RE MO(Trop(*TH)+= n);}TE IN Trop Trop::OP*(CO Trop& n)CO{RE MO(Trop(*TH)*= n);}TE IN VO Trop::Set(U n)NE{m_n = MO(n);}TE IN CO U& Trop::Get()CO NE{RE m_n;}TE IN VO Trop::SetZero(U zero)NE{ZeroForTrop::g_zero = MO(zero);};TE IN basic_istream& OP>>(basic_ostream& is,CO Trop& n)NE{U temp;is >> temp;n.Set(temp);RE is;}TE IN basic_ostream& OP<<(basic_ostream& os,CO Trop& n)NE{RE os << n.Get();} #endif #define ShapedMA ShapedMatrix #ifdef DEBUG #include "c:/Users/user/Documents/Programming/Mathematics/LinearAlgebra/Shaped/a_Body.hpp" #else TE CL COantsForShapedMA{PU:ST int g_Y;ST VE g_l;ST VE g_r;ST VE g_d;ST VE> g_mult;ST T g_zero;COantsForShapedMA()= delete;}; TE int COantsForShapedMA::g_Y{};TE VE COantsForShapedMA::g_l{};TE VE COantsForShapedMA::g_r{};TE VE COantsForShapedMA::g_d{};TE VE> COantsForShapedMA::g_mult{};TE T COantsForShapedMA::g_zero{}; TE CL ShapedMA{PU:VE> m_M;IN ShapedMA()NE;IN ShapedMA(CO T& t)NE;IN ShapedMA(CO ShapedMA& mat)NE;IN ShapedMA(ShapedMA&& mat)NE;ST VO SetShape(VE l,VE r)NE;ST IN VO SetZero(T zero)NE;ST IN int SZ()NE;IN ShapedMA& OP=(CO ShapedMA& mat)NE = default;IN ShapedMA& OP=(ShapedMA&& mat)NE = default;IN ShapedMA& OP+=(CO ShapedMA& mat);IN ShapedMA& OP-=(CO ShapedMA& mat);IN ShapedMA& OP*=(CO T& scalar)NE;IN ShapedMA& OP*=(CO ShapedMA& mat);IN ShapedMA& OP/=(CO T& scalar);IN ShapedMA& OP%=(CO T& scalar);TE IN ShapedMA& OP^=(INT EX);IN ShapedMA OP+(CO ShapedMA& mat)CO;IN ShapedMA OP-(CO ShapedMA& mat)CO;IN ShapedMA OP*(CO T& scalar)CO NE;ShapedMA OP*(CO ShapedMA& mat)CO;IN ShapedMA OP/(CO T& scalar)CO;IN ShapedMA OP%(CO T& scalar)CO;TE IN ShapedMA OP^(INT EX)CO;IN CO T& OP()(CRI y,CRI x)CO;IN T& OP()(CRI y,CRI x);US COants = COantsForShapedMA;}; TE IN ShapedMA::ShapedMA()NE:m_M(COants::g_Y){for(int y = 0;y < COants::g_Y;y++){m_M[y].resize(COants::g_d[y],COants::g_zero);}}TE IN ShapedMA::ShapedMA(CO T& t)NE:ShapedMA(){for(int y = 0;y < COants::g_Y;y++){OP()(y,y)= t;}}TE IN ShapedMA::ShapedMA(CO ShapedMA& mat)NE:m_M(mat.m_M){}TE IN ShapedMA::ShapedMA(ShapedMA&& mat)NE:m_M(MO(mat.m_M)){}TE VO ShapedMA::SetShape(VE l,VE r)NE{AS((COants::g_Y = l.SZ())== int(r.SZ()));COants::g_l = MO(l);COants::g_r = MO(r);COants::g_d.clear();COants::g_mult.clear();for(int y = 0;y < COants::g_Y;y++){AS(COants::g_l[y]<= COants::g_r[y]+ 1);CO int x0 = COants::g_l[y],x1 = COants::g_r[y],X = x1 - x0 + 1;COants::g_d <<= X;for(int x = x0;x <= x1;x++){for(int z = x0;z <= x1;z++){if(COants::g_l[x]<= z && z <= COants::g_r[x]){COants::g_mult <<={y,x,z};}}}}RE;}TE IN VO ShapedMA::SetZero(T zero)NE{COants::g_zero = MO(zero);}TE IN int ShapedMA::SZ()NE{RE COants::g_mult.SZ();}TE IN ShapedMA& ShapedMA::OP+=(CO ShapedMA& mat){for(int y = 0;y < COants::g_Y;y++){CO int X = COants::g_d[y];for(int x = 0;x < X;x++){m_M[y][x]+= mat.m_M[y][x];}}RE *TH;}TE IN ShapedMA& ShapedMA::OP-=(CO ShapedMA& mat){for(int y = 0;y < COants::g_Y;y++){CO int X = COants::g_d[y];for(int x = 0;x < X;x++){m_M[y][x]-= mat.m_M[y][x];}}RE *TH;}TE IN ShapedMA& ShapedMA::OP*=(CO T& scalar)NE{for(int y = 0;y < COants::g_Y;y++){CO int X = COants::g_d[y];for(int x = 0;x < X;x++){m_M[y][x]*= scalar;}}RE *TH;}TE IN ShapedMA& ShapedMA::OP*=(CO ShapedMA& mat){RE OP=(MO(*TH * mat));}TE IN ShapedMA& ShapedMA::OP/=(CO T& scalar){RE OP*=(T(1)/ scalar);}TE IN ShapedMA& ShapedMA::OP%=(CO T& scalar){for(int y = 0;y < COants::g_Y;y++){CO int X = COants::g_d[y];for(int x = 0;x < X;x++){m_M[y][x]%= scalar;}}RE *TH;}TE TE IN ShapedMA& ShapedMA::OP^=(INT EX){RE *TH = Power(MO(*TH),MO(EX),ShapedMA(1));}TE IN ShapedMA ShapedMA::OP+(CO ShapedMA& mat)CO{RE MO(ShapedMA(*TH)+= mat);}TE IN ShapedMA ShapedMA::OP-(CO ShapedMA& mat)CO{RE MO(ShapedMA(*TH)-= mat);}TE IN ShapedMA ShapedMA::OP*(CO T& scalar)CO NE{RE MO(ShapedMA(*TH)*= scalar);}TE ShapedMA ShapedMA::OP*(CO ShapedMA& mat)CO{ShapedMA AN{};for(auto&[y,x,z]:COants::g_mult){AN.m_M[y][z-COants::g_l[y]]+= m_M[y][x-COants::g_l[y]]* mat.m_M[x][z-COants::g_l[x]];}RE AN;}TE IN ShapedMA ShapedMA::OP/(CO T& scalar)CO{RE MO(ShapedMA(*TH)/= scalar);}TE IN ShapedMA ShapedMA::OP%(CO T& scalar)CO{RE MO(ShapedMA(*TH)%= scalar);}TE TE IN ShapedMA ShapedMA::OP^(INT EX)CO{RE MO(ShapedMA(*TH)%= MO(EX));}TE IN CO T& ShapedMA::OP()(CRI y,CRI x)CO{AS(0 <= y && y < COants::g_Y && 0 <= x);RE COants::g_l[y]<= x && x <= COants::g_r[y]?m_M[y][x - COants::g_l[y]]:COants::g_zero;}TE IN T& ShapedMA::OP()(CRI y,CRI x){AS(0 <= y && y < COants::g_Y && COants::g_l[y]<= x && x <= COants::g_r[y]);RE m_M[y][x - COants::g_l[y]];} #endif #ifdef DEBUG #include "c:/Users/user/Documents/Programming/Mathematics/SetTheory/DirectProduct/AffineSpace/SqrtDecomposition/Monoid/IntervalSet/a_Body.hpp" #else CL SqrtDecompositionCoordinate{PU:int m_N;int m_N_sqrt;int m_N_d;int m_N_m;IN SqrtDecompositionCoordinate(CRI N = 0);IN SqrtDecompositionCoordinate(CRI N,CRI N_sqrt);IN CRI size()CO NE;IN CRI BucketSize()CO NE;IN CRI BucketCount()CO NE;}; IN SqrtDecompositionCoordinate::SqrtDecompositionCoordinate(CRI N):SqrtDecompositionCoordinate(N,RoundUpSqrt(N)){};IN SqrtDecompositionCoordinate::SqrtDecompositionCoordinate(CRI N,CRI N_sqrt):m_N(N),m_N_sqrt(N_sqrt),m_N_d((m_N + m_N_sqrt - 1)/ m_N_sqrt),m_N_m(m_N_d * m_N_sqrt){}IN CRI SqrtDecompositionCoordinate::size()CO NE{RE m_N;}IN CRI SqrtDecompositionCoordinate::BucketSize()CO NE{RE m_N_sqrt;}IN CRI SqrtDecompositionCoordinate::BucketCount()CO NE{RE m_N_d;} #define SFINAE_FOR_SD_S enable_if_t>* TE CL IntervalSetMonoidSqrtDecomposition:PU SqrtDecompositionCoordinate{PU:NON_COMM_N_MODULE m_M;VE m_a;VE m_b;VE m_lazy_substitution;VE m_suspended;TE IN IntervalSetMonoidSqrtDecomposition(NON_COMM_N_MODULE M,CRI N = 1,CO Args&... args);TE IntervalSetMonoidSqrtDecomposition(NON_COMM_N_MODULE M,VE a,CO Args&... args);TE IN VO Initialise(Args&&... args);IN VO Set(CRI i,CO U& u);IN VO IntervalSet(CRI i_start,CRI i_final,CO U& u);IN U OP[](CRI i);IN U Get(CRI i);IN U IntervalProduct(CRI i_start,CRI i_final);TE IN int Search(CRI i_start,CO F& f,CO bool& reversed = false);IN int Search(CRI i_start,CO U& u,CO bool& reversed = false);IN VO IntervalSet_Body(CRI i_min,CRI i_ulim,CO U& u);IN U IntervalProduct_Body(CRI i_min,CRI i_ulim);TE int Search_Body(CRI i_start,CO F& f,U sum_temp);TE int SearchReverse_Body(CRI i_final,CO F& f,U sum_temp);};TE IntervalSetMonoidSqrtDecomposition(NON_COMM_N_MODULE M,Args&&... args)-> IntervalSetMonoidSqrtDecomposition,NON_COMM_N_MODULE>; TE TE IN IntervalSetMonoidSqrtDecomposition::IntervalSetMonoidSqrtDecomposition(NON_COMM_N_MODULE M,CRI N,CO Args&... args):SqrtDecompositionCoordinate(N,args...),m_M(MO(M)),m_a(m_N_m,m_M.One()),m_b(m_N_d,m_M.One()),m_lazy_substitution(m_N_d,m_M.One()),m_suspended(m_N_d){ST_AS(! is_same_v && is_same_v>);}TE TE IntervalSetMonoidSqrtDecomposition::IntervalSetMonoidSqrtDecomposition(NON_COMM_N_MODULE M,VE a,CO Args&... args):SqrtDecompositionCoordinate(a.SZ(),args...),m_M(MO(M)),m_a(MO(a)),m_b(m_N_d,m_M.One()),m_lazy_substitution(m_N_d,m_M.One()),m_suspended(m_N_d){ST_AS(! is_same_v && is_same_v>);m_a.resize(m_N_m,m_M.One());int i_min = 0;int i_ulim = m_N_sqrt;for(int d = 0;d < m_N_d;d++){U& m_bd = m_b[d];for(int i = i_min;i < i_ulim;i++){m_bd = m_M.Product(MO(m_bd),m_a[i]);}i_min = i_ulim;i_ulim += m_N_sqrt;}}TE TE IN VO IntervalSetMonoidSqrtDecomposition::Initialise(Args&&... args){IntervalSetMonoidSqrtDecomposition temp{m_M,forward(args)...};SqrtDecompositionCoordinate::OP=(temp);m_a = MO(temp.m_a);m_b = MO(temp.m_b);m_lazy_substitution = MO(temp.m_lazy_substitution);m_suspended = MO(temp.m_suspended);}TE IN VO IntervalSetMonoidSqrtDecomposition::Set(CRI i,CO U& u){IntervalSet(i,i,u);}TE IN VO IntervalSetMonoidSqrtDecomposition::IntervalSet(CRI i_start,CRI i_final,CO U& u){CO int i_min = max(i_start,0);CO int i_ulim = min(i_final + 1,m_N);CO int d_0 =(i_min + m_N_sqrt - 1)/ m_N_sqrt;CO int d_1 = max(d_0,i_ulim / m_N_sqrt);CO int d_0_N_sqrt = d_0 * m_N_sqrt;CO int d_1_N_sqrt = d_1 * m_N_sqrt;CO int i_0 = min(d_0_N_sqrt,i_ulim);CO int i_1 = max(i_0,d_1_N_sqrt);if(i_min < i_0){CO int d_0_minus = d_0 - 1;CO int d_0_N_sqrt_minus = d_0_N_sqrt - m_N_sqrt;U& m_bd = m_b[d_0_minus];VE::reference m_suspended_d = m_suspended[d_0_minus];if(m_suspended_d){U& m_lazy_substitution_d = m_lazy_substitution[d_0_minus];IntervalSet_Body(d_0_N_sqrt_minus,i_min,m_lazy_substitution_d);IntervalSet_Body(i_min,i_0,u);IntervalSet_Body(i_0,d_0_N_sqrt,m_lazy_substitution_d);m_suspended_d = false;m_bd = m_M.Product(m_M.Product(m_M.Power(m_lazy_substitution_d,i_min - d_0_N_sqrt_minus),m_M.Power(u,i_0 - i_min)),m_M.Power(m_lazy_substitution_d,d_0_N_sqrt - i_0));}else{IntervalSet_Body(i_min,i_0,u);m_bd = m_M.Product(m_M.Product(IntervalProduct_Body(d_0_N_sqrt_minus,i_min),m_M.Power(u,i_0 - i_min)),IntervalProduct_Body(i_0,d_0_N_sqrt));}}CO U pw = m_M.Power(u,m_N_sqrt);for(int d = d_0;d < d_1;d++){m_b[d]= pw;m_lazy_substitution[d]= u;m_suspended[d]= true;}if(i_1 < i_ulim){CO int d_1_N_sqrt_plus = d_1_N_sqrt + m_N_sqrt;U& m_bd = m_b[d_1];VE::reference m_suspended_d = m_suspended[d_1];if(m_suspended_d){U& m_lazy_substitution_d = m_lazy_substitution[d_1];IntervalSet_Body(d_1_N_sqrt,i_1,m_lazy_substitution_d);IntervalSet_Body(i_1,i_ulim,u);IntervalSet_Body(i_ulim,d_1_N_sqrt_plus,m_lazy_substitution_d);m_suspended_d = false;m_bd = m_M.Product(m_M.Product(m_M.Power(m_lazy_substitution_d,i_1 - d_1_N_sqrt),m_M.Power(u,i_ulim - i_1)),m_M.Power(m_lazy_substitution_d,d_1_N_sqrt_plus - i_ulim));}else{IntervalSet_Body(i_1,i_ulim,u);m_bd = m_M.Product(m_M.Product(IntervalProduct_Body(d_1_N_sqrt,i_1),m_M.Power(u,i_ulim - i_1)),IntervalProduct_Body(i_ulim,d_1_N_sqrt_plus));}}RE;}TE IN VO IntervalSetMonoidSqrtDecomposition::IntervalSet_Body(CRI i_min,CRI i_ulim,CO U& u){for(int i = i_min;i < i_ulim;i++){m_a[i]= u;}RE;}TE IN U IntervalSetMonoidSqrtDecomposition::OP[](CRI i){AS(0 <= i && i < m_N);CO int d = i / m_N_sqrt;RE m_suspended[d]?m_lazy_substitution[d]:m_a[i];}TE IN U IntervalSetMonoidSqrtDecomposition::Get(CRI i){RE OP[](i);}TE IN U IntervalSetMonoidSqrtDecomposition::IntervalProduct(CRI i_start,CRI i_final){CO int i_min = max(i_start,0);CO int i_ulim = min(i_final + 1,m_N);CO int d_0 =(i_min + m_N_sqrt - 1)/ m_N_sqrt;CO int d_1 = max(d_0,i_ulim / m_N_sqrt);CO int i_0 = min(d_0 * m_N_sqrt,i_ulim);CO int i_1 = max(i_0,d_1 * m_N_sqrt);U AN = m_M.One();if(i_min < i_0){CO int d_0_minus = d_0 - 1;AN = m_suspended[d_0_minus]?m_M.Power(m_lazy_substitution[d_0_minus],i_0 - i_min):IntervalProduct_Body(i_min,i_0);}for(int d = d_0;d < d_1;d++){AN = m_M.Product(MO(AN),m_b[d]);}if(i_1 < i_ulim){AN = m_M.Product(MO(AN),m_suspended[d_1]?m_M.Power(m_lazy_substitution[d_1],i_ulim - i_1):IntervalProduct_Body(i_1,i_ulim));}RE AN;}TE IN U IntervalSetMonoidSqrtDecomposition::IntervalProduct_Body(CRI i_min,CRI i_ulim){U AN = m_M.One();for(int i = i_min;i < i_ulim;i++){AN = m_M.Product(MO(AN),m_a[i]);}RE AN;}TE TE IN int IntervalSetMonoidSqrtDecomposition::Search(CRI i_start,CO F& f,CO bool& reversed){RE reversed?SearchReverse_Body(i_start,f,m_M.One()):Search_Body(i_start,f,m_M.One());}TE IN int IntervalSetMonoidSqrtDecomposition::Search(CRI i_start,CO U& u,CO bool& reversed){RE Search(i_start,[&](CO U& product,CRI){RE !(product < u);},reversed);}TE TE int IntervalSetMonoidSqrtDecomposition::Search_Body(CRI i_start,CO F& f,U product_temp){CO int i_min = max(i_start,0);CO int d_0 = i_min / m_N_sqrt + 1;CO int i_0 = min(d_0 * m_N_sqrt,m_N);if(i_min < i_0){CO int d_0_minus = d_0 - 1;if(m_suspended[d_0_minus]){CO U& m_lazy_substitution_d = m_lazy_substitution[d_0_minus];U product_next = m_M.Product(product_temp,m_lazy_substitution_d);if(f(product_next,i_min)){RE i_min;}int l = i_min,r = i_0;WH(l + 1 < r){int m =(l + r)>> 1;product_next = m_M.Product(product_temp,m_M.Power(m_lazy_substitution_d,m - i_min + 1));(f(product_next,m)?r:l)= m;}if(r < i_0){RE r;}product_temp = MO(product_next);}else{for(int i = i_min;i < i_0;i++){product_temp = m_M.Product(MO(product_temp),m_a[i]);if(f(product_temp,i)){RE i;}}}}int N_sqrt_d = m_N_sqrt * d_0;for(int d = d_0;d < m_N_d;d++,N_sqrt_d += m_N_sqrt){CO int j_rest = d + 1 < m_N_d?m_N_sqrt:m_N - N_sqrt_d;U product_next = m_M.Product(product_temp,m_suspended[d]?m_M.Power(m_lazy_substitution[d],j_rest):m_b[d]);if(f(product_next,N_sqrt_d + j_rest - 1)){RE Search_Body(N_sqrt_d,f,MO(product_temp));}product_temp = MO(product_next);}RE -1;}TE TE int IntervalSetMonoidSqrtDecomposition::SearchReverse_Body(CRI i_final,CO F& f,U product_temp){CO int i_max = min(i_final,m_N - 1);CO int d_1 = i_max / m_N_sqrt;CO int i_1 = max(d_1 * m_N_sqrt,0);if(m_suspended[d_1]){CO U& m_lazy_substitution_d = m_lazy_substitution[d_1];U product_next = m_M.Product(product_temp,m_lazy_substitution_d);if(f(product_next,i_max)){RE i_max;}int l = i_1,r = i_max;WH(l + 1 < r){int m =(l + r)>> 1;product_next = m_M.Product(m_M.Power(m_lazy_substitution_d,i_max - m + 1),product_temp);(f(product_next,m)?l:r)= m;}if(i_1 < l){RE l;}product_temp = MO(product_next);}else{for(int i = i_max;i >= i_1;i--){product_temp = m_M.Product(m_a[i],product_temp);if(f(product_temp,i)){RE i;}}}for(int d = d_1 - 1;d >= 0;d--){U product_next = m_M.Product(m_suspended[d]?m_M.Power(m_lazy_substitution[d],m_N_sqrt):m_b[d],product_temp);if(f(product_next,d * m_N_sqrt)){RE Search_Body((d + 1)* m_N_sqrt - 1,f,MO(product_temp));}product_temp = MO(product_next);}RE -1;} #endif /* AAA 常設でないライブラリは以上に挿入する。*/ #define INCLUDE_SUB #include __FILE__ #else /* INCLUDE_LIBRARY */ #ifdef DEBUG #define _GLIBCXX_DEBUG #else #pragma GCC optimize ( "O3" ) #pragma GCC optimize ( "unroll-loops" ) #pragma GCC target ( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) #define DEXPR( LL , BOUND , VALUE1 , VALUE2 ) CEXPR( LL , BOUND , VALUE1 ) #define ASSERT( A , MIN , MAX ) AS( ( MIN ) <= A && A <= ( MAX ) ) #define REPEAT_MAIN( BOUND ) START_MAIN; CEXPR( int , test_case_num_bound , BOUND ); int test_case_num = 1; if CE( test_case_num_bound > 1 ){ FINISH_MAIN #ifdef USE_GETLINE #define SET_SEPARATE( SEPARATOR , ... ) VariadicGetline( cin , SEPARATOR , __VA_ARGS__ ) #define SET( ... ) SET_SEPARATE( '\n' , __VA_ARGS__ ) #define GETLINE_SEPARATE( SEPARATOR , ... ) string __VA_ARGS__; SET_SEPARATE( SEPARATOR , __VA_ARGS__ ) #define GETLINE( ... ) GETLINE_SEPARATE( '\n' , __VA_ARGS__ ) #define FINISH_MAIN GETLINE( test_case_num_str ); test_case_num = stoi( test_case_num_str ); ASSERT( test_case_num , 1 , test_case_num_bound ); } REPEAT( test_case_num ){ Solve(); } } #else #define SET( ... ) VariadicCin( cin , __VA_ARGS__ ) #define CIN( LL , ... ) LL __VA_ARGS__; SET( __VA_ARGS__ ) #define SET_A( I , N , ... ) VariadicResize( N + I , __VA_ARGS__ ); FOR( VARIABLE_FOR_SET_A , 0 , N ){ VariadicSet( cin , VARIABLE_FOR_SET_A + I , __VA_ARGS__ ); } #define CIN_A( LL , I , N , ... ) VE __VA_ARGS__; SET_A( I , N , __VA_ARGS__ ) #define CIN_AA( LL , I0 , N0 , I1 , N1 , VAR ) VE VAR( N0 + I0 , VE( N1 + I1 ) ); FOR( VARIABLE_FOR_CIN_AA , 0 , N0 ){ SET_A( I1 , N1 , VAR[VARIABLE_FOR_CIN_AA + I0] ); } #define FINISH_MAIN SET_ASSERT( test_case_num , 1 , test_case_num_bound ); } REPEAT( test_case_num ){ Solve(); } } #endif #define SET_ASSERT( A , MIN , MAX ) SET( A ); ASSERT( A , MIN , MAX ) #define SOLVE_ONLY #define COUT( ... ) VariadicCout( cout , __VA_ARGS__ ) << ENDL #define COUTNS( ... ) VariadicCoutNonSep( cout , __VA_ARGS__ ) #define CERR( ... ) #define CERRNS( ... ) #define COUT_A( I , N , A ) CoutArray( cout , I , N , A ) << ENDL #define CERR_A( I , N , A ) #define WHAT( ... ) #define TLE( CONDITION ) if( !( CONDITION ) ){ cerr << "TLE" << endl; ll TLE_VAR = 1; while( TLE_VAR % 2 != 0 ){ ( TLE_VAR += 2 ) %= int( 1e9 ); } cerr << TLE_VAR << endl; } #define MLE( CONDITION ) if( !( CONDITION ) ){ cerr << "MLE" << endl; vector> MLE_VAR{}; REPEAT( 1e6 ){ MLE_VAR.push_back( vector( 1e6 ) ); } cerr << MLE_VAR << endl; } #define OLE( CONDITION ) if( !( CONDITION ) ){ cerr << "OLE" << endl; REPEAT( 1e8 ){ COUT( "OLE" ); } } #endif #ifdef REACTIVE #ifndef DEBUG #define LOCAL( ... ) #define RSET( A , ... ) SET( A ) #endif #define RCIN( LL , A , ... ) LL A; RSET( A , __VA_ARGS__ ) #define ENDL endl #else #define ENDL "\n" #endif #include using namespace std; #define ATT __attribute__( ( target( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) ) ) #define START_MAIN int main(){ ios_base::sync_with_stdio( false ); cin.tie( nullptr ) #define START_WATCH chrono::system_clock::time_point watch = chrono::system_clock::now(); double loop_average_time = 0.0 , loop_start_time = loop_average_time , current_time = loop_start_time; int loop_count = current_time; assert( loop_count == 0 ) #define CURRENT_TIME ( current_time = static_cast( chrono::duration_cast( chrono::system_clock::now() - watch ).count() / 1000.0 ) ) #define CHECK_WATCH( TL_MS ) ( CURRENT_TIME , loop_count == 0 ? loop_start_time = current_time : loop_average_time = ( current_time - loop_start_time ) / loop_count , ++loop_count , current_time < TL_MS - loop_average_time * 2 - 100.0 ) #define CEXPR( LL , BOUND , VALUE ) CE LL BOUND = VALUE #define SET_A_ASSERT( I , N , A , MIN , MAX ) FOR( VARIABLE_FOR_SET_A , 0 , N ){ SET_ASSERT( A[VARIABLE_FOR_SET_A + I] , MIN , MAX ); } #define SET_AA_ASSERT( I0 , N0 , I1 , N1 , A , MIN , MAX ) FOR( VARIABLE_FOR_SET_AA0 , 0 , N0 ){ FOR( VARIABLE_FOR_SET_AA1 , 0 , N1 ){ SET_ASSERT( A[VARIABLE_FOR_SET_AA0 + I0][VARIABLE_FOR_SET_AA1 + I1] , MIN , MAX ); } } #define CIN_ASSERT( A , MIN , MAX ) decldecay_t( MAX ) A; SET_ASSERT( A , MIN , MAX ) #define CIN_A_ASSERT( I , N , A , MIN , MAX ) vector A( N + I ); SET_A_ASSERT( I , N , A , MIN , MAX ) #define CIN_AA_ASSERT( I0 , N0 , I1 , N1 , A , MIN , MAX ) vector A( N0 + I0 , vector( N1 + I1 ) ); SET_AA_ASSERT( I0 , N0 , I1 , N1 , A , MIN , MAX ) #define PR1( A1 , ... ) A1 #define PR2( A1 , A2 , ... ) A2 #define PR3( A1 , A2 , A3 , ... ) A3 #define FOR_( VAR , INITIAL , FINAL , UPPER , COMP , INCR ) for( decldecay_t( UPPER ) VAR = INITIAL ; VAR COMP ( FINAL ) ; VAR INCR ) #define FOR( VAR , INITIAL , ... ) FOR_( VAR , INITIAL , PR1( __VA_ARGS__ ) , PR1( __VA_ARGS__ ) , < , PR3( __VA_ARGS__ , += PR2( __VA_ARGS__ , ? ) , ++ ) ) #define FOREQ( VAR , INITIAL , ... ) FOR_( VAR , INITIAL , PR1( __VA_ARGS__ ) , PR1( __VA_ARGS__ ) , <= , PR3( __VA_ARGS__ , += PR2( __VA_ARGS__ , ? ) , ++ ) ) #define FOREQINV( VAR , INITIAL , ... ) FOR_( VAR , INITIAL , PR1( __VA_ARGS__ ) , INITIAL , + 1 > , PR3( __VA_ARGS__ , -= PR2( __VA_ARGS__ , ? ) , -- ) ) #define ITR( ARRAY ) auto begin_ ## ARRAY = ARRAY .BE() , itr_ ## ARRAY = begin_ ## ARRAY , end_ ## ARRAY = ARRAY .EN() #define FOR_ITR( ARRAY ) for( ITR( ARRAY ) , itr = itr_ ## ARRAY ; itr_ ## ARRAY != end_ ## ARRAY ; itr_ ## ARRAY ++ , itr++ ) #define RUN( ARRAY , ... ) for( auto&& __VA_ARGS__ : ARRAY ) #define REPEAT( HOW_MANY_TIMES ) FOR( VARIABLE_FOR_REPEAT , 0 , HOW_MANY_TIMES ) #define SET_PRECISION( DECIMAL_DIGITS ) cout << fixed << setprecision( DECIMAL_DIGITS ); cerr << fixed << setprecision( DECIMAL_DIGITS ) #define RETURN( ... ) SOLVE_ONLY; COUT( __VA_ARGS__ ); RE #define COMPARE( ... ) auto naive = Naive( __VA_ARGS__ , false ); auto answer = Answer( __VA_ARGS__ , false ); bool match = naive == answer; CERR( "(" , #__VA_ARGS__ , ") == (" , __VA_ARGS__ , ") : Naive ==" , naive , match ? "==" : "!=" , answer , "== Answer" ); if( !match ){ CERR( "出力の不一致が検出されました。" ); RE; } #define CHECK( ... ) auto answer = Answer( __VA_ARGS__ , false ); CERR( "(" , #__VA_ARGS__ , ") == (" , __VA_ARGS__ , ") : Answer == " , answer ) /* 圧縮用 */ #define TE template #define TY typename #define US using #define ST static #define AS assert #define IN inline #define CL class #define PU public #define OP operator #define CE constexpr #define CO const #define NE noexcept #define RE return #define WH while #define VO void #define VE vector #define LI list #define BE begin #define EN end #define SZ size #define LE length #define MO move #define TH this #define CRI CO int& #define CRUI CO uint& #define CRL CO ll& #define VI virtual #define IS basic_istream #define OS basic_ostream #define ST_AS static_assert #define reMO_CO remove_const #define is_COructible_v is_constructible_v #define rBE rbegin /* 型のエイリアス */ #define decldecay_t(VAR)decay_t TE US ret_t = decltype(declval()(declval()...)); TE US inner_t = TY T::type; US uint = unsigned int; US ll = long long; US ull = unsigned long long; US ld = long double; US lld = __float128; /* VVV 常設ライブラリは以下に挿入する。*/ #ifdef DEBUG #include "C:/Users/user/Documents/Programming/Contest/Template/Local/a_Body.hpp" US MP = DMod; #else /* BinarySearch (2KB)*/ CE bool reactive = #ifdef REACTIVE true; #else false; #endif /* Random (1KB)*/ ll GetRand(CRL Rand_min,CRL Rand_max){AS(Rand_min <= Rand_max);ll AN = time(NULL);RE AN * rand()%(Rand_max + 1 - Rand_min)+ Rand_min;} /* Set (2KB)*/ #define DC_OF_HASH(...)struct hash<__VA_ARGS__>{IN size_t OP()(CO __VA_ARGS__& n)CO;}; CL is_ordered{PU:is_ordered()= delete;TE ST CE auto Check(CO T& t)-> decltype(t < t,true_type());ST CE false_type Check(...);TE ST CE CO bool value = is_same_v< decltype(Check(declval())),true_type >;}; TE US Set = conditional_t>,unordered_set,conditional_t,set,VO>>; #define DF_OF_POP_FOR_SET(SET)TE IN T pop_max(SET& S){AS(!S.empty());auto IT = --S.EN();T AN = *IT;S.erase(IT);RE AN;}TE IN T pop_min(SET& S){AS(!S.empty());auto IT = S.BE();T AN = *IT;S.erase(IT);RE AN;}TE IN SET& OP<<=(SET& S,T t){S.insert(MO(t));RE S;}TE IN SET& OP<<=(SET& S,U&& u){S.insert(T{forward(u)});RE S;}TE IN SET& OP>>=(SET& S,CO T& t){auto IT = S.find(t);if(IT != S.EN()){S.erase(IT);}RE S;}TE IN SET& OP>>=(SET& S,CO U& u){RE S >>= T{u};}TE IN CO T& Get(CO SET& S,int i){auto BE = S.BE(),EN = S.EN();auto& IT = i < 0?(++i,--EN):BE;WH(i > 0 && IT != EN){--i;++IT;}WH(i < 0 && IT != BE){++i;--IT;}AS(i == 0);RE *IT;} #define DF_OF_UNION_FOR_SET(SET)TE IN SET& OP|=(SET& S0,SET S1){S0.merge(MO(S1));RE S0;}TE IN SET OP|(SET S0,SET S1){RE MO(S0.SZ()< S1.SZ()?S1 |= MO(S0):S0 |= MO(S1));} TE IN TY SET::const_iterator MaximumLeq(CO SET& S,CO T& t){auto IT = S.upper_bound(t);RE IT == S.BE()?S.EN():--IT;}TE IN TY SET::const_iterator MaximumLt(CO SET& S,CO T& t){auto IT = S.lower_bound(t);RE IT == S.BE()?S.EN():--IT;}TE IN TY SET::const_iterator MinimumGeq(CO SET& S,CO T& t){RE S.lower_bound(t);}TE IN TY SET::const_iterator MinimumGt(CO SET& S,CO T& t){RE S.upper_bound(t);}TE IN VO EraseBack(SET& S,ITERATOR& IT){IT = S.erase(IT);}TE IN VO EraseFront(SET& S,ITERATOR& IT){IT = S.erase(IT);IT == S.BE()?IT = S.EN():--IT;}TE TY SET,TY T,TY...Args> IN bool In(CO T& t,CO SET& S){RE S.count(t)> 0;}DF_OF_POP_FOR_SET(set);DF_OF_POP_FOR_SET(unordered_set);DF_OF_POP_FOR_SET(multiset);DF_OF_POP_FOR_SET(unordered_multiset);DF_OF_UNION_FOR_SET(set);DF_OF_UNION_FOR_SET(unordered_set);DF_OF_UNION_FOR_SET(multiset);DF_OF_UNION_FOR_SET(unordered_multiset);DF_OF_UNION_FOR_SET(VE);DF_OF_UNION_FOR_SET(LI); /* Tuple (6KB)*/ #define DF_OF_AR_FOR_TUPLE(OPR)TE TY PAIR> IN auto OP OPR ## =(PAIR& t0,CO PAIR& t1)-> decltype((get<0>(t0),t0))&{get<0>(t0)OPR ## = get<0>(t1);get<1>(t0)OPR ## = get<1>(t1);RE t0;}TE TY TUPLE> IN auto OP OPR ## =(TUPLE& t0,CO TUPLE& t1)-> decltype((get<0>(t0),t0))&{get<0>(t0)OPR ## = get<0>(t1);get<1>(t0)OPR ## = get<1>(t1);get<2>(t0)OPR ## = get<2>(t1);RE t0;}TE TY TUPLE> IN auto OP OPR ## =(TUPLE& t0,CO TUPLE& t1)-> decltype((get<0>(t0),t0))&{get<0>(t0)OPR ## = get<0>(t1);get<1>(t0)OPR ## = get<1>(t1);get<2>(t0)OPR ## = get<2>(t1);get<3>(t0)OPR ## = get<3>(t1);RE t0;}TE TY PAIR> IN auto OP OPR ## =(PAIR& t0,CO ARG& t1)-> decltype((get<0>(t0),t0))&{get<0>(t0)OPR ## = t1;get<1>(t0)OPR ## = t1;RE t0;}TE TY TUPLE> IN auto OP OPR ## =(TUPLE& t0,CO ARG& t1)-> decltype((get<0>(t0),t0))&{get<0>(t0)OPR ## = t1;get<1>(t0)OPR ## = t1;get<2>(t0)OPR ## = t1;RE t0;}TE TY TUPLE> IN auto OP OPR ## =(TUPLE& t0,CO ARG& t1)-> decltype((get<0>(t0),t0))&{get<0>(t0)OPR ## = t1;get<1>(t0)OPR ## = t1;get<2>(t0)OPR ## = t1;get<3>(t0)OPR ## = t1;RE t0;}TE TY TUPLE,TY...ARGS,TY ARG> IN auto OP OPR(CO TUPLE& t0,CO ARG& t1)-> decldecay_t((get<0>(t0),t0)){auto t = t0;RE MO(t OPR ## = t1);} #define DF_OF_INCREMENT_FOR_TUPLE(INCR)TE TY PAIR> IN auto OP INCR(PAIR& t)-> decltype((get<0>(t),t))&{INCR get<0>(t);INCR get<1>(t);RE t;}TE TY TUPLE> IN auto OP INCR(TUPLE& t)-> decltype((get<0>(t),t))&{INCR get<0>(t);INCR get<1>(t);INCR get<2>(t);RE t;}TE TY TUPLE> IN auto OP INCR(TUPLE& t)-> decltype((get<0>(t),t))&{INCR get<0>(t);INCR get<1>(t);INCR get<2>(t);INCR get<3>(t);RE t;} TE IN IS& OP>>(IS& is,tuple& arg){RE is >> get<0>(arg);}TE TY V> IN auto OP>>(IS& is,V& arg)-> decltype((get<0>(arg),is))&{RE is >> get<0>(arg)>> get<1>(arg);}TE IN IS& OP>>(IS& is,tuple& arg){RE is >> get<0>(arg)>> get<1>(arg)>> get<2>(arg);}TE IN IS& OP>>(IS& is,tuple& arg){RE is >> get<0>(arg)>> get<1>(arg)>> get<2>(arg)>> get<3>(arg);}TE IN OS& OP<<(OS& os,CO tuple& arg){RE os << get<0>(arg);}TE TY V> IN auto OP<<(OS& os,CO V& arg)-> decltype((get<0>(arg),os))&{RE os << get<0>(arg)<< " " << get<1>(arg);}TE IN OS& OP<<(OS& os,CO tuple& arg){RE os << get<0>(arg)<< " " << get<1>(arg)<< " " << get<2>(arg);}TE IN OS& OP<<(OS& os,CO tuple& arg){RE os << get<0>(arg)<< " " << get<1>(arg)<< " " << get<2>(arg)<< " " << get<3>(arg);}DF_OF_AR_FOR_TUPLE(+);TE TY V> IN auto OP-(CO V& t)-> decldecay_t((get<0>(t),t)){RE{-get<0>(t),-get<1>(t)};}TE IN tuple OP-(CO tuple& t){RE{-get<0>(t),-get<1>(t),-get<2>(t)};}TE IN tuple OP-(CO tuple& t){RE{-get<0>(t),-get<1>(t),-get<2>(t),-get<3>(t)};}DF_OF_AR_FOR_TUPLE(-);DF_OF_AR_FOR_TUPLE(*);DF_OF_AR_FOR_TUPLE(/);DF_OF_AR_FOR_TUPLE(%);DF_OF_INCREMENT_FOR_TUPLE(++);DF_OF_INCREMENT_FOR_TUPLE(--); TE CL TupleAccessIndex{};TE CL Tuple:PU tuple{PU:IN Tuple(Types&&... args);TE IN Tuple(Args&&... args);TE IN auto& OP[](CO TupleAccessIndex& i)NE;TE IN CO auto& OP[](CO TupleAccessIndex& i)CO NE;};TE CL tuple_size>:PU tuple_size>{};TE CL tuple_element>:PU tuple_element>{}; TE US Pair = Tuple;TE US T2 = Pair;TE US T3 = Tuple;TE US T4 = Tuple; CE TupleAccessIndex<0> O{};CE TupleAccessIndex<1> I{};CE TupleAccessIndex<2> II{};CE TupleAccessIndex<3> III{}; TE IN Tuple::Tuple(Types&&... args):tuple(MO(args)...){}TE TE IN Tuple::Tuple(Args&&... args):tuple(forward(args)...){}TE TE IN auto& Tuple::OP[](CO TupleAccessIndex& i)NE{RE get(*TH);}TE TE IN CO auto& Tuple::OP[](CO TupleAccessIndex& i)CO NE{RE get(*TH);}TE TY PAIR,TY INT> T2 cast(CO PAIR& t){RE{get<0>(t),get<1>(t)};}TE T3 cast(CO tuple& t){RE{get<0>(t),get<1>(t),get<2>(t)};}TE T4 cast(CO tuple& t){RE{get<0>(t),get<1>(t),get<2>(t),get<3>(t)};} #define DF_OF_HASH_FOR_TUPLE(PAIR)TE IN size_t hash>::OP()(CO PAIR& n)CO{ST CO size_t seed =(GetRand(1e3,1e8)<< 1)| 1;ST CO hash h0;ST CO hash h1;RE(h0(get<0>(n))* seed)^ h1(get<1>(n));} TE DC_OF_HASH(tuple);TE DC_OF_HASH(pair);TE DC_OF_HASH(tuple);TE DC_OF_HASH(tuple);TE DC_OF_HASH(tuple); TE IN size_t hash>::OP()(CO tuple& n)CO{ST CO hash h;RE h(get<0>(n));}DF_OF_HASH_FOR_TUPLE(pair);DF_OF_HASH_FOR_TUPLE(tuple);TE IN size_t hash>::OP()(CO tuple& n)CO{ST CO size_t seed =(GetRand(1e3,1e8)<< 1)| 1;ST CO hash> h01;ST CO hash h2;RE(h01({get<0>(n),get<1>(n)})* seed)^ h2(get<2>(n));}TE IN size_t hash>::OP()(CO tuple& n)CO{ST CO size_t seed =(GetRand(1e3,1e8)<< 1)| 1;ST CO hash> h01;ST CO hash> h23;RE(h01({get<0>(n),get<1>(n)})* seed)^ h23({get<2>(n),get<3>(n)});} /* Vector (3KB)*/ #define DF_OF_COUT_FOR_VE(V)TE IN OS& OP<<(OS& os,CO V& arg){auto BE = arg.BE(),EN = arg.EN();auto IT = BE;WH(IT != EN){(IT == BE?os:os << " ")<< *IT;IT++;}RE os;} DF_OF_COUT_FOR_VE(VE);DF_OF_COUT_FOR_VE(LI);DF_OF_COUT_FOR_VE(set);DF_OF_COUT_FOR_VE(unordered_set);DF_OF_COUT_FOR_VE(multiset);IN VO VariadicResize(CRI SZ){}TE IN VO VariadicResize(CRI SZ,Arg& arg,ARGS&... args){arg.resize(SZ);VariadicResize(SZ,args...);} #define DF_OF_AR_FOR_VE(V,OPR)TE IN V& OP OPR ## =(V& a0,CO V& a1){AS(a0.SZ()<= a1.SZ());auto IT0 = a0.BE(),EN0 = a0.EN();auto IT1 = a1.BE();WH(IT0 != EN0){*(IT0++)OPR ## = *(IT1++);}RE a0;}TE IN V& OP OPR ## =(V& a,CO T& t){for(auto& x:a){x OPR## = t;}RE a;}TE IN V OP OPR(V a,CO U& u){RE MO(a OPR ## = u);} #define DF_OF_INCREMENT_FOR_VE(V,INCR)TE IN V& OP INCR(V& a){for(auto& i:a){INCR i;}RE a;} #define DF_OF_SHIFT_FOR_VE(V)TE IN V& OP<<=(V& a,T t){a.push_back(MO(t));RE a;}TE IN V& OP<<=(V& a,U&& u){RE a <<= forward(u);}TE IN T pop(V& a){AS(!a.empty());T AN = MO(a.back());a.pop_back();RE AN;} #define DF_OF_ARS_FOR_VE(V)DF_OF_AR_FOR_VE(V,+);DF_OF_AR_FOR_VE(V,-);DF_OF_AR_FOR_VE(V,*);DF_OF_AR_FOR_VE(V,/);DF_OF_AR_FOR_VE(V,%);DF_OF_INCREMENT_FOR_VE(V,++);DF_OF_INCREMENT_FOR_VE(V,--);TE IN V OP-(V a){RE MO(a *= T(-1));}TE IN V OP*(CO T& t,V v){RE MO(v *= t);}DF_OF_SHIFT_FOR_VE(V); #define DF_FOR_VE_BOOL(OPR)IN VE::reference OP OPR ## =(VE::reference b0,CO bool& b1){RE MO(b0 = bool(b0)OPR b1);}IN VE& OP OPR ## =(VE& b0,CO VE& b1){CO int L = b0.SZ();for(int i = 0;i < L;i++){b0[i]OPR ## = b1[i];}RE b0;}IN VE OP OPR(VE b0,CO VE& b1){RE MO(b0 OPR ## = b1);} DF_OF_ARS_FOR_VE(VE);DF_OF_ARS_FOR_VE(LI);DF_OF_SHIFT_FOR_VE(basic_string); TE IN auto Get(V& a){RE[&](CRI i = 0)-> CO decldecay_t(a[0])&{RE a[i];};}TE IN VE id(CRI SZ){VE AN(SZ);for(int i = 0;i < SZ;i++){AN[i]= i;}RE AN;}TE IN VO Sort(V& a,CO bool& reversed = false){US T = decldecay_t(a[0]);if(reversed){ST auto comp =[](CO T& t0,CO T& t1){RE t1 < t0;};sort(a.BE(),a.EN(),comp);}else{sort(a.BE(),a.EN());}}TE IN VO Sort(V0& a,V1& b,CO bool& reversed = false){CO int SZ = a.SZ();AS(SZ == int(b.SZ()));VE> v(SZ);for(int i = 0;i < SZ;i++){v[i]={MO(a[i]),MO(b[i])};}Sort(v,reversed);for(int i = 0;i < SZ;i++){a[i]= MO(v[i].first);b[i]= MO(v[i].second);}}TE IN pair,VE> IndexSort(CO V& a,CO bool& reversed = false){CO int SZ = a.SZ();auto index = id(SZ),ord = index;sort(index.BE(),index.EN(),[&](CRI i,CRI j){CO pair ti{a[i],i},tj{a[j],j};RE reversed?tj < ti:ti < tj;});for(int i = 0;i < SZ;i++){ord[index[i]]= i;}RE{MO(index),MO(ord)};}TE IN int len(CO V& a){RE a.SZ();}TE IN VO Reverse(V& a){CO int SZ = len(a),half = SZ / 2;for(int i = 0;i < half;i++){swap(a[i],a[SZ-1-i]);}};TE IN V Reversed(V a){Reverse(a);RE MO(a);}TE TY V,TY T> IN V cast(CO V& a){V AN{};for(auto& x:a){AN <<= x;}RE AN;} DF_FOR_VE_BOOL(&);DF_FOR_VE_BOOL(|);DF_FOR_VE_BOOL(^); /* Map (1KB)*/ #define DF_OF_AR_FOR_MAP(MAP,OPR)TE IN MAP& OP OPR ## =(MAP& a,CO pair& v){a[v.first]OPR ## = v.second;RE a;}TE IN MAP& OP OPR ## =(MAP& a0,CO MAP& a1){for(auto&[t,u]:a1){a0[t]OPR ## = u;}RE a0;}TE IN MAP OP OPR(MAP a,CO ARG& arg){RE MO(a OPR ## = arg);} #define DF_OF_ARS_FOR_MAP(MAP)DF_OF_AR_FOR_MAP(MAP,+);DF_OF_AR_FOR_MAP(MAP,-);DF_OF_AR_FOR_MAP(MAP,*);DF_OF_AR_FOR_MAP(MAP,/);DF_OF_AR_FOR_MAP(MAP,%); TE US Map = conditional_t>,unordered_map,conditional_t,map,VO>>; DF_OF_ARS_FOR_MAP(map);DF_OF_ARS_FOR_MAP(unordered_map); /* StdStream (2KB)*/ TE IN IS& VariadicCin(IS& is){RE is;}TE IN IS& VariadicCin(IS& is,Arg& arg,ARGS&... args){RE VariadicCin(is >> arg,args...);}TE IN IS& VariadicSet(IS& is,CRI i){RE is;}TE IN IS& VariadicSet(IS& is,CRI i,Arg& arg,ARGS&... args){RE VariadicSet(is >> arg[i],i,args...);}TE IN IS& VariadicGetline(IS& is,CO char& separator){RE is;}TE IN IS& VariadicGetline(IS& is,CO char& separator,Arg& arg,ARGS&... args){RE VariadicGetline(getline(is,arg,separator),separator,args...);}TE IN OS& VariadicCout(OS& os,Arg&& arg){RE os << forward(arg);}TE IN OS& VariadicCout(OS& os,Arg1&& arg1,Arg2&& arg2,ARGS&&... args){RE VariadicCout(os << forward(arg1)<< " ",forward(arg2),forward(args)...);}TE IN OS& VariadicCoutNonSep(OS& os,Arg&& arg){RE os << forward(arg);}TE IN OS& VariadicCoutNonSep(OS& os,Arg1&& arg1,Arg2&& arg2,ARGS&&... args){RE VariadicCoutNonSep(os << forward(arg1),forward(arg2),forward(args)...);}TE IN OS& CoutArray(OS& os,CRI i_start,CRI i_ulim,ARRAY&& a){for(int i = i_start;i < i_ulim;i++){(i == i_start?os:(os << " "))<< a[i];}RE os;} /* Module (6KB)*/ #define DC_OF_CPOINT(POINT)IN CO U& POINT()CO NE #define DC_OF_POINT(POINT)IN U& POINT()NE #define DF_OF_CPOINT(POINT)TE IN CO U& VirtualPointedSet::POINT()CO NE{RE Point();} #define DF_OF_POINT(POINT)TE IN U& VirtualPointedSet::POINT()NE{RE Point();} TE CL UnderlyingSet{PU:US type = U;};TE CL VirtualPointedSet:VI PU UnderlyingSet{PU:VI CO U& Point()CO NE = 0;VI U& Point()NE = 0;DC_OF_CPOINT(Unit);DC_OF_CPOINT(Zero);DC_OF_CPOINT(One);DC_OF_CPOINT(Infty);DC_OF_POINT(init);DC_OF_POINT(root);};TE CL PointedSet:VI PU VirtualPointedSet{PU:U m_b_U;IN PointedSet(U b_u = U());IN CO U& Point()CO NE;IN U& Point()NE;};TE CL VirtualNSet:VI PU UnderlyingSet{PU:VI U Transfer(CO U& u)= 0;IN U Inverse(CO U& u);};TE CL AbstractNSet:VI PU VirtualNSet{PU:F_U m_f_U;IN AbstractNSet(F_U f_U);IN AbstractNSet& OP=(CO AbstractNSet&)NE;IN U Transfer(CO U& u);};TE CL VirtualMagma:VI PU UnderlyingSet{PU:VI U Product(U u0,CO U& u1)= 0;IN U Sum(U u0,CO U& u1);};TE CL AdditiveMagma:VI PU VirtualMagma{PU:IN U Product(U u0,CO U& u1);};TE CL MultiplicativeMagma:VI PU VirtualMagma{PU:IN U Product(U u0,CO U& u1);};TE CL AbstractMagma:VI PU VirtualMagma{PU:M_U m_m_U;IN AbstractMagma(M_U m_U);IN AbstractMagma& OP=(CO AbstractMagma&)NE;IN U Product(U u0,CO U& u1);}; TE IN PointedSet::PointedSet(U b_U):m_b_U(MO(b_U)){}TE IN CO U& PointedSet::Point()CO NE{RE m_b_U;}TE IN U& PointedSet::Point()NE{RE m_b_U;}DF_OF_CPOINT(Unit);DF_OF_CPOINT(Zero);DF_OF_CPOINT(One);DF_OF_CPOINT(Infty);DF_OF_POINT(init);DF_OF_POINT(root);TE IN AbstractNSet::AbstractNSet(F_U f_U):m_f_U(MO(f_U)){ST_AS(is_invocable_r_v);}TE IN AbstractNSet& AbstractNSet::operator=(CO AbstractNSet&)NE{RE *TH;}TE IN U AbstractNSet::Transfer(CO U& u){RE m_f_U(u);}TE IN U VirtualNSet::Inverse(CO U& u){RE Transfer(u);}TE IN AbstractMagma::AbstractMagma(M_U m_U):m_m_U(MO(m_U)){ST_AS(is_invocable_r_v);}TE IN AbstractMagma& AbstractMagma::OP=(CO AbstractMagma&)NE{RE *TH;}TE IN U AdditiveMagma::Product(U u0,CO U& u1){RE MO(u0 += u1);}TE IN U MultiplicativeMagma::Product(U u0,CO U& u1){RE MO(u0 *= u1);}TE IN U AbstractMagma::Product(U u0,CO U& u1){RE m_m_U(MO(u0),u1);}TE IN U VirtualMagma::Sum(U u0,CO U& u1){RE Product(MO(u0),u1);} TE CL VirtualMonoid:VI PU VirtualMagma,VI PU VirtualPointedSet{};TE CL AdditiveMonoid:VI PU VirtualMonoid,PU AdditiveMagma,PU PointedSet{};TE CL MultiplicativeMonoid:VI PU VirtualMonoid,PU MultiplicativeMagma,PU PointedSet{PU:IN MultiplicativeMonoid(U e_U);};TE CL AbstractMonoid:VI PU VirtualMonoid,PU AbstractMagma,PU PointedSet{PU:IN AbstractMonoid(M_U m_U,U e_U);}; TE IN MultiplicativeMonoid::MultiplicativeMonoid(U e_U):PointedSet(MO(e_U)){}TE IN AbstractMonoid::AbstractMonoid(M_U m_U,U e_U):AbstractMagma(MO(m_U)),PointedSet(MO(e_U)){} TE CL VirtualGroup:VI PU VirtualMonoid,VI PU VirtualPointedSet,VI PU VirtualNSet{};TE CL AdditiveGroup:VI PU VirtualGroup,PU AdditiveMonoid{PU:IN U Transfer(CO U& u);};TE CL AbstractGroup:VI PU VirtualGroup,PU AbstractMonoid,PU AbstractNSet{PU:IN AbstractGroup(M_U m_U,U e_U,I_U i_U);}; TE IN AbstractGroup::AbstractGroup(M_U m_U,U e_U,I_U i_U):AbstractMonoid(MO(m_U),MO(e_U)),AbstractNSet(MO(i_U)){}TE IN U AdditiveGroup::Transfer(CO U& u){RE -u;} TE CL VirtualRSet:VI PU UnderlyingSet{PU:VI U Action(CO R& r,U u)= 0;IN U Power(U u,CO R& r);IN U ScalarProduct(CO R& r,U u);};TE >CL RegularRSet:VI PU VirtualRSet,PU MAGMA{PU:IN RegularRSet(MAGMA magma);IN U Action(CO U& r,U u);};TE CL AbstractRSet:VI PU VirtualRSet{PU:O_U m_o_U;IN AbstractRSet(CO R& dummy0,CO U& dummy1,O_U o_U);IN AbstractRSet& OP=(CO AbstractRSet&)NE;IN U Action(CO R& r,U u);};TE >CL AbstractModule:PU AbstractRSet,PU GROUP{PU:IN AbstractModule(CO R& dummy,O_U o_U,GROUP M);};TE CL Module:VI PU VirtualRSet,PU AdditiveGroup{PU:IN U Action(CO R& r,U u);}; TE IN RegularRSet::RegularRSet(MAGMA magma):MAGMA(MO(magma)){}TE IN AbstractRSet::AbstractRSet(CO R& dummy0,CO U& dummy1,O_U o_U):m_o_U(MO(o_U)){ST_AS(is_invocable_r_v);}TE IN AbstractModule::AbstractModule(CO R& dummy,O_U o_U,GROUP M):AbstractRSet(dummy,M.One(),MO(o_U)),GROUP(MO(M)){ST_AS(is_same_v>);}TE IN AbstractRSet& AbstractRSet::OP=(CO AbstractRSet&)NE{RE *TH;}TE IN U RegularRSet::Action(CO U& r,U u){RE TH->Product(r,MO(u));}TE IN U AbstractRSet::Action(CO R& r,U u){RE m_o_U(r,MO(u));}TE IN U Module::Action(CO R& r,U u){RE MO(u *= r);}TE IN U VirtualRSet::Power(U u,CO R& r){RE Action(r,MO(u));}TE IN U VirtualRSet::ScalarProduct(CO R& r,U u){RE Action(r,MO(u));} /* Iteration (3KB) */ #define SPECIALSATION_OF_AR_PROGRESSION_SUM(TYPE)TE <> IN TYPE ArithmeticProgressionSum(CO TYPE& l,CO TYPE& r,CO TYPE& d){RE SpecialisedArithmeticProgressionSum(l,r,d);} TE TY V,TY OPR> T LeftConnectiveProd(T t,CO V& f,OPR opr){for(auto& u:f){t = opr(MO(t),u);}RE MO(t);}TE TY V> IN T Sum(CO V& f){RE LeftConnectiveProd(T{},f,[](T t0,CO U& u1){RE MO(t0 += u1);});}TE TY V> IN T Prod(CO V& f){RE LeftConnectiveProd(T{1},f,[](T t0,CO U& u1){RE MO(t0 *= u1);});}TE IN T& SetMax(T& t){RE t;}TE IN T& SetMax(T& t0,CO U& u1,CO Args&... args){RE SetMax(t0 < u1?t0 = u1:t0,args...);}TE IN T& SetMin(T& t){RE t;}TE IN T& SetMin(T& t0,CO U& u1,CO Args&... args){RE SetMin(u1 < t0?t0 = u1:t0,args...);}TE IN T& ExtendedSetMin(T& t){RE t;}TE IN T& ExtendedSetMin(T& t0,CO U& u1,CO Args&... args){RE ExtendedSetMin(u1 >= 0 &&(t0 < 0 || u1 < t0)?t0 = u1:t0,args...);}TE IN CO T& Max(CO VE& f){RE *max_element(f.BE(),f.EN());}TE TY SET> IN CO T& Max(CO SET& f){RE *--f.EN();}TE IN T Max(T t0,CO U& t1,CO Args&... args){RE MO(SetMax(t0,t1,args...));}TE IN CO T& Min(CO VE& f){RE *min_element(f.BE(),f.EN());}TE TY SET> IN CO T& Min(CO SET& f){RE *f.BE();}TE IN T Min(T t0,CO U& t1,CO Args&... args){RE MO(SetMin(t0,t1,args...));}TE T Power(CO T& t,CO UINT& EX,T init = 1){RE EX > 1?Power(t * t,EX >> 1,MO(EX & 1?init *= t:init)):MO(EX > 0?init *= t:(AS(EX == 0),init));}TE IN T PowerMemorisation(CO T& t,CRI EX){AS(EX >= 0);ST Map> memory{};auto& AN = memory[t];if(AN.empty()){AN.push_back(1);}WH(int(AN.SZ())<= EX){AN.push_back(AN.back()* t);}RE AN[EX];}TE IN INT ArithmeticProgressionSum(CO INT& l,CO INT& r,CO INT& d = 1){RE(l + r)*((r - l)/ d + 1)/ 2;}TE IN INT SpecialisedArithmeticProgressionSum(CO INT& l,CO INT& r,CO INT& d){AS(l - 1 <= r);CO INT c =(r - l)/ d;RE l - 1 == r?0:(c & 1)== 0?(c + 1)*(l + d *(c >> 1)):((c + 1)>> 1)*((l << 1)+ d * c);} SPECIALSATION_OF_AR_PROGRESSION_SUM(int); SPECIALSATION_OF_AR_PROGRESSION_SUM(uint); SPECIALSATION_OF_AR_PROGRESSION_SUM(ll); SPECIALSATION_OF_AR_PROGRESSION_SUM(ull); TE IN INT ArithmeticProgressionSum(CO INT& r){RE ArithmeticProgressionSum(INT{},r);}TE IN INT SquareSum(CO INT& r){RE r *(r + 1)*(2 * r + 1)/ 6;}TE IN T GeometricProgressionSum(T rate,UINT EX_max,CO T& init = 1){T ratem = rate - 1;RE ratem == 0?init * ++EX_max:(Power(MO(rate),MO(++EX_max))- 1)/ MO(ratem)* init;}TE T GeometricProgressionLinearCombinationSum(VE rate,VE EX_max,CO VE& init){CO int SZ = init.SZ();AS(int(rate.SZ())== SZ && int(EX_max.SZ())== SZ);T AN{};for(int i = 0;i < SZ;i++){AN += GeometricProgressionSum(MO(rate[i]),MO(EX_max[i]),init[i]);}RE AN;} /* Sqrt (1KB) */ TE INT RoundDownSqrt(CO INT& n){ST_AS(is_same_v || is_same_v || is_same_v || is_same_v);AS(n >= 0);if(n <= 1){RE n;}CE INT r_max = is_same_v?46341:is_same_v?65536:is_same_v?3037000500:4294967296;INT l = 1,r = min(r_max,n);WH(l < r - 1){CO INT m =(l + r)>> 1;(m <= n / m?l:r)= m;}RE l;}TE INT RoundUpSqrt(CO INT& n){ST_AS(is_same_v || is_same_v || is_same_v || is_same_v);AS(n >= 0);if(n <= 2){RE n;}CE INT r_max = is_same_v?46341:is_same_v?65536:is_same_v?3037000500:4294967296;CO INT nm = n - 1;INT l = 1,r = min(r_max,n);WH(l + 1 < r){CO INT m =(l + r)>> 1;(m <= nm / m?l:r)= m;}RE r;}TE bool IsSquare(CO INT& n){CO INT r = RoundDownSqrt(n);RE n == r * r;} /* Loop (1KB)*/ TE bool NextLoop(CRI SZ,CO VE& lower_bound,CO VE& upper_limit,VE& index){int depth = 0;WH(depth < SZ){if(++index[depth]< upper_limit[depth]){break;}index[depth]= lower_bound[depth];depth++;}RE depth < SZ;}TE bool NextLoop(CO VE& lower_bound,CO VE& upper_limit,VE& index){RE NextLoop(index.SZ(),lower_bound,upper_limit,index);}TE bool NextLoopEq(CRI SZ,CO VE& lower_bound,CO VE& upper_bound,VE& index){int depth = 0;WH(depth < SZ){if(++index[depth]<= upper_bound[depth]){break;}index[depth]= lower_bound[depth];depth++;}RE depth < SZ;}TE bool NextLoopEq(CO VE& lower_bound,CO VE& upper_bound,VE& index){RE NextLoopEq(index.SZ(),lower_bound,upper_bound,index);} /* string (1KB)*/ TE IN char IntToChar(CO INT& i,CO char& c = 'a'){RE c + i;}TE IN INT CharToInt(CO char& i){RE i -(i < 'a'?'A':'a');}TE string ArrayToString(CO VE& A,CO char& c = 'a'){CO int N = A.SZ();string S(N,c);for(int i = 0;i < N;i++){S[i]= IntToChar(A[i],c);}RE S;}TE VE StringToArray(CO string& S){CO int N = S.SZ();VE A(N);for(int i = 0;i < N;i++){A[i]= CharToInt(S[i]);}RE A;}TE string ArrayToParenthesisString(CO VE& A){CO int N = A.SZ();string S(N,'(');for(int i = 0;i < N;i++){AS(0 <= A[i]&& A[i]<= 1);S[i]= "()"[A[i]];}RE S;}TE VE ParenthesisStringToArray(CO string& S){CO int N = S.SZ();VE A(N);for(int i = 0;i < N;i++){A[i]= S[i]- '(';}RE A;} #endif /* AAA 常設ライブラリは以上に挿入する。*/ #define INCLUDE_LIBRARY #include __FILE__ #endif /* INCLUDE_LIBRARY */ #endif /* INCLUDE_SUB */ #endif /* INCLUDE_MAIN */