#ifdef DEBUG #define _GLIBCXX_DEBUG #define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr ); signal( SIGABRT , &AlertAbort ) #define DEXPR( LL , BOUND , VALUE , DEBUG_VALUE ) CEXPR( LL , BOUND , DEBUG_VALUE ) #define CERR( ... ) VariadicCout( cerr , __VA_ARGS__ ) << endl #define COUT( ... ) VariadicCout( cout << "出力: " , __VA_ARGS__ ) << endl #define CERR_A( A , N ) OUTPUT_ARRAY( cerr , A , N ) << endl #define COUT_A( A , N ) cout << "出力: "; OUTPUT_ARRAY( cout , A , N ) << endl #define CERR_ITR( A ) OUTPUT_ITR( cerr , A ) << endl #define COUT_ITR( A ) cout << "出力: "; OUTPUT_ITR( cout , A ) << endl #define ASSERT( A , MIN , MAX ) CERR( "ASSERTチェック: " , ( MIN ) , ( ( MIN ) <= A ? "<=" : ">" ) , A , ( A <= ( MAX ) ? "<=" : ">" ) , ( MAX ) ); assert( ( MIN ) <= A && A <= ( MAX ) ) #define AUTO_CHECK int auto_checked; AutoCheck( auto_checked ); if( auto_checked == 3 ){ Jikken(); return 0; } else if( auto_checked == 4 ){ Debug(); return 0; } else if( auto_checked != 0 ){ return 0; }; #else #pragma GCC optimize ( "O3" ) #pragma GCC optimize ( "unroll-loops" ) #pragma GCC target ( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) #define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr ) #define DEXPR( LL , BOUND , VALUE , DEBUG_VALUE ) CEXPR( LL , BOUND , VALUE ) #define CERR( ... ) #define COUT( ... ) VariadicCout( cout , __VA_ARGS__ ) << "\n" #define CERR_A( A , N ) #define COUT_A( A , N ) OUTPUT_ARRAY( cout , A , N ) << "\n" #define CERR_ITR( A ) #define COUT_ITR( A ) OUTPUT_ITR( cout , A ) << "\n" #define ASSERT( A , MIN , MAX ) assert( ( MIN ) <= A && A <= ( MAX ) ) #define AUTO_CHECK #endif #include using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; using ld = long double; using lld = __float128; template using T2 = pair; template using T3 = tuple; template using T4 = tuple; using path = pair; // #define RANDOM_TEST #if defined( DEBUG ) && defined( RANDOM_TEST ) ll GetRand( const ll& Rand_min , const ll& Rand_max ); #define SET_ASSERT( A , MIN , MAX ) CERR( #A , " = " , ( A = GetRand( MIN , MAX ) ) ) #define CIN( LL , ... ) LL __VA_ARGS__; static_assert( false ) #define CIN_A( LL , A , N ) LL A[N]; static_assert( false ) #define TEST_CASE_NUM( BOUND ) DEXPR( int , bound_T , BOUND , min( BOUND , 100 ) ); int T = bound_T; static_assert( bound_T > 1 ) #define RETURN( ANSWER ) if( ( ANSWER ) == guchoku ){ CERR( ANSWER , "==" , guchoku ); goto END_MAIN; } else { CERR( ANSWER , "!=" , guchoku ); return 0; } #else #define SET_ASSERT( A , MIN , MAX ) cin >> A; ASSERT( A , MIN , MAX ) #define CIN( LL , ... ) LL __VA_ARGS__; VariadicCin( cin , __VA_ARGS__ ) #define CIN_A( LL , A , N ) LL A[N]; FOR( VARIABLE_FOR_CIN_A , 0 , N ){ cin >> A[VARIABLE_FOR_CIN_A]; } #define TEST_CASE_NUM( BOUND ) DEXPR( int , bound_T , BOUND , min( BOUND , 100 ) ); int T = 1; if constexpr( bound_T > 1 ){ SET_ASSERT( T , 1 , bound_T ); } #define RETURN( ANSWER ) COUT( ANSWER ); QUIT #endif #define ATT __attribute__( ( target( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) ) ) #define TYPE_OF( VAR ) decay_t #define CEXPR( LL , BOUND , VALUE ) constexpr LL BOUND = VALUE #define CIN_ASSERT( A , MIN , MAX ) TYPE_OF( MAX ) A; SET_ASSERT( A , MIN , MAX ) #define GETLINE_SEPARATE( SEPARATOR , ... ) string __VA_ARGS__; VariadicGetline( cin , SEPARATOR , __VA_ARGS__ ) #define GETLINE( ... ) GETLINE_SEPARATE( " " , ... ) #define FOR( VAR , INITIAL , FINAL_PLUS_ONE ) for( TYPE_OF( FINAL_PLUS_ONE ) VAR = INITIAL ; VAR < FINAL_PLUS_ONE ; VAR ++ ) #define FOREQ( VAR , INITIAL , FINAL ) for( TYPE_OF( FINAL ) VAR = INITIAL ; VAR <= FINAL ; VAR ++ ) #define FOREQINV( VAR , INITIAL , FINAL ) for( TYPE_OF( INITIAL ) VAR = INITIAL ; VAR >= FINAL ; VAR -- ) #define AUTO_ITR( ARRAY ) auto itr_ ## ARRAY = ARRAY .begin() , end_ ## ARRAY = ARRAY .end() #define FOR_ITR( ARRAY ) for( AUTO_ITR( ARRAY ) , itr = itr_ ## ARRAY ; itr_ ## ARRAY != end_ ## ARRAY ; itr_ ## ARRAY ++ , itr++ ) #define REPEAT( HOW_MANY_TIMES ) FOR( VARIABLE_FOR_REPEAT_ ## HOW_MANY_TIMES , 0 , HOW_MANY_TIMES ) #define SET_PRECISION( DECIMAL_DIGITS ) cout << fixed << setprecision( DECIMAL_DIGITS ) #define OUTPUT_ARRAY( OS , A , N ) FOR( VARIABLE_FOR_OUTPUT_ARRAY , 0 , N ){ OS << A[VARIABLE_FOR_OUTPUT_ARRAY] << (VARIABLE_FOR_OUTPUT_ARRAY==N-1?"":" "); } OS #define OUTPUT_ITR( OS , A ) { auto ITERATOR_FOR_OUTPUT_ITR = A.begin() , END_FOR_OUTPUT_ITR = A.end(); bool VARIABLE_FOR_OUTPUT_ITR = ITERATOR_FOR_COUT_ITR != END_FOR_COUT_ITR; while( VARIABLE_FOR_OUTPUT_ITR ){ OS << *ITERATOR_FOR_COUT_ITR; ( VARIABLE_FOR_OUTPUT_ITR = ++ITERATOR_FOR_COUT_ITR != END_FOR_COUT_ITR ) ? OS : OS << " "; } } OS #define QUIT goto END_MAIN #define START_MAIN REPEAT( T ){ { if constexpr( bound_T > 1 ){ CERR( "testcase " , VARIABLE_FOR_REPEAT_T , ":" ); } #define START_WATCH chrono::system_clock::time_point watch = chrono::system_clock::now() #define CURRENT_TIME static_cast( chrono::duration_cast( chrono::system_clock::now() - watch ).count() / 1000.0 ) #define CHECK_WATCH( TL_MS ) ( CURRENT_TIME < TL_MS - 100.0 ) #define FINISH_MAIN QUIT; } END_MAIN: CERR( "" ); } // 入出力用関数 template inline basic_istream& VariadicCin( basic_istream& is ) { return is; } template inline basic_istream& VariadicCin( basic_istream& is , Arg& arg , ARGS&... args ) { return VariadicCin( is >> arg , args... ); } template inline basic_istream& VariadicGetline( basic_istream& is , const char& separator ) { return is; } template inline basic_istream& VariadicGetline( basic_istream& is , const char& separator , Arg& arg , ARGS&... args ) { return VariadicGetline( getline( is , arg , separator ) , separator , args... ); } template inline basic_ostream& VariadicCout( basic_ostream& os , const Arg& arg ) { return os << arg; } template inline basic_ostream& VariadicCout( basic_ostream& os , const Arg1& arg1 , const Arg2& arg2 , const ARGS&... args ) { return VariadicCout( os << arg1 << " " , arg2 , args... ); } // 算術用関数 template inline T Residue( const T& a , const T& p ){ return a >= 0 ? a % p : p - 1 - ( ( - ( a + 1 ) ) % p ); } inline ll MIN( const ll& a , const ll& b ){ return min( a , b ); } inline ull MIN( const ull& a , const ull& b ){ return min( a , b ); } inline ll MAX( const ll& a , const ll& b ){ return max( a , b ); } inline ull MAX( const ull& a , const ull& b ){ return max( a , b ); } #define POWER( ANSWER , ARGUMENT , EXPONENT ) \ static_assert( ! is_same::value && ! is_same::value ); \ TYPE_OF( ARGUMENT ) ANSWER{ 1 }; \ { \ TYPE_OF( ARGUMENT ) ARGUMENT_FOR_SQUARE_FOR_POWER = ( ARGUMENT ); \ TYPE_OF( EXPONENT ) EXPONENT_FOR_SQUARE_FOR_POWER = ( EXPONENT ); \ while( EXPONENT_FOR_SQUARE_FOR_POWER != 0 ){ \ if( EXPONENT_FOR_SQUARE_FOR_POWER % 2 == 1 ){ \ ANSWER *= ARGUMENT_FOR_SQUARE_FOR_POWER; \ } \ ARGUMENT_FOR_SQUARE_FOR_POWER *= ARGUMENT_FOR_SQUARE_FOR_POWER; \ EXPONENT_FOR_SQUARE_FOR_POWER /= 2; \ } \ } \ #define POWER_MOD( ANSWER , ARGUMENT , EXPONENT , MODULO ) \ ll ANSWER{ 1 }; \ { \ ll ARGUMENT_FOR_SQUARE_FOR_POWER = ( ( MODULO ) + ( ( ARGUMENT ) % ( MODULO ) ) ) % ( MODULO ); \ TYPE_OF( EXPONENT ) EXPONENT_FOR_SQUARE_FOR_POWER = ( EXPONENT ); \ while( EXPONENT_FOR_SQUARE_FOR_POWER != 0 ){ \ if( EXPONENT_FOR_SQUARE_FOR_POWER % 2 == 1 ){ \ ANSWER = ( ANSWER * ARGUMENT_FOR_SQUARE_FOR_POWER ) % ( MODULO ); \ } \ ARGUMENT_FOR_SQUARE_FOR_POWER = ( ARGUMENT_FOR_SQUARE_FOR_POWER * ARGUMENT_FOR_SQUARE_FOR_POWER ) % ( MODULO ); \ EXPONENT_FOR_SQUARE_FOR_POWER /= 2; \ } \ } \ #define FACTORIAL_MOD( ANSWER , ANSWER_INV , INVERSE , MAX_INDEX , CONSTEXPR_LENGTH , MODULO ) \ static ll ANSWER[CONSTEXPR_LENGTH]; \ static ll ANSWER_INV[CONSTEXPR_LENGTH]; \ static ll INVERSE[CONSTEXPR_LENGTH]; \ { \ ll VARIABLE_FOR_PRODUCT_FOR_FACTORIAL = 1; \ ANSWER[0] = VARIABLE_FOR_PRODUCT_FOR_FACTORIAL; \ FOREQ( i , 1 , MAX_INDEX ){ \ ANSWER[i] = ( VARIABLE_FOR_PRODUCT_FOR_FACTORIAL *= i ) %= ( MODULO ); \ } \ ANSWER_INV[0] = ANSWER_INV[1] = INVERSE[1] = VARIABLE_FOR_PRODUCT_FOR_FACTORIAL = 1; \ FOREQ( i , 2 , MAX_INDEX ){ \ ANSWER_INV[i] = ( VARIABLE_FOR_PRODUCT_FOR_FACTORIAL *= INVERSE[i] = ( MODULO ) - ( ( ( ( MODULO ) / i ) * INVERSE[ ( MODULO ) % i ] ) % ( MODULO ) ) ) %= ( MODULO ); \ } \ } \ // 二分探索テンプレート // EXPRESSIONがANSWERの広義単調関数の時、EXPRESSION >= TARGETの整数解を格納。 #define BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , DESIRED_INEQUALITY , TARGET , INEQUALITY_FOR_CHECK , UPDATE_U , UPDATE_L , UPDATE_ANSWER ) \ static_assert( ! is_same::value && ! is_same::value ); \ ll ANSWER = MINIMUM; \ if( MINIMUM <= MAXIMUM ){ \ ll VARIABLE_FOR_BINARY_SEARCH_L = MINIMUM; \ ll VARIABLE_FOR_BINARY_SEARCH_U = MAXIMUM; \ ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \ ll VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH; \ while( VARIABLE_FOR_BINARY_SEARCH_L != VARIABLE_FOR_BINARY_SEARCH_U ){ \ VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( EXPRESSION ) - ( TARGET ); \ CERR( "二分探索中: " << VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << "-" << TARGET << "=" << VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH ); \ if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH INEQUALITY_FOR_CHECK 0 ){ \ VARIABLE_FOR_BINARY_SEARCH_U = UPDATE_U; \ } else { \ VARIABLE_FOR_BINARY_SEARCH_L = UPDATE_L; \ } \ ANSWER = UPDATE_ANSWER; \ } \ CERR( "二分探索終了: " << VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << ( EXPRESSION > TARGET ? ">" : EXPRESSION < TARGET ? "<" : "=" ) << TARGET ); \ if( EXPRESSION DESIRED_INEQUALITY TARGET ){ \ CERR( "二分探索成功" ); \ } else { \ CERR( "二分探索失敗" ); \ ANSWER = MAXIMUM + 1; \ } \ } else { \ CERR( "二分探索失敗: " << MINIMUM << ">" << MAXIMUM ); \ ANSWER = MAXIMUM + 1; \ } \ // 単調増加の時にEXPRESSION >= TARGETの最小解を格納。 #define BS1( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET ) \ BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , >= , TARGET , >= , ANSWER , ANSWER + 1 , ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2 ) \ // 単調増加の時にEXPRESSION <= TARGETの最大解を格納。 #define BS2( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET ) \ BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , <= , TARGET , > , ANSWER - 1 , ANSWER , ( VARIABLE_FOR_BINARY_SEARCH_L + 1 + VARIABLE_FOR_BINARY_SEARCH_U ) / 2 ) \ // 単調減少の時にEXPRESSION >= TARGETの最大解を格納。 #define BS3( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET ) \ BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , >= , TARGET , < , ANSWER - 1 , ANSWER , ( VARIABLE_FOR_BINARY_SEARCH_L + 1 + VARIABLE_FOR_BINARY_SEARCH_U ) / 2 ) \ // 単調減少の時にEXPRESSION <= TARGETの最小解を格納。 #define BS4( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET ) \ BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , <= , TARGET , <= , ANSWER , ANSWER + 1 , ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2 ) \ // t以下の値が存在すればその最大値のiterator、存在しなければend()を返す。 template inline typename set::iterator MaximumLeq( set& S , const T& t ) { const auto end = S.end(); if( S.empty() ){ return end; } auto itr = S.upper_bound( t ); return itr == end ? S.find( *( S.rbegin() ) ) : itr == S.begin() ? end : --itr; } // t未満の値が存在すればその最大値のiterator、存在しなければend()を返す。 template inline typename set::iterator MaximumLt( set& S , const T& t ) { const auto end = S.end(); if( S.empty() ){ return end; } auto itr = S.lower_bound( t ); return itr == end ? S.find( *( S.rbegin() ) ) : itr == S.begin() ? end : --itr; } // t以上の値が存在すればその最小値のiterator、存在しなければend()を返す。 template inline typename set::iterator MinimumGeq( set& S , const T& t ) { return S.lower_bound( t ); } // tより大きい値が存在すればその最小値のiterator、存在しなければend()を返す。 template inline typename set::iterator MinimumGt( set& S , const T& t ) { return S.upper_bound( t ); } // データ構造用関数 template inline T add( const T& t0 , const T& t1 ) { return t0 + t1; } template inline T xor_add( const T& t0 , const T& t1 ){ return t0 ^ t1; } template inline T multiply( const T& t0 , const T& t1 ) { return t0 * t1; } template inline const T& zero() { static const T z = 0; return z; } template inline const T& one() { static const T o = 1; return o; }\ template inline T add_inv( const T& t ) { return -t; } template inline T id( const T& v ) { return v; } // グリッド問題用関数 int H , W , H_minus , W_minus , HW; vector > non_wall; inline pair EnumHW( const int& v ) { return { v / W , v % W }; } inline int EnumHW_inv( const int& h , const int& w ) { return h * W + w; } const string direction[4] = {"U","R","D","L"}; // (i,j)->(k,h)の方向番号を取得 inline int DirectionNumberOnGrid( const int& i , const int& j , const int& k , const int& h ){return ik?0:jh?3:(assert(false),-1);} // v->wの方向番号を取得 inline int DirectionNumberOnGrid( const int& v , const int& w ){auto [i,j]=EnumHW(v);auto [k,h]=EnumHW(w);return DirectionNumberOnGrid(i,j,k,h);} // 方向番号の反転U<->D、R<->L inline int ReverseDirectionNumberOnGrid( const int& n ){assert(0<=n&&n<4);return(n+2)%4;} inline void SetEdgeOnGrid( const string& Si , const int& i , list ( &e )[] , const char& walkable = '.' ){FOR(j,0,W){if(Si[j]==walkable){int v = EnumHW_inv(i,j);if(i>0){e[EnumHW_inv(i-1,j)].push_back(v);}if(i+10){e[EnumHW_inv(i,j-1)].push_back(v);}if(j+1 > ( &e )[] , const char& walkable = '.' ){FOR(j,0,W){if(Si[j]==walkable){const int v=EnumHW_inv(i,j);if(i>0){e[EnumHW_inv(i-1,j)].push_back({v,1});}if(i+10){e[EnumHW_inv(i,j-1)].push_back({v,1});}if(j+1 >& non_wall , const char& walkable = '.' , const char& unwalkable = '#' ){non_wall.push_back(vector(W));auto& non_wall_i=non_wall[i];FOR(j,0,W){non_wall_i[j]=Si[j]==walkable?true:(assert(Si[j]==unwalkable),false);}} // グラフ用関数 template list E( const int& i ); template vector > e; // デバッグ用関数 #ifdef DEBUG inline void AlertAbort( int n ) { CERR( "abort関数が呼ばれました。assertマクロのメッセージが出力されていない場合はオーバーフローの有無を確認をしてください。" ); } void AutoCheck( int& auto_checked ); void Jikken(); void Debug(); #endif // 圧縮用 #define TE template #define TY typename #define US using #define ST static #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 MO move #define TH this #define CRI CO int& #define CRUI CO uint& #define CRL CO ll& /* C-x 3 C-x o C-x C-fによるファイル操作用 BIT: c:/Users/user/Documents/Programming/Mathematics/SetTheory/DirectProduct/AffineSpace/BIT/compress.txt BFS: c:/Users/user/Documents/Programming/Mathematics/Geometry/Graph/BreadthFirstSearch/compress.txt DFS on Tree: c:/Users/user/Documents/Programming/Mathematics/Geometry/Graph/DepththFirstSearch/Tree/compress.txt Divisor: c:/Users/user/Documents/Programming/Mathematics/Arithmetic/Prime/Divisor/compress.txt Mod: c:/Users/user/Documents/Programming/Mathematics/Arithmetic/Mod/ConstexprModulo/compress.txt Polynomial c:/Users/user/Documents/Programming/Mathematics/Polynomial/compress.txt */ // VVV ライブラリは以下に挿入する。 template class ZeroOneBreadthFirstSearch_Body { protected: int m_V; int m_init; list m_next; int m_found[V_max]; int m_prev[V_max]; int m_weight[V_max]; public: inline ZeroOneBreadthFirstSearch_Body( const int& V ); inline ZeroOneBreadthFirstSearch_Body( const int& V , const int& init ); // m_foundとm_prevとm_weightを初期化 inline void Reset( const int& init ); // m_foundとm_prevとm_weightを非初期化 inline void Shift( const int& init ); inline const int& size() const; inline const int& init() const; inline int& found( const int& i ); inline const int& prev( const int& i ) const; inline const int& weight( const int& i ) const; int Next(); // Next()の反復でm_initから到達可能な頂点を全探索。 // 計算量O((m_initの連結成分)+(m_initの連結成分におけるEのサイズの合計)) inline void SetWeight(); // Next()の反復でinitからgoalまでの最短経路を探索。 // 計算量O((initの連結成分)+(m_initの連結成分におけるEのサイズの合計)) inline const int& Solve( const int& init , const int& goal ); private: virtual list > e( const int& t ) = 0; }; template > E(const int&)> class ZeroOneBreadthFirstSearch : public ZeroOneBreadthFirstSearch_Body { public: template inline ZeroOneBreadthFirstSearch( const Args&... args ); private: inline list > e( const int& t ); }; template inline ZeroOneBreadthFirstSearch_Body::ZeroOneBreadthFirstSearch_Body( const int& V ) : m_V( V ) , m_init() , m_next() , m_found() , m_prev() , m_weight() { assert( m_V <= V_max ); for( int i = 0 ; i < m_V ; i++ ){ m_prev[i] = m_weight[i] -1; } } template inline ZeroOneBreadthFirstSearch_Body::ZeroOneBreadthFirstSearch_Body( const int& V , const int& init ) : ZeroOneBreadthFirstSearch_Body( V ) { m_init = init; m_next.push_back( m_init ); m_found[m_init] = true; } template > E(const int&)> template inline ZeroOneBreadthFirstSearch::ZeroOneBreadthFirstSearch( const Args&... args ) : ZeroOneBreadthFirstSearch_Body( args... ) {} template inline void ZeroOneBreadthFirstSearch_Body::Reset( const int& init ) { m_init = init; assert( m_init < m_V ); m_next.clear(); m_next.push_back( m_init ); for( int i = 0 ; i < m_V ; i++ ){ m_found[i] = i == m_init; m_prev[i] = m_weight[i] = -1; } } template inline void ZeroOneBreadthFirstSearch_Body::Shift( const int& init ) { m_init = init; assert( m_init < m_V ); m_next.clear(); if( ! m_found[m_init] ){ m_next.push_back( m_init ); m_found[m_init] = true; } } template inline const int& ZeroOneBreadthFirstSearch_Body::size() const { return m_V; } template inline const int& ZeroOneBreadthFirstSearch_Body::init() const { return m_init; } template inline int& ZeroOneBreadthFirstSearch_Body::found( const int& i ) { assert( i < m_V ); return m_found[i]; } template inline const int& ZeroOneBreadthFirstSearch_Body::prev( const int& i ) const { assert( i < m_V ); return m_prev[i]; } template inline const int& ZeroOneBreadthFirstSearch_Body::weight( const int& i ) const { assert( i < m_V ); return m_weight[i]; } template int ZeroOneBreadthFirstSearch_Body::Next() { if( m_next.empty() ){ return -1; } const int i_curr = m_next.front(); if( m_weight[i_curr] == -1 ){ m_weight[i_curr] = 0; } m_next.pop_front(); auto edge = e( i_curr ); while( ! edge.empty() ){ const auto& [i,weighted] = edge.front(); int& found_i = m_found[i]; m_prev[i] = i_curr; if( weighted ){ if( found_i < 1 ){ m_next.push_back( i ); m_weight[i] = m_weight[i_curr] + 1; found_i = 1; } } else { if( found_i < 2 ){ m_next.push_front( i ); m_weight[i] = m_weight[i_curr]; found_i = 2; } } edge.pop_front(); } return i_curr; } template inline void ZeroOneBreadthFirstSearch_Body::SetWeight() { while( Next() != -1 ){} } template inline const int& ZeroOneBreadthFirstSearch_Body::Solve( const int& init , const int& goal ) { Reset( init ); assert( goal < m_V ); int i = Next(); while( i != -1 && i != goal ){ i = Next(); } return m_weight[goal]; } template > E(const int&)> inline list > ZeroOneBreadthFirstSearch::e( const int& t ) { return E( t ); } // AAA ライブラリは以上に挿入する。 vector > i_max; template list E( const int& i ) { list answer{}; // list answer = e[i]; // VVV 入力によらない処理は以下に挿入する。 auto [x,y] = EnumHW( i ); if( y > 0 ){ int& x_max = i_max[min(x+1,H-1)][y-1]; if( x_max != -1 ){ answer.push_back( { EnumHW_inv( x_max , y - 1 ) , false } ); if( y > 1 ){ int& x_next = i_max[min(x+1,H-1)][y-2]; if( x_next != -1 ){ answer.push_back( { EnumHW_inv( x_next , y - 2 ) , true } ); } } int& x_next = i_max[min(x+1,H-1)][y]; if( x_next != -1 ){ answer.push_back( { EnumHW_inv( x_next , y ) , true } ); } } } if( y < W - 1 ){ int& x_max = i_max[min(x+1,H-1)][y+1]; if( x_max != -1 ){ answer.push_back( { EnumHW_inv( x_max , y + 1 ) , false } ); if( y < W - 2 ){ int& x_next = i_max[min(x+1,H-1)][y+2]; if( x_next != -1 ){ answer.push_back( { EnumHW_inv( x_next , y + 2 ) , true } ); } } int& x_next = i_max[min(x+1,H-1)][y]; if( x_next != -1 ){ answer.push_back( { EnumHW_inv( x_next , y ) , true } ); } } } // AAA 入力によらない処理は以上に挿入する。 return answer; } ll Guchoku( int N , int M , int K ) { ll answer = N + M + K; return answer; } ll Answer( ll N , ll M , ll K ) { ll answer = N + M + K; return answer; } int main() { UNTIE; AUTO_CHECK; // START_WATCH; TEST_CASE_NUM( 1 ); START_MAIN; // Jikken(); // // 大きな素数 // CEXPR( ll , P , 998244353 ); // // CEXPR( ll , P , 1000000007 ); // Mod

を使う時はP2に変更。 // // データ構造使用畤のNの上限 // DEXPR( int , bound_N , 100000 , 100 ); // 0が5個 // // CEXPR( int , bound_N , 1000000000 ); // 0が9個 // // CEXPR( ll , bound_N , 1000000000000000000 ); // 0が18個 // // データ構造使用畤のMの上限 // // CEXPR( TYPE_OF( bound_N ) , bound_M , bound_N ); // DEXPR( int , bound_M , 100000 , 100 ); // 0が5個 // // CEXPR( int , bound_M , 1000000000 ); // 0が9個 // // CEXPR( ll , bound_M , 1000000000000000000 ); // 0が18個 // // 数 // CIN( ll , N ); // CIN( ll , M ); // CIN( ll , N , M , K ); // // CIN_ASSERT( N , 1 , bound_N ); // ランダムテスト用。上限のデフォルト値は10^5。 // // CIN_ASSERT( M , 1 , bound_M ); // ランダムテスト用。上限のデフォルト値は10^5。 // // 文字列 // CIN( string , S ); // CIN( string , T ); // // 配列 // CIN_A( ll , A , N ); // // CIN_A( ll , B , N ); // // ll A[N]; // // ll B[N]; // // ll A[bound_N]; // 関数(コンストラクタ)の引数に使う。長さのデフォルト値は10^5。 // // ll B[bound_N]; // 関数(コンストラクタ)の引数に使う。長さのデフォルト値は10^5。 // // FOR( i , 0 , N ){ // // cin >> A[i] >> B[i]; // // } // // 順列 // int P[N]; // int P_inv[N]; // FOR( i , 0 , N ){ // cin >> P[i]; // P_inv[--P[i]] = i; // } // // グラフ // FOR( j , 0 , M ){ // CIN_ASSERT( uj , 1 , N ); // CIN_ASSERT( vj , 1 , N ); // uj--; // vj--; // e[uj].push_back( vj ); // e[vj].push_back( uj ); // // CIN( ll , wj ); // // e[uj].push_back( { vj , wj } ); // // e[vj].push_back( { uj , wj } ); // } // // 座標圧縮や単一クエリタイプなどのための入力格納 // T3 data[M]; // FOR( j , 0 , M ){ // CIN( ll , x , y , z ); // data[j] = { x , y , z }; // } // // 一般のクエリ // CIN( int , Q ); // // DEXPR( int , bound_Q , 100000 , 100 ); // 基本不要。 // // CIN_ASSERT( Q , 1 , bound_Q ); // 基本不要。 // // T3 query[Q]; // // T2 query[Q]; // FOR( q , 0 , Q ){ // CIN( int , type ); // if( type == 1 ){ // CIN( int , x , y ); // // query[q] = { type , x , y }; // } else if( type == 2 ){ // CIN( int , x , y ); // // query[q] = { type , x , y }; // } else { // CIN( int , x , y ); // // query[q] = { type , x , y }; // } // // CIN( int , x , y ); // // // query[q] = { x , y }; // } // // sort( query , query + Q ); // // FOR( q , 0 , Q ){ // // auto& [x,y] = query[q]; // // // auto& [type,x,y] = query[q]; // // } // データ構造や壁配列使用畤のH,Wの上限 DEXPR( int , bound_H , 2000 , 20 ); // DEXPR( int , bound_H , 100000 , 10 ); // 0が5個 // CEXPR( int , bound_H , 1000000000 ); // 0が9個 CEXPR( int , bound_W , bound_H ); static_assert( ll( bound_H ) * bound_W < ll( 1 ) << 31 ); CEXPR( int , bound_HW , bound_H * bound_W ); // CEXPR( int , bound_HW , 100000 ); // 0が5個 // CEXPR( int , bound_HW , 1000000 ); // 0が6個 // グリッド cin >> H >> W; // SET_ASSERT( H , 1 , bound_H ); // ランダムテスト用。上限のデフォルト値は10^3。 // SET_ASSERT( W , 1 , bound_W ); // ランダムテスト用。上限のデフォルト値は10^3。 H_minus = H - 1; W_minus = W - 1; HW = H * W; // assert( HW <= bound_HW ); // 基本不要。上限のデフォルト値は10^6。 CIN( int , sx ); CIN( int , sy ); CIN( int , gx ); CIN( int , gy ); sx--; sy--; gx--; gy--; string S[H]; FOR( i , 0 , H ){ cin >> S[i]; // SetEdgeOnGrid( S[i] , i , e ); SetWallOnGrid( S[i] , i , non_wall ); } // {h,w}へデコード: EnumHW( v ) // {h,w}をコード: EnumHW_inv( h , w ); // (i,j)->(k,h)の方向番号を取得: DirectionNumberOnGrid( i , j , k , h ); // v->wの方向番号を取得: DirectionNumberOnGrid( v , w ); // 方向番号の反転U<->D、R<->L: ReverseDirectionNumberOnGrid( n ); i_max.resize( H ); FOR( i , 0 , H ){ i_max[i].resize( W ); } FOR( j , 0 , W ){ int temp = -1; FOR( i , 0 , H ){ non_wall[i][j] ? temp = i : temp; i_max[i][j] = temp; } } ZeroOneBreadthFirstSearch > > zobfs( HW ); // // TLに準じる乱択や全探索。デフォルトの猶予は100.0[ms]。 // CEXPR( double , TL , 2000.0 ); // while( CHECK_WATCH( TL ) ){ // } // // デバッグ用の愚直解 // auto guchoku = Guchoku( N , M , K ); // ll answer = 0; // // MP answer{}; // FOR( i , 0 , N ){ // answer += A[i]; // } // RETURN( answer ); // // COUT( answer ); // // COUT_A( A , N ); RETURN( zobfs.Solve( EnumHW_inv( sx , sy ) , EnumHW_inv( gx , gy ) ) ); FINISH_MAIN; } void Jikken() { // CEXPR( int , bound , 10 ); // FOREQ( N , 0 , bound ){ // FOREQ( M , 0 , bound ){ // FOREQ( K , 0 , bound ){ // COUT( N , M , K , ":" , Guchoku( N , M , K ) ); // } // } // // cout << Guchoku( N ) << ",\n"[N==bound]; // } } void Debug() { // CEXPR( int , bound , 10 ); // FOREQ( N , 0 , bound ){ // FOREQ( M , 0 , bound ){ // FOREQ( K , 0 , bound ){ // auto guchoku = Guchoku( N , M , K ); // auto answer = Answer( N , M , K ); // bool match = guchoku == answer; // COUT( N , M , K , ":" , guchoku , match ? "==" : "!=" , answer ); // if( !match ){ // return; // } // } // } // // auto guchoku = Guchoku( N ); // // auto answer = Answer( N ); // // bool match = guchoku == answer; // // COUT( N , ":" , guchoku , match ? "==" : "!=" , answer ); // // if( !match ){ // // return; // // } // } }