結果
問題 | No.2463 ストレートフラッシュ |
ユーザー | 👑 p-adic |
提出日時 | 2023-09-08 23:15:12 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 23,796 bytes |
コンパイル時間 | 4,011 ms |
コンパイル使用メモリ | 231,040 KB |
実行使用メモリ | 11,136 KB |
最終ジャッジ日時 | 2024-06-26 16:37:46 |
合計ジャッジ時間 | 8,082 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
10,752 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 11 ms
5,376 KB |
testcase_04 | AC | 56 ms
5,376 KB |
testcase_05 | AC | 73 ms
5,376 KB |
testcase_06 | AC | 3 ms
5,376 KB |
testcase_07 | AC | 17 ms
5,376 KB |
testcase_08 | AC | 42 ms
5,376 KB |
testcase_09 | AC | 271 ms
5,376 KB |
testcase_10 | AC | 20 ms
5,376 KB |
testcase_11 | AC | 3 ms
5,376 KB |
testcase_12 | AC | 69 ms
5,376 KB |
testcase_13 | TLE | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
ソースコード
#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( MESSAGE ) cerr << MESSAGE << endl; #define COUT( ANSWER ) cout << "出力: " << ANSWER << endl #define ASSERT( A , MIN , MAX ) CERR( "ASSERTチェック: " << ( MIN ) << ( ( MIN ) <= A ? "<=" : ">" ) << A << ( A <= ( MAX ) ? "<=" : ">" ) << ( MAX ) ); assert( ( MIN ) <= A && A <= ( MAX ) ) #define AUTO_CHECK bool auto_checked = true; AutoCheck( auto_checked ); if( auto_checked ){ return 0; }; #define START_WATCH( PROCESS_NAME ) StartWatch( PROCESS_NAME ) #define STOP_WATCH( HOW_MANY_TIMES ) StopWatch( HOW_MANY_TIMES ) #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( MESSAGE ) #define COUT( ANSWER ) cout << ANSWER << "\n" #define ASSERT( A , MIN , MAX ) assert( ( MIN ) <= A && A <= ( MAX ) ) #define AUTO_CHECK #define START_WATCH( PROCESS_NAME ) #define STOP_WATCH( HOW_MANY_TIMES ) #endif // #define RANDOM_TEST #include <bits/stdc++.h> using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; #define ATT __attribute__( ( target( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) ) ) #define TYPE_OF( VAR ) decay_t<decltype( VAR )> #define CEXPR( LL , BOUND , VALUE ) constexpr LL BOUND = VALUE #define CIN( LL , A ) LL A; cin >> A #define CIN_ASSERT( A , MIN , MAX ) TYPE_OF( MAX ) A; SET_ASSERT( A , MIN , MAX ) #define GETLINE( A ) string A; getline( cin , A ) #define GETLINE_SEPARATE( A , SEPARATOR ) string A; getline( cin , A , SEPARATOR ) #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 QUIT goto END_MAIN #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 START_MAIN REPEAT( T ){ if constexpr( bound_T > 1 ){ CERR( "testcase " << VARIABLE_FOR_REPEAT_T << ":" ); } #define FINISH_MAIN QUIT; } END_MAIN: CERR( "" ); #ifdef DEBUG inline void AlertAbort( int n ) { CERR( "abort関数が呼ばれました。assertマクロのメッセージが出力されていない場合はオーバーフローの有無を確認をしてください。" ); } void AutoCheck( bool& auto_checked ); void StartWatch( const string& process_name = "nothing" ); void StopWatch( const int& how_many_times = 1 ); #endif #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 RETURN( ANSWER ) if( ( ANSWER ) == guchoku ){ CERR( ( ANSWER ) << " == " << guchoku ); goto END_MAIN; } else { CERR( ( ANSWER ) << " != " << guchoku ); QUIT; } #else #define SET_ASSERT( A , MIN , MAX ) cin >> A; ASSERT( A , MIN , MAX ) #define RETURN( ANSWER ) COUT( ( ANSWER ) ); QUIT #endif // 算術的関数 template <typename T> inline T Absolute( const T& a ){ return a > 0 ? a : -a; } template <typename T> 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<TYPE_OF( ARGUMENT ),int>::value && ! is_same<TYPE_OF( ARGUMENT ),uint>::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<TYPE_OF( TARGET ),uint>::value && ! is_same<TYPE_OF( TARGET ),ull>::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 ); \ CERR( ( EXPRESSION DESIRED_INEQUALITY TARGET ? "二分探索成功" : "二分探索失敗" ) ); \ assert( EXPRESSION DESIRED_INEQUALITY TARGET ); \ } else { \ CERR( "二分探索失敗: " << MINIMUM << ">" << MAXIMUM ); \ assert( MINIMUM <= MAXIMUM ); \ } \ // 単調増加の時に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 <typename T> inline typename set<T>::iterator MaximumLeq( set<T>& 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 <typename T> inline typename set<T>::iterator MaximumLt( set<T>& 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 <typename T> inline typename set<T>::iterator MinimumGeq( set<T>& S , const T& t ) { return S.lower_bound( t ); } // tより大きい値が存在すればその最小値のiterator、存在しなければend()を返す。 template <typename T> inline typename set<T>::iterator MinimumGt( set<T>& S , const T& t ) { return S.upper_bound( t ); } // データ構造用関数 template <typename T> inline T add( const T& t0 , const T& t1 ) { return t0 + t1; } template <typename T> inline const T& zero() { static const T z = 0; return z; } template <typename T> inline T add_inv( const T& t ) { return -t; } template <typename T> inline T multiply( const T& t0 , const T& t1 ) { return t0 * t1; } template <typename T> inline const T& one() { static const T o = 1; return o; } template <typename T> inline T id( const T& v ) { return v; } // グリッド問題用関数 int H , W , H_minus , W_minus , HW; inline pair<int,int> 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 i<k?2:i>k?0:j<h?1:j>h?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;} // 圧縮用 #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& // 大きな素数 // inline CEXPR( ll , P , 998244353 ); // // inline CEXPR( ll , P , 1000000007 ); // データ構造使用畤のNの上限 // inline CEXPR( int , bound_N , 10 ); inline DEXPR( int , bound_N , 100000 , 100 ); // 0が5個 // inline CEXPR( int , bound_N , 1000000000 ); // 0が9個 // inline CEXPR( ll , bound_N , 1000000000000000000 ); // 0が18個 // データ構造使用畤のMの上限 // inline CEXPR( TYPE_OF( bound_N ) , bound_M , bound_N ); // inline CEXPR( int , bound_M , 10 ); inline DEXPR( int , bound_M , 100000 , 100 ); // 0が5個 // inline CEXPR( int , bound_M , 1000000000 ); // 0が9個 // inline CEXPR( ll , bound_M , 1000000000000000000 ); // 0が18個 // データ構造や壁配列使用畤のH,Wの上限 inline DEXPR( int , bound_H , 1000 , 10 ); // inline DEXPR( int , bound_H , 100000 , 10 ); // 0が5個 // inline CEXPR( int , bound_H , 1000000000 ); // 0が9個 inline CEXPR( int , bound_W , bound_H ); static_assert( ll( bound_H ) * bound_W < ll( 1 ) << 31 ); inline CEXPR( int , bound_HW , bound_H * bound_W ); // CEXPR( int , bound_HW , 100000 ); // 0が5個 // CEXPR( int , bound_HW , 1000000 ); // 0が6個 inline void SetEdgeOnGrid( const string& Si , const int& i , list<int> ( &e )[bound_HW] , 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+1<H){e[EnumHW_inv(i+1,j)].push_back(v);}if(j>0){e[EnumHW_inv(i,j-1)].push_back(v);}if(j+1<W){e[EnumHW_inv(i,j+1)].push_back(v);}}}} inline void SetWallOnGrid( const string& Si , const int& i , bool ( &non_wall )[bound_H+1][bound_W+1] , const char& walkable = '.' ){bool(&non_wall_i)[bound_W+1]=non_wall[i];FOR(j,0,W){non_wall_i[j]=Si[j]==walkable?true:(assert(Si[j]=='#'),false);}} // using path_type = int; // // using path_type = pair<int,ll>; // list<path_type> e[bound_M]; // bound_Mのデフォルト値は10^5 // // list<path_type> e[bound_HW]; // bound_HWのデフォルト値は10^6 // list<path_type> E( const int& i ) // { // list<path_type> answer = e[i]; // // 入力によらない処理 // return answer; // } // 配列の各要素がint型の範疇でも総和がそうでない場合はTをint型にすると正しく動作しないことに注意。 // InitialSegmentSumで負の入力を扱うためにuintではなくintをテンプレート引数にする。 // 使用演算: // T& T::operator=( const T& ) // T& T::operator+=( const T& ) // T operator-( const T& , const T& )(ただしIntervalSumを用いない場合は不要) // T operator<( const T& , const T& )(ただしBinarySearchを用いない場合は不要) template <typename T , int N> class BIT { private: T m_fenwick[N + 1]; public: inline BIT(); BIT( const T ( & a )[N] ); // const参照でないことに注意。 inline T Get( const int& i ) const; inline void Set( const int& i , const T& n ); inline void Set( const T ( & a )[N] ); inline BIT<T,N>& operator+=( const T ( & a )[N] ); // 0<=i<Nの場合は第i成分にnを足し、そうでない場合は何もしない。 void Add( const int& i , const T& n ); T InitialSegmentSum( const int& i_final ) const; inline T IntervalSum( const int& i_start , const int& i_final ) const; // operator+=の単位元T()より小さくない要素のみを成分に持つ場合のみサポート。 // InitialSegmentSum( i )がn以上となるiが存在する場合にその最小値を2進法で探索。 int BinarySearch( const T& n ) const; // IntervalSum( i_start , i )がt以上となるi_start以上のiが存在する場合にその最小値を2進法で探索。 inline int BinarySearch( const int& i_start , const T& n ) const; }; template <typename T , int N> inline BIT<T,N>::BIT() : m_fenwick() { static_assert( ! is_same<T,int>::value ); } template <typename T , int N> BIT<T,N>::BIT( const T ( & a )[N] ) : m_fenwick() { static_assert( ! is_same<T,int>::value ); for( int j = 1 ; j <= N ; j++ ){ T& fenwick_j = m_fenwick[j]; int i = j - 1; fenwick_j = a[i]; int i_lim = j - ( j & -j ); while( i != i_lim ){ fenwick_j += m_fenwick[i]; i -= ( i & -i ); } } } template <typename T , int N> inline T BIT<T,N>::Get( const int& i ) const { return IntervalSum( i , i ); } template <typename T , int N> inline void BIT<T,N>::Set( const int& i , const T& n ) { Add( i , n - IntervalSum( i , i ) ); } template <typename T , int N> inline void BIT<T,N>::Set( const T ( & a )[N] ) { BIT<T,N> a_copy{ a }; swap( m_fenwick , a_copy.m_fenwick ); } template <typename T , int N> inline BIT<T,N>& BIT<T,N>::operator+=( const T ( & a )[N] ) { for( int i = 0 ; i < N ; i++ ){ Add( i , a[i] ); } return *this; } template <typename T , int N> void BIT<T,N>::Add( const int& i , const T& n ) { int j = i + 1; while( j <= N ){ m_fenwick[j] += n; j += ( j & -j ); } return; } template <typename T , int N> T BIT<T,N>::InitialSegmentSum( const int& i_final ) const { T sum = 0; int j = ( i_final < N ? i_final : N - 1 ) + 1; while( j > 0 ){ sum += m_fenwick[j]; j -= j & -j; } return sum; } template <typename T , int N> inline T BIT<T,N>::IntervalSum( const int& i_start , const int& i_final ) const { return InitialSegmentSum( i_final ) - InitialSegmentSum( i_start - 1 ); } // 使用演算: // T& T::operator=( const T& )(BITそのものに使用) // T& T::operator+=( const T& ) // T& operator+( const T& , const T& ) // T operator-( const T& ) // T operator-( const T& , const T& ) template <typename T , int N> class IntervalAddBIT { private: // 母関数の微分の負の階差数列((i-1)a_{i-1} - ia_i)の管理 BIT<T,N> m_bit_0; // 階差数列(a_i - a_{i-1})の管理 BIT<T,N> m_bit_1; public: inline IntervalAddBIT(); inline IntervalAddBIT( const T ( &a )[N] ); // const参照でないことに注意。 inline T Get( const int& i ) const; inline void Set( const int& i , const T& n ); inline void Set( const T ( &a )[N] ); inline IntervalAddBIT<T,N>& operator+=( const T ( & a )[N] ); // 0<=i<Nの場合は第i成分にnを足し、そうでない場合は何もしない。 inline void Add( const int& i , const T& n ); // max(0,i_start)<=i<=min(N-1,i_final)を満たすiに対し各第i成分にnを足す。 inline void IntervalAdd( const int& i_start , const int& i_final , const T& n ); inline T InitialSegmentSum( const int& i_final ) const; inline T IntervalSum( const int& i_start , const int& i_final ) const; }; template <typename T , int N> inline IntervalAddBIT<T,N>::IntervalAddBIT() : m_bit_0() , m_bit_1() {} template <typename T , int N> inline IntervalAddBIT<T,N>::IntervalAddBIT( const T ( &a )[N] ) : m_bit_0() , m_bit_1() { operator+=( a ); } template <typename T , int N> inline T IntervalAddBIT<T,N>::Get( const int& i ) const { return IntervalSum( i , i ); } template <typename T , int N> inline void IntervalAddBIT<T,N>::Set( const int& i , const T& n ) { Add( i , n - IntervalSum( i , i ) ); } template <typename T , int N> inline void IntervalAddBIT<T,N>::Set( const T ( &a )[N] ) { IntervalAddBIT<T,N> a_copy{ a }; swap( m_bit_0 , a_copy.m_bit_0 ); swap( m_bit_1 , a_copy.m_bit_1 ); } template <typename T , int N> inline IntervalAddBIT<T,N>& IntervalAddBIT<T,N>::operator+=( const T ( & a )[N] ) { for( int i = 0 ; i < N ; i++ ){ Add( i , a[i] ); } return *this; } template <typename T , int N> inline void IntervalAddBIT<T,N>::Add( const int& i , const T& n ) { IntervalAdd( i , i , n ); } template <typename T , int N> inline void IntervalAddBIT<T,N>::IntervalAdd( const int& i_start , const int& i_final , const T& n ) { m_bit_0.Add( i_start , - ( i_start - 1 ) * n ); m_bit_0.Add( i_final + 1 , i_final * n ); m_bit_1.Add( i_start , n ); m_bit_1.Add( i_final + 1 , - n ); } template <typename T , int N> inline T IntervalAddBIT<T,N>::InitialSegmentSum( const int& i_final ) const { return m_bit_0.InitialSegmentSum( i_final ) + i_final * m_bit_1.InitialSegmentSum( i_final ); } template <typename T , int N> inline T IntervalAddBIT<T,N>::IntervalSum( const int& i_start , const int& i_final ) const { return InitialSegmentSum( i_final ) - InitialSegmentSum( i_start - 1 ); } int main() { UNTIE; AUTO_CHECK; TEST_CASE_NUM( 1 ); START_MAIN; CIN( ll , N ); CIN( ll , M ); // // CIN_ASSERT( N , 1 , bound_N ); // 基本不要、上限のデフォルト値は10^5 // // CIN_ASSERT( M , 1 , bound_M ); // 基本不要、上限のデフォルト値は10^5 // CIN( string , S ); // CIN( string , T ); // 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 // string S[H]; // // bool non_wall[bound_H+1][bound_W+1]={}; // FOR( i , 0 , H ){ // cin >> S[i]; // // SetEdgeOnGrid( S[i] , i , e ); // eの宣言のコメントアウト必要 // // SetWallOnGrid( S[i] , i , non_wall ); // } // // (i,j)->(k,h)の方向番号を取得: DirectionNumberOnGrid( i , j , k , h ); // // v->wの方向番号を取得: DirectionNumberOnGrid( v , w ); // // 方向番号の反転U<->D、R<->L: ReverseDirectionNumberOnGrid( n ); ll A[N*M]; ll B[N*M]; // ll A[bound_N]; // 基本不要、長さのデフォルト値は10^5 // ll B[bound_N]; // 基本不要、長さのデフォルト値は10^5 FOR( i , 0 , N * M ){ cin >> A[i]; cin >> B[i]; } ll answer = N * M; FOREQ( m , 1 , M ){ set<pair<int,int> > hand{}; set<pair<int,int> > sf{ { 1 , m } , { N , m } , { N - 1 , m } , { N - 2 , m } , { N - 3 , m } }; FOR( i , 0 , 5 ){ hand.insert( { A[i] , B[i] } ); } int i = 5; ll temp = 0; while( hand != sf ){ AUTO_ITR( hand ); while( itr_hand != end_hand ){ if( sf.count( *itr_hand ) == 0 ){ itr_hand = hand.erase( itr_hand ); } else { itr_hand++; } } while( hand.size() < 5 ){ hand.insert( { A[i] , B[i] } ); i++; } temp++; } answer = min( answer , temp ); } FOREQ( n , 1 , N - 4 ){ FOREQ( m , 1 , M ){ set<pair<int,int> > hand{}; set<pair<int,int> > sf{ { n , m } , { n + 1 , m } , { n + 2 , m } , { n + 3 , m } , { n + 4 , m } }; FOR( i , 0 , 5 ){ hand.insert( { A[i] , B[i] } ); } int i = 5; ll temp = 0; while( hand != sf ){ AUTO_ITR( hand ); while( itr_hand != end_hand ){ if( sf.count( *itr_hand ) == 0 ){ itr_hand = hand.erase( itr_hand ); } else { itr_hand++; } } while( hand.size() < 5 ){ hand.insert( { A[i] , B[i] } ); i++; } temp++; } answer = min( answer , temp ); } } // FOR( i , 0 , M ){ // CIN_ASSERT( ui , 1 , N ); // CIN_ASSERT( vi , 1 , N ); // ui--; // vi--; // e[ui].push_back( vi ); // e[vi].push_back( ui ); // } // // ll guchoku = Guchoku(); // ll answer = 0; // FOR( i , 0 , N ){ // answer += A[i]; // } // // COUT( ( answer ) ); RETURN( answer ); FINISH_MAIN; }