#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; // const int MOD = 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); } } iosetup; struct CentroidDecomposition { int root; vector> comp; vector par; CentroidDecomposition(const vector> &graph) : graph(graph) { int n = graph.size(); alive.assign(n, true); subtree.resize(n); comp.resize(n); par.assign(n, -1); root = build(0); } private: const vector> graph; vector alive; vector subtree; int build(int s) { int centroid = search_centroid(-1, s, calc_subtree(-1, s) / 2); alive[centroid] = false; for (int e : graph[centroid]) { if (alive[e]) { comp[centroid].emplace_back(build(e)); par[e] = centroid; } } alive[centroid] = true; return centroid; } int calc_subtree(int par, int ver) { subtree[ver] = 1; for (int e : graph[ver]) { if (e != par && alive[e]) subtree[ver] += calc_subtree(ver, e); } return subtree[ver]; } int search_centroid(int par, int ver, int half) { for (int e : graph[ver]) { if (e != par && alive[e]) { if (subtree[e] > half) return search_centroid(ver, e, half); } } return ver; } }; template struct BITRangeAdd { BITRangeAdd(int n_, const Abelian UNITY = 0) : n(n_), UNITY(UNITY) { ++n; dat_const.assign(n, UNITY); dat_linear.assign(n, UNITY); } void add(int left, int right, Abelian val) { if (right < ++left) return; for (int i = left; i < n; i += i & -i) { dat_const[i] -= val * (left - 1); dat_linear[i] += val; } for (int i = right + 1; i < n; i += i & -i) { dat_const[i] += val * right; dat_linear[i] -= val; } } Abelian sum(int idx) { Abelian res = UNITY; for (int i = idx; i > 0; i -= i & -i) res += dat_linear[i]; res *= idx; for (int i = idx; i > 0; i -= i & -i) res += dat_const[i]; return res; } Abelian sum(int left, int right) { if (right <= left) return UNITY; return sum(right) - sum(left); } Abelian operator[](const int idx) { return sum(idx, idx + 1); } int n; private: const Abelian UNITY; vector dat_const, dat_linear; }; int main() { int n, q; cin >> n >> q; vector> graph(n); REP(_, n - 1) { int a, b; cin >> a >> b; --a; --b; graph[a].emplace_back(b); graph[b].emplace_back(a); } CentroidDecomposition cd(graph); vector visited(n, false); vector> mp(n); vector> resp(n), depth(n, vector(1, 0)); vector> bit; vector>>> minus(n); vector> bit2; function rec = [&](int root, int idx) { visited[root] = true; mp[idx][root] = 0; resp[root].emplace_back(idx); vector que{root}; for (int dep = 1; !que.empty(); ++dep) { vector nx; for (int ver : que) { for (int e : graph[ver]) { if (!visited[e] && mp[idx].count(e) == 0) { int sz = mp[idx].size(); mp[idx][e] = sz; resp[e].emplace_back(idx); depth[idx].emplace_back(dep); nx.emplace_back(e); } } } que.swap(nx); } bit.emplace_back(mp[idx].size()); minus[root][idx] = {-1, {-1, -1}}; int sum_dep = 0; for (int dst : graph[root]) { if (visited[dst]) continue; vector> aft; int max_depth = 0; function dfs = [&](int par, int ver, int dep) { chmax(max_depth, dep); aft.emplace_back(ver, dep); for (int e : graph[ver]) { if (!visited[e] && e != par) dfs(ver, e, dep + 1); } }; dfs(root, dst, 1); for (auto [ver, dep] : aft) minus[ver][idx] = {sum_dep + dep, {sum_dep, sum_dep + max_depth}}; sum_dep += max_depth + 1; } bit2.emplace_back(sum_dep); for (int e : cd.comp[root]) { if (!visited[e]) rec(e, ++idx); } }; rec(cd.root, 0); while (q--) { int x, y, z; cin >> x >> y >> z; --x; ll ans = 0; for (int idx : resp[x]) { ans += bit[idx][mp[idx][x]]; if (minus[x][idx].first != -1) ans -= bit2[idx][minus[x][idx].first]; } cout << ans << '\n'; for (int idx : resp[x]) { int ver = mp[idx][x], dist = y - depth[idx][ver]; if (dist >= 0) { int l = 0, r = depth[idx].size(); while (r - l > 1) { int mid = (l + r) >> 1; (depth[idx][mid] <= dist ? l : r) = mid; } bit[idx].add(0, l + 1, z); auto [dep, mnmx] = minus[x][idx]; if (dep != -1) bit2[idx].add(mnmx.first, min(mnmx.first + dist, mnmx.second) + 1, z); } } } return 0; }