#include <bits/stdc++.h>
constexpr int N = 1e5;
struct segtree{
std::vector<int> t;
int n;
segtree(int n) : n(n), t(2 * n) { }
void update(int p, int val) {
for (t[p += n] = val; p > 1; p >>= 1) {
t[p >> 1] = t[p] + t[p ^ 1];
}
}
int get(int l, int r) {
int res = 0;
for (l += n, r += n; l <= r; l >>= 1, r >>= 1) {
if (l % 2 == 1) res += t[l++];
if (r % 2 == 0) res += t[r--];
}
return res;
}
} dpsum(N), childsum(N);
std::vector<int> e[N];
int n, m, in[N], out[N], up[N][20], timeStamp, siz[N], heavy[N], D[N], par[N], head[N], pos[N];
void precalc(int u, int p, int d = 0) {
par[u] = p;
D[u] = d;
siz[u] = 1;
in[u] = timeStamp++;
up[u][0] = p;
for (int i = 1; i < 20; i++) {
up[u][i] = up[up[u][i - 1]][i - 1];
}
int mx_size = 0;
for (auto v : e[u]) {
if (v != p) {
precalc(v, u, d + 1);
siz[u] += siz[v];
if (mx_size < siz[v]) {
mx_size = siz[v];
heavy[u] = v;
}
}
}
out[u] = timeStamp;
}
bool is(int u, int v) { return in[u] <= in[v] && out[u] >= out[v]; }
int lca(int u, int v) {
if (is(u, v)) return u;
if (is(v, u)) return v;
for (int i = 19; i >= 0; i--) {
if (!is(up[u][i], v)) {
u = up[u][i];
}
}
return up[u][0];
}
void dfs(int u, int h) {
head[u] = h;
pos[u] = timeStamp++;
if (~heavy[u]) {
dfs(heavy[u], h);
}
for (auto v : e[u]) {
if (v != par[u] && v != heavy[u]) {
dfs(v, v);
}
}
}
int queryChild(int u, int v) {
int res = 0;
for (; head[u] != head[v]; v = par[head[v]]) {
if (D[head[u]] > D[head[v]]) {
std::swap(u, v);
}
res += childsum.get(pos[head[v]], pos[v]);
}
if (D[u] > D[v]) {
std::swap(u, v);
}
res += childsum.get(pos[u], pos[v]);
return res;
}
int queryDp(int u, int v) {
int res = 0;
for (; head[u] != head[v]; v = par[head[v]]) {
if (D[head[u]] > D[head[v]]) {
std::swap(u, v);
}
res += dpsum.get(pos[head[v]], pos[v]);
}
if (D[u] > D[v]) {
std::swap(u, v);
}
res += dpsum.get(pos[u], pos[v]);
return res;
}
std::vector<std::tuple<int, int, int>> q[N];
int dp[N];
void calc(int u) {
int chsum = 0;
for (auto v : e[u]) {
if (v != par[u]) {
calc(v);
chsum += dp[v];
}
}
childsum.update(pos[u], chsum);
dp[u] = chsum;
for (auto [a, b, c] : q[u]) {
int res = 0;
res += queryChild(a, b);
res -= queryDp(a, b);
res += c;
dp[u] = std::max(dp[u], res);
}
dpsum.update(pos[u], dp[u]);
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
std::cin >> u >> v;
u--;
v--;
e[u].push_back(v);
e[v].push_back(u);
}
memset(heavy, -1, sizeof heavy);
precalc(0, 0);
timeStamp = 0;
dfs(0, 0);
std::cin >> m;
for (int i = 0; i < m; i++) {
int u, v, c;
std::cin >> u >> v >> c;
u--;
v--;
q[lca(u, v)].emplace_back(u, v, c);
}
calc(0);
std::cout << dp[0] << "\n";
return 0;
}
Compilation message
election_campaign.cpp: In constructor 'segtree::segtree(int)':
election_campaign.cpp:5:9: warning: 'segtree::n' will be initialized after [-Wreorder]
5 | int n;
| ^
election_campaign.cpp:4:22: warning: 'std::vector<int> segtree::t' [-Wreorder]
4 | std::vector<int> t;
| ^
election_campaign.cpp:6:5: warning: when initialized here [-Wreorder]
6 | segtree(int n) : n(n), t(2 * n) { }
| ^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7012 KB |
Output is correct |
2 |
Correct |
4 ms |
6988 KB |
Output is correct |
3 |
Correct |
4 ms |
6960 KB |
Output is correct |
4 |
Correct |
5 ms |
7116 KB |
Output is correct |
5 |
Correct |
110 ms |
22284 KB |
Output is correct |
6 |
Correct |
65 ms |
29996 KB |
Output is correct |
7 |
Correct |
118 ms |
27204 KB |
Output is correct |
8 |
Correct |
87 ms |
22656 KB |
Output is correct |
9 |
Correct |
108 ms |
25668 KB |
Output is correct |
10 |
Correct |
89 ms |
22596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6984 KB |
Output is correct |
2 |
Correct |
4 ms |
6988 KB |
Output is correct |
3 |
Correct |
5 ms |
7252 KB |
Output is correct |
4 |
Correct |
133 ms |
31268 KB |
Output is correct |
5 |
Correct |
130 ms |
31156 KB |
Output is correct |
6 |
Correct |
117 ms |
31112 KB |
Output is correct |
7 |
Correct |
133 ms |
31080 KB |
Output is correct |
8 |
Correct |
135 ms |
31212 KB |
Output is correct |
9 |
Correct |
117 ms |
31240 KB |
Output is correct |
10 |
Correct |
131 ms |
31156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6984 KB |
Output is correct |
2 |
Correct |
4 ms |
6988 KB |
Output is correct |
3 |
Correct |
5 ms |
7252 KB |
Output is correct |
4 |
Correct |
133 ms |
31268 KB |
Output is correct |
5 |
Correct |
130 ms |
31156 KB |
Output is correct |
6 |
Correct |
117 ms |
31112 KB |
Output is correct |
7 |
Correct |
133 ms |
31080 KB |
Output is correct |
8 |
Correct |
135 ms |
31212 KB |
Output is correct |
9 |
Correct |
117 ms |
31240 KB |
Output is correct |
10 |
Correct |
131 ms |
31156 KB |
Output is correct |
11 |
Correct |
16 ms |
7988 KB |
Output is correct |
12 |
Correct |
137 ms |
31148 KB |
Output is correct |
13 |
Correct |
140 ms |
31140 KB |
Output is correct |
14 |
Correct |
122 ms |
31428 KB |
Output is correct |
15 |
Correct |
137 ms |
31068 KB |
Output is correct |
16 |
Correct |
118 ms |
31088 KB |
Output is correct |
17 |
Correct |
137 ms |
31172 KB |
Output is correct |
18 |
Correct |
151 ms |
31056 KB |
Output is correct |
19 |
Correct |
121 ms |
31160 KB |
Output is correct |
20 |
Correct |
136 ms |
31120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
248 ms |
22868 KB |
Output is correct |
2 |
Correct |
120 ms |
33348 KB |
Output is correct |
3 |
Correct |
247 ms |
30180 KB |
Output is correct |
4 |
Correct |
155 ms |
25576 KB |
Output is correct |
5 |
Correct |
218 ms |
29776 KB |
Output is correct |
6 |
Correct |
165 ms |
25568 KB |
Output is correct |
7 |
Correct |
231 ms |
29436 KB |
Output is correct |
8 |
Correct |
190 ms |
25476 KB |
Output is correct |
9 |
Correct |
117 ms |
33352 KB |
Output is correct |
10 |
Correct |
233 ms |
28488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7012 KB |
Output is correct |
2 |
Correct |
4 ms |
6988 KB |
Output is correct |
3 |
Correct |
4 ms |
6960 KB |
Output is correct |
4 |
Correct |
5 ms |
7116 KB |
Output is correct |
5 |
Correct |
110 ms |
22284 KB |
Output is correct |
6 |
Correct |
65 ms |
29996 KB |
Output is correct |
7 |
Correct |
118 ms |
27204 KB |
Output is correct |
8 |
Correct |
87 ms |
22656 KB |
Output is correct |
9 |
Correct |
108 ms |
25668 KB |
Output is correct |
10 |
Correct |
89 ms |
22596 KB |
Output is correct |
11 |
Correct |
5 ms |
7128 KB |
Output is correct |
12 |
Correct |
5 ms |
7260 KB |
Output is correct |
13 |
Correct |
6 ms |
7244 KB |
Output is correct |
14 |
Correct |
5 ms |
7116 KB |
Output is correct |
15 |
Correct |
6 ms |
7116 KB |
Output is correct |
16 |
Correct |
5 ms |
7116 KB |
Output is correct |
17 |
Correct |
5 ms |
7172 KB |
Output is correct |
18 |
Correct |
7 ms |
7252 KB |
Output is correct |
19 |
Correct |
5 ms |
7116 KB |
Output is correct |
20 |
Correct |
6 ms |
7244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7012 KB |
Output is correct |
2 |
Correct |
4 ms |
6988 KB |
Output is correct |
3 |
Correct |
4 ms |
6960 KB |
Output is correct |
4 |
Correct |
5 ms |
7116 KB |
Output is correct |
5 |
Correct |
110 ms |
22284 KB |
Output is correct |
6 |
Correct |
65 ms |
29996 KB |
Output is correct |
7 |
Correct |
118 ms |
27204 KB |
Output is correct |
8 |
Correct |
87 ms |
22656 KB |
Output is correct |
9 |
Correct |
108 ms |
25668 KB |
Output is correct |
10 |
Correct |
89 ms |
22596 KB |
Output is correct |
11 |
Correct |
4 ms |
6984 KB |
Output is correct |
12 |
Correct |
4 ms |
6988 KB |
Output is correct |
13 |
Correct |
5 ms |
7252 KB |
Output is correct |
14 |
Correct |
133 ms |
31268 KB |
Output is correct |
15 |
Correct |
130 ms |
31156 KB |
Output is correct |
16 |
Correct |
117 ms |
31112 KB |
Output is correct |
17 |
Correct |
133 ms |
31080 KB |
Output is correct |
18 |
Correct |
135 ms |
31212 KB |
Output is correct |
19 |
Correct |
117 ms |
31240 KB |
Output is correct |
20 |
Correct |
131 ms |
31156 KB |
Output is correct |
21 |
Correct |
16 ms |
7988 KB |
Output is correct |
22 |
Correct |
137 ms |
31148 KB |
Output is correct |
23 |
Correct |
140 ms |
31140 KB |
Output is correct |
24 |
Correct |
122 ms |
31428 KB |
Output is correct |
25 |
Correct |
137 ms |
31068 KB |
Output is correct |
26 |
Correct |
118 ms |
31088 KB |
Output is correct |
27 |
Correct |
137 ms |
31172 KB |
Output is correct |
28 |
Correct |
151 ms |
31056 KB |
Output is correct |
29 |
Correct |
121 ms |
31160 KB |
Output is correct |
30 |
Correct |
136 ms |
31120 KB |
Output is correct |
31 |
Correct |
248 ms |
22868 KB |
Output is correct |
32 |
Correct |
120 ms |
33348 KB |
Output is correct |
33 |
Correct |
247 ms |
30180 KB |
Output is correct |
34 |
Correct |
155 ms |
25576 KB |
Output is correct |
35 |
Correct |
218 ms |
29776 KB |
Output is correct |
36 |
Correct |
165 ms |
25568 KB |
Output is correct |
37 |
Correct |
231 ms |
29436 KB |
Output is correct |
38 |
Correct |
190 ms |
25476 KB |
Output is correct |
39 |
Correct |
117 ms |
33352 KB |
Output is correct |
40 |
Correct |
233 ms |
28488 KB |
Output is correct |
41 |
Correct |
5 ms |
7128 KB |
Output is correct |
42 |
Correct |
5 ms |
7260 KB |
Output is correct |
43 |
Correct |
6 ms |
7244 KB |
Output is correct |
44 |
Correct |
5 ms |
7116 KB |
Output is correct |
45 |
Correct |
6 ms |
7116 KB |
Output is correct |
46 |
Correct |
5 ms |
7116 KB |
Output is correct |
47 |
Correct |
5 ms |
7172 KB |
Output is correct |
48 |
Correct |
7 ms |
7252 KB |
Output is correct |
49 |
Correct |
5 ms |
7116 KB |
Output is correct |
50 |
Correct |
6 ms |
7244 KB |
Output is correct |
51 |
Correct |
221 ms |
25700 KB |
Output is correct |
52 |
Correct |
143 ms |
33732 KB |
Output is correct |
53 |
Correct |
240 ms |
28848 KB |
Output is correct |
54 |
Correct |
152 ms |
25856 KB |
Output is correct |
55 |
Correct |
270 ms |
25468 KB |
Output is correct |
56 |
Correct |
142 ms |
33632 KB |
Output is correct |
57 |
Correct |
207 ms |
29720 KB |
Output is correct |
58 |
Correct |
170 ms |
25708 KB |
Output is correct |
59 |
Correct |
213 ms |
25668 KB |
Output is correct |
60 |
Correct |
140 ms |
33516 KB |
Output is correct |
61 |
Correct |
206 ms |
29776 KB |
Output is correct |
62 |
Correct |
155 ms |
25556 KB |
Output is correct |
63 |
Correct |
228 ms |
25168 KB |
Output is correct |
64 |
Correct |
143 ms |
33588 KB |
Output is correct |
65 |
Correct |
246 ms |
29416 KB |
Output is correct |
66 |
Correct |
142 ms |
25816 KB |
Output is correct |
67 |
Correct |
261 ms |
25528 KB |
Output is correct |
68 |
Correct |
141 ms |
33540 KB |
Output is correct |
69 |
Correct |
200 ms |
28356 KB |
Output is correct |
70 |
Correct |
154 ms |
25944 KB |
Output is correct |