競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
HeavyLightDecompositionは無向森を分解し、経路や部分木をDFS添字の半開区間へ変換します。with_values<Range>(values)が返すHeavyLightValuesは分解と集計器を所有し、頂点番号で値を更新・集計できます。頂点vの値がvalues[v]になるように値を渡します。
BIT/Segtreeは別途貼り付けます。Rangeはfenwick_tree<T>・segtree<S,op,e>・lazy_segtree<S,op,e,F,mapping,composition,id>、または実ACLの対応型です。分解だけならHLD単独で使えます。本体・完全な使用例。
Graph tree{{1}, {0,2}, {1}};
auto hld = HeavyLightDecomposition(tree)
.with_values<fenwick_tree<std::int64_t>>(std::vector<std::int64_t>{10,20,30});
hld.add(1, 5);
auto sum = hld.path_prod(0, 2); // 65。
auto subtree = hld.subtree_prod(1); // 55。
以下、H=HeavyLightDecomposition、V=HeavyLightValues<Range,Ordered>、N=頂点数、M=辺数です。value_typeはBITならT、Segtree/LazyならS。集計器の型引数・作用の約束はSegtree・Lazy Segtreeと同じです。
value_typeは保持する値・集計結果の型です。
HeavyLightValues();
HeavyLightValues(const Graph& graph,const std::vector<value_type>& values,int root=0);
HeavyLightValues(const HeavyLightDecomposition& source,const std::vector<value_type>& values);
HeavyLightValues(HeavyLightDecomposition&& source,const std::vector<value_type>& values);
HeavyLightValues(const HeavyLightValues& other);
HeavyLightValues(HeavyLightValues&&);
空の状態、または分解と頂点順の値から構築します。既存の分解はコピー/移動で取り込み、コピー構築は独立した状態を作ります。
制約: 頂点数・入力構造・型の前提を満たすこと。空の構築も可能です。
計算量: 空の構築・ムーブはO(1)。GraphからはO(N+M)+集計器構築。既存分解からは並べ替えO(N)+集計器構築で、分解をコピーする場合はさらにO(N)です。コピー構築は保持領域に比例します。
V::operator=HeavyLightValues& operator=(HeavyLightValues other) noexcept;
コピーまたはムーブで代入し、自身への参照を返します。
制約: なし。
計算量: コピーは保持領域に比例します。旧状態の破棄費用も加わります。
V::buildvoid build(const Graph& graph,const std::vector<value_type>& values,int root=0);
分解と値を一緒に再構築します。戻り値はvoidです。
制約: 値の列は長さN。グラフは対称な無向森で、集計器は下記の対応条件を満たすこと。
計算量: O(N+M)+集計器構築。旧領域の破棄費用を加えます。
V::clearvoid clear() noexcept;
領域を解放して空の状態へ戻します。
制約: なし。
計算量: 保持領域の解放に比例します。
V::swapvoid swap(HeavyLightValues& other) noexcept;
保持するグラフ・結果・状態を交換します。
制約: なし。
計算量: O(1)。
V::getvalue_type get(int v);
指定頂点の値をvalue_typeで返します。
制約: 頂点番号が有効で、使う操作に対応した集計器を用意すること。
計算量: SegtreeでO(1)、他の対応集計器でO(log N)。
V::setvoid set(int v,const value_type x);
指定頂点の値を置き換えます。
制約: 頂点番号が有効で、使う操作に対応した集計器を用意すること。
計算量: 標準の対応集計器でO(log N)。
V::addvoid add(int v,const value_type x);
指定頂点の値へ加算します。
制約: 頂点番号が有効で、使う操作に対応した集計器を用意すること。
計算量: 標準の対応集計器でO(log N)。
V::applytemplate<class F> void apply(int v,const F f);
指定頂点の値へ作用を適用します。
制約: 頂点番号が有効で、使う操作に対応した集計器を用意すること。
計算量: 標準の対応集計器でO(log N)。
V::path_prodvalue_type path_prod(int u,int v,bool edges=false);
u→vの通行順に経路を集約して返します。
制約: 両端は同じ木の有効な頂点番号です。集計器・通行順・辺モードの前提はこの節の共通条件に従います。
計算量: 標準のBIT・Segtree・Lazy SegtreeでO(log²N)。
V::subtree_prodvalue_type subtree_prod(int v,bool edges=false);
部分木をDFS添字昇順に集約して返します。
制約: 頂点番号が有効で、使う操作に対応した集計器を用意すること。
計算量: 標準のBIT・Segtree・Lazy SegtreeでO(log N)。
V::path_applytemplate<class F> void path_apply(int u,int v,const F f,bool edges=false);
経路の各区間へ指定の作用を適用します。
制約: 両端は同じ木の有効な頂点番号です。集計器・通行順・辺モードの前提はこの節の共通条件に従います。
計算量: 標準のLazy SegtreeでO(log²N)。
V::subtree_applytemplate<class F> void subtree_apply(int v,const F f,bool edges=false);
部分木の区間へ指定の作用を適用します。
制約: 頂点番号が有効で、使う操作に対応した集計器を用意すること。
計算量: 標準のLazy SegtreeでO(log N)。
V::valuesstd::vector<value_type> values();
全値を頂点番号順のvector
制約: 引数の頂点番号・添字は有効範囲内です。その他の前提は共通条件に従います。
計算量: Range.values()の費用+O(N)。values()がなければN回のgetの費用です。
値操作は非constです。get/set/addはBIT、get/setはSegtree、get/set/applyはLazy等の対応する集計器で使えます。Vからも以下の分解照会を呼べますが、Vにwith_valuesや外部集計器用オーバーロードはありません。
Ordered=falseは和・最小値など可換な演算用です。文字列連結・関数合成ではtrueを指定し、経路u→vの順に結合します。集計器2本分を使い、値の順だけを反転します。個々の値を逆元へ変換する機能ではありません。部分木積の順はDFS添字昇順です。
辺の値は親辺を子頂点の位置へ置き、各根に単位元を置いてedges=trueを指定します。経路はLCAの位置、部分木は指定頂点の位置を除きます。path_prod(v,v,true)や葉の辺部分木は単位元、空区間への更新は無操作です。
HeavyLightDecomposition();
explicit HeavyLightDecomposition(const Graph& graph,int root=0);
HeavyLightDecomposition(const HeavyLightDecomposition&);
HeavyLightDecomposition(HeavyLightDecomposition&& other) noexcept;
空の状態、または無向森から分解を構築します。コピーは独立し、ムーブ元は空になります。
制約: 頂点数・入力構造・型の前提を満たすこと。空の構築も可能です。
計算量: 空の構築はO(1)、分解はO(N+M)時間・O(N)領域。旧状態の破棄費用を加えます。
H::operator=HeavyLightDecomposition& operator=(HeavyLightDecomposition other) noexcept;
コピーまたはムーブで代入し、自身への参照を返します。
制約: なし。
計算量: コピーは保持領域に比例します。旧状態の破棄費用も加わります。
H::buildvoid build(const Graph& graph,int root=0);
再構築/空にする/交換。戻り値void。
制約: 値の列は長さN。グラフは対称な無向森で、集計器は下記の対応条件を満たすこと。
計算量: 空の構築はO(1)、分解はO(N+M)時間・O(N)領域。旧状態の破棄費用を加えます。
H::clearvoid clear() noexcept;
領域を解放して空の状態へ戻します。
制約: なし。
計算量: 保持領域の解放に比例します。
H::swapvoid swap(HeavyLightDecomposition& other) noexcept;
保持するグラフ・結果・状態を交換します。
制約: なし。
計算量: O(1)。
H::with_valuestemplate<class Range,bool Ordered=false,class T> HeavyLightValues<Range,Ordered> with_values(const std::vector<T>& values) const &;
template<class Range,bool Ordered=false,class T> HeavyLightValues<Range,Ordered> with_values(const std::vector<T>& values) &&;
長さNの頂点順vectorを受け、分解をコピーしてVを返す。 同じ入力から、分解を移動してVを返す。
制約: 値の列は長さN。グラフは対称な無向森で、集計器は下記の対応条件を満たすこと。
計算量: 並べ替えO(N)+集計器構築。const&版は分解のコピーO(N)も加わります。
Graphはstd::vector<std::vector<int>>です。with_valuesは値配列への参照を保持しません。コピー/move代入・buildは各操作の費用に旧状態の破棄を加えます。
全てconstで呼べます。頂点・DFS添字はint、祖先・jumpのkはstd::int64_tです。H::Intervalは{int left,right; bool reversed;}。区間は頂点番号ではなくDFS添字で、reversedならright−1からleftへ読みます。VにIntervalの型別名はありません。
H::sizeint size() const noexcept;
頂点数を返します。
制約: なし。
計算量: O(1)。
H::emptybool empty() const noexcept;
頂点数が0ならtrueを返します。
制約: なし。
計算量: O(1)。
H::rootint root() const noexcept;
根の頂点番号を返します。空なら-1です。
制約: なし。
計算量: O(1)。
H::component_countint component_count() const noexcept;
分解した連結成分の数を返します。
制約: 引数の頂点番号・添字は有効範囲内です。その他の前提は共通条件に従います。
計算量: O(1)。
H::parentint parent(int v) const;
親の頂点番号を返します。根の親は-1です。
制約: 引数の頂点番号・添字は有効範囲内です。その他の前提は共通条件に従います。
計算量: O(1)。
H::depthint depth(int v) const;
根からの深さを返します。
制約: 引数の頂点番号・添字は有効範囲内です。その他の前提は共通条件に従います。
計算量: O(1)。
H::componentint component(int v) const;
所属する木の番号を0始まりで返します。
制約: 引数の頂点番号・添字は有効範囲内です。その他の前提は共通条件に従います。
計算量: O(1)。
H::root_ofint root_of(int v) const;
所属する木の根の頂点番号を返します。
制約: 引数の頂点番号・添字は有効範囲内です。その他の前提は共通条件に従います。
計算量: O(1)。
H::indexint index(int v) const;
頂点番号をDFS添字へ変換します。
制約: 引数の頂点番号・添字は有効範囲内です。その他の前提は共通条件に従います。
計算量: O(1)。
H::vertex_atint vertex_at(int i) const;
DFS添字を頂点番号へ変換します。
制約: 0<=i<N。
計算量: O(1)。
H::orderconst std::vector<int>& order() const noexcept;
添字iにvertex_at(i)を置くvector
制約: なし。
計算量: O(1)。
H::reordertemplate<class T> std::vector<T> reorder(const std::vector<T>& values) const;
頂点順の値をDFS添字順へコピーして返します。
制約: 値の列は長さN。グラフは対称な無向森で、集計器は下記の対応条件を満たすこと。
計算量: O(N)時間・追加領域。
H::restoretemplate<class T> std::vector<T> restore(const std::vector<T>& values) const;
DFS添字順の値を頂点順へコピーして返します。
制約: 値の列は長さN。グラフは対称な無向森で、集計器は下記の対応条件を満たすこと。
計算量: O(N)時間・追加領域。
H::same_componentbool same_component(int u,int v) const;
2頂点が同じ木に属すならtrueを返します。
制約: 引数の頂点番号・添字は有効範囲内です。その他の前提は共通条件に従います。
計算量: O(1)。
H::is_ancestorbool is_ancestor(int u,int v) const;
uがvの祖先ならtrueを返します。自身は祖先とし、異成分ならfalseです。
制約: 引数の頂点番号・添字は有効範囲内です。その他の前提は共通条件に従います。
計算量: O(1)。
H::subtree_rangestd::pair<int,int> subtree_range(int v,bool edges=false) const;
pair<int,int>の半開区間[l,r)。edges=trueはvの位置を除く。
制約: 引数の頂点番号・添字は有効範囲内です。その他の前提は共通条件に従います。
計算量: O(1)。
H::subtree_sizeint subtree_size(int v) const;
指定頂点を含む部分木の頂点数を返します。
制約: 引数の頂点番号・添字は有効範囲内です。その他の前提は共通条件に従います。
計算量: O(1)。
H::lcaint lca(int u,int v) const;
intのLCA/経路の辺数。別の木なら-1。
制約: 引数の頂点番号・添字は有効範囲内です。その他の前提は共通条件に従います。
計算量: O(log N)。pathの結果領域はO(log N)、for_each_pathにはコールバックの費用を加えます。
H::distanceint distance(int u,int v) const;
intのLCA/経路の辺数。別の木なら-1。
制約: 引数の頂点番号・添字は有効範囲内です。その他の前提は共通条件に従います。
計算量: O(log N)。pathの結果領域はO(log N)、for_each_pathにはコールバックの費用を加えます。
H::kth_ancestorint kth_ancestor(int v,std::int64_t k) const;
k個上の頂点をintで返す。k=0はv、負数・根を超える場合は-1。
制約: 引数の頂点番号・添字は有効範囲内です。その他の前提は共通条件に従います。
計算量: O(log N)。pathの結果領域はO(log N)、for_each_pathにはコールバックの費用を加えます。
H::jumpint jump(int u,int v,std::int64_t k) const;
u→vの0始まりk番目の頂点をintで返す。負数・範囲外・別の木は-1。
制約: 引数の頂点番号・添字は有効範囲内です。その他の前提は共通条件に従います。
計算量: O(log N)。pathの結果領域はO(log N)、for_each_pathにはコールバックの費用を加えます。
H::for_each_pathtemplate<class Fn> bool for_each_path(int u,int v,Fn&& fn,bool edges=false) const;
通行順にfn(left,right,reversed)を呼び、接続ならtrue、別の木ならfalse。別の木では呼ばず、fnの戻り値は無視する。
制約: 引数の頂点番号・添字は有効範囲内です。その他の前提は共通条件に従います。
計算量: O(log N)。pathの結果領域はO(log N)、for_each_pathにはコールバックの費用を加えます。
H::pathstd::vector<Interval> path(int u,int v,bool edges=false) const;
同じ通行順のvector<H::Interval>を返す。別の木は空。
制約: 引数の頂点番号・添字は有効範囲内です。その他の前提は共通条件に従います。
計算量: O(log N)。pathの結果領域はO(log N)、for_each_pathにはコールバックの費用を加えます。
for_each_pathにはfnの実行費用も加わります。辺モードでu=vならfnを呼ばずtrue、pathは空です。
HeavyLightDecomposition layout(tree); // 集計器なし。
auto [l,r] = layout.subtree_range(1);
auto parts = layout.path(2,0); // 通行順の区間列。
auto vertex = layout.vertex_at(l); // DFS添字を頂点へ戻す。
Hはconstでも使えます。集計器にはDFS添字順・長さNの値を置きます。Qは区間照会1回、Aは区間更新1回の費用です。
H::path_prodtemplate<class Identity,class Q,class Op> auto path_prod(int u,int v,Identity&& identity,Q&& query,Op&& op,bool edges=false) const;
template<class Identity,class Q,class Op,class Reverse> auto path_prod(int u,int v,Identity&& identity,Q&& query,Op&& op,Reverse&& reverse,bool edges=false) const;
template<class Range> auto path_prod(int u,int v,Range& range,bool edges=false) const;
template<template<class,auto,auto> class R,class S,auto Op,auto E> S path_prod(int u,int v,const R<S,Op,E>& range,bool edges=false) const;
template<template<class,auto,auto> class R,class S,auto Op,auto E,class Reverse> S path_prod(int u,int v,const R<S,Op,E>& range,Reverse&& reverse,bool edges=false) const;
template<template<class,auto,auto,class,auto,auto,auto> class R,class S,auto Op,auto E,class F,auto M,auto C,auto I> S path_prod(int u,int v,R<S,Op,E,F,M,C,I>& range,bool edges=false) const;
template<template<class,auto,auto,class,auto,auto,auto> class R,class S,auto Op,auto E,class F,auto M,auto C,auto I,class Reverse> S path_prod(int u,int v,R<S,Op,E,F,M,C,I>& range,Reverse&& reverse,bool edges=false) const;
u→vの通行順に経路を集約して返します。
制約: 両端は同じ木の有効な頂点番号です。集計器・通行順・辺モードの前提はこの節の共通条件に従います。
計算量: O(log N)回の区間照会・結合。照会1回Q、値コピー・結合をO(1)とするとO(Q log N)。
H::subtree_prodtemplate<class Q> auto subtree_prod(int v,Q&& query,bool edges=false) const;
部分木をDFS添字昇順に集約して返します。
制約: 頂点番号が有効で、使う操作に対応した集計器を用意すること。
計算量: 区間照会1回の費用。
H::path_applytemplate<class Range,class F> void path_apply(int u,int v,Range& range,const F f,bool edges=false) const;
経路の各区間へ指定の作用を適用します。
制約: 両端は同じ木の有効な頂点番号です。集計器・通行順・辺モードの前提はこの節の共通条件に従います。
計算量: O(log N)回の区間更新。1回の費用をAとしてO(A log N)。
H::subtree_applytemplate<class Range,class F> void subtree_apply(int v,Range& range,const F f,bool edges=false) const;
部分木の区間へ指定の作用を適用します。
制約: 頂点番号が有効で、使う操作に対応した集計器を用意すること。
計算量: 区間更新1回の費用。
queryにはprodかsumを持つ集計器、または(l,r)か(l,r,reversed)を受け取る関数を渡します。2引数の関数は可換演算専用です。3引数の関数は通行順に集約した結果を返します。reverseは逆順の集計値への変換に使います。std::identityを除き、3引数のqueryとは併用しません。空区間は単位元です。opは結合的でidentityを左右の単位元とし、照会・値のコピー・結合の費用も加わります。集計器や関数の参照は保持しません。
入力は頂点0..N−1の対称な無向森です。指定根の木を先に分解し、残りは最小未訪問頂点を根にします。空入力はroot=0。経路集計・更新の両端は同じ木が前提です。頂点・自己辺・閉路・多重辺は探索中に検査し、隣接列の対称性は利用側で保証します。SKIP_RUNTIME_CHECKSで入力検査と検査専用状態を除きます。BITの整数和は型幅で周回します。
コピーは独立、move元は空です。build・コピー代入の失敗時は元を保ちますが、分解の移動を指定した構築の失敗では移動元が空になる場合があります。更新時の例外は巻き戻しません。Ordered=trueはclear/buildで作り直してください。
pathの配列は独立所有、orderは参照です。参照はclear・再構築・代入・破棄まで有効です。根や木を変えるbuildには値も渡し、辺の値は新たな親子対応へ置き直します。
並べ替えのO(N)に集計器の構築費用が加わります。実ACL Fenwickの構築ではaddをN回呼ぶためO(N log N)です。計算量は値の演算とコピーをO(1)としており、文字列などにかかる費用は別です。設計・検証の記録を参照してください。
[共通型] Graphはvector<vector<int>>。HeavyLightDecomposition::Intervalは{int left,right;bool reversed;}。
[値を持つHLD: HeavyLightValues<Range,Ordered=false>]
value_typeはBITのT/Segtree・LazyのS。HeavyLightValues()は空。コピー構築/代入は独立、移動構築はO(1)。
operator=(other)は自身への参照を返す。代入/buildは旧状態の破棄費用も加わる。
with_values<Range,Ordered>(values): values[v]を頂点vの値とする長さNのvectorから、分解と集計器を独立所有するHLDを返す。
元の値配列とは更新を共有しない。get/set/add/applyのvは頂点番号で、index(v)への変換は不要。
一時HLDなら分解を移動、既存HLDならコピー。RangeはBIT/ACL形式Segtree/Lazy。値の型はRangeから推論。
HeavyLightValues(g,values,root=0)/build(g,values,root=0): 分解と値を同時に構築/再構築。失敗時は元を保持。
HeavyLightValues(layout,values): 分解をコピー、std::move(layout)なら移動。移動後の失敗では元分解が空の場合あり。
構築は分解O(N+M)+集計器のvector構築。vector構築なしのBITはN回add。所有状態を1回確保する。
get(v)/set(v,x)/add(v,x): 頂点番号で取得/代入/加算。addは対応BITのみ。通常の集計器ではO(log N)。
apply(v,f)/path_apply(u,v,f,edges=false)/subtree_apply(v,f,edges=false): 点/経路/部分木へ作用。
path_prod(u,v,edges=false)/subtree_prod(v,edges=false): 所有集計器で経路/部分木を集計。通常O(log²N)/O(log N)。
Ordered=falseは和・最小値などの可換演算用。trueは文字列連結などをu→vの順に集計。領域と更新は集計器2本分。
trueは値の並び順を守る設定で、個々の辺の値を逆元へ変える機能ではない。部分木の積はDFS添字昇順。
辺モードは子vに親辺の値を置き、各根には0/e()を置く。初期値はN個必要。edges=trueでLCA/部分木の指定頂点を除く。
path_prod(v,v)は頂点の値、path_prod(v,v,true)や葉の辺部分木は空区間の単位元。空区間の更新は何もしない。
values(): 元の頂点順へ復元。Range.valuesがあればO(N)の並べ直し、なければN回点照会。
clear()/swap(other): 空へ/交換。コピーは独立、move元は空。build/コピー失敗時は元を保持。
分解情報・reorder/restore/for_each_path/pathも同名で取得可能。対応applyを持つRangeでのみ作用APIを使用できる。
所有型にInterval型別名・with_values・外部集計器を受けるオーバーロードはない。pathの要素型は上記Interval。
値の照会も非constの所有HLDから呼ぶ。根を変えて辺の値をbuildする場合は、新しい親子対応で値を配置し直す。
値の更新中の例外では変更は巻き戻さない。Ordered=trueでは例外後にclear/buildで再構築すること。
[分解だけを持つHLD: HeavyLightDecomposition]
HeavyLightDecomposition()は空、コピー構築はO(N)、移動構築はO(1)。operator=(other)は自身への参照を返す。
コピー・移動代入とbuildは旧状態の破棄費用も加わる。
HeavyLightDecomposition(g,root=0)/build(g,root=0): 無向森を反復DFSで分解。O(n+m)時間・領域。
頂点番号[0,n)。隣接リストの対称性は前提。rootの木から、残りは最小未訪問頂点から構築する。
失敗時は元を保持。空のgにはroot=0。閉路・自己辺・多重辺は検査有効時に例外。
size()/empty()/root()/component_count(): 頂点数/空か/指定根(空なら-1)/木の数。O(1)。
parent(v)/depth(v)/component(v)/root_of(v): 親(根は-1)/深さ/木の番号/木の根。O(1)。
index(v)/vertex_at(i)/order(): heavy優先DFS添字/対応頂点/順序配列へのconst参照。O(1)。
same_component(u,v)/is_ancestor(u,v): 同じ木か/祖先か(自身を含む)。O(1)。
subtree_range(v,edges=false): 部分木の半開区間。edges=trueは根vを除き子側の頂点で辺を表す。O(1)。
subtree_size(v): 部分木の頂点数。O(1)。lca(u,v)/distance(u,v): LCA/辺数、異なる木は-1。O(log n)。
kth_ancestor(v,k): k個上の頂点。k<0/根を越すと-1。jump(u,v,k): u→vのk番目、範囲外/異なる木は-1。O(log n)。
for_each_path(u,v,fn,edges=false): 通行順にfn(left,right,reversed)を呼ぶ。fnの戻り値は無視。接続ならtrue、異なる木はfalse。
reversed=trueはright-1からleftへ読む。区間は半開。edges=trueはLCAを除く。O(log n)、ヒープ確保なし。
path(u,v,edges=false): 同じ順序のInterval{left,right,reversed}配列を返す。異なる木は空。O(log n)時間・領域。
clear()/swap(other): 空に戻す/O(1)交換。コピーは独立所有、ムーブ元は空。検査はSKIP_RUNTIME_CHECKSで除く。
reorder(values)/restore(ordered): 長さNのvectorをDFS添字順/元の頂点順へコピー。O(N)時間・領域。
path_prod(u,v,bit,edges=false): sumを持つBITの和。単位元はT{}、整数は型幅で周回。O(log N)回照会。
path_prod(u,v,seg,edges=false): ACL形式のSegtreeからop/eを推論した可換積。Segはconst可、Lazyは非const。
path_prod(u,v,identity,query,op,edges=false): queryはprod/sum、2引数関数なら可換、3引数(l,r,reversed)なら向き対応。
path_prod(u,v,seg,reverse,edges=false)/汎用版のop直後にreverse指定: 正しい逆順集約を返す非可換積。
subtree_prod(v,query,edges=false): 部分木のDFS添字昇順の積。区間照会1回。非可換も可。
path_apply(u,v,range,f,edges=false)/subtree_apply(v,range,f,edges=false): range.apply(l,r,f)で更新。O(log N)/1回。
prod/applyのrangeは同じDFS添字順で用意する。path_prod/path_applyは同じ木が前提、検査時は別木を例外。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| Jump on Tree | hld_jump.cpp | 現行未照合・要再提出 | AC |
| Vertex Set Path Composite | hld_path_composite.cpp | 現行未照合・要再提出 | AC |
| Vertex Add Path Sum (HLD path_prod) | hld_path_prod.cpp | 現行未照合・要再提出 | AC |
| Vertex Add Path Sum | hld_vertex_add_path_sum.cpp | 現行未照合・要再提出 | AC |
| J - Road Network Development | tree_path_difference_road.cpp | 未提出 | — |
✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。
| 検査 | 状態 |
|---|---|
| 正常系 | 未実施 |
| 失敗系 | 未測定 |
| Library Checker公式ケース | 未測定 |
| 性能測定 | 未実施 |
| ASan/UBSan | 未実施 |
| カバレッジ | 未実施 |
到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。
// SPDX-License-Identifier: CC0-1.0
// 参考(API設計・CC0): https://github.com/NyaanNyaan/library/blob/2e713880f0d5041d794e0f09c7f31c1826ba0ccf/tree/heavy-light-decomposition.hpp
// 参考元の提供条件: https://github.com/NyaanNyaan/library/blob/2e713880f0d5041d794e0f09c7f31c1826ba0ccf/LICENSE
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/heavy_light_decomposition.md
//
// API一覧
// [共通型] Graphはvector<vector<int>>。HeavyLightDecomposition::Intervalは{int left,right;bool reversed;}。
// [値を持つHLD: HeavyLightValues<Range,Ordered=false>]
// value_typeはBITのT/Segtree・LazyのS。HeavyLightValues()は空。コピー構築/代入は独立、移動構築はO(1)。
// operator=(other)は自身への参照を返す。代入/buildは旧状態の破棄費用も加わる。
// with_values<Range,Ordered>(values): values[v]を頂点vの値とする長さNのvectorから、分解と集計器を独立所有するHLDを返す。
// 元の値配列とは更新を共有しない。get/set/add/applyのvは頂点番号で、index(v)への変換は不要。
// 一時HLDなら分解を移動、既存HLDならコピー。RangeはBIT/ACL形式Segtree/Lazy。値の型はRangeから推論。
// HeavyLightValues(g,values,root=0)/build(g,values,root=0): 分解と値を同時に構築/再構築。失敗時は元を保持。
// HeavyLightValues(layout,values): 分解をコピー、std::move(layout)なら移動。移動後の失敗では元分解が空の場合あり。
// 構築は分解O(N+M)+集計器のvector構築。vector構築なしのBITはN回add。所有状態を1回確保する。
// get(v)/set(v,x)/add(v,x): 頂点番号で取得/代入/加算。addは対応BITのみ。通常の集計器ではO(log N)。
// apply(v,f)/path_apply(u,v,f,edges=false)/subtree_apply(v,f,edges=false): 点/経路/部分木へ作用。
// path_prod(u,v,edges=false)/subtree_prod(v,edges=false): 所有集計器で経路/部分木を集計。通常O(log²N)/O(log N)。
// Ordered=falseは和・最小値などの可換演算用。trueは文字列連結などをu→vの順に集計。領域と更新は集計器2本分。
// trueは値の並び順を守る設定で、個々の辺の値を逆元へ変える機能ではない。部分木の積はDFS添字昇順。
// 辺モードは子vに親辺の値を置き、各根には0/e()を置く。初期値はN個必要。edges=trueでLCA/部分木の指定頂点を除く。
// path_prod(v,v)は頂点の値、path_prod(v,v,true)や葉の辺部分木は空区間の単位元。空区間の更新は何もしない。
// values(): 元の頂点順へ復元。Range.valuesがあればO(N)の並べ直し、なければN回点照会。
// clear()/swap(other): 空へ/交換。コピーは独立、move元は空。build/コピー失敗時は元を保持。
// 分解情報・reorder/restore/for_each_path/pathも同名で取得可能。対応applyを持つRangeでのみ作用APIを使用できる。
// 所有型にInterval型別名・with_values・外部集計器を受けるオーバーロードはない。pathの要素型は上記Interval。
// 値の照会も非constの所有HLDから呼ぶ。根を変えて辺の値をbuildする場合は、新しい親子対応で値を配置し直す。
// 値の更新中の例外では変更は巻き戻さない。Ordered=trueでは例外後にclear/buildで再構築すること。
// [分解だけを持つHLD: HeavyLightDecomposition]
// HeavyLightDecomposition()は空、コピー構築はO(N)、移動構築はO(1)。operator=(other)は自身への参照を返す。
// コピー・移動代入とbuildは旧状態の破棄費用も加わる。
// HeavyLightDecomposition(g,root=0)/build(g,root=0): 無向森を反復DFSで分解。O(n+m)時間・領域。
// 頂点番号[0,n)。隣接リストの対称性は前提。rootの木から、残りは最小未訪問頂点から構築する。
// 失敗時は元を保持。空のgにはroot=0。閉路・自己辺・多重辺は検査有効時に例外。
// size()/empty()/root()/component_count(): 頂点数/空か/指定根(空なら-1)/木の数。O(1)。
// parent(v)/depth(v)/component(v)/root_of(v): 親(根は-1)/深さ/木の番号/木の根。O(1)。
// index(v)/vertex_at(i)/order(): heavy優先DFS添字/対応頂点/順序配列へのconst参照。O(1)。
// same_component(u,v)/is_ancestor(u,v): 同じ木か/祖先か(自身を含む)。O(1)。
// subtree_range(v,edges=false): 部分木の半開区間。edges=trueは根vを除き子側の頂点で辺を表す。O(1)。
// subtree_size(v): 部分木の頂点数。O(1)。lca(u,v)/distance(u,v): LCA/辺数、異なる木は-1。O(log n)。
// kth_ancestor(v,k): k個上の頂点。k<0/根を越すと-1。jump(u,v,k): u→vのk番目、範囲外/異なる木は-1。O(log n)。
// for_each_path(u,v,fn,edges=false): 通行順にfn(left,right,reversed)を呼ぶ。fnの戻り値は無視。接続ならtrue、異なる木はfalse。
// reversed=trueはright-1からleftへ読む。区間は半開。edges=trueはLCAを除く。O(log n)、ヒープ確保なし。
// path(u,v,edges=false): 同じ順序のInterval{left,right,reversed}配列を返す。異なる木は空。O(log n)時間・領域。
// clear()/swap(other): 空に戻す/O(1)交換。コピーは独立所有、ムーブ元は空。検査はSKIP_RUNTIME_CHECKSで除く。
// reorder(values)/restore(ordered): 長さNのvectorをDFS添字順/元の頂点順へコピー。O(N)時間・領域。
// path_prod(u,v,bit,edges=false): sumを持つBITの和。単位元はT{}、整数は型幅で周回。O(log N)回照会。
// path_prod(u,v,seg,edges=false): ACL形式のSegtreeからop/eを推論した可換積。Segはconst可、Lazyは非const。
// path_prod(u,v,identity,query,op,edges=false): queryはprod/sum、2引数関数なら可換、3引数(l,r,reversed)なら向き対応。
// path_prod(u,v,seg,reverse,edges=false)/汎用版のop直後にreverse指定: 正しい逆順集約を返す非可換積。
// subtree_prod(v,query,edges=false): 部分木のDFS添字昇順の積。区間照会1回。非可換も可。
// path_apply(u,v,range,f,edges=false)/subtree_apply(v,range,f,edges=false): range.apply(l,r,f)で更新。O(log N)/1回。
// prod/applyのrangeは同じDFS添字順で用意する。path_prod/path_applyは同じ木が前提、検査時は別木を例外。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <array>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <limits>
#include <memory>
#include <optional>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_UNWEIGHTED_GRAPH_DEFINED
#define SIRKIT_UNWEIGHTED_GRAPH_DEFINED
using Graph = std::vector<std::vector<int>>;
#endif
#ifndef SIRKIT_HLD_QUERY_HELPERS_DEFINED
#define SIRKIT_HLD_QUERY_HELPERS_DEFINED
namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ {
inline int HldVertexCount(std::size_t vertices) {
if (vertices > static_cast<std::size_t>(std::numeric_limits<int>::max()))
throw std::length_error("HLD: int size overflow");
return static_cast<int>(vertices);
}
template<class T, bool = std::is_integral_v<T> && !std::is_same_v<T,bool>>
struct HldArithmetic {
static T add(const T& a,const T& b) { return a+b; }
static T subtract(const T& a,const T& b) { return a-b; }
};
template<class T> struct HldArithmetic<T,true> {
using U=std::make_unsigned_t<T>;
static T add(T a,T b) { return static_cast<T>(static_cast<U>(a)+static_cast<U>(b)); }
static T subtract(T a,T b) { return static_cast<T>(static_cast<U>(a)-static_cast<U>(b)); }
};
#if defined(__SIZEOF_INT128__)
template<> struct HldArithmetic<__int128,false> {
using U=unsigned __int128;
static __int128 add(__int128 a,__int128 b) { return static_cast<__int128>(static_cast<U>(a)+static_cast<U>(b)); }
static __int128 subtract(__int128 a,__int128 b) { return static_cast<__int128>(static_cast<U>(a)-static_cast<U>(b)); }
};
template<> struct HldArithmetic<unsigned __int128,false> {
using U=unsigned __int128;
static U add(U a,U b) { return a+b; }
static U subtract(U a,U b) { return a-b; }
};
#endif
template<class Q> concept HldQuery = requires(Q& q) { q.prod(0,0); } ||
requires(Q& q) { q.sum(0,0); } || std::invocable<Q&,int,int,bool> || std::invocable<Q&,int,int>;
template<class Q> decltype(auto) hld_query(Q& q,int l,int r,bool reverse=false) {
if constexpr (requires { q.prod(l,r); }) return q.prod(l,r);
else if constexpr (requires { q.sum(l,r); }) return q.sum(l,r);
else if constexpr (std::invocable<Q&,int,int,bool>) return std::invoke(q,l,r,reverse);
else return std::invoke(q,l,r);
}
template<class Q> using HldQueryValue=std::remove_cvref_t<decltype(hld_query(std::declval<Q&>(),0,0))>;
template<class Q> inline constexpr bool hld_directional_query =
!requires(Q& q) { q.prod(0,0); } && !requires(Q& q) { q.sum(0,0); } && std::invocable<Q&,int,int,bool>;
template<class I,class Q,class Op> concept HldProduct = HldQuery<Q> &&
std::constructible_from<HldQueryValue<Q>,I> &&
std::is_invocable_r_v<HldQueryValue<Q>,Op&,HldQueryValue<Q>,HldQueryValue<Q>>;
template<class I,class Q,class Op,class Reverse> concept HldReversedProduct = HldProduct<I,Q,Op> &&
std::is_invocable_r_v<HldQueryValue<Q>,Reverse&,HldQueryValue<Q>> &&
(!hld_directional_query<Q> || std::is_same_v<std::remove_cvref_t<Reverse>,std::identity>);
}
#endif
#ifndef SIRKIT_HEAVY_LIGHT_VALUES_FORWARD_DEFINED
#define SIRKIT_HEAVY_LIGHT_VALUES_FORWARD_DEFINED
template<class Range,bool Ordered=false> class HeavyLightValues;
#endif
#ifndef SIRKIT_HEAVY_LIGHT_DECOMPOSITION_DEFINED
#define SIRKIT_HEAVY_LIGHT_DECOMPOSITION_DEFINED
class HeavyLightDecomposition {
std::vector<int> parent_,depth_,component_,roots_,index_,order_,subtree_,head_;
int root_=-1;
void check_vertex(int v) const {
#ifndef SKIP_RUNTIME_CHECKS
if(v<0 || v>=size()) throw std::out_of_range("HLD: vertex");
#else
(void)v;
#endif
}
void check_size(std::size_t n) const {
#ifndef SKIP_RUNTIME_CHECKS
if(n!=order_.size()) throw std::invalid_argument("HLD: values size");
#else
(void)n;
#endif
}
void construct(const Graph& graph,int root) {
const int n=/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::HldVertexCount(graph.size());
#ifndef SKIP_RUNTIME_CHECKS
if((n==0 && root!=0) || (n!=0 && (root<0 || root>=n))) throw std::out_of_range("HLD: root");
#endif
if(n==0) return;
root_=root;
parent_.assign(n,-2); depth_.resize(n); component_.resize(n);
index_.resize(n); subtree_.assign(n,1); head_.resize(n); order_.reserve(n);
std::vector<int> pending,discovery,heavy(n,-1);
pending.reserve(n); discovery.reserve(n);
const auto grow=[&](int start) {
const int id=static_cast<int>(roots_.size()); roots_.push_back(start);
parent_[start]=-1;component_[start]=id;pending.push_back(start);
while(!pending.empty()) {
const int v=pending.back();pending.pop_back();discovery.push_back(v);
#ifndef SKIP_RUNTIME_CHECKS
bool parent_seen=false;
#endif
for(int u:graph[v]) {
#ifndef SKIP_RUNTIME_CHECKS
if(u<0 || u>=n) throw std::out_of_range("HLD: neighbor");
if(u==v) throw std::invalid_argument("HLD: self edge");
#endif
if(u==parent_[v]) {
#ifndef SKIP_RUNTIME_CHECKS
if(parent_seen) throw std::invalid_argument("HLD: multiple edge");
parent_seen=true;
#endif
continue;
}
#ifndef SKIP_RUNTIME_CHECKS
if(parent_[u]!=-2) throw std::invalid_argument("HLD: cycle or multiple edge");
#endif
parent_[u]=v;depth_[u]=depth_[v]+1;component_[u]=id;pending.push_back(u);
}
}
};
grow(root);
for(int v=0;v<n;++v) if(parent_[v]==-2) grow(v);
for(auto it=discovery.rbegin();it!=discovery.rend();++it) {
const int v=*it,p=parent_[v];
if(p<0) continue;
subtree_[p]+=subtree_[v];
if(heavy[p]<0 || subtree_[v]>subtree_[heavy[p]]) heavy[p]=v;
}
for(int start:roots_) {
pending.push_back(start);
while(!pending.empty()) {
const int chain=pending.back();pending.pop_back();
for(int v=chain;v!=-1;v=heavy[v]) {
head_[v]=chain;index_[v]=static_cast<int>(order_.size());order_.push_back(v);
for(auto it=graph[v].rbegin();it!=graph[v].rend();++it)
if(parent_[*it]==v && *it!=heavy[v]) pending.push_back(*it);
}
}
}
}
public:
struct Interval { int left;int right;bool reversed; };
HeavyLightDecomposition()=default;
explicit HeavyLightDecomposition(const Graph& graph,int root=0) { construct(graph,root); }
HeavyLightDecomposition(const HeavyLightDecomposition&)=default;
HeavyLightDecomposition(HeavyLightDecomposition&& other) noexcept { swap(other); }
HeavyLightDecomposition& operator=(HeavyLightDecomposition other) noexcept { swap(other);return *this; }
void swap(HeavyLightDecomposition& other) noexcept {
parent_.swap(other.parent_);depth_.swap(other.depth_);component_.swap(other.component_);
roots_.swap(other.roots_);index_.swap(other.index_);order_.swap(other.order_);
subtree_.swap(other.subtree_);head_.swap(other.head_);std::swap(root_,other.root_);
}
void clear() noexcept { HeavyLightDecomposition{}.swap(*this); }
void build(const Graph& graph,int root=0) { HeavyLightDecomposition(graph,root).swap(*this); }
int size() const noexcept { return static_cast<int>(order_.size()); }
bool empty() const noexcept { return order_.empty(); }
int root() const noexcept { return root_; }
int component_count() const noexcept { return static_cast<int>(roots_.size()); }
int parent(int v) const { check_vertex(v);return parent_[v]; }
int depth(int v) const { check_vertex(v);return depth_[v]; }
int component(int v) const { check_vertex(v);return component_[v]; }
int root_of(int v) const { check_vertex(v);return roots_[component_[v]]; }
int index(int v) const { check_vertex(v);return index_[v]; }
int vertex_at(int i) const { check_vertex(i);return order_[i]; }
const std::vector<int>& order() const noexcept { return order_; }
bool same_component(int u,int v) const { check_vertex(u);check_vertex(v);return roots_.size()==1 || component_[u]==component_[v]; }
bool is_ancestor(int u,int v) const {
check_vertex(u);check_vertex(v);return component_[u]==component_[v] && index_[u]<=index_[v] && index_[v]-index_[u]<subtree_[u];
}
std::pair<int,int> subtree_range(int v,bool edges=false) const {
check_vertex(v);return {index_[v]+static_cast<int>(edges),index_[v]+subtree_[v]};
}
int subtree_size(int v) const { check_vertex(v);return subtree_[v]; }
int lca(int u,int v) const {
if(!same_component(u,v)) return -1;
while(head_[u]!=head_[v]) {
if(depth_[head_[u]]>=depth_[head_[v]]) u=parent_[head_[u]];
else v=parent_[head_[v]];
}
return depth_[u]<depth_[v]?u:v;
}
int distance(int u,int v) const {
const int a=lca(u,v);return a<0?-1:(depth_[u]-depth_[a])+(depth_[v]-depth_[a]);
}
int kth_ancestor(int v,std::int64_t k) const {
check_vertex(v);if(k<0 || k>depth_[v]) return -1;
while(k>depth_[v]-depth_[head_[v]]) {
k-=depth_[v]-depth_[head_[v]]+1;v=parent_[head_[v]];
}
return order_[index_[v]-static_cast<int>(k)];
}
int jump(int u,int v,std::int64_t k) const {
check_vertex(u);check_vertex(v);
if(k<0) return -1;
if(head_[u]==head_[v]) {
const int delta=index_[v]-index_[u];
if(k>(delta<0?-delta:delta)) return -1;
return order_[delta<0?index_[u]-static_cast<int>(k):index_[u]+static_cast<int>(k)];
}
if(roots_.size()!=1 && component_[u]!=component_[v]) return -1;
if(k>static_cast<std::int64_t>(depth_[u])+depth_[v]) return -1;
if(k==0) return u;
const int a=lca(u,v);
const int before=depth_[u]-depth_[a],after=depth_[v]-depth_[a];
if(k>static_cast<std::int64_t>(before)+after) return -1;
return k<=before?kth_ancestor(u,k):kth_ancestor(v,static_cast<std::int64_t>(before)+after-k);
}
template<class Fn> bool for_each_path(int u,int v,Fn&& fn,bool edges=false) const {
if(!same_component(u,v)) return false;
// 軽辺を一つ上がるごとに部分木が倍以上になるため、intの桁数で足りる。
std::array<Interval,std::numeric_limits<unsigned>::digits> tail;
std::size_t count=0;
while(head_[u]!=head_[v]) {
if(index_[u]>=index_[v]) {
std::invoke(fn,index_[head_[u]],index_[u]+1,true);u=parent_[head_[u]];
} else {
tail[count++]={index_[head_[v]],index_[v]+1,false};v=parent_[head_[v]];
}
}
if(index_[u]>=index_[v]) {
const int left=index_[v]+static_cast<int>(edges),right=index_[u]+1;
if(left<right) std::invoke(fn,left,right,true);
} else {
const int left=index_[u]+static_cast<int>(edges),right=index_[v]+1;
std::invoke(fn,left,right,false);
}
while(count) { const auto p=tail[--count];std::invoke(fn,p.left,p.right,p.reversed); }
return true;
}
std::vector<Interval> path(int u,int v,bool edges=false) const {
std::vector<Interval> result;
for_each_path(u,v,[&](int l,int r,bool rev){result.push_back({l,r,rev});},edges);return result;
}
template<class T> std::vector<T> reorder(const std::vector<T>& values) const {
check_size(values.size());std::vector<T> result;result.reserve(values.size());
for(int v:order_) result.push_back(values[v]);
return result;
}
template<class T> std::vector<T> restore(const std::vector<T>& values) const {
check_size(values.size());std::vector<T> result;result.reserve(values.size());
for(int i:index_) result.push_back(values[i]);
return result;
}
template<class Identity,class Q,class Op> requires /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::HldProduct<Identity,Q,Op>
auto path_prod(int u,int v,Identity&& identity,Q&& query,Op&& op,bool edges=false) const {
if constexpr(/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::hld_directional_query<Q>) {
return path_prod(u,v,std::forward<Identity>(identity),std::forward<Q>(query),std::forward<Op>(op),std::identity{},edges);
} else {
using Value=/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::HldQueryValue<Q>;
if(!same_component(u,v)) {
#ifndef SKIP_RUNTIME_CHECKS
throw std::invalid_argument("HLD: disconnected path");
#else
return Value(std::forward<Identity>(identity));
#endif
}
Value result(std::forward<Identity>(identity));
// 可換積では通行順の保存を省き、両端から直接結合する。
while(head_[u]!=head_[v]) {
if(index_[u]<index_[v]) std::swap(u,v);
result=std::invoke(op,std::move(result),/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::hld_query(query,index_[head_[u]],index_[u]+1));
u=parent_[head_[u]];
}
if(index_[u]>index_[v]) std::swap(u,v);
const int left=index_[u]+static_cast<int>(edges),right=index_[v]+1;
if(left<right) result=std::invoke(op,std::move(result),/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::hld_query(query,left,right));
return result;
}
}
template<class Identity,class Q,class Op,class Reverse> requires /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::HldReversedProduct<Identity,Q,Op,Reverse>
auto path_prod(int u,int v,Identity&& identity,Q&& query,Op&& op,Reverse&& reverse,bool edges=false) const {
using Value=/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::HldQueryValue<Q>;
if(!same_component(u,v)) {
#ifndef SKIP_RUNTIME_CHECKS
throw std::invalid_argument("HLD: disconnected path");
#else
return Value(std::forward<Identity>(identity));
#endif
}
Value empty_result(std::forward<Identity>(identity));
std::optional<Value> from_u,from_v;
const auto read=[&](int l,int r,bool rev) -> Value {
Value part=/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::hld_query(query,l,r,rev);
if constexpr(!/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::hld_directional_query<Q>) if(rev) part=std::invoke(reverse,std::move(part));
return part;
};
const auto append=[&](Value part) {
if(from_u) *from_u=std::invoke(op,std::move(*from_u),std::move(part));
else from_u.emplace(std::move(part));
};
const auto prepend=[&](Value part) {
if(from_v) *from_v=std::invoke(op,std::move(part),std::move(*from_v));
else from_v.emplace(std::move(part));
};
while(head_[u]!=head_[v]) {
if(index_[u]>=index_[v]) {
append(read(index_[head_[u]],index_[u]+1,true));u=parent_[head_[u]];
} else {
prepend(read(index_[head_[v]],index_[v]+1,false));v=parent_[head_[v]];
}
}
if(index_[u]>=index_[v]) {
const int left=index_[v]+static_cast<int>(edges),right=index_[u]+1;
if(left<right) append(read(left,right,true));
} else {
const int left=index_[u]+static_cast<int>(edges),right=index_[v]+1;
append(read(left,right,false));
}
if(from_u && from_v) return Value(std::invoke(op,std::move(*from_u),std::move(*from_v)));
if(from_u) return std::move(*from_u);
if(from_v) return std::move(*from_v);
return empty_result;
}
template<class Range> requires (requires(Range& r){r.sum(0,0);} && !requires(Range& r){r.prod(0,0);})
auto path_prod(int u,int v,Range& range,bool edges=false) const {
using Value=std::remove_cvref_t<decltype(range.sum(0,0))>;
return path_prod(u,v,Value{},range,[](const Value& a,const Value& b){return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::HldArithmetic<Value>::add(a,b);},edges);
}
template<template<class,auto,auto> class R,class S,auto Op,auto E>
S path_prod(int u,int v,const R<S,Op,E>& range,bool edges=false) const {
return path_prod(u,v,std::invoke(E),range,Op,edges);
}
template<template<class,auto,auto> class R,class S,auto Op,auto E,class Reverse>
requires std::is_invocable_r_v<S,Reverse&,S>
S path_prod(int u,int v,const R<S,Op,E>& range,Reverse&& reverse,bool edges=false) const {
return path_prod(u,v,std::invoke(E),range,Op,std::forward<Reverse>(reverse),edges);
}
template<template<class,auto,auto,class,auto,auto,auto> class R,class S,auto Op,auto E,class F,auto M,auto C,auto I>
S path_prod(int u,int v,R<S,Op,E,F,M,C,I>& range,bool edges=false) const {
return path_prod(u,v,std::invoke(E),range,Op,edges);
}
template<template<class,auto,auto,class,auto,auto,auto> class R,class S,auto Op,auto E,class F,auto M,auto C,auto I,class Reverse>
requires std::is_invocable_r_v<S,Reverse&,S>
S path_prod(int u,int v,R<S,Op,E,F,M,C,I>& range,Reverse&& reverse,bool edges=false) const {
return path_prod(u,v,std::invoke(E),range,Op,std::forward<Reverse>(reverse),edges);
}
template<class Q> requires /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::HldQuery<Q>
auto subtree_prod(int v,Q&& query,bool edges=false) const {
auto[l,r]=subtree_range(v,edges);return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::hld_query(query,l,r);
}
template<class Range,class F> void path_apply(int u,int v,Range& range,const F f,bool edges=false) const {
#ifndef SKIP_RUNTIME_CHECKS
if(!same_component(u,v)) throw std::invalid_argument("HLD: disconnected path");
#endif
for_each_path(u,v,[&](int l,int r,bool){range.apply(l,r,f);},edges);
}
template<class Range,class F> void subtree_apply(int v,Range& range,const F f,bool edges=false) const {
auto[l,r]=subtree_range(v,edges);if(l<r) range.apply(l,r,f);
}
template<class Range,bool Ordered=false,class T>
HeavyLightValues<Range,Ordered> with_values(const std::vector<T>& values) const &;
template<class Range,bool Ordered=false,class T>
HeavyLightValues<Range,Ordered> with_values(const std::vector<T>& values) &&;
};
#endif
#ifndef SIRKIT_HLD_OWNED_RANGE_TRAITS_DEFINED
#define SIRKIT_HLD_OWNED_RANGE_TRAITS_DEFINED
namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ {
template<class Range> struct HldOwnedRangeTraits {
using Value=HldQueryValue<Range>;
static Value identity() {return Value{};}
static Value combine(const Value& a,const Value& b) {return HldArithmetic<Value>::add(a,b);}
};
template<template<class,auto,auto> class R,class S,auto Op,auto E>
struct HldOwnedRangeTraits<R<S,Op,E>> {
using Value=S;
static Value identity(){return std::invoke(E);}
static Value combine(const Value& a,const Value& b){return std::invoke(Op,a,b);}
};
template<template<class,auto,auto,class,auto,auto,auto> class R,class S,auto Op,auto E,class F,auto M,auto C,auto I>
struct HldOwnedRangeTraits<R<S,Op,E,F,M,C,I>> {
using Value=S;
static Value identity(){return std::invoke(E);}
static Value combine(const Value& a,const Value& b){return std::invoke(Op,a,b);}
};
}
#endif
#ifndef SIRKIT_HLD_SEGMENT_ACCESS_DEFINED
#define SIRKIT_HLD_SEGMENT_ACCESS_DEFINED
namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ {
template<class Range> struct HldSegmentAccess {
static constexpr bool enabled=[] {
if constexpr(requires {typename Range::HldSegmentTag;}) {
using T=typename HldOwnedRangeTraits<Range>::Value;
return sizeof(T)==16 && std::is_trivially_copyable_v<T>;
} else return false;
}();
};
}
#endif
#ifndef SIRKIT_HEAVY_LIGHT_VALUES_DEFINED
#define SIRKIT_HEAVY_LIGHT_VALUES_DEFINED
template<class Range,bool Ordered> class HeavyLightValues {
using Traits=/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::HldOwnedRangeTraits<Range>;
public:
using value_type=typename Traits::Value;
private:
struct State {
HeavyLightDecomposition layout;
Range forward;
struct NoReverse {};
[[no_unique_address]] std::conditional_t<Ordered,std::optional<Range>,NoReverse> backward;
static Range make_range(const std::vector<value_type>& values) {
if constexpr(std::constructible_from<Range,const std::vector<value_type>&>) return Range(values);
else {
Range range(static_cast<int>(values.size()));
for(int i=0;i<static_cast<int>(values.size());++i) range.add(i,values[i]);
return range;
}
}
template<class Layout> State(Layout&& source,const std::vector<value_type>& ordered)
:layout(std::forward<Layout>(source)),forward(make_range(ordered)) {
if constexpr(Ordered) {
auto reversed=ordered;std::reverse(reversed.begin(),reversed.end());backward.emplace(make_range(reversed));
}
}
};
std::unique_ptr<State> state_;
static const HeavyLightDecomposition& empty_layout() {static const HeavyLightDecomposition h;return h;}
const HeavyLightDecomposition& layout() const noexcept {return state_?state_->layout:empty_layout();}
static value_type range_get(Range& range,int p) {
if constexpr(requires {range.get(p);}) return range.get(p);
else return range.sum(p,p+1);
}
static void range_set(Range& range,int p,const value_type& x) {
if constexpr(requires {range.set(p,x);}) range.set(p,x);
else range.add(p,/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::HldArithmetic<value_type>::subtract(x,range_get(range,p)));
}
void check_vertex(int v) const {(void)layout().index(v);}
static value_type range_query(Range& range,int l,int r) {
// 所有BITの一点区間は、二つの累積和を求めず直接読む。
if constexpr(requires {range.sum(l,r);range.get(l);} && !requires {range.prod(l,r);})
if(r-l==1) return range.get(l);
return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::hld_query(range,l,r);
}
value_type query(int l,int r,bool reverse) {
if constexpr(Ordered) if(reverse) return range_query(*state_->backward,size()-r,size()-l);
return range_query(state_->forward,l,r);
}
public:
HeavyLightValues()=default;
HeavyLightValues(const Graph& graph,const std::vector<value_type>& values,int root=0)
:HeavyLightValues(HeavyLightDecomposition(graph,root),values) {}
HeavyLightValues(const HeavyLightDecomposition& source,const std::vector<value_type>& values) {
auto ordered=source.reorder(values);state_=std::make_unique<State>(source,ordered);
}
HeavyLightValues(HeavyLightDecomposition&& source,const std::vector<value_type>& values) {
// valuesがsource.order()を参照する場合も、移動前に並べ替える。
auto ordered=source.reorder(values);state_=std::make_unique<State>(std::move(source),ordered);
}
HeavyLightValues(const HeavyLightValues& other) {
if(other.state_) state_=std::make_unique<State>(*other.state_);
}
HeavyLightValues(HeavyLightValues&&)=default;
HeavyLightValues& operator=(HeavyLightValues other) noexcept {swap(other);return *this;}
void swap(HeavyLightValues& other) noexcept {state_.swap(other.state_);}
void clear() noexcept {state_.reset();}
void build(const Graph& graph,const std::vector<value_type>& values,int root=0) {HeavyLightValues(graph,values,root).swap(*this);}
int size() const noexcept {return layout().size();}
bool empty() const noexcept {return layout().empty();}
int root() const noexcept {return layout().root();}
int component_count() const noexcept {return layout().component_count();}
int parent(int v) const {return layout().parent(v);}
int depth(int v) const {return layout().depth(v);}
int component(int v) const {return layout().component(v);}
int root_of(int v) const {return layout().root_of(v);}
int index(int v) const {return layout().index(v);}
int vertex_at(int i) const {return layout().vertex_at(i);}
const std::vector<int>& order() const noexcept {return layout().order();}
bool same_component(int u,int v) const {return layout().same_component(u,v);}
bool is_ancestor(int u,int v) const {return layout().is_ancestor(u,v);}
std::pair<int,int> subtree_range(int v,bool edges=false) const {return layout().subtree_range(v,edges);}
int subtree_size(int v) const {return layout().subtree_size(v);}
int lca(int u,int v) const {return layout().lca(u,v);}
int distance(int u,int v) const {return layout().distance(u,v);}
int kth_ancestor(int v,std::int64_t k) const {return layout().kth_ancestor(v,k);}
int jump(int u,int v,std::int64_t k) const {return layout().jump(u,v,k);}
template<class Fn> bool for_each_path(int u,int v,Fn&& fn,bool edges=false) const {return layout().for_each_path(u,v,std::forward<Fn>(fn),edges);}
std::vector<HeavyLightDecomposition::Interval> path(int u,int v,bool edges=false) const {return layout().path(u,v,edges);}
template<class T> std::vector<T> reorder(const std::vector<T>& values) const {return layout().reorder(values);}
template<class T> std::vector<T> restore(const std::vector<T>& values) const {return layout().restore(values);}
value_type get(int v) requires (requires(Range& r){r.get(0);} || requires(Range& r){r.sum(0,0);}) {
const int p=index(v);return range_get(state_->forward,p);
}
void set(int v,const value_type x) requires (requires(Range& r){r.set(0,x);} || requires(Range& r){r.add(0,x-x);}) {
const int p=index(v);range_set(state_->forward,p,x);
if constexpr(Ordered) range_set(*state_->backward,size()-1-p,x);
}
void add(int v,const value_type x) requires requires(Range& r){r.add(0,x);} {
const int p=index(v);state_->forward.add(p,x);
if constexpr(Ordered) state_->backward->add(size()-1-p,x);
}
template<class F> void apply(int v,const F f) requires requires(Range& r){r.apply(0,f);} {
const int p=index(v);state_->forward.apply(p,f);
if constexpr(Ordered) state_->backward->apply(size()-1-p,f);
}
value_type path_prod(int u,int v,bool edges=false) {
if constexpr(Ordered) {
auto interval=[&](int l,int r,bool reverse){return query(l,r,reverse);};
return layout().path_prod(u,v,Traits::identity(),interval,Traits::combine,edges);
} else {
auto interval=[&](int l,int r){return query(l,r,false);};
return layout().path_prod(u,v,Traits::identity(),interval,Traits::combine,edges);
}
}
value_type subtree_prod(int v,bool edges=false) {
auto[l,r]=subtree_range(v,edges);return query(l,r,false);
}
template<class F> void path_apply(int u,int v,const F f,bool edges=false) requires requires(Range& r){r.apply(0,0,f);} {
#ifndef SKIP_RUNTIME_CHECKS
if(!same_component(u,v)) throw std::invalid_argument("HLD: disconnected path");
#endif
for_each_path(u,v,[&](int l,int r,bool) {
state_->forward.apply(l,r,f);
if constexpr(Ordered) state_->backward->apply(size()-r,size()-l,f);
},edges);
}
template<class F> void subtree_apply(int v,const F f,bool edges=false) requires requires(Range& r){r.apply(0,0,f);} {
auto[l,r]=subtree_range(v,edges);if(l==r) return;
state_->forward.apply(l,r,f);
if constexpr(Ordered) state_->backward->apply(size()-r,size()-l,f);
}
std::vector<value_type> values() {
if(!state_) return {};
if constexpr(requires {state_->forward.values();}) return restore(state_->forward.values());
else {
std::vector<value_type> ordered;ordered.reserve(static_cast<std::size_t>(size()));
for(int i=0;i<size();++i) ordered.push_back(range_get(state_->forward,i));
return restore(ordered);
}
}
};
template<class Range,bool Ordered,class T>
HeavyLightValues<Range,Ordered> HeavyLightDecomposition::with_values(const std::vector<T>& values) const & {return HeavyLightValues<Range,Ordered>(*this,values);}
template<class Range,bool Ordered,class T>
HeavyLightValues<Range,Ordered> HeavyLightDecomposition::with_values(const std::vector<T>& values) && {return HeavyLightValues<Range,Ordered>(std::move(*this),values);}
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1