text: w); downex[node] = cur; down[node].sum = cur.sum + e[i].w * (n - size[node]); down[node].sqr = delta(cur, n - size[node], e[i].w); preDown(node, x); } } int p[18 + 1][100100 + 1], logn, dep[100100 + 1]; hashInt dis[100100 + 1], dissum[100100 + 1], dissqr[100100 + 1]; void preDA(int x, int f) { p[0][x] = f; for (int i = head[x]; i; i = e[i].next) { int node = e[i].node; if (node == f) continue; dep[node] = dep[x] + 1; dis[node] = dis[x] + e[i].w; dissum[node] = dissum[x] + dis[node]; dissqr[node] = dissqr[x] + dis[node] * dis[node]; preDA(node, x); } } inline int LCA(int x, int y) { if (dep[x] > dep[y]) swap(x, y); for (int i = logn; i >= 0; --i) if (dep[x] <= dep[p[i][y]]) y = p[i][y]; if (x == y) return x; for (int i = logn; i >= 0; --i) if (p[i][x] != p[i][y]) x = p[i][x], y = p[i][y]; return p[0][x]; } inline int LCA2(int x, int y) { for (int i = logn; i >= 0; --i) if (dep[x] < dep[p[i][y]]) y = p[i][y]; return y; } vector<pair<int, int> > Q[100100 + 1]; int q, ans[100100 + 1], seq[100100 + 1], cnt = 0; DP pre[100100 + 1], suf[100100 + 1]; struct BIT { hashInt a[100100 + 1]; inline void modify(int x, hashInt w) { for (; x <= n; x += x & -x) a[x] = a[x] + w; } inline hashInt query(int x) { hashInt r = 0; for (; x > 0; x -= x & -x) r = r + a[x]; return r; } } sufsum, sufsqr; void dfs(int x, int f) { for (int i = 0; i < Q[x].size(); ++i) { int node = Q[x][i].first, id = Q[x][i].second; int p = LCA(x, node); hashInt b = dis[node] - dis[p]; b = b * b; } for (int i = head[x]; i; i = e[i].next) { int node = e[i].node; if (node == f) continue; seq[++cnt] = x; pre[cnt].sum = pre[cnt - 1].sum + e[i].w * (cnt - 1); pre[cnt].sqr = delta(pre[cnt - 1], cnt - 1, e[i].w); dfs(node, x); --cnt; } } int main(int argc, char *argv[]) { scanf("%d", &n); for (int i = 1; i < n; ++i) { int x, y, w; scanf("%d%d%d", &x, &y, &w); addedge(x, y, w), addedge(y, x, w); } preUp(1, 0); preDown(1, 0); dep[1] = 1, preDA(1, 0); logn = (int)(log((double)n) / log(2.