#ifdef DEBUG #define _GLIBCXX_DEBUG #define REPEAT_MAIN( BOUND ) START_MAIN; signal( SIGABRT , &AlertAbort ); AutoCheck( exec_mode ); if( exec_mode == debug_mode || exec_mode == library_search_mode ){ return 0; } else if( exec_mode == experiment_mode ){ Experiment(); return 0; } else if( exec_mode == small_test_mode ){ SmallTest(); return 0; }; DEXPR( int , bound_test_case_num , BOUND , min( BOUND , 100 ) ); int test_case_num = 1; if( exec_mode == solve_mode ){ if constexpr( bound_test_case_num > 1 ){ SET_ASSERT( test_case_num , 1 , bound_test_case_num ); } } else if( exec_mode == random_test_mode ){ CERR( "ランダムテストを行う回数を指定してください。" ); cin >> test_case_num; } FINISH_MAIN #define DEXPR( LL , BOUND , VALUE , DEBUG_VALUE ) CEXPR( LL , BOUND , DEBUG_VALUE ) #define ASSERT( A , MIN , MAX ) CERR( "ASSERTチェック: " , ( MIN ) , ( ( MIN ) <= A ? "<=" : ">" ) , A , ( A <= ( MAX ) ? "<=" : ">" ) , ( MAX ) ); assert( ( MIN ) <= A && A <= ( MAX ) ) #define SET_ASSERT( A , MIN , MAX ) if( exec_mode == solve_mode ){ cin >> A; ASSERT( A , MIN , MAX ); } else if( exec_mode == random_test_mode ){ CERR( #A , " = " , ( A = GetRand( MIN , MAX ) ) ); } else { assert( false ); } #define SOLVE_ONLY static_assert( __FUNCTION__[0] == 'S' ) #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 #else #pragma GCC optimize ( "O3" ) #pragma GCC optimize ( "unroll-loops" ) #pragma GCC target ( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) #define REPEAT_MAIN( BOUND ) START_MAIN; CEXPR( int , bound_test_case_num , BOUND ); int test_case_num = 1; if constexpr( bound_test_case_num > 1 ){ SET_ASSERT( test_case_num , 1 , bound_test_case_num ); } FINISH_MAIN #define DEXPR( LL , BOUND , VALUE , DEBUG_VALUE ) CEXPR( LL , BOUND , VALUE ) #define ASSERT( A , MIN , MAX ) assert( ( MIN ) <= A && A <= ( MAX ) ) #define SET_ASSERT( A , MIN , MAX ) cin >> A; ASSERT( A , MIN , MAX ) #define SOLVE_ONLY #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" #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 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 FINISH_MAIN REPEAT( test_case_num ){ if constexpr( bound_test_case_num > 1 ){ CERR( "testcase " , VARIABLE_FOR_REPEAT_test_case_num , ":" ); } Solve(); CERR( "" ); } } #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 TYPE_OF( VAR ) decay_t #define CEXPR( LL , BOUND , VALUE ) constexpr LL BOUND = VALUE #define CIN( LL , ... ) SOLVE_ONLY; LL __VA_ARGS__; VariadicCin( cin , __VA_ARGS__ ) #define CIN_ASSERT( A , MIN , MAX ) TYPE_OF( MAX ) A; SET_ASSERT( A , MIN , MAX ) #define SET_A( A , N ) SOLVE_ONLY; FOR( VARIABLE_FOR_CIN_A , 0 , N ){ cin >> A[VARIABLE_FOR_CIN_A]; } #define CIN_A( LL , A , N ) LL A[N]; SET_A( A , N ); #define GETLINE_SEPARATE( SEPARATOR , ... ) SOLVE_ONLY; string __VA_ARGS__; VariadicGetline( cin , SEPARATOR , __VA_ARGS__ ) #define GETLINE( ... ) SOLVE_ONLY; GETLINE_SEPARATE( '\n' , __VA_ARGS__ ) #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 RETURN( ... ) SOLVE_ONLY; COUT( __VA_ARGS__ ); return #define COMPARE( ... ) auto naive = Naive( __VA_ARGS__ ); auto answer = Answer( __VA_ARGS__ ); bool match = naive == answer; COUT( #__VA_ARGS__ , ":" , naive , match ? "==" : "!=" , answer ); if( !match ){ return; } // 入出力用 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 ); } #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 XorAdd( 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 AddInv( const T& t ) { return -t; } template inline T Id( const T& v ) { return v; } template inline T Min( const T& a , const T& b ){ return a < b ? a : b; } template inline T Max( const T& a , const T& b ){ return a < b ? b : a; } // グリッド問題用 int H , W , H_minus , W_minus , HW; vector > non_wall; inline T2 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& exec_mode ); inline void Solve(); inline void Experiment(); inline void SmallTest(); inline void RandomTest(); ll GetRand( const ll& Rand_min , const ll& Rand_max ); int exec_mode; CEXPR( int , solve_mode , 0 ); CEXPR( int , debug_mode , 1 ); CEXPR( int , library_search_mode , 2 ); CEXPR( int , experiment_mode , 3 ); CEXPR( int , small_test_mode , 4 ); CEXPR( int , random_test_mode , 5 ); #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 ライブラリは以下に挿入する。 // 入力の範囲内で要件 // (1) (T,A_T:T^2->T)がmeet半束(可換羃等半群)である。 // (以下A_Tの定める等号つき半順序を<=と置く) // (2) (T,m_T:T^2->T,e_T:1->T)がモノイドである。 // (3) m_TがA_T上分配的、つまり // - Tの任意の要素s,t,uに対しならば // - m_T(u,A_T(s,t)) = A_T(m_T(u,s),m_T(u,t)) // - m_T(A_T(s,t),u) = A_T(m_T(s,u),m_T(t,u)) // である。 // (4) dの値は<=に関してe_T()以上またはinftyである。 // (5) inftyが<=に関してe_T()以上の項size_max個以下のm_Tに関する積で表せない。 // が成り立つ場合にのみサポート。 // ただしinftyが<=から定まる場合、(3)は // (3)' m_Tが<=に関する順序モノイド演算、つまり // - Tの任意の要素s,t,uに対しs<=tならば // - m_T(u,s) <= m_T(u,t) // - m_T(s,u) <= m_T(t,u) // である。 // と同値であることに注意。 // O(size_max^3)で全経路の重み(edgeごとの重みのm_Tに関する積)の // A_Tに関する下限(多変数化したA_Tへの適用値)を計算。 template void FloydWarshall( const T ( &d )[size_max][size_max] , T ( &weight )[size_max][size_max] , const int& size , const T& infty ) { for( int i = 0 ; i < size ; i++ ){ T ( &weight_i )[size_max] = weight[i]; const T ( &d_i )[size_max] = d[i]; for( int j = 0 ; j < size ; j++ ){ weight_i[j] = d_i[j]; } } for( int k = 0 ; k < size ; k++ ){ T ( &weight_k )[size_max] = weight[k]; for( int i = 0 ; i < size ; i++ ){ T ( &weight_i )[size_max] = weight[i]; const T& weight_ik = weight_i[k]; if( weight_ik != infty ){ for( int j = 0 ; j < size ; j++ ){ const T& weight_kj = weight_k[j]; if( weight_kj != infty ){ T& weight_ij = weight_i[j]; const T weight_curr = m_T( weight_ik , weight_kj ); weight_ij = weight_ij == infty ? weight_curr : A_T( weight_ij , weight_curr ); } } } } } return; } #define AREA_OF_TRIANGLE ( x1 - x0 ) * ( y2 - y0 ) - ( y1 - y0 ) * ( x2 - x0 ) #define CALL_AREA_OF_TRIANGLE Area( v0.first , v0.second , v1.first , v1.second , v2.first , v2.second ) // xy平面内の3点がなす三角形の符号つき面積 template inline ll Area( const INT& x0 , const INT& y0 , const INT& x1 , const INT& y1 , const INT& x2 , const INT& y2 ); inline double Area( const double& x0 , const double& y0 , const double& x1 , const double& y1 , const double& x2 , const double& y2 ); template inline ll Area( const pair& v0 , const pair& v1 , const pair& v2 ); inline double Area( const pair& v0 , const pair& v1 , const pair& v2 ); // xy平面内の2線分(1点に潰れている場合含む)の交差判定 // 返り値1:交差する // 返り値0:交差しないが、接触するか線分が1点に潰れている // 返り値-1:接触しないかつ線分が1点に潰れてもいない template inline int Intersect( const INT& x0 , const INT& y0 , const INT& x1 , const INT& y1 , const INT& z0 , const INT& w0 , const INT& z1 , const INT& w1 ); inline int Intersect( const double& x0 , const double& y0 , const double& x1 , const double& y1 , const double& z0 , const double& w0 , const double& z1 , const double& w1 , const double& epsilon ); template inline int Intersect( const pair& v0 , const pair& v1 , const pair& w0 , const pair& w1 ); inline int Intersect( const pair& v0 , const pair& v1 , const pair& w0 , const pair& w1 , const double& epsilon ); inline ll Area_ll( const ll& x0 , const ll& y0 , const ll& x1 , const ll& y1 , const ll& x2 , const ll& y2 ) { return AREA_OF_TRIANGLE; } template inline ll Area( const INT& x0 , const INT& y0 , const INT& x1 , const INT& y1 , const INT& x2 , const INT& y2 ) { return Area_ll( x0 ,y0 , x1 ,y1 , x2 ,y2 ); } inline double Area( const double& x0 , const double& y0 , const double& x1 , const double& y1 , const double& x2 , const double& y2 ) { return AREA_OF_TRIANGLE; } template inline ll Area( const pair& v0 , const pair& v1 , const pair& v2 ) { return CALL_AREA_OF_TRIANGLE; } inline double Area( const pair& v0 , const pair& v1 , const pair& v2 ) { return CALL_AREA_OF_TRIANGLE; } template inline int Intersect( const INT& x0 , const INT& y0 , const INT& x1 , const INT& y1 , const INT& z0 , const INT& w0 , const INT& z1 , const INT& w1 ) { static_assert( ! is_same::value ); const ll sign0 = Area( x0 , y0 , x1 , y1 , z0 , w0 ) * Area( x0 , y0 , x1 , y1 , z1 , w1 ); const ll sign1 = Area( z0 , w0 , z1 , w1 , x0 , y0 ) * Area( z0 , w0 , z1 , w1 , x1 , y1 ); return ( sign0 < 0 && sign1 < 0 ) ? 1 : ( sign0 == 0 || sign1 == 0 ) ? 0 : -1; } inline int Intersect( const double& x0 , const double& y0 , const double& x1 , const double& y1 , const double& z0 , const double& w0 , const double& z1 , const double& w1 , const double& epsilon ) { const double sign0 = Area( x0 , y0 , x1 , y1 , z0 , w0 ) * Area( x0 , y0 , x1 , y1 , z1 , w1 ); const double sign1 = Area( z0 , w0 , z1 , w1 , x0 , y0 ) * Area( z0 , w0 , z1 , w1 , x1 , y1 ); return ( sign0 <= -epsilon && sign1 <= -epsilon ) ? 1 : ( ( -epsilon < sign0 && sign0 < epsilon ) || ( -epsilon < sign1 && sign1 < epsilon ) ) ? 0 : -1; } template inline int Intersect( const pair& v0 , const pair& v1 , const pair& w0 , const pair& w1 ) { return Intersect( v0.first , v0.second , v1.first , v1.second , w0.first , w0.second , w1.first , w1.second ); } inline int Intersect( const pair& v0 , const pair& v1 , const pair& w0 , const pair& w1 , const double& epsilon ) { return Intersect( v0.first , v0.second , v1.first , v1.second , w0.first , w0.second , w1.first , w1.second , epsilon ); } // AAA ライブラリは以上に挿入する。 void Solve() { CEXPR( int , bound_N , 150 ); CIN_ASSERT( N , 2 , bound_N ); DEXPR( int , bound_M , 200000 , 100 ); // 0が5個 CIN_ASSERT( M , 1 , bound_M ); CEXPR( int , bound_xy , 10000 ); pair xy[bound_N * 2]; int N2 = N * 2; FOR( i , 0 , N2 ){ CIN_ASSERT( xi , -bound_xy , bound_xy ); CIN_ASSERT( yi , -bound_xy , bound_xy ); xy[i] = { xi , yi }; } T2 ab[bound_M * 2]; int M2 = M * 2; FOR( i , 0 , M2 ){ CIN_ASSERT( ai , 1 , N ); CIN_ASSERT( bi , 1 , 2 ); ab[i] = { --ai , --bi }; } double d[bound_N*2][bound_N*2]; double infty = -1.0; FOR( i , 0 , N2 ){ pair& xyi = xy[i]; double( &d_i )[bound_N*2] = d[i]; FOR( j , i + 1 , N2 ){ pair& xyj = xy[j]; pair dij = { xyj.first - xyi.first , xyj.second - xyi.second }; bool connected = true; FOR( k , 0 , N ){ pair& xyk0 = xy[2 * k]; pair& xyk1 = xy[2 * k + 1]; if( Intersect( xyi ,xyj , xyk0 , xyk1 , 0.1 ) == 1 ){ connected = false; break; } } d_i[j] = d[j][i] = connected ? sqrt( dij.first * dij.first + dij.second * dij.second ) : infty; CERR( i << "," << j << "," << d_i[j] ); } } double weight[bound_N*2][bound_N*2]; FloydWarshall,Zero,Min,bound_N*2>( d , weight , N * 2 , infty ); SET_PRECISION( 6 ); FOR( i , 0 , M ){ T2& ab_i0 = ab[2 * i]; T2& ab_i1 = ab[2 * i + 1]; COUT( weight[2 * ab_i0.first + ab_i0.second][2 * ab_i1.first + ab_i1.second] ); } } REPEAT_MAIN(1);