結果
問題 | No.2456 Stamp Art |
ユーザー | 👑 p-adic |
提出日時 | 2023-08-19 21:07:34 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 907 ms / 5,000 ms |
コード長 | 21,907 bytes |
コンパイル時間 | 3,529 ms |
コンパイル使用メモリ | 231,388 KB |
実行使用メモリ | 128,704 KB |
最終ジャッジ日時 | 2024-06-11 17:54:38 |
合計ジャッジ時間 | 16,295 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 1 ms
6,944 KB |
testcase_03 | AC | 754 ms
128,576 KB |
testcase_04 | AC | 2 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,944 KB |
testcase_06 | AC | 808 ms
128,452 KB |
testcase_07 | AC | 767 ms
128,576 KB |
testcase_08 | AC | 634 ms
128,576 KB |
testcase_09 | AC | 782 ms
128,576 KB |
testcase_10 | AC | 868 ms
128,576 KB |
testcase_11 | AC | 727 ms
128,576 KB |
testcase_12 | AC | 718 ms
128,576 KB |
testcase_13 | AC | 907 ms
128,576 KB |
testcase_14 | AC | 871 ms
128,528 KB |
testcase_15 | AC | 715 ms
128,572 KB |
testcase_16 | AC | 341 ms
81,200 KB |
testcase_17 | AC | 2 ms
6,940 KB |
testcase_18 | AC | 2 ms
6,940 KB |
testcase_19 | AC | 470 ms
88,424 KB |
testcase_20 | AC | 788 ms
128,576 KB |
testcase_21 | AC | 766 ms
119,396 KB |
testcase_22 | AC | 834 ms
128,704 KB |
testcase_23 | AC | 23 ms
37,628 KB |
testcase_24 | AC | 54 ms
24,120 KB |
testcase_25 | AC | 2 ms
6,944 KB |
ソースコード
// 入力フォーマットチェック #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 ) ) #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 ) ) #endif #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 SET_ASSERT( A , MIN , MAX ) cin >> A; ASSERT( A , MIN , MAX ) #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 return 0 #define RETURN( ANSWER ) COUT( ( ANSWER ) ); QUIT #ifdef DEBUG inline void AlertAbort( int n ) { CERR( "abort関数が呼ばれました。assertマクロのメッセージが出力されていない場合はオーバーフローの有無を確認をしてください。" ); } #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 ); } // 二分探索テンプレート // 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 ) \ // 入力フォーマットチェック用 // 1行中の変数の個数を確認 #define GETLINE_COUNT( S , VARIABLE_NUMBER ) GETLINE( S ); int VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S = 0; int VARIABLE_FOR_SIZE_FOR_GETLINE_FOR_ ## S = S.size(); { int size = S.size(); int count = 0; for( int i = 0 ; i < size ; i++ ){ if( S.substr( i , 1 ) == " " ){ count++; } } assert( count + 1 == VARIABLE_NUMBER ); } // 余計な入力の有無を確認 #ifdef DEBUG #define CHECK_REDUNDANT_INPUT #else // #define CHECK_REDUNDANT_INPUT string VARIABLE_FOR_CHECK_REDUNDANT_INPUT = ""; cin >> VARIABLE_FOR_CHECK_REDUNDANT_INPUT; assert( VARIABLE_FOR_CHECK_REDUNDANT_INPUT == "" ); assert( ! cin ) #define CHECK_REDUNDANT_INPUT string VARIABLE_FOR_CHECK_REDUNDANT_INPUT = ""; getline( cin , VARIABLE_FOR_CHECK_REDUNDANT_INPUT ); assert( VARIABLE_FOR_CHECK_REDUNDANT_INPUT == "" ); assert( ! cin ) #endif // |N| <= BOUNDを満たすNをSから構築 #define STOI( S , N , BOUND ) TYPE_OF( BOUND ) N = 0; { bool VARIABLE_FOR_POSITIVITY_FOR_GETLINE = true; assert( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S < VARIABLE_FOR_SIZE_FOR_GETLINE_FOR_ ## S ); if( S.substr( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S , 1 ) == "-" ){ VARIABLE_FOR_POSITIVITY_FOR_GETLINE = false; VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S ++; assert( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S < VARIABLE_FOR_SIZE_FOR_GETLINE_FOR_ ## S ); } assert( S.substr( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S , 1 ) != " " ); string VARIABLE_FOR_LETTER_FOR_GETLINE{}; int VARIABLE_FOR_DIGIT_FOR_GETLINE{}; while( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S < VARIABLE_FOR_SIZE_FOR_GETLINE_FOR_ ## S ? ( VARIABLE_FOR_LETTER_FOR_GETLINE = S.substr( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S , 1 ) ) != " " : false ){ VARIABLE_FOR_DIGIT_FOR_GETLINE = stoi( VARIABLE_FOR_LETTER_FOR_GETLINE ); assert( N < BOUND / 10 ? true : N == BOUND / 10 && VARIABLE_FOR_DIGIT_FOR_GETLINE <= BOUND % 10 ); N = N * 10 + VARIABLE_FOR_DIGIT_FOR_GETLINE; VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S ++; } if( ! VARIABLE_FOR_POSITIVITY_FOR_GETLINE ){ N *= -1; } if( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S < VARIABLE_FOR_SIZE_FOR_GETLINE_FOR_ ## S ){ VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S ++; } } // SをSEPARATORで区切りTを構築 #define SEPARATE( S , T , SEPARATOR ) string T{}; { assert( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S < VARIABLE_FOR_SIZE_FOR_GETLINE_FOR_ ## S ); int VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S_prev = VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S; assert( S.substr( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S , 1 ) != SEPARATOR ); string VARIABLE_FOR_LETTER_FOR_GETLINE{}; while( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S < VARIABLE_FOR_SIZE_FOR_GETLINE_FOR_ ## S ? ( VARIABLE_FOR_LETTER_FOR_GETLINE = S.substr( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S , 1 ) ) != SEPARATOR : false ){ VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S ++; } T = S.substr( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S_prev , VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S - VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S_prev ); if( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S < VARIABLE_FOR_SIZE_FOR_GETLINE_FOR_ ## S ){ VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S ++; } } // 2次元配列上の累積和。 // 入力の範囲内で要件 // (1) (T,m_T:T^2->T,e_T:1->T,i_T:T->T)が可換群である。 // が成り立つ場合のみサポート。 // 配列による初期化O(size_X*size_Y) // 始矩形和O(1) // 矩形和O(1) template <typename T , T m_T(const T&,const T&) , const T& e_T() , T i_T(const T&)> class TwoDimensionalCumulativeSum { private: int m_size_X; int m_size_Y; vector<vector<T> > m_a; public: TwoDimensionalCumulativeSum( const vector<vector<T> >& a ); template <int size_X_max , int size_Y_max> TwoDimensionalCumulativeSum( const T ( &a )[size_X_max][size_Y_max] , const int& size_X , const int& size_Y ); // 条件 // (1) -1 <= i_final_x < m_size_X // (2) -1 <= i_final_y < m_size_Y // を満たす場合のみサポート。 // a[0][i_start_y]...a[i_final_x][i_start_y]... // a[0][i_final_y]...a[i_final_x][i_final_y] // をm_Tに関して計算する。 inline const T& InitialRectangleSum( const int& i_x , const int& i_y ) const; // 条件 // (1) 0 <= i_start_xかつi_start_x-1 <= i_final_x < m_size_X // (2) 0 <= i_start_yかつi_start_y-1 <= i_final_y < m_size_Y // を満たす場合のみサポート。 // a[i_start_x][i_start_y]...a[i_final_x][i_start_y]... // a[i_start_x][i_final_y]...a[i_final_x][i_final_y] // をm_Tに関して計算する。 inline T RectangleSum( const int& i_start_x , const int& i_start_y , const int& i_final_x , const int& i_final_y ) const; }; template <typename T , T m_T(const T&,const T&) , const T& e_T() , T i_T(const T&)> TwoDimensionalCumulativeSum<T,m_T,e_T,i_T>::TwoDimensionalCumulativeSum( const vector<vector<T> >& a ) : m_size_X( a.size() ) , m_size_Y() , m_a( m_size_X + 1 ) { static_assert( ! is_same<T,int>::value ); const T& zero = e_T(); if( ! a.empty() ){ m_size_Y = a[0].size(); } m_a[0] = vector<T>( m_size_Y + 1 , zero ); for( int x = 0 ; x < m_size_X ; x++ ){ const vector<T>& a_x = a[x]; const vector<T>& m_a_x_minus = m_a[x]; vector<T>& m_a_x = m_a[x+1]; m_a_x = vector<T>( m_size_Y + 1 , zero ); T temp = zero; for( int y = 0 ; y < m_size_Y ; y++ ){ m_a_x[y+1] = m_T( m_a_x_minus[y+1] , temp = m_T( temp , a_x[y] ) ); } } } template <typename T , T m_T(const T&,const T&) , const T& e_T() , T i_T(const T&)> template <int size_X_max , int size_Y_max> TwoDimensionalCumulativeSum<T,m_T,e_T,i_T>::TwoDimensionalCumulativeSum( const T ( &a )[size_X_max][size_Y_max] , const int& size_X , const int& size_Y ) : m_size_X( size_X ) , m_size_Y( size_Y ) , m_a( m_size_X + 1 , vector<T>( m_size_Y + 1 , e_T() ) ) { assert( m_size_X <= size_X_max && m_size_Y <= size_Y_max ); const T& zero = e_T(); for( int x = 0 ; x < m_size_X ; x++ ){ const T ( &a_x )[size_Y_max] = a[x]; const vector<T>& m_a_x_minus = m_a[x]; vector<T>& m_a_x = m_a[x+1]; T temp = zero; for( int y = 0 ; y < m_size_Y ; y++ ){ m_a_x[y+1] = m_T( m_a_x_minus[y+1] , temp = m_T( temp , a_x[y] ) ); } } } template <typename T , T m_T(const T&,const T&) , const T& e_T() , T i_T(const T&)> inline const T& TwoDimensionalCumulativeSum<T,m_T,e_T,i_T>::InitialRectangleSum( const int& i_x , const int& i_y ) const { assert( - 1 <= i_x && i_x < m_size_X && - 1 <= i_y && i_y < m_size_Y ); return m_a[i_x+1][i_y+1]; } template <typename T , T m_T(const T&,const T&) , const T& e_T() , T i_T(const T&)> inline T TwoDimensionalCumulativeSum<T,m_T,e_T,i_T>::RectangleSum( const int& i_start_x , const int& i_start_y , const int& i_final_x , const int& i_final_y ) const { assert( 0 <= i_start_x && i_start_x - 1 <= i_final_x && i_final_x < m_size_X && 0 <= i_start_y && i_start_y - 1 <= i_final_y && i_final_y < m_size_Y ); return m_T( m_T( m_a[i_start_x][i_start_y] , i_T( m_T( m_a[i_final_x+1][i_start_y] , m_a[i_start_x][i_final_y+1] ) ) ) , m_a[i_final_x+1][i_final_y+1] ); } // 2次元配列上の階差数列。基本的に2次元imos法 // https://imoz.jp/algorithms/imos_method.html // に準拠。 // 入力の範囲内で要件 // (6) (T,operator+:T^2->T,T(),operator-:T->T)は可換群である。 // が成り立つ場合にのみサポート。 // initによる初期化O(size_X*size_Y) // 配列による初期化O(size_X*size_Y) // 一点代入O(size_X*size_Y)(作用の遅延評価を解消する。元々作用の遅延評価がない場合はO(1)) // 一点取得O(size_X*size_Y)(作用の遅延評価を解消する。元々作用の遅延評価がない場合はO(1)) // 一点加算O(1)(作用を遅延評価しない) // 始矩形加算O(1)(作用を遅延評価する) // 矩形加算O(1)(作用を遅延評価する) // 加法O(size_X*size_Y)(作用の遅延評価を解消する) template <typename T> class TwoDimensionalDifferenceSequence { private: int m_size_X; int m_size_Y; vector<vector<T> > m_a; vector<vector<T> > m_lazy_addition; bool m_updated; public: inline TwoDimensionalDifferenceSequence( const vector<vector<T> >& a ); inline TwoDimensionalDifferenceSequence( vector<vector<T> >&& a ); inline TwoDimensionalDifferenceSequence( const int& size_X , const int& size_Y ); template <int size_X_max , int size_Y_max> inline TwoDimensionalDifferenceSequence( const T ( &a )[size_X_max][size_Y_max] , const int& size_X , const int& size_Y ); // 作用の遅延評価を解消してから値を代入する。 inline void Set( const int& i_x , const int& i_y , const T& t ); // 作用の遅延評価を解消してから値を参照する。 inline const T& Get( const int& i_x , const int& i_y ); // 作用の遅延評価を解消せずに元々の値を参照する。 inline T& Ref( const int& i_x , const int& i_y ); // (i_x,i_y)での値にyを遅延評価せずに加算する。 inline void Add( const int& i_x , const int& i_y , const T& t ); // tを遅延評価で加算する。 inline void RectangleAdd( const int& i_x_start , const int& i_start_y , const int& i_final_x , const int& i_final_y , const T& t ); // tを遅延評価で減算する。 inline void RectangleSubtract( const int& i_x_start , const int& i_start_y , const int& i_final_x , const int& i_final_y , const T& t ); // 作用の遅延評価を解消してから全体を加算する。 inline TwoDimensionalDifferenceSequence<T>& operator+=( const TwoDimensionalDifferenceSequence<T>& a ); // 作用の遅延評価を解消する。 void Update(); }; template <typename T> inline TwoDimensionalDifferenceSequence<T>::TwoDimensionalDifferenceSequence( const vector<vector<T> >& a ) : m_size_X( a.size() ) , m_size_Y() , m_a( a ) , m_lazy_addition( m_size_X , vector<T>( m_size_X > 0 ? m_a.front().size() : 0 ) ) , m_updated( false ) { static_assert( ! is_same<T,int>::value ); } template <typename T> inline TwoDimensionalDifferenceSequence<T>::TwoDimensionalDifferenceSequence( vector<vector<T> >&& a ) : m_size_X( a.size() ) , m_size_Y() , m_a( move( a ) ) , m_lazy_addition( m_size_X , vector<T>( m_size_X > 0 ? m_a.front().size() : 0 ) ) , m_updated( false ) { static_assert( ! is_same<T,int>::value ); } template <typename T> inline TwoDimensionalDifferenceSequence<T>::TwoDimensionalDifferenceSequence( const int& size_X , const int& size_Y ) : m_size_X( size_X ) , m_size_Y( size_Y ) , m_a( m_size_X , vector<T>( m_size_Y ) ) , m_lazy_addition( m_size_X , vector<T>( m_size_Y ) ) , m_updated( false ) { static_assert( ! is_same<T,int>::value ); } template <typename T> template <int size_X_max , int size_Y_max> inline TwoDimensionalDifferenceSequence<T>::TwoDimensionalDifferenceSequence( const T ( &a )[size_X_max][size_Y_max] , const int& size_X , const int& size_Y ) : m_size_X( size_X ) , m_size_Y( size_Y ) , m_a( m_size_X ) , m_lazy_addition( m_size_X ) , m_updated( false ) { static_assert( ! is_same<T,int>::value ); assert( m_size_X <= size_X_max && m_size_Y <= size_Y_max ); for( int x = 0 ; x < m_size_X ; x++ ){ const T ( &a_x )[size_Y_max] = a[x]; vector<T>& m_a_x = m_a[x]; m_a_x.reserve( m_size_Y ); for( int y = 0 ; y < m_size_Y ; y++ ){ m_a_x.push_back( a_x[y] ); } } } template <typename T> inline void TwoDimensionalDifferenceSequence<T>::Set( const int& i_x , const int& i_y , const T& t ) { Update(); m_a[i_x][i_y] = t; } template <typename T> inline const T& TwoDimensionalDifferenceSequence<T>::Get( const int& i_x , const int& i_y ) { Update(); return m_a[i_x][i_y]; } template <typename T> inline T& TwoDimensionalDifferenceSequence<T>::Ref( const int& i_x , const int& i_y ) { return m_a[i_x][i_y]; } template <typename T> inline void TwoDimensionalDifferenceSequence<T>::Add( const int& i_x , const int& i_y , const T& t ) { m_a[i_x][i_y] += t; } template <typename T> inline void TwoDimensionalDifferenceSequence<T>::RectangleAdd( const int& i_start_x , const int& i_start_y , const int& i_final_x , const int& i_final_y , const T& t ) { m_updated = true; vector<T>& m_lazy_addition_i_start_x = m_lazy_addition[i_start_x]; m_lazy_addition_i_start_x[i_start_y] += t; const int i_final_y_plus = i_final_y + 1; if( i_final_y_plus < m_size_Y ){ m_lazy_addition_i_start_x[i_final_y_plus] -= t; } const int i_final_x_plus = i_final_x + 1; if( i_final_x_plus < m_size_X ){ vector<T>& m_lazy_addition_i_final_x_plus = m_lazy_addition[i_final_x_plus]; m_lazy_addition_i_final_x_plus[i_start_y] -= t; if( i_final_y_plus < m_size_Y ){ m_lazy_addition_i_final_x_plus[i_final_y_plus] += t; } } return; } template <typename T> inline void TwoDimensionalDifferenceSequence<T>::RectangleSubtract( const int& i_start_x , const int& i_start_y , const int& i_final_x , const int& i_final_y , const T& t ) { RectangleAdd( i_start_x , i_start_y , i_final_x , i_final_y , -t ); } template <typename T> inline TwoDimensionalDifferenceSequence<T>& TwoDimensionalDifferenceSequence<T>::operator+=( const TwoDimensionalDifferenceSequence<T>& a ) { assert( m_size_X == a.m_size_X && m_size_Y == a.m_size_Y ); for( int x = 0 ; x < m_size_X ; x++ ){ vector<T>& m_a_x = m_a[x]; vector<T>& m_lazy_addition_x = m_a[x]; const vector<T>& a_x = a.m_a[x]; const vector<T>& lazy_addition_x = a.m_lazy_addition[x]; for( int y = 0 ; y < m_size_Y ; y++ ){ m_a_x[y] += a_x[y]; m_lazy_addition_x[y] += lazy_addition_x[y]; } } Update(); return *this; } template <typename T> void TwoDimensionalDifferenceSequence<T>::Update() { if( ! m_updated ){ return; } vector<T> diff( m_size_Y ); T zero{}; for( int x = 0 ; x < m_size_X ; x++ ){ vector<T>& m_a_x = m_a[x]; vector<T>& m_lazy_addition_x = m_lazy_addition[x]; T temp = zero; for( int y = 0 ; y < m_size_Y ; y++ ){ T& m_lazy_addition_xy = m_lazy_addition_x[y]; m_a_x[y] += diff[y] += temp += m_lazy_addition_xy; m_lazy_addition_xy = zero; } } m_updated = false; return; } inline DEXPR( int , bound_H , 2000 , 10 ); 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 ); int H , W , HW; 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; } ll black[bound_H][bound_W]; int Constructible( const ll& k , const TwoDimensionalCumulativeSum<ll,add<ll>,zero<ll>,add_inv<ll> >& tdca ) { ll k2 = k * k; int H_k = H - k; int W_k = W - k; TwoDimensionalDifferenceSequence<ll> tdds{ H , W }; FOREQ( i , 0 , H_k ){ FOREQ( j , 0 , W_k ){ if( tdca.RectangleSum( i , j , i + k - 1 , j + k - 1 ) == k2 ){ tdds.RectangleAdd( i , j , i + k - 1 , j + k - 1 , 1 ); } } } FOR( i , 0 , H ){ ll ( &black_i )[bound_W] = black[i]; FOR( j , 0 , W ){ if( ( black_i[j] > 0 ) != ( tdds.Get( i , j ) > 0 ) ){ return 0; } } } return 1; } int main() { UNTIE; GETLINE_COUNT( HW_str , 2 ); STOI( HW_str , H_copy , bound_H ); H = H_copy; STOI( HW_str , W_copy , bound_W ); W = W_copy; cerr << H << "," << W << endl; HW = H * W; FOR( i , 0 , H ){ GETLINE( Si ); ll ( &black_i )[bound_W] = black[i]; FOR( j , 0 , W ){ const char& Sij = Si[j]; if( Sij == '.' ){ black_i[j] = 0; } else{ assert( Sij == '#' ); black_i[j] = 1; } } } CHECK_REDUNDANT_INPUT; TwoDimensionalCumulativeSum<ll,add<ll>,zero<ll>,add_inv<ll> > tdca{ black , H , W }; BS3( answer , 1 , min( H , W ) , Constructible( answer , tdca ) , 1 ); RETURN( answer ); }