【题解】P5351 Ruri Loves Maschera

题目传送门

简单的淀粉质板子。

分析

考虑统计经过当前分治中心 \(u\) 的答案,对于每个 \(v \in \text{subtree}(u)\),我们维护 \(d_v\) 和 \(w_v\),表示 \(u \rightarrow v\) 的边数和路径上的最大值,然后我们考虑对所有点按照 \(w\) 进行排序。假设我们现在要计算一个点 \(v\) 的贡献,由于按照 \(w\) 排序过了,所以它的贡献就是 \(w_v\) 乘上满足路径长度的路径条数,后面的问题显然是一个 BIT 就可以解决,所以我们就用 \(O(n \log^2n)\) 解决了这道题。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); i++)
#define R(i, j, k) for(int i = (j); i >= (k); i--)
#define G(i, u) for (int i = h[(u)]; ~i; i = ne[i])
#define ll long long
#define ld long double
#define ull unsigned long long

using namespace std;
const int N = 1e5 + 10;
int h[N], e[N << 1], ne[N << 1], w[N << 1], idx;
int vis[N];
vector<pair<int, int> > now, all;
int n, L, R;
int tr[N];
ll res;

inline int read() {
int x = 0, y = 1; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') y = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * y;
}

inline void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

inline void add(int x, int v) {
for (; x <= n; x += x & -x) tr[x] += v;
}

inline int ask(int x) {
int res = 0;
for (; x; x -= x & -x) res += tr[x];
return res;
}

inline int getSize(int u, int p) {
if (vis[u]) return 0;
int res = 1;
G(i, u) if (e[i] != p) res += getSize(e[i], u);
return res;
}

inline int getRoot(int u, int p, int all, int &root) {
if (vis[u]) return 0;
int now = 0, sum = 1;
G(i, u) {
int v = e[i];
if (v == p) continue;
int k = getRoot(v, u, all, root);
now = max(now, k), sum += k;
}
now = max(now, all - sum);
if (now <= all / 2) root = u;
return sum;
}

inline void getDist(int u, int p, int dist, int pe) {
if (vis[u]) return;
now.push_back({pe, dist});
if (dist < R) G(i, u) if (e[i] != p) getDist(e[i], u, dist + 1, max(pe, w[i]));
}

inline ll calc(vector<pair<int, int> > &a) {
ll res = 0;
sort(a.begin(), a.end());
for (pair<int, int> t : a) res += 1ll * t.first * (ask(R - t.second) - ask(max(0, L - t.second - 1))), add(t.second, 1);
for (pair<int, int> t : a) add(t.second, -1);
return res;
}

inline void calc(int u) {
all.clear();
G(i, u) {
int v = e[i];
if (vis[v]) continue;
now.clear();
getDist(v, u, 1, w[i]);
res -= calc(now);
for (pair<int, int> t : now) all.push_back(t);
}
res += calc(all);
for (pair<int, int> t : all) if (t.second >= L) res += t.first;
}

inline void dfs(int u) {
if (vis[u]) return;
getRoot(u, -1, getSize(u, -1), u);
vis[u] = true;
calc(u);
G(i, u) dfs(e[i]);
}

int main() {
memset(h, -1, sizeof h);
n = read(), L = read(), R = read();
L(_, 1, n - 1) {
int a = read(), b = read(), c = read();
add(a, b, c), add(b, a, c);
}
dfs(1);
printf("%lld\n", res << 1);
return 0;
}