競技プログラミング用の C++ ライブラリ
#include "src/data_structures/link-cut-tree.hpp"森 (頂点に値を持つ) に対して、辺の追加・削除、パス上の値の集約、頂点の値の更新をならし $O(\log n)$ で行います。モノイド (algebra/monoid.hpp) を渡す LinkCutTree<M> と、可換な群 (algebra/group.hpp) を渡して部分木の集約もできる SubtreeLinkCutTree<G> があります。
LinkCutTree<monoid::Sum<long long>> lct(a); // 頂点 i の値が a[i]、辺なし
lct.link(0, 1);
lct.link(1, 2);
long long s = lct.prod(0, 2); // a[0] + a[1] + a[2]
lct.cut(0, 1);
SubtreeLinkCutTree<group::Sum<long long>> st(a);
st.link(0, 1);
long long t = st.subtree_prod(1, 0); // 0 を親としたときの 1 の部分木の和
LinkCutTree のモノイドは可換でなくてもかまいません。prod(u, v) は u の側から順に op でたたみます。たとえば monoid::Composite なら $f_u \circ \cdots \circ f_v$ になるので、u の側から先に適用したいときは monoid::Reverse<monoid::Composite<T>> を使います。
SubtreeLinkCutTree は、パスに含まれない子の部分木の値を足し引きして持つので、逆元のある可換な群が必要です。
木の根は操作によって変わります。evert(v) で v を根にでき、prod(u, v) は u を、subtree_prod(v, p) は p を根にします。lca(u, v) はその時点の根についての最小共通祖先です。
サイズを取得でき、各要素から S を構築できる範囲を直接渡せます。std::vector・std::array・std::list・組み込み配列などに対応します。C++20 ではサイズ付きの入力範囲 (input_range かつ sized_range) のビューも使えます。各要素を内部へ直接構築するので、explicit なコンストラクタにも対応します。初期化リストも使えます。
LinkCutTree と SubtreeLinkCutTree に共通です (subtree_prod は SubtreeLinkCutTree だけ)。
| 関数 | 説明 | 計算量 |
|---|---|---|
LinkCutTree(n, m = M()) |
頂点数 n、辺なし、値はすべて単位元 |
$O(n)$ |
LinkCutTree(a, m = M()) |
頂点 i の値が a[i]、辺なし |
$O(n)$ |
size() |
頂点数 | $O(1)$ |
link(u, v) |
辺 (u, v) を追加する。u と v は別の木にあること |
ならし $O(\log n)$ |
cut(u, v) |
辺 (u, v) を削除する。辺 (u, v) があること |
ならし $O(\log n)$ |
connected(u, v) |
u と v が同じ木にあるか |
ならし $O(\log n)$ |
evert(v) |
v を根にする |
ならし $O(\log n)$ |
root(v) |
v を含む木の今の根 |
ならし $O(\log n)$ |
lca(u, v) |
今の根についての u と v の最小共通祖先。同じ木にあること |
ならし $O(\log n)$ |
get(v) |
頂点 v の値 |
$O(1)$ |
set(v, x) |
頂点 v の値を x にする |
ならし $O(\log n)$ |
apply(v, x) |
頂点 v の値を op(get(v), x) にする |
ならし $O(\log n)$ |
prod(u, v) |
u から v へのパス上の頂点 (端点を含む) の値を u の側から順にたたんだもの。同じ木にあること。u が根になる |
ならし $O(\log n)$ |
subtree_prod(v, p) |
p を根としたときの v の部分木の値の和 (p == v なら木全体)。同じ木にあること。p が根になる |
ならし $O(\log n)$ |
#pragma once
/**
* @file link-cut-tree.hpp
* @brief Link-Cut Tree
*/
#include <cassert>
#include <initializer_list>
#include <type_traits>
#include <utility>
#include <vector>
#include "../utility/traits.hpp"
/**
* @brief Link-Cut Tree (森で、辺の追加・削除とパス上の値の集約ができる)
*
* 各操作はならし O(log n)。内部の配列は int で持つ
* 例:
* LinkCutTree<monoid::Sum<long long>> lct(a);
* lct.link(0, 1);
* long long s = lct.prod(0, 1);
*
* @tparam M モノイド (algebra/monoid.hpp)。可換でなくてもよい (prod は u から v の向きにたたむ)
*/
template <typename M>
class LinkCutTree {
using S = typename M::S;
struct Node {
int l = -1, r = -1, p = -1;
bool rev = false;
S val, sum, rsum; // sum は左から右、rsum は右から左にたたんだもの
};
M m;
S op(const S& a, const S& b) const { return m.op(a, b); }
S e() const { return m.e(); }
std::vector<Node> t;
std::vector<int> stk;
bool is_root(int x) const {
int p = t[x].p;
return p == -1 || (t[p].l != x && t[p].r != x);
}
S sum(int x) const { return x == -1 ? e() : t[x].sum; }
S rsum(int x) const { return x == -1 ? e() : t[x].rsum; }
void update(int x) {
t[x].sum = op(op(sum(t[x].l), t[x].val), sum(t[x].r));
t[x].rsum = op(op(rsum(t[x].r), t[x].val), rsum(t[x].l));
}
void toggle(int x) {
if (x == -1) return;
std::swap(t[x].l, t[x].r);
std::swap(t[x].sum, t[x].rsum);
t[x].rev = !t[x].rev;
}
void push(int x) {
if (!t[x].rev) return;
toggle(t[x].l);
toggle(t[x].r);
t[x].rev = false;
}
void rotate(int x) {
int p = t[x].p, g = t[p].p;
if (t[p].l == x) {
t[p].l = t[x].r;
if (t[x].r != -1) t[t[x].r].p = p;
t[x].r = p;
} else {
t[p].r = t[x].l;
if (t[x].l != -1) t[t[x].l].p = p;
t[x].l = p;
}
t[p].p = x, t[x].p = g;
if (g != -1) {
if (t[g].l == p) t[g].l = x;
if (t[g].r == p) t[g].r = x;
}
update(p), update(x);
}
void splay(int x) {
for (int y = x;; y = t[y].p) {
stk.push_back(y);
if (is_root(y)) break;
}
while (!stk.empty()) push(stk.back()), stk.pop_back();
while (!is_root(x)) {
int p = t[x].p;
if (!is_root(p)) rotate((t[t[p].p].l == p) == (t[p].l == x) ? p : x);
rotate(x);
}
}
// x から根までのパスを 1 本の splay 木にし、x をその根にする。最後に splay した頂点を返す
int access(int x) {
int last = -1;
for (int y = x; y != -1; y = t[y].p) {
splay(y);
t[y].r = last;
update(y);
last = y;
}
splay(x);
return last;
}
public:
/**
* @brief 頂点数 n、辺なし、値はすべて単位元
*
* @param m モノイド
*/
explicit LinkCutTree(long long n = 0, M m = M()) : m(std::move(m)) {
Node x;
x.val = x.sum = x.rsum = e();
t.assign(n, x);
}
/**
* @brief 頂点 i の値が a[i]、辺なし
*/
#if __cplusplus >= 202002L
template <class R = std::initializer_list<S>>
requires internal_traits::sized_constructible_range<R, S>
#else
template <class R = std::initializer_list<S>, ENABLE_IF_T(internal_traits::is_sized_constructible_range<R, S>::value)>
#endif
explicit LinkCutTree(R&& a, M m = M()) : LinkCutTree(internal_traits::range_size(a), std::move(m)) {
long long i = 0;
for (auto&& x : a) {
t[i].val = t[i].sum = t[i].rsum = S(std::forward<decltype(x)>(x));
++i;
}
}
/**
* @brief 頂点数
*/
long long size() const { return static_cast<long long>(t.size()); }
/**
* @brief v を根にする
*/
void evert(long long v) {
access(v);
toggle(v);
push(v);
}
/**
* @brief 辺 (u, v) を追加する。u と v は別の木にあること
*/
void link(long long u, long long v) {
assert(!connected(u, v));
evert(u);
t[u].p = static_cast<int>(v);
}
/**
* @brief 辺 (u, v) を削除する。辺 (u, v) があること
*/
void cut(long long u, long long v) {
evert(u);
access(v);
assert(t[v].l == u && t[u].r == -1);
t[v].l = t[u].p = -1;
update(v);
}
/**
* @brief v を含む木の根
*/
long long root(long long v) {
access(v);
int x = static_cast<int>(v);
while (push(x), t[x].l != -1) x = t[x].l;
splay(x);
return x;
}
/**
* @brief u と v が同じ木にあるか
*/
bool connected(long long u, long long v) { return u == v || root(u) == root(v); }
/**
* @brief u と v の最小共通祖先 (根は最後に evert した頂点など、今の根)。u と v は同じ木にあること
*/
long long lca(long long u, long long v) {
assert(connected(u, v));
access(u);
return access(v);
}
/**
* @brief 頂点 v の値を x にする
*/
void set(long long v, const S& x) {
access(v);
t[v].val = x;
update(v);
}
/**
* @brief 頂点 v の値
*/
S get(long long v) const { return t[v].val; }
/**
* @brief 頂点 v の値を op(get(v), x) にする
*/
void apply(long long v, const S& x) { set(v, op(t[v].val, x)); }
/**
* @brief u から v へのパス上の頂点 (端点を含む) の値を、u の側から順にたたんだもの。u と v は同じ木にあること
*
* u が根になる
*/
S prod(long long u, long long v) {
assert(connected(u, v));
evert(u);
access(v);
return t[v].sum;
}
};
/**
* @brief 部分木の値の集約もできる Link-Cut Tree
*
* パスに含まれない子の部分木の値 (virtual) を持つ。各操作はならし O(log n)
* 例:
* SubtreeLinkCutTree<group::Sum<long long>> lct(a);
* lct.link(0, 1);
* long long s = lct.subtree_prod(1, 0); // 0 を親としたときの 1 の部分木の和
*
* @tparam G 可換な群 (algebra/group.hpp)
*/
template <typename M>
LinkCutTree(long long, M) -> LinkCutTree<M>;
template <class R, typename M>
LinkCutTree(R&&, M) -> LinkCutTree<M>;
template <typename M>
LinkCutTree(std::initializer_list<typename M::S>, M) -> LinkCutTree<M>;
template <typename G>
class SubtreeLinkCutTree {
using S = typename G::S;
struct Node {
int l = -1, r = -1, p = -1;
bool rev = false;
S val, path, virt, sum; // path はパス上、sum は virtual も含めた和
};
G g;
S op(const S& a, const S& b) const { return g.op(a, b); }
S e() const { return g.e(); }
S inv(const S& a) const { return g.inv(a); }
std::vector<Node> t;
std::vector<int> stk;
bool is_root(int x) const {
int p = t[x].p;
return p == -1 || (t[p].l != x && t[p].r != x);
}
S path(int x) const { return x == -1 ? e() : t[x].path; }
S sum(int x) const { return x == -1 ? e() : t[x].sum; }
void update(int x) {
t[x].path = op(op(path(t[x].l), t[x].val), path(t[x].r));
t[x].sum = op(op(sum(t[x].l), op(t[x].val, t[x].virt)), sum(t[x].r));
}
void toggle(int x) {
if (x == -1) return;
std::swap(t[x].l, t[x].r);
t[x].rev = !t[x].rev;
}
void push(int x) {
if (!t[x].rev) return;
toggle(t[x].l);
toggle(t[x].r);
t[x].rev = false;
}
void rotate(int x) {
int p = t[x].p, g = t[p].p;
if (t[p].l == x) {
t[p].l = t[x].r;
if (t[x].r != -1) t[t[x].r].p = p;
t[x].r = p;
} else {
t[p].r = t[x].l;
if (t[x].l != -1) t[t[x].l].p = p;
t[x].l = p;
}
t[p].p = x, t[x].p = g;
if (g != -1) {
if (t[g].l == p) t[g].l = x;
if (t[g].r == p) t[g].r = x;
}
update(p), update(x);
}
void splay(int x) {
for (int y = x;; y = t[y].p) {
stk.push_back(y);
if (is_root(y)) break;
}
while (!stk.empty()) push(stk.back()), stk.pop_back();
while (!is_root(x)) {
int p = t[x].p;
if (!is_root(p)) rotate((t[t[p].p].l == p) == (t[p].l == x) ? p : x);
rotate(x);
}
}
int access(int x) {
int last = -1;
for (int y = x; y != -1; y = t[y].p) {
splay(y);
// 今の右の子は virtual になり、last は virtual でなくなる
t[y].virt = op(op(t[y].virt, sum(t[y].r)), inv(sum(last)));
t[y].r = last;
update(y);
last = y;
}
splay(x);
return last;
}
public:
explicit SubtreeLinkCutTree(long long n = 0, G g = G()) : g(std::move(g)) {
Node x;
x.val = x.path = x.virt = x.sum = e();
t.assign(n, x);
}
#if __cplusplus >= 202002L
template <class R = std::initializer_list<S>>
requires internal_traits::sized_constructible_range<R, S>
#else
template <class R = std::initializer_list<S>, ENABLE_IF_T(internal_traits::is_sized_constructible_range<R, S>::value)>
#endif
explicit SubtreeLinkCutTree(R&& a, G g = G()) : SubtreeLinkCutTree(internal_traits::range_size(a), std::move(g)) {
long long i = 0;
for (auto&& x : a) {
t[i].val = t[i].path = t[i].sum = S(std::forward<decltype(x)>(x));
++i;
}
}
long long size() const { return static_cast<long long>(t.size()); }
void evert(long long v) {
access(v);
toggle(v);
push(v);
}
void link(long long u, long long v) {
assert(!connected(u, v));
evert(u);
access(v);
t[u].p = static_cast<int>(v);
t[v].virt = op(t[v].virt, t[u].sum);
update(v);
}
void cut(long long u, long long v) {
evert(u);
access(v);
assert(t[v].l == u && t[u].r == -1);
t[v].l = t[u].p = -1;
update(v);
}
long long root(long long v) {
access(v);
int x = static_cast<int>(v);
while (push(x), t[x].l != -1) x = t[x].l;
splay(x);
return x;
}
bool connected(long long u, long long v) { return u == v || root(u) == root(v); }
long long lca(long long u, long long v) {
assert(connected(u, v));
access(u);
return access(v);
}
void set(long long v, const S& x) {
access(v);
t[v].val = x;
update(v);
}
S get(long long v) const { return t[v].val; }
void apply(long long v, const S& x) { set(v, op(t[v].val, x)); }
/**
* @brief u から v へのパス上の頂点 (端点を含む) の値の和。u と v は同じ木にあること
*/
S prod(long long u, long long v) {
assert(connected(u, v));
evert(u);
access(v);
return t[v].path;
}
/**
* @brief p を根としたときの v の部分木の値の和 (p == v なら木全体)。v と p は同じ木にあること
*
* p が根になる
*/
S subtree_prod(long long v, long long p) {
assert(connected(v, p));
evert(p);
access(v);
return op(t[v].val, t[v].virt);
}
};
template <typename G>
SubtreeLinkCutTree(long long, G) -> SubtreeLinkCutTree<G>;
template <class R, typename G>
SubtreeLinkCutTree(R&&, G) -> SubtreeLinkCutTree<G>;
template <typename G>
SubtreeLinkCutTree(std::initializer_list<typename G::S>, G) -> SubtreeLinkCutTree<G>;#line 2 "src/data_structures/link-cut-tree.hpp"
/**
* @file link-cut-tree.hpp
* @brief Link-Cut Tree
*/
#include <cassert>
#include <initializer_list>
#include <type_traits>
#include <utility>
#include <vector>
#line 2 "src/utility/traits.hpp"
/**
* @file traits.hpp
* @brief 型特性
*/
#include <cstddef>
#include <functional>
#include <iterator>
#include <set>
#line 14 "src/utility/traits.hpp"
#if __cplusplus >= 202002L
#include <concepts>
#include <ranges>
#endif
namespace internal_traits {
using std::begin, std::end;
#if __cplusplus >= 202002L
template <class T>
struct is_range_impl : std::bool_constant < requires(T& t) {
begin(t);
end(t);
}>{};
#else
template <class T, class = void>
struct is_range_impl : std::false_type {};
template <class T>
struct is_range_impl<T, std::void_t<decltype(begin(std::declval<T&>()), end(std::declval<T&>()))>> : std::true_type {};
#endif
// サイズ付き範囲から要素を直接構築するコンストラクタで共有する。
#if __cplusplus >= 202002L
template <class R, class S>
concept sized_constructible_range = std::ranges::input_range<R> && std::ranges::sized_range<R> &&
std::is_constructible_v<S, std::ranges::range_reference_t<R>>;
template <class R>
auto range_size(R& r) {
return std::ranges::size(r);
}
template <class R>
auto range_begin(R& r) {
return std::ranges::begin(r);
}
template <class R>
using range_value_t = std::ranges::range_value_t<R>;
#else
using std::size;
template <class R>
auto range_size(R& r) -> decltype(size(r)) {
return size(r);
}
template <class R>
auto range_begin(R& r) -> decltype(begin(r)) {
return begin(r);
}
template <class R>
using range_value_t = typename std::iterator_traits<decltype(range_begin(std::declval<R&>()))>::value_type;
template <class R, class S, class = void>
struct is_sized_constructible_range : std::false_type {};
template <class R, class S>
struct is_sized_constructible_range<R, S, std::void_t<decltype(range_size(std::declval<R&>())), decltype(++std::declval<decltype(begin(std::declval<R&>()))&>()), decltype(begin(std::declval<R&>()) != end(std::declval<R&>())), decltype(*begin(std::declval<R&>()))>>
: std::is_constructible<S, decltype(*begin(std::declval<R&>()))> {};
#endif
} // namespace internal_traits
template <class T>
struct is_range : internal_traits::is_range_impl<T>::type {};
template <class T>
inline constexpr bool is_range_v = is_range<T>::value;
#if __cplusplus >= 202002L
#define HAS_METHOD(func_name) \
namespace internal_traits { \
template <class T> \
concept has_##func_name##_impl = requires { std::declval<T>().func_name(); }; \
} \
template <class T> \
struct has_##func_name : std::bool_constant<internal_traits::has_##func_name##_impl<T>> {}; \
template <class T> \
inline constexpr bool has_##func_name##_v = has_##func_name<T>::value;
#define HAS_METHOD_ARG(func_name) \
namespace internal_traits { \
template <class T, class U> \
concept has_##func_name##_impl = requires { std::declval<T>().func_name(std::declval<U>()); }; \
} \
template <class T, class U> \
struct has_##func_name : std::bool_constant<internal_traits::has_##func_name##_impl<T, U>> {}; \
template <class T, class U> \
inline constexpr bool has_##func_name##_v = has_##func_name<T, U>::value;
#else
#define HAS_METHOD(func_name) \
namespace internal_traits { \
template <class T, class = void> \
struct has_##func_name##_impl : std::false_type {}; \
template <class T> \
struct has_##func_name##_impl<T, std::void_t<decltype(std::declval<T>().func_name())>> \
: std::true_type {}; \
} \
template <class T> \
struct has_##func_name : internal_traits::has_##func_name##_impl<T>::type {}; \
template <class T> \
inline constexpr bool has_##func_name##_v = has_##func_name<T>::value;
#define HAS_METHOD_ARG(func_name) \
namespace internal_traits { \
template <class T, typename U, class = void> \
struct has_##func_name##_impl : std::false_type {}; \
template <class T, typename U> \
struct has_##func_name##_impl<T, U, std::void_t<decltype(std::declval<T>().func_name(std::declval<U>()))>> \
: std::true_type {}; \
} \
template <class T, typename U> \
struct has_##func_name : internal_traits::has_##func_name##_impl<T, U>::type {}; \
template <class T, typename U> \
inline constexpr bool has_##func_name##_v = has_##func_name<T, U>::value;
#endif
HAS_METHOD(repr)
HAS_METHOD(type_str)
HAS_METHOD(initializer_str)
HAS_METHOD(max)
HAS_METHOD(min)
HAS_METHOD(reversed)
HAS_METHOD(sorted)
HAS_METHOD(sum)
HAS_METHOD(product)
HAS_METHOD(product_xor)
HAS_METHOD_ARG(count)
HAS_METHOD_ARG(find)
HAS_METHOD_ARG(lower_bound)
HAS_METHOD_ARG(upper_bound)
#define ENABLE_IF_T_IMPL(expr) std::enable_if_t<expr, std::nullptr_t> = nullptr
#define ENABLE_IF_T(...) ENABLE_IF_T_IMPL((__VA_ARGS__))
namespace internal_traits {
// 射影 (proj) の既定値
#if __cplusplus >= 202002L
using identity = std::identity;
#else
struct identity {
using is_transparent = void;
template <class T>
constexpr T&& operator()(T&& x) const noexcept {
return std::forward<T>(x);
}
};
#endif
} // namespace internal_traits
template <class C>
using mem_value_type = typename C::value_type;
template <class C>
using mem_difference_type = typename C::difference_type;
namespace internal_traits {
template <class T, class = void>
struct is_sorted_container_impl : std::false_type {};
template <class T>
struct is_sorted_container_impl<std::set<T>> : std::true_type {};
template <class T>
struct is_sorted_container_impl<std::multiset<T>> : std::true_type {};
} // namespace internal_traits
template <class T>
struct is_sorted_container : internal_traits::is_sorted_container_impl<T>::type {};
template <class T>
inline constexpr bool is_sorted_container_v = is_sorted_container<T>::value;
#line 15 "src/data_structures/link-cut-tree.hpp"
/**
* @brief Link-Cut Tree (森で、辺の追加・削除とパス上の値の集約ができる)
*
* 各操作はならし O(log n)。内部の配列は int で持つ
* 例:
* LinkCutTree<monoid::Sum<long long>> lct(a);
* lct.link(0, 1);
* long long s = lct.prod(0, 1);
*
* @tparam M モノイド (algebra/monoid.hpp)。可換でなくてもよい (prod は u から v の向きにたたむ)
*/
template <typename M>
class LinkCutTree {
using S = typename M::S;
struct Node {
int l = -1, r = -1, p = -1;
bool rev = false;
S val, sum, rsum; // sum は左から右、rsum は右から左にたたんだもの
};
M m;
S op(const S& a, const S& b) const { return m.op(a, b); }
S e() const { return m.e(); }
std::vector<Node> t;
std::vector<int> stk;
bool is_root(int x) const {
int p = t[x].p;
return p == -1 || (t[p].l != x && t[p].r != x);
}
S sum(int x) const { return x == -1 ? e() : t[x].sum; }
S rsum(int x) const { return x == -1 ? e() : t[x].rsum; }
void update(int x) {
t[x].sum = op(op(sum(t[x].l), t[x].val), sum(t[x].r));
t[x].rsum = op(op(rsum(t[x].r), t[x].val), rsum(t[x].l));
}
void toggle(int x) {
if (x == -1) return;
std::swap(t[x].l, t[x].r);
std::swap(t[x].sum, t[x].rsum);
t[x].rev = !t[x].rev;
}
void push(int x) {
if (!t[x].rev) return;
toggle(t[x].l);
toggle(t[x].r);
t[x].rev = false;
}
void rotate(int x) {
int p = t[x].p, g = t[p].p;
if (t[p].l == x) {
t[p].l = t[x].r;
if (t[x].r != -1) t[t[x].r].p = p;
t[x].r = p;
} else {
t[p].r = t[x].l;
if (t[x].l != -1) t[t[x].l].p = p;
t[x].l = p;
}
t[p].p = x, t[x].p = g;
if (g != -1) {
if (t[g].l == p) t[g].l = x;
if (t[g].r == p) t[g].r = x;
}
update(p), update(x);
}
void splay(int x) {
for (int y = x;; y = t[y].p) {
stk.push_back(y);
if (is_root(y)) break;
}
while (!stk.empty()) push(stk.back()), stk.pop_back();
while (!is_root(x)) {
int p = t[x].p;
if (!is_root(p)) rotate((t[t[p].p].l == p) == (t[p].l == x) ? p : x);
rotate(x);
}
}
// x から根までのパスを 1 本の splay 木にし、x をその根にする。最後に splay した頂点を返す
int access(int x) {
int last = -1;
for (int y = x; y != -1; y = t[y].p) {
splay(y);
t[y].r = last;
update(y);
last = y;
}
splay(x);
return last;
}
public:
/**
* @brief 頂点数 n、辺なし、値はすべて単位元
*
* @param m モノイド
*/
explicit LinkCutTree(long long n = 0, M m = M()) : m(std::move(m)) {
Node x;
x.val = x.sum = x.rsum = e();
t.assign(n, x);
}
/**
* @brief 頂点 i の値が a[i]、辺なし
*/
#if __cplusplus >= 202002L
template <class R = std::initializer_list<S>>
requires internal_traits::sized_constructible_range<R, S>
#else
template <class R = std::initializer_list<S>, ENABLE_IF_T(internal_traits::is_sized_constructible_range<R, S>::value)>
#endif
explicit LinkCutTree(R&& a, M m = M()) : LinkCutTree(internal_traits::range_size(a), std::move(m)) {
long long i = 0;
for (auto&& x : a) {
t[i].val = t[i].sum = t[i].rsum = S(std::forward<decltype(x)>(x));
++i;
}
}
/**
* @brief 頂点数
*/
long long size() const { return static_cast<long long>(t.size()); }
/**
* @brief v を根にする
*/
void evert(long long v) {
access(v);
toggle(v);
push(v);
}
/**
* @brief 辺 (u, v) を追加する。u と v は別の木にあること
*/
void link(long long u, long long v) {
assert(!connected(u, v));
evert(u);
t[u].p = static_cast<int>(v);
}
/**
* @brief 辺 (u, v) を削除する。辺 (u, v) があること
*/
void cut(long long u, long long v) {
evert(u);
access(v);
assert(t[v].l == u && t[u].r == -1);
t[v].l = t[u].p = -1;
update(v);
}
/**
* @brief v を含む木の根
*/
long long root(long long v) {
access(v);
int x = static_cast<int>(v);
while (push(x), t[x].l != -1) x = t[x].l;
splay(x);
return x;
}
/**
* @brief u と v が同じ木にあるか
*/
bool connected(long long u, long long v) { return u == v || root(u) == root(v); }
/**
* @brief u と v の最小共通祖先 (根は最後に evert した頂点など、今の根)。u と v は同じ木にあること
*/
long long lca(long long u, long long v) {
assert(connected(u, v));
access(u);
return access(v);
}
/**
* @brief 頂点 v の値を x にする
*/
void set(long long v, const S& x) {
access(v);
t[v].val = x;
update(v);
}
/**
* @brief 頂点 v の値
*/
S get(long long v) const { return t[v].val; }
/**
* @brief 頂点 v の値を op(get(v), x) にする
*/
void apply(long long v, const S& x) { set(v, op(t[v].val, x)); }
/**
* @brief u から v へのパス上の頂点 (端点を含む) の値を、u の側から順にたたんだもの。u と v は同じ木にあること
*
* u が根になる
*/
S prod(long long u, long long v) {
assert(connected(u, v));
evert(u);
access(v);
return t[v].sum;
}
};
/**
* @brief 部分木の値の集約もできる Link-Cut Tree
*
* パスに含まれない子の部分木の値 (virtual) を持つ。各操作はならし O(log n)
* 例:
* SubtreeLinkCutTree<group::Sum<long long>> lct(a);
* lct.link(0, 1);
* long long s = lct.subtree_prod(1, 0); // 0 を親としたときの 1 の部分木の和
*
* @tparam G 可換な群 (algebra/group.hpp)
*/
template <typename M>
LinkCutTree(long long, M) -> LinkCutTree<M>;
template <class R, typename M>
LinkCutTree(R&&, M) -> LinkCutTree<M>;
template <typename M>
LinkCutTree(std::initializer_list<typename M::S>, M) -> LinkCutTree<M>;
template <typename G>
class SubtreeLinkCutTree {
using S = typename G::S;
struct Node {
int l = -1, r = -1, p = -1;
bool rev = false;
S val, path, virt, sum; // path はパス上、sum は virtual も含めた和
};
G g;
S op(const S& a, const S& b) const { return g.op(a, b); }
S e() const { return g.e(); }
S inv(const S& a) const { return g.inv(a); }
std::vector<Node> t;
std::vector<int> stk;
bool is_root(int x) const {
int p = t[x].p;
return p == -1 || (t[p].l != x && t[p].r != x);
}
S path(int x) const { return x == -1 ? e() : t[x].path; }
S sum(int x) const { return x == -1 ? e() : t[x].sum; }
void update(int x) {
t[x].path = op(op(path(t[x].l), t[x].val), path(t[x].r));
t[x].sum = op(op(sum(t[x].l), op(t[x].val, t[x].virt)), sum(t[x].r));
}
void toggle(int x) {
if (x == -1) return;
std::swap(t[x].l, t[x].r);
t[x].rev = !t[x].rev;
}
void push(int x) {
if (!t[x].rev) return;
toggle(t[x].l);
toggle(t[x].r);
t[x].rev = false;
}
void rotate(int x) {
int p = t[x].p, g = t[p].p;
if (t[p].l == x) {
t[p].l = t[x].r;
if (t[x].r != -1) t[t[x].r].p = p;
t[x].r = p;
} else {
t[p].r = t[x].l;
if (t[x].l != -1) t[t[x].l].p = p;
t[x].l = p;
}
t[p].p = x, t[x].p = g;
if (g != -1) {
if (t[g].l == p) t[g].l = x;
if (t[g].r == p) t[g].r = x;
}
update(p), update(x);
}
void splay(int x) {
for (int y = x;; y = t[y].p) {
stk.push_back(y);
if (is_root(y)) break;
}
while (!stk.empty()) push(stk.back()), stk.pop_back();
while (!is_root(x)) {
int p = t[x].p;
if (!is_root(p)) rotate((t[t[p].p].l == p) == (t[p].l == x) ? p : x);
rotate(x);
}
}
int access(int x) {
int last = -1;
for (int y = x; y != -1; y = t[y].p) {
splay(y);
// 今の右の子は virtual になり、last は virtual でなくなる
t[y].virt = op(op(t[y].virt, sum(t[y].r)), inv(sum(last)));
t[y].r = last;
update(y);
last = y;
}
splay(x);
return last;
}
public:
explicit SubtreeLinkCutTree(long long n = 0, G g = G()) : g(std::move(g)) {
Node x;
x.val = x.path = x.virt = x.sum = e();
t.assign(n, x);
}
#if __cplusplus >= 202002L
template <class R = std::initializer_list<S>>
requires internal_traits::sized_constructible_range<R, S>
#else
template <class R = std::initializer_list<S>, ENABLE_IF_T(internal_traits::is_sized_constructible_range<R, S>::value)>
#endif
explicit SubtreeLinkCutTree(R&& a, G g = G()) : SubtreeLinkCutTree(internal_traits::range_size(a), std::move(g)) {
long long i = 0;
for (auto&& x : a) {
t[i].val = t[i].path = t[i].sum = S(std::forward<decltype(x)>(x));
++i;
}
}
long long size() const { return static_cast<long long>(t.size()); }
void evert(long long v) {
access(v);
toggle(v);
push(v);
}
void link(long long u, long long v) {
assert(!connected(u, v));
evert(u);
access(v);
t[u].p = static_cast<int>(v);
t[v].virt = op(t[v].virt, t[u].sum);
update(v);
}
void cut(long long u, long long v) {
evert(u);
access(v);
assert(t[v].l == u && t[u].r == -1);
t[v].l = t[u].p = -1;
update(v);
}
long long root(long long v) {
access(v);
int x = static_cast<int>(v);
while (push(x), t[x].l != -1) x = t[x].l;
splay(x);
return x;
}
bool connected(long long u, long long v) { return u == v || root(u) == root(v); }
long long lca(long long u, long long v) {
assert(connected(u, v));
access(u);
return access(v);
}
void set(long long v, const S& x) {
access(v);
t[v].val = x;
update(v);
}
S get(long long v) const { return t[v].val; }
void apply(long long v, const S& x) { set(v, op(t[v].val, x)); }
/**
* @brief u から v へのパス上の頂点 (端点を含む) の値の和。u と v は同じ木にあること
*/
S prod(long long u, long long v) {
assert(connected(u, v));
evert(u);
access(v);
return t[v].path;
}
/**
* @brief p を根としたときの v の部分木の値の和 (p == v なら木全体)。v と p は同じ木にあること
*
* p が根になる
*/
S subtree_prod(long long v, long long p) {
assert(connected(v, p));
evert(p);
access(v);
return op(t[v].val, t[v].virt);
}
};
template <typename G>
SubtreeLinkCutTree(long long, G) -> SubtreeLinkCutTree<G>;
template <class R, typename G>
SubtreeLinkCutTree(R&&, G) -> SubtreeLinkCutTree<G>;
template <typename G>
SubtreeLinkCutTree(std::initializer_list<typename G::S>, G) -> SubtreeLinkCutTree<G>;