#include <iostream>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define ppi pair<pii, int>
#define fst first
#define snd second
using namespace std;
const int LG = 17;
struct BIT
{
int N; ll A[100001];
BIT() {}
BIT(int size) {N = size;}
void init(int size) {N = size;}
void update(int x, ll val)
{
for (; x <= N; x += x & (-x)) {A[x] += val;}
}
ll query(int x)
{
ll ret = 0;
for (; x; x -= x & (-x)) {ret += A[x];}
return ret;
}
};
int N, Q, h[100001], L[100001], R[100001], sps[100001][LG], idx = 1;
BIT D1, D2;
ll DP[100001];
vector<int> adj[100001];
vector<ppi> solve[100001];
void prep(int u, int p)
{
h[u] = (p == -1) ? 0 : (h[p] + 1); L[u] = R[u] = idx++;
sps[u][0] = p;
for (int i = 1; i < LG; i++)
{
if (sps[u][i - 1] != -1) sps[u][i] = sps[sps[u][i - 1]][i - 1];
else sps[u][i] = -1;
}
for (auto v : adj[u])
{
if (v != p)
{
prep(v, u);
R[u] = max(R[u], R[v]);
}
}
}
inline int getLCA(int u, int v)
{
if (h[u] > h[v]) {swap(u, v);}
for (int i = 0; h[v] - h[u]; i++)
{
if ((h[v] - h[u]) & (1 << i)) {v = sps[v][i];}
}
if (u == v) {return u;}
for (int i = LG - 1; i >= 0; i--)
{
if (sps[u][i] != sps[v][i]) {u = sps[u][i]; v = sps[v][i];}
}
return sps[u][0];
}
void DFS(int u, int p)
{
ll childSum = 0;
for (auto v : adj[u])
{
if (v != p) {DFS(v, u); childSum += DP[v];}
}
DP[u] = childSum;
for (auto v : solve[u])
{
ll v1 = D1.query(L[v.fst.fst]) + D1.query(L[v.fst.snd]) - 2 * D1.query(L[u]);
ll v2 = D2.query(L[v.fst.fst]) + D2.query(L[v.fst.snd]) - 2 * D2.query(L[u]);
DP[u] = max(DP[u], v1 - v2 + childSum + v.snd);
}
D1.update(L[u], childSum);
D1.update(R[u] + 1, -childSum);
D2.update(L[u], DP[u]);
D2.update(R[u] + 1, -DP[u]);
}
int main()
{
ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N; D1.init(N); D2.init(N);
for (int i = 1; i < N; i++)
{
int u, v; cin >> u >> v;
adj[u - 1].push_back(v - 1);
adj[v - 1].push_back(u - 1);
}
prep(0, -1);
cin >> Q;
for (int i = 0; i < Q; i++)
{
int u, v, w; cin >> u >> v >> w; u--; v--;
solve[getLCA(u, v)].push_back({{u, v}, w});
}
DFS(0, -1);
cout << DP[0] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5248 KB |
Output is correct |
5 |
Correct |
129 ms |
19704 KB |
Output is correct |
6 |
Correct |
60 ms |
27256 KB |
Output is correct |
7 |
Correct |
121 ms |
24568 KB |
Output is correct |
8 |
Correct |
86 ms |
19960 KB |
Output is correct |
9 |
Correct |
120 ms |
23032 KB |
Output is correct |
10 |
Correct |
101 ms |
19960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5248 KB |
Output is correct |
4 |
Correct |
159 ms |
30584 KB |
Output is correct |
5 |
Correct |
161 ms |
30584 KB |
Output is correct |
6 |
Correct |
130 ms |
30584 KB |
Output is correct |
7 |
Correct |
151 ms |
30712 KB |
Output is correct |
8 |
Correct |
157 ms |
30712 KB |
Output is correct |
9 |
Correct |
131 ms |
30584 KB |
Output is correct |
10 |
Correct |
145 ms |
30584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5248 KB |
Output is correct |
4 |
Correct |
159 ms |
30584 KB |
Output is correct |
5 |
Correct |
161 ms |
30584 KB |
Output is correct |
6 |
Correct |
130 ms |
30584 KB |
Output is correct |
7 |
Correct |
151 ms |
30712 KB |
Output is correct |
8 |
Correct |
157 ms |
30712 KB |
Output is correct |
9 |
Correct |
131 ms |
30584 KB |
Output is correct |
10 |
Correct |
145 ms |
30584 KB |
Output is correct |
11 |
Correct |
17 ms |
6016 KB |
Output is correct |
12 |
Correct |
155 ms |
30920 KB |
Output is correct |
13 |
Correct |
148 ms |
30968 KB |
Output is correct |
14 |
Correct |
143 ms |
30968 KB |
Output is correct |
15 |
Correct |
148 ms |
30948 KB |
Output is correct |
16 |
Correct |
134 ms |
30964 KB |
Output is correct |
17 |
Correct |
153 ms |
30840 KB |
Output is correct |
18 |
Correct |
159 ms |
30908 KB |
Output is correct |
19 |
Correct |
129 ms |
30968 KB |
Output is correct |
20 |
Correct |
163 ms |
30856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
208 ms |
22424 KB |
Output is correct |
2 |
Correct |
117 ms |
30588 KB |
Output is correct |
3 |
Correct |
300 ms |
27512 KB |
Output is correct |
4 |
Correct |
139 ms |
22956 KB |
Output is correct |
5 |
Correct |
215 ms |
27012 KB |
Output is correct |
6 |
Correct |
137 ms |
22896 KB |
Output is correct |
7 |
Correct |
299 ms |
26804 KB |
Output is correct |
8 |
Correct |
195 ms |
22648 KB |
Output is correct |
9 |
Correct |
128 ms |
30584 KB |
Output is correct |
10 |
Correct |
250 ms |
25776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5248 KB |
Output is correct |
5 |
Correct |
129 ms |
19704 KB |
Output is correct |
6 |
Correct |
60 ms |
27256 KB |
Output is correct |
7 |
Correct |
121 ms |
24568 KB |
Output is correct |
8 |
Correct |
86 ms |
19960 KB |
Output is correct |
9 |
Correct |
120 ms |
23032 KB |
Output is correct |
10 |
Correct |
101 ms |
19960 KB |
Output is correct |
11 |
Correct |
6 ms |
5248 KB |
Output is correct |
12 |
Correct |
4 ms |
5248 KB |
Output is correct |
13 |
Correct |
4 ms |
5248 KB |
Output is correct |
14 |
Correct |
5 ms |
5248 KB |
Output is correct |
15 |
Correct |
4 ms |
5248 KB |
Output is correct |
16 |
Correct |
4 ms |
5248 KB |
Output is correct |
17 |
Correct |
4 ms |
5248 KB |
Output is correct |
18 |
Correct |
5 ms |
5248 KB |
Output is correct |
19 |
Correct |
5 ms |
5248 KB |
Output is correct |
20 |
Correct |
5 ms |
5376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5248 KB |
Output is correct |
5 |
Correct |
129 ms |
19704 KB |
Output is correct |
6 |
Correct |
60 ms |
27256 KB |
Output is correct |
7 |
Correct |
121 ms |
24568 KB |
Output is correct |
8 |
Correct |
86 ms |
19960 KB |
Output is correct |
9 |
Correct |
120 ms |
23032 KB |
Output is correct |
10 |
Correct |
101 ms |
19960 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
3 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5248 KB |
Output is correct |
14 |
Correct |
159 ms |
30584 KB |
Output is correct |
15 |
Correct |
161 ms |
30584 KB |
Output is correct |
16 |
Correct |
130 ms |
30584 KB |
Output is correct |
17 |
Correct |
151 ms |
30712 KB |
Output is correct |
18 |
Correct |
157 ms |
30712 KB |
Output is correct |
19 |
Correct |
131 ms |
30584 KB |
Output is correct |
20 |
Correct |
145 ms |
30584 KB |
Output is correct |
21 |
Correct |
17 ms |
6016 KB |
Output is correct |
22 |
Correct |
155 ms |
30920 KB |
Output is correct |
23 |
Correct |
148 ms |
30968 KB |
Output is correct |
24 |
Correct |
143 ms |
30968 KB |
Output is correct |
25 |
Correct |
148 ms |
30948 KB |
Output is correct |
26 |
Correct |
134 ms |
30964 KB |
Output is correct |
27 |
Correct |
153 ms |
30840 KB |
Output is correct |
28 |
Correct |
159 ms |
30908 KB |
Output is correct |
29 |
Correct |
129 ms |
30968 KB |
Output is correct |
30 |
Correct |
163 ms |
30856 KB |
Output is correct |
31 |
Correct |
208 ms |
22424 KB |
Output is correct |
32 |
Correct |
117 ms |
30588 KB |
Output is correct |
33 |
Correct |
300 ms |
27512 KB |
Output is correct |
34 |
Correct |
139 ms |
22956 KB |
Output is correct |
35 |
Correct |
215 ms |
27012 KB |
Output is correct |
36 |
Correct |
137 ms |
22896 KB |
Output is correct |
37 |
Correct |
299 ms |
26804 KB |
Output is correct |
38 |
Correct |
195 ms |
22648 KB |
Output is correct |
39 |
Correct |
128 ms |
30584 KB |
Output is correct |
40 |
Correct |
250 ms |
25776 KB |
Output is correct |
41 |
Correct |
6 ms |
5248 KB |
Output is correct |
42 |
Correct |
4 ms |
5248 KB |
Output is correct |
43 |
Correct |
4 ms |
5248 KB |
Output is correct |
44 |
Correct |
5 ms |
5248 KB |
Output is correct |
45 |
Correct |
4 ms |
5248 KB |
Output is correct |
46 |
Correct |
4 ms |
5248 KB |
Output is correct |
47 |
Correct |
4 ms |
5248 KB |
Output is correct |
48 |
Correct |
5 ms |
5248 KB |
Output is correct |
49 |
Correct |
5 ms |
5248 KB |
Output is correct |
50 |
Correct |
5 ms |
5376 KB |
Output is correct |
51 |
Correct |
211 ms |
23004 KB |
Output is correct |
52 |
Correct |
154 ms |
30840 KB |
Output is correct |
53 |
Correct |
267 ms |
26196 KB |
Output is correct |
54 |
Correct |
142 ms |
23092 KB |
Output is correct |
55 |
Correct |
198 ms |
22832 KB |
Output is correct |
56 |
Correct |
149 ms |
30968 KB |
Output is correct |
57 |
Correct |
216 ms |
26900 KB |
Output is correct |
58 |
Correct |
139 ms |
23084 KB |
Output is correct |
59 |
Correct |
206 ms |
23160 KB |
Output is correct |
60 |
Correct |
156 ms |
30896 KB |
Output is correct |
61 |
Correct |
239 ms |
27028 KB |
Output is correct |
62 |
Correct |
141 ms |
22860 KB |
Output is correct |
63 |
Correct |
204 ms |
22624 KB |
Output is correct |
64 |
Correct |
159 ms |
30944 KB |
Output is correct |
65 |
Correct |
268 ms |
26916 KB |
Output is correct |
66 |
Correct |
131 ms |
23220 KB |
Output is correct |
67 |
Correct |
206 ms |
22740 KB |
Output is correct |
68 |
Correct |
136 ms |
30840 KB |
Output is correct |
69 |
Correct |
213 ms |
25720 KB |
Output is correct |
70 |
Correct |
132 ms |
23260 KB |
Output is correct |