結果
問題 | No.399 動的な領主 |
ユーザー | 👑 seekworser |
提出日時 | 2023-04-11 04:54:00 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,157 ms / 2,000 ms |
コード長 | 29,727 bytes |
コンパイル時間 | 4,360 ms |
コンパイル使用メモリ | 275,552 KB |
実行使用メモリ | 69,024 KB |
最終ジャッジ日時 | 2024-10-06 17:12:10 |
合計ジャッジ時間 | 14,651 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
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 | 6 ms
5,248 KB |
testcase_05 | AC | 68 ms
5,248 KB |
testcase_06 | AC | 1,092 ms
14,504 KB |
testcase_07 | AC | 1,072 ms
14,756 KB |
testcase_08 | AC | 1,080 ms
14,504 KB |
testcase_09 | AC | 1,109 ms
14,628 KB |
testcase_10 | AC | 8 ms
5,248 KB |
testcase_11 | AC | 58 ms
5,248 KB |
testcase_12 | AC | 851 ms
14,248 KB |
testcase_13 | AC | 809 ms
14,244 KB |
testcase_14 | AC | 157 ms
69,024 KB |
testcase_15 | AC | 249 ms
68,772 KB |
testcase_16 | AC | 476 ms
41,508 KB |
testcase_17 | AC | 1,157 ms
14,372 KB |
testcase_18 | AC | 1,102 ms
14,504 KB |
ソースコード
// line 1 "answer.cpp" #if !__INCLUDE_LEVEL__ #include __FILE__ int main() { ll n; input(n); vi from(n-1), to(n-1), cost(n-1, 0); rep(i, n-1) input(from[i], to[i]); --from; --to; Graph g(n, from, to, cost, true); lseg_add_radd<ll> seg(n); HeavyLightDecomposition hld(g, seg, false); ll q; input(q); ll ans = 0; rep(q) { ll u,v; input(u,v); --u; --v; hld.apply(u,v,1); ans += hld.prod(u,v).value; } print(ans); } #else // line 2 "/home/seekworser/.cpp_lib/competitive_library/competitive/std/std.hpp" #include <bits/stdc++.h> #ifndef LOCAL_TEST #pragma GCC target ("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #endif // LOCAL_TEST using namespace std; // 型名の短縮 using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>; using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>; using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>; using vs = vector<string>; using vvs = vector<vector<string>>; using vvvs = vector<vector<vector<string>>>; template<typename T> vector<vector<T>> vv(int h, int w, T val = T()) { return vector(h, vector<T>(w, val)); } template<typename T> vector<vector<vector<T>>> vvv(int h1, int h2, int h3, T val = T()) { return vector(h1, vector(h2, vector<T>(h3, val))); } template<typename T> vector<vector<vector<vector<T>>>> vvvv(int h1, int h2, int h3, int h4, T val = T()) { return vector(h1, vector(h2, vector(h3, vector<T>(h4, val)))); } template <class T> using priority_queue_min = priority_queue<T, vector<T>, greater<T>>; // 定数の定義 constexpr double PI = 3.14159265358979323; constexpr int INF = 100100111; constexpr ll INFL = 3300300300300300491LL; float EPS = 1e-8; double EPSL = 1e-16; template<typename T> bool eq(const T x, const T y) { return x == y; } template<> bool eq<double>(const double x, const double y) { return abs(x - y) < EPSL; } template<> bool eq<float>(const float x, const float y) { return abs(x - y) < EPS; } template<typename T> bool neq(const T x, const T y) { return !(eq<T>(x, y)); } template<typename T> bool ge(const T x, const T y) { return (eq<T>(x, y) || (x > y)); } template<typename T> bool le(const T x, const T y) { return (eq<T>(x, y) || (x < y)); } template<typename T> bool gt(const T x, const T y) { return !(le<T>(x, y)); } template<typename T> bool lt(const T x, const T y) { return !(ge<T>(x, y)); } constexpr int MODINT998244353 = 998244353; constexpr int MODINT1000000007 = 1000000007; // 入出力高速化 struct Nyan { Nyan() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } nyan; // 汎用マクロの定義 #define all(a) (a).begin(), (a).end() #define sz(x) ((ll)(x).size()) #define rep1(n) for(ll dummy_iter = 0LL; dummy_iter < n; ++dummy_iter) // 0 から n-1 まで昇順 #define rep2(i, n) for(ll i = 0LL, i##_counter = 0LL; i##_counter < ll(n); ++(i##_counter), (i) = i##_counter) // 0 から n-1 まで昇順 #define rep3(i, s, t) for(ll i = ll(s), i##_counter = ll(s); i##_counter < ll(t); ++(i##_counter), (i) = (i##_counter)) // s から t まで昇順 #define rep4(i, s, t, step) for(ll i##_counter = step > 0 ? ll(s) : -ll(s), i##_end = step > 0 ? ll(t) : -ll(t), i##_step = abs(step), i = ll(s); i##_counter < i##_end; i##_counter += i##_step, i = step > 0 ? i##_counter : -i##_counter) // s から t まで stepずつ #define overload4(a, b, c, d, e, ...) e #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define repe(a, v) for(auto& a : (v)) // v の全要素(変更可能) #define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod #define sdiv(n, m) (((n) - smod(n, m)) / (m)) // 非負div #define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去 void Yes(bool b) { cout << (b ? "Yes\n" : "No\n"); return; }; void YES(bool b) { cout << (b ? "YES\n" : "NO\n"); return; }; template<typename T, size_t N> T max(array<T, N>& a) { return *max_element(all(a)); }; template<typename T, size_t N> T min(array<T, N>& a) { return *min_element(all(a)); }; template<typename T> T max(vector<T>& a) { return *max_element(all(a)); }; template<typename T> T min(vector<T>& a) { return *min_element(all(a)); }; template<typename T> vector<T> vec_slice(const vector<T>& a, int l, int r) { vector<T> rev; rep(i, l, r) rev.push_back(a[i]); return rev; }; template<typename T> T sum(vector<T>& a, T zero = T(0)) { T rev = zero; rep(i, sz(a)) rev += a[i]; return rev; }; template<typename T> bool in_range(const T& val, const T& s, const T& t) { return s <= val && val < t; }; template <class T> inline vector<T>& operator--(vector<T>& v) { repe(x, v) --x; return v; } template <class T> inline vector<T>& operator++(vector<T>& v) { repe(x, v) ++x; return v; } // modでのpow ll powm(ll a, ll n, ll mod=INFL) { ll res = 1; while (n > 0) { if (n & 1) res = (res * a) % mod; if (n > 1) a = (a * a) % mod; n >>= 1; } return res; } // 整数Sqrt ll sqrtll(ll x) { assert(x >= 0); ll rev = sqrt(x); while(rev * rev > x) --rev; while((rev+1) * (rev+1)<=x) ++rev; return rev; } template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す) template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す) int digit(ll x, int d=10) { int rev=0; while (x > 0) { rev++; x /= d;}; return rev; } // xのd進数桁数 /** * @brief std.hpp * @docs docs/std/std.md */ // line 3 "/home/seekworser/.cpp_lib/competitive_library/competitive/graph/graph.hpp" template <class Cost> struct Graph{ public: struct Edge { int to; Cost cost; Edge() {}; Edge(int _to, Cost _cost) : to(_to), cost(_cost) {}; }; struct AdjacencyListRange{ using iterator = typename std::vector<Edge>::const_iterator; iterator begi, endi; iterator begin() const { return begi; } iterator end() const { return endi; } int size() const { return (int)distance(begi, endi); } const Edge& operator[](int i) const { return begi[i]; } }; private: public: vector<Edge> E; vector<int> I; int n; Graph() : n(0) {} Graph(int _n) : n(_n) {} Graph(int _n, const vector<int>& from, vector<int>& to, vector<Cost>& cost, bool rev = false) : n(_n) { vector<int> buf(n+1, 0); for(int i=0; i<(int)from.size(); i++){ ++buf[from[i]]; if (rev) ++buf[to[i]]; } for(int i=1; i<=_n; i++) buf[i] += buf[i-1]; E.resize(buf[n]); for(int i=(int)from.size()-1; i>=0; i--){ int u = from[i]; int v = to[i]; Cost c = cost[i]; E[--buf[u]] = Edge(v, c); if(rev) E[--buf[v]] = Edge(u, c); } I = move(buf); } AdjacencyListRange operator[](int u) const { return AdjacencyListRange{ E.begin() + I[u], E.begin() + I[u+1] }; } int num_vertices() const { return n; } int size() const { return num_vertices(); } int num_edges() const { return E.size(); } Graph<Cost> reversed_edges() const { Graph<Cost> res; int _n = res.n = n; vi buf(n+1, 0); for(auto v : E) ++buf[v.to]; for(int i=1; i<=n; i++) buf[i] += buf[i-1]; res.E.resize(buf[n]); for(int u=0; u<n; u++) for(auto v : operator[](u)) res.E[--buf[v.to]] = {u, v.cost}; res.I = std::move(buf); return res; } }; template <class T> ostream& operator<<(ostream& os, Graph<T> g) { bool first = true; rep(i, g.n) repe(e, g[i]) { if (first) first = false; else os << endl; os << i << "->" << e.to << ": " << e.cost; } return os; } /** * @brief graph.hpp * @docs docs/graph/graph.md */ // line 5 "/home/seekworser/.cpp_lib/competitive_library/atcoder/segtree.hpp" // line 2 "/home/seekworser/.cpp_lib/competitive_library/atcoder/internal_bit.hpp" #ifdef _MSC_VER #include <intrin.h> #endif namespace atcoder { namespace internal { // @param n `0 <= n` // @return minimum non-negative `x` s.t. `n <= 2**x` int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } // @param n `1 <= n` // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0` constexpr int bsf_constexpr(unsigned int n) { int x = 0; while (!(n & (1 << x))) x++; return x; } // @param n `1 <= n` // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0` int bsf(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index, n); return index; #else return __builtin_ctz(n); #endif } } // namespace internal } // namespace atcoder // line 7 "/home/seekworser/.cpp_lib/competitive_library/atcoder/segtree.hpp" namespace atcoder { template <class S, S (*_op)(S, S), S (*_e)()> struct segtree { public: S (*op)(S, S) = _op; S (*e)() = _e; segtree() : segtree(0) {} explicit segtree(const std::vector<S>& v) : _n(int(v.size())) { log = internal::ceil_pow2(_n); size = 1 << log; d = std::vector<S>(2 * size, e()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } explicit segtree(int n) : segtree(std::vector<S>(n, _e())) {} void set(int p, S x) { assert(0 <= p && p < _n); p += size; d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } void add(int p, S x) { assert(0 <= p && p < _n); (*this).set(p, (*this).get(p) + x); } S get(int p) const { assert(0 <= p && p < _n); return d[p + size]; } S prod(int l, int r) const { assert(0 <= l && l <= r && r <= _n); S sml = e(), smr = e(); l += size; r += size; while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() const { return d[1]; } template <bool (*f)(S)> int max_right(int l) const { return max_right(l, [](S x) { return f(x); }); } template <class F> int max_right(int l, F f) const { assert(0 <= l && l <= _n); assert(f(e())); if (l == _n) return _n; l += size; S sm = e(); do { while (l % 2 == 0) l >>= 1; if (!f(op(sm, d[l]))) { while (l < size) { l = (2 * l); if (f(op(sm, d[l]))) { sm = op(sm, d[l]); l++; } } return l - size; } sm = op(sm, d[l]); l++; } while ((l & -l) != l); return _n; } template <bool (*f)(S)> int min_left(int r) const { return min_left(r, [](S x) { return f(x); }); } template <class F> int min_left(int r, F f) const { assert(0 <= r && r <= _n); assert(f(e())); if (r == 0) return 0; r += size; S sm = e(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!f(op(d[r], sm))) { while (r < size) { r = (2 * r + 1); if (f(op(d[r], sm))) { sm = op(d[r], sm); r--; } } return r + 1 - size; } sm = op(d[r], sm); } while ((r & -r) != r); return 0; } int n() const {return (*this)._n;} private: int _n, size, log; std::vector<S> d; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } }; } // namespace atcoder // line 4 "/home/seekworser/.cpp_lib/competitive_library/competitive/data_structure/segtree.hpp" template <class S, S (*op)(S, S), S (*e)()> std::ostream& operator<<(std::ostream& os, const atcoder::segtree<S, op, e> seg) { int n = seg.n(); rep(i, n) { os << seg.get(i); if (i != n-1) os << " "; } return os; }; namespace segtree { template<typename T> T op_max(T x, T y) { return x > y? x : y; } template<typename T> T op_min(T x, T y) { return x < y? x : y; } template<typename T> T op_add(T x, T y) { return x + y; } int e_max() { return -INF; } template<typename T> T e_max() { return -INFL; } int e_min() { return INF; } template<typename T> T e_min() { return INFL; } template<typename T> T e_add() { return 0; } } template<typename T> using seg_add = atcoder::segtree<T, segtree::op_add, segtree::e_add>; template<typename T> using seg_max = atcoder::segtree<T, segtree::op_max, segtree::e_max>; template<typename T> using seg_min = atcoder::segtree<T, segtree::op_min, segtree::e_min>; /** * @brief セグメント木(ラッパー) * @docs docs/data_structure/segtree.md */ // line 6 "/home/seekworser/.cpp_lib/competitive_library/atcoder/lazysegtree.hpp" // line 8 "/home/seekworser/.cpp_lib/competitive_library/atcoder/lazysegtree.hpp" namespace atcoder { template <class S, S (*_op)(S, S), S (*_e)(), class F, S (*_mapping)(F, S), F (*_composition)(F, F), F (*_id)()> struct lazy_segtree { public: S (*op)(S, S) = _op; S (*e)() = _e; S (*mapping)(F, S) = _mapping; F (*composition)(F, F) = _composition; F (*id)() = _id; lazy_segtree() : lazy_segtree(0) {} explicit lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, _e())) {} explicit lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) { log = internal::ceil_pow2(_n); size = 1 << log; d = std::vector<S>(2 * size, e()); lz = std::vector<F>(size, id()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } void add(int p, S x) { assert(0 <= p && p < _n); (*this).set(p, (*this).get(p) + x); } S get(int p) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); return d[p]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); if (l == r) return e(); l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } S sml = e(), smr = e(); while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() { return d[1]; } void apply(int p, F f) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = mapping(f, d[p]); for (int i = 1; i <= log; i++) update(p >> i); } void apply(int l, int r, F f) { assert(0 <= l && l <= r && r <= _n); if (l == r) return; l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } { int l2 = l, r2 = r; while (l < r) { if (l & 1) all_apply(l++, f); if (r & 1) all_apply(--r, f); l >>= 1; r >>= 1; } l = l2; r = r2; } for (int i = 1; i <= log; i++) { if (((l >> i) << i) != l) update(l >> i); if (((r >> i) << i) != r) update((r - 1) >> i); } } template <bool (*g)(S)> int max_right(int l) { return max_right(l, [](S x) { return g(x); }); } template <class G> int max_right(int l, G g) { assert(0 <= l && l <= _n); assert(g(e())); if (l == _n) return _n; l += size; for (int i = log; i >= 1; i--) push(l >> i); S sm = e(); do { while (l % 2 == 0) l >>= 1; if (!g(op(sm, d[l]))) { while (l < size) { push(l); l = (2 * l); if (g(op(sm, d[l]))) { sm = op(sm, d[l]); l++; } } return l - size; } sm = op(sm, d[l]); l++; } while ((l & -l) != l); return _n; } template <bool (*g)(S)> int min_left(int r) { return min_left(r, [](S x) { return g(x); }); } template <class G> int min_left(int r, G g) { assert(0 <= r && r <= _n); assert(g(e())); if (r == 0) return 0; r += size; for (int i = log; i >= 1; i--) push((r - 1) >> i); S sm = e(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!g(op(d[r], sm))) { while (r < size) { push(r); r = (2 * r + 1); if (g(op(d[r], sm))) { sm = op(d[r], sm); r--; } } return r + 1 - size; } sm = op(d[r], sm); } while ((r & -r) != r); return 0; } int n() {return (*this)._n;} private: int _n, size, log; std::vector<S> d; std::vector<F> lz; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } void all_apply(int k, F f) { d[k] = mapping(f, d[k]); if (k < size) lz[k] = composition(f, lz[k]); } void push(int k) { all_apply(2 * k, lz[k]); all_apply(2 * k + 1, lz[k]); lz[k] = id(); } }; } // namespace atcoder // line 4 "/home/seekworser/.cpp_lib/competitive_library/competitive/data_structure/lazysegtree.hpp" template <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S), F (*composition)(F, F), F (*id)()> std::ostream& operator<<(std::ostream& os, atcoder::lazy_segtree<S, op, e, F, mapping, composition, id> seg) { int n = seg.n(); rep(i, n) { os << seg.get(i); if (i != n-1) os << " "; } return os; }; namespace lsegtree { template<typename T> struct AddNode { T value; ll size; AddNode() : value(T(0)), size(1) {}; AddNode(T value, ll size) : value(value), size(size) {}; friend ostream& operator<<(std::ostream& os, const AddNode<T> &n) { os << n.value; return os; }; }; int e_max() { return -INF; } template<typename T> T e_max() { return -INFL; } int e_min() { return INF; } template<typename T> T e_min() { return INFL; } template<typename T> AddNode<T> e_add() { return {0, 1}; } template<typename T> T op_max(T x, T y) { return x > y ? x : y; } template<typename T> T op_min(T x, T y) { return x < y ? x : y; } template<typename T> AddNode<T> op_add(AddNode<T> x, AddNode<T> y) { return {x.value + y.value, x.size + y.size}; } template<typename T> T id_radd(){ return 0; } int id_rupdate(){ return INF; } template<typename T> T id_rupdate(){ return INFL; } template<typename T> AddNode<T> mapping_add_radd(T f, AddNode<T> x){ return {x.value + f * x.size, x.size}; } template<typename T> AddNode<T> mapping_add_rupdate(T f, AddNode<T> x){ AddNode<T> rev = AddNode<T>(x); if(f != id_rupdate<T>()) rev.value = f * rev.size; return rev; } template<typename T> T mapping_radd(T f, T x){ return f+x; } template<typename T> T mapping_rupdate(T f, T x){ return (f == id_rupdate() ? x : f); } template<typename T> T composition_radd(T f, T g){ return f+g; } template<typename T> T composition_rupdate(T f, T g){ return (f == id_rupdate() ? g : f); } } template<typename T> using lseg_add_radd = atcoder::lazy_segtree<lsegtree::AddNode<T>, lsegtree::op_add, lsegtree::e_add, T, lsegtree::mapping_add_radd, lsegtree::composition_radd, lsegtree::id_radd>; template<typename T> using lseg_min_radd = atcoder::lazy_segtree<T, lsegtree::op_min, lsegtree::e_min, T, lsegtree::mapping_radd, lsegtree::composition_radd, lsegtree::id_radd>; template<typename T> using lseg_max_radd = atcoder::lazy_segtree<T, lsegtree::op_max, lsegtree::e_max, T, lsegtree::mapping_radd, lsegtree::composition_radd, lsegtree::id_radd>; template<typename T> using lseg_add_rupdate = atcoder::lazy_segtree<lsegtree::AddNode<T>, lsegtree::op_add, lsegtree::e_add, T, lsegtree::mapping_add_rupdate, lsegtree::composition_rupdate, lsegtree::id_rupdate>; template<typename T> using lseg_min_rupdate = atcoder::lazy_segtree<T, lsegtree::op_min, lsegtree::e_min, T, lsegtree::mapping_rupdate, lsegtree::composition_rupdate, lsegtree::id_rupdate>; template<typename T> using lseg_max_rupdate = atcoder::lazy_segtree<T, lsegtree::op_max, lsegtree::e_max, T, lsegtree::mapping_rupdate, lsegtree::composition_rupdate, lsegtree::id_rupdate>; /** * @brief 遅延セグメント木(ラッパー) * @docs docs/data_structure/lazysegtree.md */ // line 6 "/home/seekworser/.cpp_lib/competitive_library/competitive/tree/heavy_light_decomposition.hpp" template <typename Cost, typename Seg> struct HeavyLightDecomposition { vi heavy_edge,in,out,head,par,pos; Seg &seg; bool edge; HeavyLightDecomposition(Graph<Cost>& g, Seg& seg, bool edge = true) : heavy_edge(g.n), in(g.n), out(g.n), head(g.n), par(g.n), pos(g.n), seg(seg), edge(edge) { build(g); } void build(Graph<Cost>& g) { vi subtree_size(g.n, 0); auto dfs_sz = [&] (auto self, int x, int p) -> int { par[x] = p; subtree_size[x] = 1; repe(e, g[x]) { if (e.to == p) continue; subtree_size[x] += self(self, e.to, x); } int maxs = -INF; heavy_edge[x] = -1; repe(e, g[x]) { if (e.to == p) continue; if (chmax(maxs, subtree_size[e.to])) heavy_edge[x] = e.to; } return subtree_size[x]; }; dfs_sz(dfs_sz, 0, -1); int t = 0; auto dfs_hld = [&] (auto self, int x, int par) -> void { in[x] = t++; pos[in[x]] = x; if (heavy_edge[x] != -1) { head[heavy_edge[x]] = head[x]; self(self, heavy_edge[x], x); } repe(e, g[x]) { if (e.to == par || e.to == heavy_edge[x]) continue; head[e.to] = e.to; self(self, e.to, x); } out[x] = t; }; dfs_hld(dfs_hld, 0, -1); } int lca(int u, int v) { while (head[u] != head[v]) { if (in[u] > in[v]) swap(u, v); v = par[head[v]]; } return in[u] < in[v] ? u : v; } int la(int u, int k) { while (true) { if (u == -1) return -1; if (u == 0 && k > 0) return -1; int v = head[u]; if (in[u] - k >= in[v]) return pos[in[u] - k]; k -= in[u] - in[v] + 1; u = par[head[u]]; } } decltype(seg.e()) prod(int u, int v) { using T = decltype(seg.e()); T l = seg.e(); T r = seg.e(); while (head[u] != head[v]) { if (in[u] > in[v]) swap(u, v), swap(l, r); l = seg.op(seg.prod(in[head[v]], in[v] + 1), l); v = par[head[v]]; } if (in[u] > in[v]) swap(u, v), swap(l, r); // パスクエリの場合はu(uからuの親へのパス)は足さない return seg.op(seg.op(seg.prod(in[u] + edge, in[v] + 1), l) , r); } void apply(int u, int v, decltype(seg.id()) f) { while (head[u] != head[v]) { if (in[u] > in[v]) swap(u, v); seg.apply(in[head[v]], in[v] + 1, f); v = par[head[v]]; } if (in[u] > in[v]) swap(u, v); seg.apply(in[u] + edge, in[v] + 1, f); } int edge_pos(int u, int v) { if (par[u] != v) swap(u, v); assert(par[u] == v); return in[u]; } }; /** * @brief HL分解 (Heavy Light Decomposition) * @docs docs/tree/heavy_light_decomposition.md */ // line 3 "/home/seekworser/.cpp_lib/competitive_library/competitive/std/io.hpp" // 演算子オーバーロード(プロトタイプ宣言) template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p); template <class T> inline istream& operator>>(istream& is, vector<T>& v); template <class T, class U> inline ostream& operator<<(ostream& os, const pair<T, U>& p); template <class T> inline ostream& operator<<(ostream& os, const vector<T>& v); template <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &mp); template <typename T> ostream &operator<<(ostream &os, const set<T> &st); template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st); template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &st); template <typename T> ostream &operator<<(ostream &os, queue<T> q); template <typename T> ostream &operator<<(ostream &os, deque<T> q); template <typename T> ostream &operator<<(ostream &os, stack<T> st); template <class T, class Container, class Compare> ostream &operator<<(ostream &os, priority_queue<T, Container, Compare> pq); // 演算子オーバーロード template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; } template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repe(x, v) is >> x; return is; } template <class T, class U> inline ostream& operator<<(ostream& os, const pair<T, U>& p) { os << p.first << " " << p.second; return os; } template <class T> inline ostream& operator<<(ostream& os, const vector<T>& v) { rep(i, sz(v)) { os << v.at(i); if (i != sz(v) - 1) os << " "; } return os; } template <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &mp) { for (auto &[key, val] : mp) { os << key << ":" << val << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &st) { auto itr = st.begin(); for (int i = 0; i < (int)st.size(); i++) { os << *itr << (i + 1 != (int)st.size() ? " " : ""); itr++; } return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st) { auto itr = st.begin(); for (int i = 0; i < (int)st.size(); i++) { os << *itr << (i + 1 != (int)st.size() ? " " : ""); itr++; } return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &st) { ll cnt = 0; for (auto &e : st) { os << e << (++cnt != (int)st.size() ? " " : ""); } return os; } template <typename T> ostream &operator<<(ostream &os, queue<T> q) { while (q.size()) { os << q.front() << " "; q.pop(); } return os; } template <typename T> ostream &operator<<(ostream &os, deque<T> q) { while (q.size()) { os << q.front() << " "; q.pop_front(); } return os; } template <typename T> ostream &operator<<(ostream &os, stack<T> st) { while (st.size()) { os << st.top() << " "; st.pop(); } return os; } template <class T, class Container, class Compare> ostream &operator<<(ostream &os, priority_queue<T, Container, Compare> pq) { while (pq.size()) { os << pq.top() << " "; pq.pop(); } return os; } template <typename T> void print_sep_end(string sep, string end, const T& val) { (void)sep; cout << val << end; }; template <typename T1, typename... T2> void print_sep_end(string sep, string end, const T1 &val, const T2 &...remain) { cout << val << sep; print_sep_end(sep, end, remain...); }; template <typename... T> void print(const T &...args) { print_sep_end(" ", "\n", args...); }; template <typename... T> void flush() { cout << flush; }; template <typename... T> void print_and_flush(const T &...args) { print(args...); flush(); }; #define debug(...) debug_func(0, #__VA_ARGS__, __VA_ARGS__) // debug print template <typename T> void input(T &a) { cin >> a; }; template <typename T1, typename... T2> void input(T1&a, T2 &...b) { cin >> a; input(b...); }; #ifdef LOCAL_TEST template <typename T> void debug_func(int i, T name) { (void)i; (void)name; cerr << endl; } template <typename T1, typename T2, typename... T3> void debug_func(int i, const T1 &name, const T2 &a, const T3 &...b) { int scope = 0; for ( ; (scope != 0 || name[i] != ',') && name[i] != '\0'; i++ ) { cerr << name[i]; if (name[i] == '(' || name[i] == '{') scope++; if (name[i] == ')' || name[i] == '}') scope--; } cerr << ":" << a << " "; debug_func(i + 1, name, b...); } #endif #ifndef LOCAL_TEST template <typename... T> void debug_func(const T &...) {} #endif /** * @brief io.hpp * @docs docs/std/io.md */ // line 28 "answer.cpp" #endif