結果
問題 | No.2909 Imaginary Summer |
ユーザー | 👑 p-adic |
提出日時 | 2024-09-27 08:30:53 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,146 ms / 6,000 ms |
コード長 | 50,437 bytes |
コンパイル時間 | 5,559 ms |
コンパイル使用メモリ | 294,348 KB |
実行使用メモリ | 60,520 KB |
最終ジャッジ日時 | 2024-10-02 17:32:54 |
合計ジャッジ時間 | 34,062 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 3 ms
5,248 KB |
testcase_07 | AC | 3 ms
5,248 KB |
testcase_08 | AC | 2 ms
5,248 KB |
testcase_09 | AC | 241 ms
22,628 KB |
testcase_10 | AC | 166 ms
15,856 KB |
testcase_11 | AC | 137 ms
14,972 KB |
testcase_12 | AC | 225 ms
22,432 KB |
testcase_13 | AC | 171 ms
16,984 KB |
testcase_14 | AC | 144 ms
15,384 KB |
testcase_15 | AC | 699 ms
41,004 KB |
testcase_16 | AC | 849 ms
46,160 KB |
testcase_17 | AC | 704 ms
41,648 KB |
testcase_18 | AC | 810 ms
45,752 KB |
testcase_19 | AC | 613 ms
39,480 KB |
testcase_20 | AC | 1,047 ms
58,828 KB |
testcase_21 | AC | 1,054 ms
58,908 KB |
testcase_22 | AC | 893 ms
50,484 KB |
testcase_23 | AC | 966 ms
55,712 KB |
testcase_24 | AC | 1,068 ms
59,036 KB |
testcase_25 | AC | 952 ms
54,420 KB |
testcase_26 | AC | 948 ms
52,168 KB |
testcase_27 | AC | 1,097 ms
59,584 KB |
testcase_28 | AC | 1,135 ms
60,520 KB |
testcase_29 | AC | 1,115 ms
60,484 KB |
testcase_30 | AC | 1,133 ms
60,220 KB |
testcase_31 | AC | 1,128 ms
60,520 KB |
testcase_32 | AC | 1,118 ms
60,488 KB |
testcase_33 | AC | 1,146 ms
60,048 KB |
testcase_34 | AC | 1,121 ms
60,520 KB |
testcase_35 | AC | 1,110 ms
60,088 KB |
testcase_36 | AC | 1,093 ms
60,084 KB |
testcase_37 | AC | 1,103 ms
60,048 KB |
testcase_38 | AC | 1,104 ms
60,504 KB |
testcase_39 | AC | 1,101 ms
60,056 KB |
ソースコード
// 入力制約/フォーマットチェック #ifndef INCLUDE_MODE #define INCLUDE_MODE // #define REACTIVE #define USE_GETLINE #endif #ifdef INCLUDE_MAIN inline void Solve() { CEXPR( int , bound , 1e5 ); GETLINE_COUNT( NMK_str , 3 , ' ' ); STOI( NMK_str , N , 2 , bound ); int bound_M = int( min( ll( bound ) , ll( N ) * ( N - 1 ) / 2 ) ); STOI( NMK_str , M , N - 1 , bound_M ); STOI( NMK_str , K , 1 , bound ); vector<T2<ll>> XY( N + 1 ); Set<T2<ll>> points{}; FOREQ( i , 0 , N ){ GETLINE_COUNT( XYi_str , 2 , ' ' ); STOI( XYi_str , Xi , -bound , bound ); STOI( XYi_str , Yi , -bound , bound ); assert( points.count( XY[i] = { Xi , Yi } ) == 0 ); points.insert( XY[i] ); } vector<T2<ll>> AB( K ); FOR( k , 0 , K ){ GETLINE_COUNT( ABk_str , 2 , ' ' ); STOI( ABk_str , Ak , -bound , bound ); STOI( ABk_str , Bk , -bound , bound ); assert( points.count( AB[k] = { Ak , Bk } ) == 0 ); points.insert( AB[k] ); } CEXPR( ll , V , 1e18 ); GETLINE_COUNT( V_str , 1 , ' ' ); STOI( V_str , V_same , V , V ); vector<vector<int>> e( N ); Set<T2<ll>> edges{}; REPEAT( M ){ GETLINE_COUNT( STj_str , 2 , ' ' ); STOI( STj_str , Sj , 1 , N ); STOI( STj_str , Tj , 1 , N ); assert( edges.count( { Sj , Tj } ) == 0 ); assert( edges.count( { Tj , Sj } ) == 0 ); edges.insert( { Sj , Tj } ); Sj--; Tj--; e[Sj].push_back( Tj ); e[Tj].push_back( Sj ); } Graph graph{ N , Get( e ) }; BreadthFirstSearch bfs{ graph , -1 }; auto [colour,cc] = bfs.GetConnectedComponent(); assert( cc == 1 ); double d = 1e18; FOREQ( i , 1 , N ){ d = min( d , L2_Distance( XY[0] , XY[i] ) ); } auto d2 = [&]( const T2<ll>& u , const T2<ll>& v ){ return L22_Distance( u , v ); }; auto ann = TwoDimensionalAllNearestNeighbour( d2 , AB , XY ); CERR( ann ); double answer = 0; FOR( k , 0 , K ){ double temp = L2_Distance( XY[0] , AB[k] ); RUN( ann[k] , i ){ temp = min( temp , d + L2_Distance( XY[i] , AB[k] ) ); } answer += temp; CERR( temp ); } SET_PRECISION( 6 ); RETURN( answer * 2 ); } REPEAT_MAIN(1); #else // INCLUDE_MAIN #ifdef INCLUDE_LIBRARY // https://github.com/p-adic/cpp // VVV ライブラリは以下に挿入する。redefinitionを避けるため圧縮元はincludeしない。 // 圧縮用 #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 PW Power #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<char,Traits> #define OS basic_ostream<char,Traits> #define ST_AS static_assert #define reMO_CO remove_const #define is_COructible_v is_constructible_v #define rBE rbegin #define reSZ resize #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 <TY U> IN CO U& VirtualPointedSet<U>::POINT()CO NE{RE Point();} #define DF_OF_POINT(POINT)TE <TY U> IN U& VirtualPointedSet<U>::POINT()NE{RE Point();} TE <TY U>CL UnderlyingSet{PU:US type = U;};TE <TY U>CL VirtualPointedSet:VI PU UnderlyingSet<U>{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 <TY U>CL PointedSet:VI PU VirtualPointedSet<U>{PU:U m_b_U;IN PointedSet(U b_u = U());IN CO U& Point()CO NE;IN U& Point()NE;};TE <TY U>CL VirtualNSet:VI PU UnderlyingSet<U>{PU:VI U Transfer(CO U& u)= 0;IN U Inverse(CO U& u);};TE <TY U,TY F_U>CL AbstractNSet:VI PU VirtualNSet<U>{PU:F_U m_f_U;IN AbstractNSet(F_U f_U);IN AbstractNSet<U,F_U>& OP=(CO AbstractNSet&)NE;IN U Transfer(CO U& u);};TE <TY U>CL VirtualMagma:VI PU UnderlyingSet<U>{PU:VI U Product(U u0,CO U& u1)= 0;IN U Sum(U u0,CO U& u1);};TE <TY U = ll>CL AdditiveMagma:VI PU VirtualMagma<U>{PU:IN U Product(U u0,CO U& u1);};TE <TY U = ll>CL MultiplicativeMagma:VI PU VirtualMagma<U>{PU:IN U Product(U u0,CO U& u1);};TE <TY U,TY M_U>CL AbstractMagma:VI PU VirtualMagma<U>{PU:M_U m_m_U;IN AbstractMagma(M_U m_U);IN AbstractMagma<U,M_U>& OP=(CO AbstractMagma<U,M_U>&)NE;IN U Product(U u0,CO U& u1);}; TE <TY U> IN PointedSet<U>::PointedSet(U b_U):m_b_U(MO(b_U)){}TE <TY U> IN CO U& PointedSet<U>::Point()CO NE{RE m_b_U;}TE <TY U> IN U& PointedSet<U>::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 <TY U,TY F_U> IN AbstractNSet<U,F_U>::AbstractNSet(F_U f_U):m_f_U(MO(f_U)){ST_AS(is_invocable_r_v<U,F_U,U>);}TE <TY U,TY F_U> IN AbstractNSet<U,F_U>& AbstractNSet<U,F_U>::operator=(CO AbstractNSet<U,F_U>&)NE{RE *TH;}TE <TY U,TY F_U> IN U AbstractNSet<U,F_U>::Transfer(CO U& u){RE m_f_U(u);}TE <TY U> IN U VirtualNSet<U>::Inverse(CO U& u){RE Transfer(u);}TE <TY U,TY M_U> IN AbstractMagma<U,M_U>::AbstractMagma(M_U m_U):m_m_U(MO(m_U)){ST_AS(is_invocable_r_v<U,M_U,U,U>);}TE <TY U,TY M_U> IN AbstractMagma<U,M_U>& AbstractMagma<U,M_U>::OP=(CO AbstractMagma<U,M_U>&)NE{RE *TH;}TE <TY U> IN U AdditiveMagma<U>::Product(U u0,CO U& u1){RE MO(u0 += u1);}TE <TY U> IN U MultiplicativeMagma<U>::Product(U u0,CO U& u1){RE MO(u0 *= u1);}TE <TY U,TY M_U> IN U AbstractMagma<U,M_U>::Product(U u0,CO U& u1){RE m_m_U(MO(u0),u1);}TE <TY U> IN U VirtualMagma<U>::Sum(U u0,CO U& u1){RE Product(MO(u0),u1);} TE <TY U>CL VirtualMonoid:VI PU VirtualMagma<U>,VI PU VirtualPointedSet<U>{};TE <TY U = ll>CL AdditiveMonoid:VI PU VirtualMonoid<U>,PU AdditiveMagma<U>,PU PointedSet<U>{};TE <TY U = ll>CL MultiplicativeMonoid:VI PU VirtualMonoid<U>,PU MultiplicativeMagma<U>,PU PointedSet<U>{PU:IN MultiplicativeMonoid(U e_U);};TE <TY U,TY M_U>CL AbstractMonoid:VI PU VirtualMonoid<U>,PU AbstractMagma<U,M_U>,PU PointedSet<U>{PU:IN AbstractMonoid(M_U m_U,U e_U);}; TE <TY U> IN MultiplicativeMonoid<U>::MultiplicativeMonoid(U e_U):PointedSet<U>(MO(e_U)){}TE <TY U,TY M_U> IN AbstractMonoid<U,M_U>::AbstractMonoid(M_U m_U,U e_U):AbstractMagma<U,M_U>(MO(m_U)),PointedSet<U>(MO(e_U)){} TE <TY U>CL VirtualGroup:VI PU VirtualMonoid<U>,VI PU VirtualPointedSet<U>,VI PU VirtualNSet<U>{};TE <TY U = ll>CL AdditiveGroup:VI PU VirtualGroup<U>,PU AdditiveMonoid<U>{PU:IN U Transfer(CO U& u);};TE <TY U,TY M_U,TY I_U>CL AbstractGroup:VI PU VirtualGroup<U>,PU AbstractMonoid<U,M_U>,PU AbstractNSet<U,I_U>{PU:IN AbstractGroup(M_U m_U,U e_U,I_U i_U);}; TE <TY U,TY M_U,TY I_U> IN AbstractGroup<U,M_U,I_U>::AbstractGroup(M_U m_U,U e_U,I_U i_U):AbstractMonoid<U,M_U>(MO(m_U),MO(e_U)),AbstractNSet<U,I_U>(MO(i_U)){}TE <TY U> IN U AdditiveGroup<U>::Transfer(CO U& u){RE -u;} TE <TY T,TY R1,TY R2,TY E>CL VirtualGraph:VI PU UnderlyingSet<T>{PU:VI R1 Enumeration(CRI i)= 0;IN R2 Enumeration_inv(CO T& t);TE <TY PATH> IN R2 Enumeration_inv(CO PATH& p);IN VO Reset();VI CRI SZ()CO NE = 0;VI E& edge()NE = 0;VI ret_t<E,T> Edge(CO T& t)= 0;TE <TY PATH> IN ret_t<E,T> Edge(CO PATH& p);ST IN CO T& Vertex(CO T& t)NE;TE <TY PATH> ST IN CO T& Vertex(CO PATH& e)NE;VI R2 Enumeration_inv_Body(CO T& t)= 0;};TE <TY T,TY R1,TY R2,TY E>CL EdgeImplimentation:VI PU VirtualGraph<T,R1,R2,E>{PU:int m_SZ;E m_edge;IN EdgeImplimentation(CRI SZ,E edge);IN CRI SZ()CO NE;IN E& edge()NE;IN ret_t<E,T> Edge(CO T& t);};TE <TY E>CL Graph:PU EdgeImplimentation<int,CRI,CRI,E>{PU:IN Graph(CRI SZ,E edge);IN CRI Enumeration(CRI i);TE <TY F> IN Graph<F> GetGraph(F edge)CO;IN CRI Enumeration_inv_Body(CRI t);};TE <TY T,TY Enum_T,TY Enum_T_inv,TY E>CL EnumerationGraph:PU EdgeImplimentation<T,ret_t<Enum_T,int>,ret_t<Enum_T_inv,T>,E>{PU:Enum_T m_enum_T;Enum_T_inv m_enum_T_inv;IN EnumerationGraph(CRI SZ,Enum_T enum_T,Enum_T_inv enum_T_inv,E edge);IN ret_t<Enum_T,int> Enumeration(CRI i);TE <TY F> IN EnumerationGraph<T,Enum_T,Enum_T_inv,F> GetGraph(F edge)CO;IN ret_t<Enum_T_inv,T> Enumeration_inv_Body(CO T& t);};TE <TY Enum_T,TY Enum_T_inv,TY E> EnumerationGraph(CRI SZ,Enum_T enum_T,Enum_T_inv enum_T_inv,E edge)-> EnumerationGraph<decldecay_t(declval<Enum_T>()(0)),Enum_T,Enum_T_inv,E>;TE <TY T,TY E>CL MemorisationGraph:PU EdgeImplimentation<T,T,CRI,E>{PU:int m_LE;VE<T> m_memory;Map<T,int> m_memory_inv;IN MemorisationGraph(CRI SZ,CO T& dummy,E edge);IN T Enumeration(CRI i);IN VO Reset();TE <TY F> IN MemorisationGraph<T,F> GetGraph(F edge)CO;IN CRI Enumeration_inv_Body(CO T& t);}; TE <TY T,TY R1,TY R2,TY E> IN EdgeImplimentation<T,R1,R2,E>::EdgeImplimentation(CRI SZ,E edge):m_SZ(SZ),m_edge(MO(edge)){ST_AS(is_COructible_v<T,R1> && is_COructible_v<int,R2> && is_invocable_v<E,T>);}TE <TY E> IN Graph<E>::Graph(CRI SZ,E edge):EdgeImplimentation<int,CRI,CRI,E>(SZ,MO(edge)){}TE <TY T,TY Enum_T,TY Enum_T_inv,TY E> IN EnumerationGraph<T,Enum_T,Enum_T_inv,E>::EnumerationGraph(CRI SZ,Enum_T enum_T,Enum_T_inv enum_T_inv,E edge):EdgeImplimentation<T,ret_t<Enum_T,int>,ret_t<Enum_T_inv,T>,E>(SZ,MO(edge)),m_enum_T(MO(enum_T)),m_enum_T_inv(MO(enum_T_inv)){}TE <TY T,TY E> IN MemorisationGraph<T,E>::MemorisationGraph(CRI SZ,CO T& dummy,E edge):EdgeImplimentation<T,T,CRI,E>(SZ,MO(edge)),m_LE(),m_memory(),m_memory_inv(){ST_AS(is_invocable_v<E,T>);}TE <TY E> IN CRI Graph<E>::Enumeration(CRI i){RE i;}TE <TY T,TY Enum_T,TY Enum_T_inv,TY E> IN ret_t<Enum_T,int> EnumerationGraph<T,Enum_T,Enum_T_inv,E>::Enumeration(CRI i){RE m_enum_T(i);}TE <TY T,TY E> IN T MemorisationGraph<T,E>::Enumeration(CRI i){AS(0 <= i && i < m_LE);RE m_memory[i];}TE <TY T,TY R1,TY R2,TY E> IN R2 VirtualGraph<T,R1,R2,E>::Enumeration_inv(CO T& t){RE Enumeration_inv_Body(t);}TE <TY T,TY R1,TY R2,TY E> TE <TY PATH> IN R2 VirtualGraph<T,R1,R2,E>::Enumeration_inv(CO PATH& p){RE Enumeration_inv_Body(get<0>(p));}TE <TY E> IN CRI Graph<E>::Enumeration_inv_Body(CRI i){RE i;}TE <TY T,TY Enum_T,TY Enum_T_inv,TY E> IN ret_t<Enum_T_inv,T> EnumerationGraph<T,Enum_T,Enum_T_inv,E>::Enumeration_inv_Body(CO T& t){RE m_enum_T_inv(t);}TE <TY T,TY E> IN CRI MemorisationGraph<T,E>::Enumeration_inv_Body(CO T& t){if(m_memory_inv.count(t)== 0){AS(m_LE < TH->SZ());m_memory.push_back(t);RE m_memory_inv[t]= m_LE++;}RE m_memory_inv[t];}TE <TY T,TY R1,TY R2,TY E> VO VirtualGraph<T,R1,R2,E>::Reset(){}TE <TY T,TY E> IN VO MemorisationGraph<T,E>::Reset(){m_LE = 0;m_memory.clear();m_memory_inv.clear();}TE <TY T,TY R1,TY R2,TY E> IN CRI EdgeImplimentation<T,R1,R2,E>::SZ()CO NE{RE m_SZ;}TE <TY T,TY R1,TY R2,TY E> IN E& EdgeImplimentation<T,R1,R2,E>::edge()NE{RE m_edge;}TE <TY T,TY R1,TY R2,TY E> IN ret_t<E,T> EdgeImplimentation<T,R1,R2,E>::Edge(CO T& t){RE m_edge(t);}TE <TY T,TY R1,TY R2,TY E> TE <TY PATH> IN ret_t<E,T> VirtualGraph<T,R1,R2,E>::Edge(CO PATH& p){RE Edge(get<0>(p));}TE <TY E> TE <TY F> IN Graph<F> Graph<E>::GetGraph(F edge)CO{RE Graph<F>(TH->SZ(),MO(edge));}TE <TY T,TY Enum_T,TY Enum_T_inv,TY E> TE <TY F> IN EnumerationGraph<T,Enum_T,Enum_T_inv,F> EnumerationGraph<T,Enum_T,Enum_T_inv,E>::GetGraph(F edge)CO{RE EnumerationGraph<T,Enum_T,Enum_T_inv,F>(TH->SZ(),m_enum_T,m_enum_T_inv,MO(edge));}TE <TY T,TY E> TE <TY F> IN MemorisationGraph<T,F> MemorisationGraph<T,E>::GetGraph(F edge)CO{RE MemorisationGraph<T,F>(TH->SZ(),MO(edge));}TE <TY T,TY R1,TY R2,TY E> IN CO T& VirtualGraph<T,R1,R2,E>::Vertex(CO T& t)NE{RE t;}TE <TY T,TY R1,TY R2,TY E> TE <TY PATH> IN CO T& VirtualGraph<T,R1,R2,E>::Vertex(CO PATH& e)NE{RE Vertex(get<0>(e));} TE <TY T,TY GRAPH>CL VirtualBreadthFirstSearch{PU:GRAPH& m_G;T m_not_found;bool m_initialised;LI<T> m_next;VE<bool> m_found;VE<T> m_prev;IN VirtualBreadthFirstSearch(GRAPH& G,CO T& not_found);TE <TY Arg> IN VirtualBreadthFirstSearch(GRAPH& G,CO T& not_found,Arg&& init);IN VO Initialise();IN VO Initialise(CO T& init);IN VO Initialise(LI<T> inits);IN VO Shift(CO T& init);IN VO Shift(LI<T> inits);IN CRI SZ()CO NE;IN VE<bool>::reference found(CO T& t);IN CO T& prev(CO T& t);IN T Next();TE <TY U = T> auto GetDistance()-> enable_if_t<is_same_v<GRAPH,MemorisationGraph<U,decldecay_t(declval<GRAPH>().edge())>>,Map<T,int>>;TE <TY U = T> auto GetDistance()-> enable_if_t<!is_same_v<GRAPH,MemorisationGraph<U,decldecay_t(declval<GRAPH>().edge())>>,VE<int>>;pair<VE<int>,int> GetConnectedComponent();VE<T> GetNodeEnumeration();VE<T> GetReversedNodeEnumeration();VI VO Push(LI<T>& next,CO T& t)= 0;TE <TY PATH> IN VO Push(LI<T>& next,CO PATH& p);}; TE <TY T,TY GRAPH> IN VirtualBreadthFirstSearch<T,GRAPH>::VirtualBreadthFirstSearch(GRAPH& G,CO T& not_found):m_G(G),m_not_found(not_found),m_initialised(false),m_next(),m_found(),m_prev(){ST_AS(is_same_v<inner_t<GRAPH>,T>);}TE <TY T,TY GRAPH> TE <TY Arg> IN VirtualBreadthFirstSearch<T,GRAPH>::VirtualBreadthFirstSearch(GRAPH& G,CO T& not_found,Arg&& init):VirtualBreadthFirstSearch<T,GRAPH>(G,not_found){Initialise(forward<Arg>(init));}TE <TY T,TY GRAPH> IN VO VirtualBreadthFirstSearch<T,GRAPH>::Initialise(){m_initialised = true;CRI V = SZ();m_next.clear();m_found = VE<bool>(V);m_prev = VE<T>(V,m_not_found);}TE <TY T,TY GRAPH> IN VO VirtualBreadthFirstSearch<T,GRAPH>::Initialise(CO T& init){auto&& i = m_G.Enumeration_inv(init);AS(0 <= i && i < SZ());Initialise();m_next.push_back(init);m_found[i]= true;}TE <TY T,TY GRAPH> IN VO VirtualBreadthFirstSearch<T,GRAPH>::Initialise(LI<T> inits){Initialise();m_next = MO(inits);CRI V = SZ();for(auto& u:m_next){auto&& i = m_G.Enumeration_inv(u);AS(0 <= i && i < V);m_found[i]= true;}}TE <TY T,TY GRAPH> IN VO VirtualBreadthFirstSearch<T,GRAPH>::Shift(CO T& init){if(m_initialised){CRI V = SZ();auto&& i = m_G.Enumeration_inv(init);AS(0 <= i && i < V);m_next.clear();if(! m_found[i]){m_next.push_back(init);m_found[i]= true;}}else{Initialise(init);}}TE <TY T,TY GRAPH> IN VO VirtualBreadthFirstSearch<T,GRAPH>::Shift(LI<T> inits){if(m_initialised){m_next.clear();CRI V = SZ();for(auto& u:m_next){auto&& i = m_G.Enumeration_inv(u);AS(0 <= i && i < V);if(! m_found[i]){m_next.push_back(u);m_found[i]= true;}}}else{Initialise(MO(inits));}}TE <TY T,TY GRAPH> IN CRI VirtualBreadthFirstSearch<T,GRAPH>::SZ()CO NE{RE m_G.SZ();}TE <TY T,TY GRAPH> IN VE<bool>::reference VirtualBreadthFirstSearch<T,GRAPH>::found(CO T& t){auto&& i = m_G.Enumeration_inv(t);AS(0 <= i && i < SZ());if(!m_initialised){Initialise();}RE m_found[i];}TE <TY T,TY GRAPH> IN CO T& VirtualBreadthFirstSearch<T,GRAPH>::prev(CO T& t){auto&& i = m_G.Enumeration_inv(t);AS(0 <= i && i < SZ());if(!m_initialised){Initialise();}RE m_prev[i];}TE <TY T,TY GRAPH> IN T VirtualBreadthFirstSearch<T,GRAPH>::Next(){if(m_next.empty()){RE m_not_found;}CO T t_curr = m_next.front();m_next.pop_front();for(auto& t:m_G.Edge(t_curr)){auto&& i = m_G.Enumeration_inv(t);auto&& found_i = m_found[i];if(! found_i){Push(m_next,t);m_prev[i]= t_curr;found_i = true;}}RE t_curr;}TE <TY T,TY GRAPH> TE <TY U>auto VirtualBreadthFirstSearch<T,GRAPH>::GetDistance()-> enable_if_t<is_same_v<GRAPH,MemorisationGraph<U,decldecay_t(declval<GRAPH>().edge())>>,Map<T,int>>{Map<T,int> AN{};for(auto IT = m_next.BE(),EN = m_next.EN();IT != EN;IT++){AN[*IT]= 0;}T t;WH((t = Next())!= m_not_found){if(AN.count(t)== 0){AN[t]= AN[m_prev[m_G.Enumeration_inv(t)]]+ 1;}}RE AN;}TE <TY T,TY GRAPH> TE <TY U>auto VirtualBreadthFirstSearch<T,GRAPH>::GetDistance()-> enable_if_t<!is_same_v<GRAPH,MemorisationGraph<U,decldecay_t(declval<GRAPH>().edge())>>,VE<int>>{VE AN(SZ(),-1);for(auto IT = m_next.BE(),EN = m_next.EN();IT != EN;IT++){AN[m_G.Enumeration_inv(*IT)]= 0;}T t;WH((t = Next())!= m_not_found){auto&& i = m_G.Enumeration_inv(t);int& AN_i = AN[i];AN_i == -1?AN_i = AN[m_G.Enumeration_inv(m_prev[i])]+ 1:AN_i;}RE AN;}TE <TY T,TY GRAPH>pair<VE<int>,int> VirtualBreadthFirstSearch<T,GRAPH>::GetConnectedComponent(){ST_AS(!is_same_v<GRAPH,MemorisationGraph<T,decldecay_t(m_G.edge())>>);CRI V = SZ();VE cc_num(V,-1);int count = 0;for(int i = 0;i < V;i++){if(cc_num[i]== -1){Shift(m_G.Enumeration(i));T t = Next();if(t != m_not_found){WH(t != m_not_found){cc_num[m_G.Enumeration_inv(t)]= count;t = Next();}count++;}}}RE{MO(cc_num),MO(count)};}TE <TY T,TY GRAPH>VE<T> VirtualBreadthFirstSearch<T,GRAPH>::GetNodeEnumeration(){VE<T> AN{};T t = Next();WH(t != m_not_found){AN.push_back(t);t = Next();}RE AN;}TE <TY T,TY GRAPH>VE<T> VirtualBreadthFirstSearch<T,GRAPH>::GetReversedNodeEnumeration(){VE<T> AN{};VE<T> next{};T t;bool searched;WH(!(searched =(t = Next())== m_not_found)|| !next.empty()){WH(!next.empty()&&(searched || next.back()!= m_prev[m_G.Enumeration_inv(t)])){AN.push_back(next.back());next.pop_back();}if(!searched){next.push_back(t);}}RE AN;}TE <TY T,TY GRAPH> TE <TY PATH> IN VO VirtualBreadthFirstSearch<T,GRAPH>::Push(LI<T>& next,CO PATH& p){Push(next,get<0>(p));} TE <TY T,TY GRAPH>CL BreadthFirstSearch:PU VirtualBreadthFirstSearch<T,GRAPH>{PU:TE <TY...Args> IN BreadthFirstSearch(GRAPH& G,CO T& not_found,Args&&... args);IN VO Push(LI<T>& next,CO T& t);}; TE <TY T,TY GRAPH> TE <TY...Args> IN BreadthFirstSearch<T,GRAPH>::BreadthFirstSearch(GRAPH& G,CO T& not_found,Args&&... args):VirtualBreadthFirstSearch<T,GRAPH>(G,not_found,forward<Args>(args)...){}TE <TY T,TY GRAPH> IN VO BreadthFirstSearch<T,GRAPH>::Push(LI<T>& next,CO T& t){next.push_back(t);} TE <TY INT> IN INT L22(CO INT& x,CO INT& y){RE x * x + y * y;}TE <TY INT> IN INT L22(CO pair<INT,INT>& v){RE L22(v.first,v.second);}TE <TY INT> IN INT L22_Distance(CO INT& x0,CO INT& y0,CO INT& x1,CO INT& y1){RE L22(x0 - x1,y0 - y1);}TE <TY INT> IN INT L22_Distance(CO pair<INT,INT>& v0,CO pair<INT,INT>& v1){RE L22(v0 - v1);}TE <TY INT> IN double L2(CO INT& x,CO INT& y){RE sqrt(L22(x,y));}TE <TY INT> IN double L2(CO pair<INT,INT>& v){RE sqrt(L22(v));}TE <TY INT> IN double L2_Distance(CO INT& x0,CO INT& y0,CO INT& x1,CO INT& y1){RE sqrt(L22_Distance(x0,y0,x1,y1));}TE <TY INT> IN double L2_Distance(CO pair<INT,INT>& v0,CO pair<INT,INT>& v1){RE sqrt(L22_Distance(v0,v1));} template <typename Dist2 , typename INT> vector<vector<int>> TwoDimensionalAllNearestNeighbour( const Dist2& d2 , const vector<pair<INT,INT>>& S , const vector<pair<INT,INT>>& T ) { const int S_size = S.size(); if( S_size == 0 ){ return {}; } const int T_size = T.size(); assert( T_size > 0 ); if( T_size == 1 ){ return vector( S_size , vector<int>{ 0 } ); } auto [x_min,y_min] = T[0]; auto [x_max,y_max] = T[0]; for( int j = 1 ; j < T_size ; j++ ){ auto& [x,y] = T[j]; x_min = min( x_min , x ); x_max = max( x , x_max ); y_min = min( y_min , y ); y_max = max( y , y_max ); } using Block = tuple<INT,INT,INT,INT,vector<int>>; vector<vector<pair<bool,int>>> neighbours( S_size , { { true , 0 } } ); vector<bool> updatable( S_size , true ); vector<Block> divisible_block = { { x_min , x_max , y_min , y_max , vector<int>( T_size ) } }; for( int j = 0 ; j < T_size ; j++ ){ get<4>( divisible_block[0] )[j] = j; } vector<Block> indivisible_block = {}; // 最悪O(T_size)回のループ、良いケースではO(log T_size)回のループ while( !divisible_block.empty() ){ const int divisible_block_size = divisible_block.size(); vector<Block> next_divisible_block{}; vector<vector<pair<bool,int>>> Partition( divisible_block_size ); // 分割可能ブロックの分割の計算 // 合計O(T_size) for( int num = 0 ; num < divisible_block_size ; num++ ){ auto& [x_min,x_max,y_min,y_max,t] = divisible_block[num]; const INT x_sum = x_min + x_max , x_mid = ( x_sum < 0 ? x_sum - 1 : x_sum ) / 2; const INT y_sum = y_min + y_max , y_mid = ( y_sum < 0 ? y_sum - 1 : y_sum ) / 2; Block block_sub[2][2]{}; for( auto& j : t ){ auto& [x,y] = T[j]; auto& [x_min_sub,x_max_sub,y_min_sub,y_max_sub,t_sub] = block_sub[x <= x_mid ? 0 : 1][y <= y_mid ? 0 : 1]; if( t_sub.empty() ){ x_min_sub = x_max_sub = x; y_min_sub = y_max_sub = y; } else { x_min_sub = min( x_min_sub , x ); x_max_sub = max( x , x_max_sub ); y_min_sub = min( y_min_sub , y ); y_max_sub = max( y , y_max_sub ); } t_sub.push_back( j ); } for( int num_x = 0 ; num_x <= 1 ; num_x++ ){ for( int num_y = 0 ; num_y <= 1 ; num_y++ ){ Block& block_xy = block_sub[num_x][num_y]; const int t_size = get<4>( block_xy ).size(); if( t_size != 0 ){ if( t_size == 1 ){ Partition[num].push_back( { false , indivisible_block.size() } ); indivisible_block.push_back( move( block_xy ) ); } else { Partition[num].push_back( { true , next_divisible_block.size() } ); next_divisible_block.push_back( move( block_xy ) ); } } } } } divisible_block = move( next_divisible_block ); // 近傍の再計算 // 合計O(近傍数総和) for( int i = 0 ; i < S_size ; i++ ){ if( updatable[i] ){ updatable[i] = false; vector<pair<bool,int>> neighbour_partition{}; for( auto& coord : neighbours[i] ){ auto& [divisible,num] = coord; if( divisible ){ for( auto& coord_sub : Partition[num] ){ neighbour_partition.push_back( coord_sub ); } } else { neighbour_partition.push_back( coord ); } } auto& [x,y] = S[i]; auto d2_max = [&]( const pair<bool,int>& coord ){ auto& [divisible,num] = coord; auto& [x_min,x_max,y_min,y_max,t] = ( divisible ? divisible_block : indivisible_block )[num]; return d2( { 0 , 0 } , { max( abs( x - x_min ) , abs( x_max - x ) ) , max( abs( y - y_min ) , abs( y_max - y ) ) } ); }; auto d2_min = [&]( const pair<bool,int>& coord ){ auto& [divisible,num] = coord; auto& [x_min,x_max,y_min,y_max,t] = ( divisible ? divisible_block : indivisible_block )[num]; return d2( S[i] , { x < x_min ? x_min : x_max < x ? x_max : x , y < y_min ? y_min : y_max < y ? y_max : y } ); }; const int neighbour_partition_size = neighbour_partition.size(); assert( neighbour_partition_size > 0 ); decltype( d2({0,0},{0,0}) ) d2_max_min = d2_max( neighbour_partition[0] ); for( int num = 1 ; num < neighbour_partition_size ; num++ ){ d2_max_min = min( d2_max_min , d2_max( neighbour_partition[num] ) ); } neighbours[i].clear(); for( int num = 0 ; num < neighbour_partition_size ; num++ ){ if( d2_min( neighbour_partition[num] ) <= d2_max_min ){ updatable[i] = updatable[i] || neighbour_partition[num].first; neighbours[i].push_back( move( neighbour_partition[num] ) ); } } } } } vector<vector<int>> answer( S_size ); // 最近点の計算 // 合計O(最近接数の総和) for( int i = 0 ; i < S_size ; i++ ){ for( auto& [divisible,num] : neighbours[i] ){ assert( !divisible ); auto& [x_min,x_max,y_min,y_max,t] = indivisible_block[num]; assert( t.size() == 1 && x_min == x_max && x_min == T[t[0]].first && y_min == y_max && y_min == T[t[0]].second ); answer[i].push_back( t[0] ); } } return answer; } // AAA ライブラリは以上に挿入する。 #define INCLUDE_MAIN #include __FILE__ #else // INCLUDE_LIBRARY #ifdef DEBUG #define _GLIBCXX_DEBUG #define DEXPR( LL , BOUND , VALUE1 , VALUE2 ) CEXPR( LL , BOUND , VALUE2 ) #define SIGNAL signal( SIGABRT , &AlertAbort ); #define ASSERT( A , MIN , MAX ) CERR( "ASSERTチェック: " , ( MIN ) , ( ( MIN ) <= A ? "<=" : ">" ) , A , ( A <= ( MAX ) ? "<=" : ">" ) , ( MAX ) ); assert( ( MIN ) <= A && A <= ( MAX ) ) #define COUT( ... ) VariadicCout( cout << "出力:" , __VA_ARGS__ ) << endl #define COUTNS( ... ) VariadicCoutNonSep( cout , __VA_ARGS__ ) << flush #define CERR( ... ) VariadicCout( cerr , __VA_ARGS__ ) << endl #define CERRNS( ... ) VariadicCout( cerr , __VA_ARGS__ ) << flush #define COUT_A( A , N ) OUTPUT_ARRAY( cout << "出力:" , A , N ) << endl #define CERR_A( A , N ) OUTPUT_ARRAY( cerr , A , N ) << endl #else #pragma GCC optimize ( "O3" ) #pragma GCC optimize ( "unroll-loops" ) #pragma GCC target ( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) #define SIGNAL #define DEXPR( LL , BOUND , VALUE1 , VALUE2 ) CEXPR( LL , BOUND , VALUE1 ) #define ASSERT( A , MIN , MAX ) assert( ( MIN ) <= A && A <= ( MAX ) ) #define COUT( ... ) VariadicCout( cout , __VA_ARGS__ ) << ENDL #define COUTNS( ... ) VariadicCoutNonSep( cout , __VA_ARGS__ ) #define CERR( ... ) #define CERRNS( ... ) #define COUT_A( A , N ) OUTPUT_ARRAY( cout , A , N ) << ENDL #define CERR_A( A , N ) #endif #ifdef REACTIVE #ifdef DEBUG #define RSET( A , ... ) A = __VA_ARGS__ #else #define RSET( A , ... ) cin >> A #endif #define RCIN( LL , A , ... ) LL A; RSET( A , __VA_ARGS__ ) #define ENDL endl #else #define ENDL "\n" #endif #ifdef USE_GETLINE #define SET_LL( A ) { GETLINE( A ## _str ); A = stoll( A ## _str ); } #define GETLINE_SEPARATE( SEPARATOR , ... ) string __VA_ARGS__; VariadicGetline( cin , SEPARATOR , __VA_ARGS__ ) #define GETLINE( ... ) GETLINE_SEPARATE( '\n' , __VA_ARGS__ ) #else #define SET_LL( A ) cin >> A #define CIN( LL , ... ) LL __VA_ARGS__; VariadicCin( cin , __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 , ... ) vector<LL> __VA_ARGS__; SET_A( I , N , __VA_ARGS__ ) #define CIN_AA( LL , I0 , N0 , I1 , N1 , VAR ) vector<vector<LL>> VAR( N0 + I0 ); FOR( VARIABLE_FOR_CIN_AA , 0 , N0 ){ SET_A( I1 , N1 , VAR[VARIABLE_FOR_CIN_AA + I0] ); } #endif #include <bits/stdc++.h> using namespace std; #define REPEAT_MAIN( BOUND ) int main(){ ios_base::sync_with_stdio( false ); cin.tie( nullptr ); SIGNAL; CEXPR( int , bound_test_case_num , BOUND ); int test_case_num = 1; if constexpr( bound_test_case_num > 1 ){ CERR( "テストケースの個数を入力してください。" ); SET_ASSERT( test_case_num , 1 , bound_test_case_num ); } REPEAT( test_case_num ){ if constexpr( bound_test_case_num > 1 ){ CERR( "testcase " , VARIABLE_FOR_REPEAT_test_case_num , ":" ); } Solve(); CERR( "" ); } CHECK_REDUNDANT_INPUT; } #define START_WATCH chrono::system_clock::time_point watch = chrono::system_clock::now() #define CURRENT_TIME static_cast<double>( chrono::duration_cast<chrono::microseconds>( chrono::system_clock::now() - watch ).count() / 1000.0 ) #define CHECK_WATCH( TL_MS ) ( CURRENT_TIME < TL_MS - 100.0 ) #define CEXPR( LL , BOUND , VALUE ) constexpr LL BOUND = VALUE #define SET_ASSERT( A , MIN , MAX ) SET_LL( A ); ASSERT( A , MIN , MAX ) #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<decldecay_t( MAX )> 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<decldecay_t( MAX )>( N1 + I1 ) ); SET_AA_ASSERT( I0 , N0 , I1 , N1 , A , MIN , MAX ) #define FOR( VAR , INITIAL , FINAL_PLUS_ONE ) for( decldecay_t( FINAL_PLUS_ONE ) VAR = INITIAL ; VAR < FINAL_PLUS_ONE ; VAR ++ ) #define FOREQ( VAR , INITIAL , FINAL ) for( decldecay_t( FINAL ) VAR = INITIAL ; VAR <= FINAL ; VAR ++ ) #define FOREQINV( VAR , INITIAL , FINAL ) for( decldecay_t( INITIAL ) VAR = INITIAL ; VAR + 1 > FINAL ; VAR -- ) #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_ ## HOW_MANY_TIMES , 0 , HOW_MANY_TIMES ) #define SET_PRECISION( DECIMAL_DIGITS ) cout << fixed << setprecision( DECIMAL_DIGITS ) #define RETURN( ... ) COUT( __VA_ARGS__ ); return // 型のエイリアス #define decldecay_t( VAR ) decay_t<decltype( VAR )> template <typename F , typename...Args> using ret_t = decltype( declval<F>()( declval<Args>()... ) ); template <typename T> using inner_t = typename T::type; using uint = unsigned int; using ll = long long; using ull = unsigned long long; using ld = long double; using lld = __float128; template <typename INT> using T2 = pair<INT,INT>; template <typename INT> using T3 = tuple<INT,INT,INT>; template <typename INT> using T4 = tuple<INT,INT,INT,INT>; using path = pair<int,ll>; /* VVV 常設ライブラリの非圧縮版は以下に挿入する。*/ // Random ll GetRand( const int& Rand_min , const int& Rand_max ) { assert( Rand_min <= Rand_max ); ll answer = time( NULL ); return answer * rand() % ( Rand_max + 1 - Rand_min ) + Rand_min; } // Set #define DECLARATION_OF_HASH( ... ) \ struct hash<__VA_ARGS__> \ { \ \ inline size_t operator()( const __VA_ARGS__& n ) const; \ \ }; \ class is_ordered { private: is_ordered() = delete; template <typename T> static constexpr auto Check( const T& t ) -> decltype( t < t , true_type() ); static constexpr false_type Check( ... ); public: template <typename T> static constexpr const bool value = is_same_v< decltype( Check( declval<T>() ) ) , true_type >; }; template <typename T> using Set = conditional_t<is_constructible_v<unordered_set<T>>,unordered_set<T>,conditional_t<is_ordered::value<T>,set<T>,void>>; // Tuple #define DECLARATION_OF_ARITHMETIC_FOR_TUPLE( OPR ) \ template <typename T , typename U , template <typename...> typename V> inline auto operator OPR ## =( V<T,U>& t0 , const V<T,U>& t1 ) -> decltype( ( get<0>( t0 ) , t0 ) )&; \ template <typename T , typename U , typename V> inline tuple<T,U,V>& operator OPR ## =( tuple<T,U,V>& t0 , const tuple<T,U,V>& t1 ); \ template <typename T , typename U , typename V , typename W> inline tuple<T,U,V,W>& operator OPR ## =( tuple<T,U,V,W>& t0 , const tuple<T,U,V,W>& t1 ); \ template <typename ARG , typename T , typename U , template <typename...> typename V> inline auto operator OPR ## =( V<T,U>& t0 , const ARG& t1 ) -> decltype( ( get<0>( t0 ) , t0 ) )&; \ template <typename ARG , typename T , typename U , typename V> inline tuple<T,U,V>& operator OPR ## =( tuple<T,U,V>& t0 , const ARG& t1 ); \ template <typename ARG , typename T , typename U , typename V , typename W> inline tuple<T,U,V,W>& operator OPR ## =( tuple<T,U,V,W>& t0 , const ARG& t1 ); \ template <template <typename...> typename V , typename...ARGS , typename ARG> inline auto operator OPR( const V<ARGS...>& t0 , const ARG& t1 ) -> decldecay_t( ( get<0>( t0 ) , t0 ) ) \ #define DEFINITION_OF_ARITHMETIC_FOR_TUPLE( OPR ) \ template <typename T , typename U , template <typename...> typename V> inline auto operator OPR ## =( V<T,U>& t0 , const V<T,U>& t1 ) -> decltype( ( get<0>( t0 ) , t0 ) )& { get<0>( t0 ) OPR ## = get<0>( t1 ); get<1>( t0 ) OPR ## = get<1>( t1 ); return t0; } \ template <typename T , typename U , typename V> inline tuple<T,U,V>& operator OPR ## =( tuple<T,U,V>& t0 , const tuple<T,U,V>& t1 ) { get<0>( t0 ) OPR ## = get<0>( t1 ); get<1>( t0 ) OPR ## = get<1>( t1 ); get<2>( t0 ) OPR ## = get<2>( t1 ); return t0; } \ template <typename T , typename U , typename V , typename W> inline tuple<T,U,V,W>& operator OPR ## =( tuple<T,U,V,W>& t0 , const tuple<T,U,V,W>& t1 ) { 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 ); return t0; } \ template <typename ARG , typename T , typename U , template <typename...> typename V> inline auto operator OPR ## =( V<T,U>& t0 , const ARG& t1 ) -> decltype( ( get<0>( t0 ) , t0 ) )& { get<0>( t0 ) OPR ## = t1; get<1>( t0 ) OPR ## = t1; return t0; } \ template <typename ARG , typename T , typename U , typename V> inline tuple<T,U,V>& operator OPR ## =( tuple<T,U,V>& t0 , const ARG& t1 ) { get<0>( t0 ) OPR ## = t1; get<1>( t0 ) OPR ## = t1; get<2>( t0 ) OPR ## = t1; return t0; } \ template <typename ARG , typename T , typename U , typename V , typename W> inline tuple<T,U,V,W>& operator OPR ## =( tuple<T,U,V,W>& t0 , const ARG& t1 ) { get<0>( t0 ) OPR ## = t1; get<1>( t0 ) OPR ## = t1; get<2>( t0 ) OPR ## = t1; get<3>( t0 ) OPR ## = t1; return t0; } \ template <template <typename...> typename V , typename...ARGS , typename ARG> inline auto operator OPR( const V<ARGS...>& t0 , const ARG& t1 ) -> decldecay_t( ( get<0>( t0 ) , t0 ) ) { auto t = t0; return move( t OPR ## = t1 ); } \ #define DECLARATION_OF_INCREMENT_FOR_TUPLE( INCR ) \ template <typename T , typename U , template <typename...> typename V> inline auto operator INCR( V<T,U>& t ) -> decltype( ( get<0>( t ) , t ) )&; \ template <typename T , typename U , typename V> inline tuple<T,U,V>& operator INCR ( tuple<T,U,V>& t ); \ template <typename T , typename U , typename V , typename W> inline tuple<T,U,V,W>& operator INCR ( tuple<T,U,V,W>& t ); \ #define DEFINITION_OF_INCREMENT_FOR_TUPLE( INCR ) \ template <typename T , typename U , template <typename...> typename V> inline auto operator INCR( V<T,U>& t ) -> decltype( ( get<0>( t ) , t ) )& { INCR get<0>( t ); INCR get<1>( t ); return t; } \ template <typename T , typename U , typename V> inline tuple<T,U,V>& operator INCR ( tuple<T,U,V>& t ) { INCR get<0>( t ); INCR get<1>( t ); INCR get<2>( t ); return t; } \ template <typename T , typename U , typename V , typename W> inline tuple<T,U,V,W>& operator INCR ( tuple<T,U,V,W>& t ) { INCR get<0>( t ); INCR get<1>( t ); INCR get<2>( t ); INCR get<3>( t ); return t; } \ DEFINITION_OF_ARITHMETIC_FOR_TUPLE( + ); DEFINITION_OF_ARITHMETIC_FOR_TUPLE( - ); DEFINITION_OF_ARITHMETIC_FOR_TUPLE( * ); DEFINITION_OF_ARITHMETIC_FOR_TUPLE( / ); DEFINITION_OF_ARITHMETIC_FOR_TUPLE( % ); DEFINITION_OF_INCREMENT_FOR_TUPLE( ++ ); DEFINITION_OF_INCREMENT_FOR_TUPLE( -- ); template <class Traits , typename T> inline basic_istream<char,Traits>& operator>>( basic_istream<char,Traits>& is , tuple<T>& arg ){ return is >> get<0>( arg ); } template <class Traits , typename T , typename U , template <typename...> typename V> inline auto operator>>( basic_istream<char,Traits>& is , V<T,U>& arg ) -> decltype((get<0>(arg),is))& { return is >> get<0>( arg ) >> get<1>( arg ); } template <class Traits , typename T , typename U , typename V> inline basic_istream<char,Traits>& operator>>( basic_istream<char,Traits>& is , tuple<T,U,V>& arg ) { return is >> get<0>( arg ) >> get<1>( arg ) >> get<2>( arg ); } template <class Traits , typename T , typename U , typename V , typename W> inline basic_istream<char,Traits>& operator>>( basic_istream<char,Traits>& is , tuple<T,U,V,W>& arg ) { return is >> get<0>( arg ) >> get<1>( arg ) >> get<2>( arg ) >> get<3>( arg ); } template <class Traits , typename T> inline basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os , const tuple<T>& arg ) { return os << get<0>( arg ); } template <class Traits , typename T , typename U , template <typename...> typename V> inline auto operator<<( basic_ostream<char,Traits>& os , const V<T,U>& arg ) -> decltype((get<0>(arg),os))& { return os << get<0>( arg ) << " " << get<1>( arg ); } template <class Traits , typename T , typename U , typename V> inline basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os , const tuple<T,U,V>& arg ) { return os << get<0>( arg ) << " " << get<1>( arg ) << " " << get<2>( arg ); } template <class Traits , typename T , typename U , typename V , typename W> inline basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os , const tuple<T,U,V,W>& arg ) { return os << get<0>( arg ) << " " << get<1>( arg ) << " " << get<2>( arg ) << " " << get<3>( arg ); } #define DEFINITION_OF_HASH_FOR_TUPLE( PAIR ) \ template <typename T , typename U> inline size_t hash<PAIR<T,U>>::operator()( const PAIR<T,U>& n ) const { static const size_t seed = ( GetRand( 1e3 , 1e8 ) << 1 ) | 1; static const hash<T> h0; static const hash<U> h1; return ( h0( get<0>( n ) ) * seed ) ^ h1( get<1>( n ) ); } \ template <typename T> DECLARATION_OF_HASH( tuple<T> ); template <typename T , typename U> DECLARATION_OF_HASH( pair<T,U> ); template <typename T , typename U> DECLARATION_OF_HASH( tuple<T,U> ); template <typename T , typename U , typename V> DECLARATION_OF_HASH( tuple<T,U,V> ); template <typename T , typename U , typename V , typename W> DECLARATION_OF_HASH( tuple<T,U,V,W> ); template <typename T> inline size_t hash<tuple<T>>::operator()( const tuple<T>& n ) const { static const hash<T> h; return h(get<0>( n ) ); } DEFINITION_OF_HASH_FOR_TUPLE( pair ); DEFINITION_OF_HASH_FOR_TUPLE( tuple ); template <typename T , typename U , typename V> inline size_t hash<tuple<T,U,V>>::operator()( const tuple<T,U,V>& n ) const { static const size_t seed = ( GetRand( 1e3 , 1e8 ) << 1 ) | 1; static const hash<pair<T,U>> h01; static const hash<V> h2; return ( h01( { get<0>( n ) , get<1>( n ) } ) * seed ) ^ h2( get<2>( n ) ); } template <typename T , typename U , typename V , typename W> inline size_t hash<tuple<T,U,V,W>>::operator()( const tuple<T,U,V,W>& n ) const { static const size_t seed = ( GetRand( 1e3 , 1e8 ) << 1 ) | 1; static const hash<pair<T,U>> h01; static const hash<pair<V,W>> h23; return ( h01( { get<0>( n ) , get<1>( n ) } ) * seed ) ^ h23( { get<2>( n ) , get<3>( n ) } ); } // Vector #define DECLARATION_OF_ARITHMETIC_FOR_VECTOR( V , OPR ) \ template <typename T> inline V<T>& operator OPR ## = ( V<T>& a , const T& t ); \ template <typename T> inline V<T>& operator OPR ## = ( V<T>& a0 , const V<T>& a1 ); \ template <typename T , typename U> inline V<T> operator OPR( V<T> a , const U& u ); \ #define DEFINITION_OF_ARITHMETIC_FOR_VECTOR( V , OPR ) \ template <typename T> inline V<T>& operator OPR ## = ( V<T>& a , const T& t ) { for( auto& s : a ){ s OPR ## = t; } return a; } \ template <typename T> inline V<T>& operator OPR ## = ( V<T>& a0 , const V<T>& a1 ) { assert( a0.size() <= a1.size() ); auto itr0 = a0.begin() , end0 = a0.end(); auto itr1 = a1.begin(); while( itr0 != end0 ){ *( itr0++ ) OPR ## = *( itr1++ ); } return a0; } \ template <typename T , typename U> inline V<T> operator OPR( V<T> a , const U& u ) { return move( a OPR ## = u ); } \ #define DECLARATION_OF_INCREMENT_FOR_VECTOR( V , INCR ) \ template <typename T> inline V<T>& operator INCR( V<T>& a ); \ #define DEFINITION_OF_INCREMENT_FOR_VECTOR( V , INCR ) \ template <typename T> inline V<T>& operator INCR( V<T>& a ) { for( auto& i : a ){ INCR i; } return a; } \ #define DECLARATION_OF_ARITHMETICS_FOR_VECTOR( V ) \ DECLARATION_OF_ARITHMETIC_FOR_VECTOR( V , + ); \ DECLARATION_OF_ARITHMETIC_FOR_VECTOR( V , - ); \ DECLARATION_OF_ARITHMETIC_FOR_VECTOR( V , * ); \ DECLARATION_OF_ARITHMETIC_FOR_VECTOR( V , / ); \ DECLARATION_OF_ARITHMETIC_FOR_VECTOR( V , % ); \ DECLARATION_OF_INCREMENT_FOR_VECTOR( V , ++ ); \ DECLARATION_OF_INCREMENT_FOR_VECTOR( V , -- ); \ template <typename T> inline V<T> operator*( const T& scalar , V<T> v ) \ #define DEFINITION_OF_ARITHMETICS_FOR_VECTOR( V ) \ DEFINITION_OF_ARITHMETIC_FOR_VECTOR( V , + ); \ DEFINITION_OF_ARITHMETIC_FOR_VECTOR( V , - ); \ DEFINITION_OF_ARITHMETIC_FOR_VECTOR( V , * ); \ DEFINITION_OF_ARITHMETIC_FOR_VECTOR( V , / ); \ DEFINITION_OF_ARITHMETIC_FOR_VECTOR( V , % ); \ DEFINITION_OF_INCREMENT_FOR_VECTOR( V , ++ ); \ DEFINITION_OF_INCREMENT_FOR_VECTOR( V , -- ); \ template <typename T> inline V<T> operator*( const T& scalar , V<T> v ) { for( auto& t : v ){ t *= scalar; } return move( v ); } \ DEFINITION_OF_ARITHMETICS_FOR_VECTOR( vector ); DEFINITION_OF_ARITHMETICS_FOR_VECTOR( list ); template <typename V> inline auto Get( V& a ) { return [&]( const int& i = 0 ) -> const decldecay_t( a[0] )& { return a[i]; }; } template <typename T = int> inline vector<T> id( const int& size ) { vector<T> answer( size ); for( int i = 0 ; i < size ; i++ ){ answer[i] = i; } return answer; } template <typename T> inline void Sort( vector<T>& a , const bool& reversed ) { if( reversed ){ static auto comp = []( const T& t0 , const T& t1 ) { return t1 < t0; }; sort( a.begin() , a.end() , comp ); } else { sort( a.begin() , a.end() ); } } template <typename T> inline vector<int> IndexSort( const vector<T>& a , const bool& reversed ) { auto index = id<int>( a.size() ); if( reversed ){ sort( index.begin() , index.end() , [&]( const int& i , const int& j ) { return a[j] < a[i]; } ); } else { sort( index.begin() , index.end() , [&]( const int& i , const int& j ) { return a[i] < a[j]; } ); } return index; } #define DECLARATION_OF_COUT_FOR_VECTOR( V ) template <class Traits , typename Arg> inline basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os , const V<Arg>& arg ) #define DEFINITION_OF_COUT_FOR_VECTOR( V ) template <class Traits , typename Arg> inline basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os , const V<Arg>& arg ) { auto begin = arg.begin() , end = arg.end(); auto itr = begin; while( itr != end ){ ( itr == begin ? os : os << " " ) << *itr; itr++; } return os; } DEFINITION_OF_COUT_FOR_VECTOR( vector ); DEFINITION_OF_COUT_FOR_VECTOR( list ); DEFINITION_OF_COUT_FOR_VECTOR( set ); DEFINITION_OF_COUT_FOR_VECTOR( unordered_set ); inline void VariadicResize( const int& size ) {} template <typename Arg , typename... ARGS> inline void VariadicResize( const int& size , Arg& arg , ARGS&... args ) { arg.resize( size ); VariadicResize( size , args... ); } // Map #define DECLARATION_OF_ARITHMETIC_FOR_MAP( MAP , OPR ) \ template <typename T , typename U> inline MAP<T,U>& operator OPR ## = ( MAP<T,U>& a , const pair<T,U>& v ); \ template <typename T , typename U> inline MAP<T,U>& operator OPR ## = ( MAP<T,U>& a0 , const MAP<T,U>& a1 ); \ template <typename T , typename U , typename ARG> inline MAP<T,U> operator OPR( MAP<T,U> a , const ARG& arg ); \ #define DEFINITION_OF_ARITHMETIC_FOR_MAP( MAP , OPR ) \ template <typename T , typename U> inline MAP<T,U>& operator OPR ## = ( MAP<T,U>& a , const pair<T,U>& v ) { a[v.first] OPR ## = v.second; return a; } \ template <typename T , typename U> inline MAP<T,U>& operator OPR ## = ( MAP<T,U>& a0 , const MAP<T,U>& a1 ) { for( auto& [t,u] : a1 ){ a0[t] OPR ## = u; } return a0; } \ template <typename T , typename U , typename ARG> inline MAP<T,U> operator OPR( MAP<T,U> a , const ARG& arg ) { return move( a OPR ## = arg ); } \ #define DECLARATION_OF_ARITHMETICS_FOR_MAP( MAP ) \ DECLARATION_OF_ARITHMETIC_FOR_MAP( MAP , + ); \ DECLARATION_OF_ARITHMETIC_FOR_MAP( MAP , - ); \ DECLARATION_OF_ARITHMETIC_FOR_MAP( MAP , * ); \ DECLARATION_OF_ARITHMETIC_FOR_MAP( MAP , / ); \ DECLARATION_OF_ARITHMETIC_FOR_MAP( MAP , % ); \ #define DEFINITION_OF_ARITHMETICS_FOR_MAP( MAP ) \ DEFINITION_OF_ARITHMETIC_FOR_MAP( MAP , + ); \ DEFINITION_OF_ARITHMETIC_FOR_MAP( MAP , - ); \ DEFINITION_OF_ARITHMETIC_FOR_MAP( MAP , * ); \ DEFINITION_OF_ARITHMETIC_FOR_MAP( MAP , / ); \ DEFINITION_OF_ARITHMETIC_FOR_MAP( MAP , % ); \ template <typename T , typename U> using Map = conditional_t<is_constructible_v<unordered_map<T,int>>,unordered_map<T,U>,conditional_t<is_ordered::value<T>,map<T,U>,void>>; DEFINITION_OF_ARITHMETICS_FOR_MAP( map ); DEFINITION_OF_ARITHMETICS_FOR_MAP( unordered_map ); // StdStream template <class Traits> inline basic_istream<char,Traits>& VariadicCin( basic_istream<char,Traits>& is ) { return is; } template <class Traits , typename Arg , typename... ARGS> inline basic_istream<char,Traits>& VariadicCin( basic_istream<char,Traits>& is , Arg& arg , ARGS&... args ) { return VariadicCin( is >> arg , args... ); } template <class Traits> inline basic_istream<char,Traits>& VariadicSet( basic_istream<char,Traits>& is , const int& i ) { return is; } template <class Traits , typename Arg , typename... ARGS> inline basic_istream<char,Traits>& VariadicSet( basic_istream<char,Traits>& is , const int& i , Arg& arg , ARGS&... args ) { return VariadicSet( is >> arg[i] , i , args... ); } template <class Traits> inline basic_istream<char,Traits>& VariadicGetline( basic_istream<char,Traits>& is , const char& separator ) { return is; } template <class Traits , typename Arg , typename... ARGS> inline basic_istream<char,Traits>& VariadicGetline( basic_istream<char,Traits>& is , const char& separator , Arg& arg , ARGS&... args ) { return VariadicGetline( getline( is , arg , separator ) , separator , args... ); } template <class Traits , typename Arg> inline basic_ostream<char,Traits>& VariadicCout( basic_ostream<char,Traits>& os , Arg&& arg ) { return os << forward<Arg>( arg ); } template <class Traits , typename Arg1 , typename Arg2 , typename... ARGS> inline basic_ostream<char,Traits>& VariadicCout( basic_ostream<char,Traits>& os , Arg1&& arg1 , Arg2&& arg2 , ARGS&&... args ) { return VariadicCout( os << forward<Arg1>( arg1 ) << " " , forward<Arg2>( arg2 ) , forward<ARGS>( args )... ); } template <class Traits , typename Arg> inline basic_ostream<char,Traits>& VariadicCoutNonSep( basic_ostream<char,Traits>& os , Arg&& arg ) { return os << forward<Arg>( arg ); } template <class Traits , typename Arg1 , typename Arg2 , typename... ARGS> inline basic_ostream<char,Traits>& VariadicCoutNonSep( basic_ostream<char,Traits>& os , Arg1&& arg1 , Arg2&& arg2 , ARGS&&... args ) { return VariadicCoutNonSep( os << forward<Arg1>( arg1 ) , forward<Arg2>( arg2 ) , forward<ARGS>( args )... ); } template <class Traits , typename ARRAY> inline basic_ostream<char,Traits>& CoutArray( basic_ostream<char,Traits>& os , const int& i_start , const int& i_ulim , ARRAY&& a ) { for( int i = i_start ; i < i_ulim ; i++ ){ ( i == i_start ? os : ( os << " " ) ) << a[i]; } return os; } /* AAA 常設ライブラリの非圧縮版は以上に挿入する。*/ // デバッグ用 #ifdef DEBUG inline void AlertAbort( int n ) { CERR( "abort関数が呼ばれました。assertマクロのメッセージが出力されていない場合はオーバーフローの有無を確認をしてください。" ); } #endif // 入力フォーマットチェック用 // 1行中の変数の個数をSEPARATOR区切りで確認 #define GETLINE_COUNT( S , VARIABLE_NUMBER , SEPARATOR ) 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[i] == SEPARATOR ){ count++; } } assert( VARIABLE_NUMBER == 0 ? size == 0 : count + 1 == VARIABLE_NUMBER ); } // 余計な入力の有無を確認 #if defined( DEBUG ) || defined( REACTIVE ) #define CHECK_REDUNDANT_INPUT #else #ifdef USE_GETLINE #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 ) #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 ) #endif #endif // MIN <= N <= MAXを満たすNをSから構築 #define STOI( S , N , MIN , MAX ) decldecay_t( MAX ) N = 0; decldecay_t( MAX ) BOUND ## N = max( decldecay_t( MAX )( abs( MIN ) ) , abs( MAX ) ); { 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 ## N / 10 ? true : N == BOUND ## N / 10 && VARIABLE_FOR_DIGIT_FOR_GETLINE <= BOUND ## N % 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 ++; } ASSERT( N , MIN , MAX ); } #define STOI_A( S , I , N , A , MIN , MAX ) vector<decldecay_t( MAX )> A( N + I ); FOR( VARIABLE_FOR_STOI_A , 0 , N ){ STOI( S , A ##_VARIABLE_FOR_STOI_A , MIN , MAX ); A[VARIABLE_FOR_STOI_A + I] = A ##_VARIABLE_FOR_STOI_A; } // 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 ++; } } #define INCLUDE_LIBRARY #include __FILE__ #endif // INCLUDE_LIBRARY #endif // INCLUDE_MAIN