#include #include #include #include #include #include #include #include #include #include namespace suisen { namespace default_operator { template auto zero() -> decltype(T { 0 }) { return T { 0 }; } template auto one() -> decltype(T { 1 }) { return T { 1 }; } template auto add(const T &x, const T &y) -> decltype(x + y) { return x + y; } template auto sub(const T &x, const T &y) -> decltype(x - y) { return x - y; } template auto mul(const T &x, const T &y) -> decltype(x * y) { return x * y; } template auto div(const T &x, const T &y) -> decltype(x / y) { return x / y; } template auto mod(const T &x, const T &y) -> decltype(x % y) { return x % y; } template auto neg(const T &x) -> decltype(-x) { return -x; } template auto inv(const T &x) -> decltype(one() / x) { return one() / x; } } // default_operator namespace default_operator_noref { template auto zero() -> decltype(T { 0 }) { return T { 0 }; } template auto one() -> decltype(T { 1 }) { return T { 1 }; } template auto add(T x, T y) -> decltype(x + y) { return x + y; } template auto sub(T x, T y) -> decltype(x - y) { return x - y; } template auto mul(T x, T y) -> decltype(x * y) { return x * y; } template auto div(T x, T y) -> decltype(x / y) { return x / y; } template auto mod(T x, T y) -> decltype(x % y) { return x % y; } template auto neg(T x) -> decltype(-x) { return -x; } template auto inv(T x) -> decltype(one() / x) { return one() / x; } } // default_operator } // namespace suisen namespace suisen { template , T(*_zero)() = default_operator_noref::zero, T(*_neg)(T) = default_operator_noref::neg> struct PointGetRangeContourAddOnTree { using value_type = T; private: struct InternalFenwickTree { InternalFenwickTree() = default; InternalFenwickTree(int n) : _n(n), _dat(_n + 1, _zero()) {} value_type get(int i) const { value_type res = _zero(); for (++i; i; i -= i & -i) res = _add(res, _dat[i]); return res; } void add(int l, int r, const value_type& val) { l = std::max(0, l), r = std::min(r, _n); if (l < r) add(l, val), add(r, _neg(val)); } private: int _n; std::vector _dat; void add(int r, const value_type& val) { for (++r; r <= _n; r += r & -r) _dat[r] = _add(_dat[r], val); } }; using sequence_type = InternalFenwickTree; struct AuxInfo { int child_index; int dist; }; struct TreeNode { std::vector adj; typename std::array::iterator info_it; }; public: PointGetRangeContourAddOnTree(int n = 0, const value_type& fill_value = _zero()) : PointGetRangeContourAddOnTree(std::vector(n, fill_value)) {} PointGetRangeContourAddOnTree(const std::vector& dat) : _n(dat.size()), _dat(dat), _nodes(_n), _par(_n, -1), _info(_n), _subtrees(_n), _trees(_n) { _par.reserve(2 * _n); _subtrees.reserve(2 * _n); for (int i = 0; i < _n; ++i) { _nodes[i].info_it = _info[i].begin(); } } void add_edge(int u, int v) { _nodes[u].adj.push_back(v); _nodes[v].adj.push_back(u); } void build() { std::vector sub_size(_n, 0); auto build_sequence = [this](int root, const int child_index) { std::deque> dq { { root, -1 } }; int dist = -1, nxt = root; while (dq.size()) { const auto [u, pu] = dq.front(); dq.pop_front(); if (u == nxt) ++dist, nxt = -1; auto& node = _nodes[u]; if (child_index >= 0) *node.info_it++ = { child_index, dist }; for (int v : node.adj) if (v != pu) { dq.emplace_back(v, u); if (nxt < 0) nxt = v; } } return sequence_type(dist + 1); }; auto rec = [&](auto rec, int r, int siz) -> int { int c = -1; auto get_centroid = [&](auto get_centroid, int u, int p) -> void { sub_size[u] = 1; for (int v : _nodes[u].adj) if (v != p) { get_centroid(get_centroid, v, u); if (v == c) { sub_size[u] = siz - sub_size[c]; break; } sub_size[u] += sub_size[v]; } if (c < 0 and sub_size[u] * 2 > siz) c = u; }; get_centroid(get_centroid, r, -1); const int ch_num = _nodes[c].adj.size(); _subtrees[c].resize(ch_num); for (int i = 0; i < ch_num; ++i) { const int v = _nodes[c].adj[i]; _nodes[v].adj.erase(std::find(_nodes[v].adj.begin(), _nodes[v].adj.end(), c)); _par[rec(rec, v, sub_size[v])] = c; _subtrees[c][i] = build_sequence(v, i); } _trees[c] = build_sequence(c, -1); for (int v : _nodes[c].adj) _nodes[v].adj.push_back(c); return c; }; rec(rec, 0, _n); } value_type get(int u) const { value_type res = _dat[u] + _trees[u].get(0); int v = _par[u]; const auto it_end = _nodes[u].info_it; for (auto it = _info[u].begin(); it != it_end; ++it) { res = _add(res, _trees[v].get(it->dist + 1)); res = _add(res, _subtrees[v][it->child_index].get(it->dist)); v = _par[v]; } return res; } void set(int u, const value_type& new_val) { add(u, _add(new_val, _neg(get(u)))); } void add(int u, const value_type &val) { _nodes[u].dat = _add(_dat[u], val); } void add(int u, int dl, int dr, const value_type &val) { _trees[u].add(dl, dr, val); int v = _par[u]; const auto it_end = _nodes[u].info_it; for (auto it = _info[u].begin(); it != it_end; ++it) { int ql = dl - it->dist - 1, qr = dr - it->dist - 1; _trees[v].add(ql, qr, val); _subtrees[v][it->child_index].add(ql - 1, qr - 1, _neg(val)); v = _par[v]; } } private: int _n; std::vector _dat; std::vector _nodes; std::vector _par; std::vector> _info; std::vector> _subtrees; std::vector _trees; }; } // namespace suisen int main() { auto beg_time = std::chrono::system_clock::now(); std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, q; std::cin >> n >> q; suisen::PointGetRangeContourAddOnTree g(n); for (int i = 0; i < n - 1; ++i) { int u, v; std::cin >> u >> v; --u, --v; g.add_edge(u, v); } g.build(); auto mid_time = std::chrono::system_clock::now(); std::cerr << "build time : " << std::chrono::duration_cast(mid_time - beg_time).count() << std::endl; for (int i = 0; i < q; ++i) { int x, y, z; std::cin >> x >> y >> z; --x, ++y; std::cout << g.get(x) << '\n'; g.add(x, 0, y, z); } auto end_time = std::chrono::system_clock::now(); std::cerr << "query time : " << std::chrono::duration_cast(end_time - mid_time).count() << std::endl; return 0; }