#include <bits/stdc++.h>
#define X first
#define Y second
#define MP make_pair
#define ll long long
using namespace std;
const int N = 3e5 + 12;
const ll INF = 1e18;
int n, q, lca[17][N], T, timer, tin[N], tout[N];
ll dp[N], sp[N], fw[2][N];
vector< int > g[N];
vector< pair< pair<int, int>, int > > ask[N];
void upd(int v, ll val){
for(;v <= timer;v |= v + 1)
fw[T][v] += val;
}
ll get(int v){
ll res = 0;
for(;v >= 0;v = (v & (v + 1)) - 1)
res += fw[T][v];
return res;
}
void dfs(int v, int pr){
lca[0][v] = pr;
tin[v] = ++timer;
for(int to: g[v]){
if(to != pr){
dfs(to, v);
}
}
tout[v] = ++timer;
}
bool check(int x, int y){
if(tin[x] <= tin[y] && tout[y] <= tout[x])
return 1;
return 0;
}
int get_pr(int x, int y){
if(check(x, y))
return x;
else if(check(y, x))
return y;
for(int i = 16;i >= 0;i--){
if(!check(lca[i][x], y))
x = lca[i][x];
}
return lca[0][x];
}
void dfs2(int v, int pr){
for(int to: g[v]){
if(to != pr){
dfs2(to, v);
sp[v] += dp[to];
}
}
dp[v] = sp[v];
T = 0, upd(tin[v], sp[v]), upd(tout[v], -sp[v]);
for(pair< pair<int, int>, int > tmp: ask[v]){
int x = tmp.X.X, y = tmp.X.Y;
ll cur = tmp.Y;
T = 0, cur += get(tin[x]) + get(tin[y]) - 2 * get(tin[v]) + sp[v];
T = 1;
if(x != v)
cur -= get(tin[x]) - get(tin[v]);
if(y != v)
cur -= get(tin[y]) - get(tin[v]);
dp[v] = max(dp[v], cur);
}
T = 1, upd(tin[v], dp[v]), upd(tout[v], -dp[v]);
}
int main () {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1, x, y;i < n;i++){
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1, 1);
for(int i = 1;i < 17;i++){
for(int j = 1;j <= n;j++){
lca[i][j] = lca[i - 1][lca[i - 1][j]];
}
}
cin >> q;
for(int i = 1, x, y, z;i <= q;i++){
cin >> x >> y >> z;
int v = get_pr(x, y);
ask[v].push_back(MP(MP(x, y), z));
}
dfs2(1, 1);
cout << dp[1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14572 KB |
Output is correct |
2 |
Correct |
10 ms |
14572 KB |
Output is correct |
3 |
Correct |
10 ms |
14572 KB |
Output is correct |
4 |
Correct |
11 ms |
14720 KB |
Output is correct |
5 |
Correct |
108 ms |
30188 KB |
Output is correct |
6 |
Correct |
65 ms |
40448 KB |
Output is correct |
7 |
Correct |
90 ms |
37100 KB |
Output is correct |
8 |
Correct |
83 ms |
30828 KB |
Output is correct |
9 |
Correct |
91 ms |
35308 KB |
Output is correct |
10 |
Correct |
84 ms |
30828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14572 KB |
Output is correct |
2 |
Correct |
10 ms |
14572 KB |
Output is correct |
3 |
Correct |
12 ms |
14828 KB |
Output is correct |
4 |
Correct |
108 ms |
41216 KB |
Output is correct |
5 |
Correct |
109 ms |
43756 KB |
Output is correct |
6 |
Correct |
108 ms |
43756 KB |
Output is correct |
7 |
Correct |
109 ms |
43756 KB |
Output is correct |
8 |
Correct |
114 ms |
43676 KB |
Output is correct |
9 |
Correct |
110 ms |
44012 KB |
Output is correct |
10 |
Correct |
110 ms |
43752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14572 KB |
Output is correct |
2 |
Correct |
10 ms |
14572 KB |
Output is correct |
3 |
Correct |
12 ms |
14828 KB |
Output is correct |
4 |
Correct |
108 ms |
41216 KB |
Output is correct |
5 |
Correct |
109 ms |
43756 KB |
Output is correct |
6 |
Correct |
108 ms |
43756 KB |
Output is correct |
7 |
Correct |
109 ms |
43756 KB |
Output is correct |
8 |
Correct |
114 ms |
43676 KB |
Output is correct |
9 |
Correct |
110 ms |
44012 KB |
Output is correct |
10 |
Correct |
110 ms |
43752 KB |
Output is correct |
11 |
Correct |
18 ms |
15596 KB |
Output is correct |
12 |
Correct |
115 ms |
44012 KB |
Output is correct |
13 |
Correct |
113 ms |
44012 KB |
Output is correct |
14 |
Correct |
111 ms |
44012 KB |
Output is correct |
15 |
Correct |
113 ms |
44012 KB |
Output is correct |
16 |
Correct |
113 ms |
44012 KB |
Output is correct |
17 |
Correct |
113 ms |
44140 KB |
Output is correct |
18 |
Correct |
112 ms |
44012 KB |
Output is correct |
19 |
Correct |
113 ms |
44040 KB |
Output is correct |
20 |
Correct |
114 ms |
44012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
192 ms |
31524 KB |
Output is correct |
2 |
Correct |
110 ms |
43756 KB |
Output is correct |
3 |
Correct |
254 ms |
40036 KB |
Output is correct |
4 |
Correct |
165 ms |
33700 KB |
Output is correct |
5 |
Correct |
171 ms |
39404 KB |
Output is correct |
6 |
Correct |
161 ms |
33636 KB |
Output is correct |
7 |
Correct |
212 ms |
39208 KB |
Output is correct |
8 |
Correct |
226 ms |
34156 KB |
Output is correct |
9 |
Correct |
112 ms |
43756 KB |
Output is correct |
10 |
Correct |
207 ms |
38004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14572 KB |
Output is correct |
2 |
Correct |
10 ms |
14572 KB |
Output is correct |
3 |
Correct |
10 ms |
14572 KB |
Output is correct |
4 |
Correct |
11 ms |
14720 KB |
Output is correct |
5 |
Correct |
108 ms |
30188 KB |
Output is correct |
6 |
Correct |
65 ms |
40448 KB |
Output is correct |
7 |
Correct |
90 ms |
37100 KB |
Output is correct |
8 |
Correct |
83 ms |
30828 KB |
Output is correct |
9 |
Correct |
91 ms |
35308 KB |
Output is correct |
10 |
Correct |
84 ms |
30828 KB |
Output is correct |
11 |
Correct |
11 ms |
14700 KB |
Output is correct |
12 |
Correct |
10 ms |
14828 KB |
Output is correct |
13 |
Correct |
11 ms |
14828 KB |
Output is correct |
14 |
Correct |
12 ms |
14828 KB |
Output is correct |
15 |
Correct |
11 ms |
14700 KB |
Output is correct |
16 |
Correct |
11 ms |
14828 KB |
Output is correct |
17 |
Correct |
11 ms |
14828 KB |
Output is correct |
18 |
Correct |
11 ms |
14828 KB |
Output is correct |
19 |
Correct |
11 ms |
14828 KB |
Output is correct |
20 |
Correct |
11 ms |
14828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14572 KB |
Output is correct |
2 |
Correct |
10 ms |
14572 KB |
Output is correct |
3 |
Correct |
10 ms |
14572 KB |
Output is correct |
4 |
Correct |
11 ms |
14720 KB |
Output is correct |
5 |
Correct |
108 ms |
30188 KB |
Output is correct |
6 |
Correct |
65 ms |
40448 KB |
Output is correct |
7 |
Correct |
90 ms |
37100 KB |
Output is correct |
8 |
Correct |
83 ms |
30828 KB |
Output is correct |
9 |
Correct |
91 ms |
35308 KB |
Output is correct |
10 |
Correct |
84 ms |
30828 KB |
Output is correct |
11 |
Correct |
10 ms |
14572 KB |
Output is correct |
12 |
Correct |
10 ms |
14572 KB |
Output is correct |
13 |
Correct |
12 ms |
14828 KB |
Output is correct |
14 |
Correct |
108 ms |
41216 KB |
Output is correct |
15 |
Correct |
109 ms |
43756 KB |
Output is correct |
16 |
Correct |
108 ms |
43756 KB |
Output is correct |
17 |
Correct |
109 ms |
43756 KB |
Output is correct |
18 |
Correct |
114 ms |
43676 KB |
Output is correct |
19 |
Correct |
110 ms |
44012 KB |
Output is correct |
20 |
Correct |
110 ms |
43752 KB |
Output is correct |
21 |
Correct |
18 ms |
15596 KB |
Output is correct |
22 |
Correct |
115 ms |
44012 KB |
Output is correct |
23 |
Correct |
113 ms |
44012 KB |
Output is correct |
24 |
Correct |
111 ms |
44012 KB |
Output is correct |
25 |
Correct |
113 ms |
44012 KB |
Output is correct |
26 |
Correct |
113 ms |
44012 KB |
Output is correct |
27 |
Correct |
113 ms |
44140 KB |
Output is correct |
28 |
Correct |
112 ms |
44012 KB |
Output is correct |
29 |
Correct |
113 ms |
44040 KB |
Output is correct |
30 |
Correct |
114 ms |
44012 KB |
Output is correct |
31 |
Correct |
192 ms |
31524 KB |
Output is correct |
32 |
Correct |
110 ms |
43756 KB |
Output is correct |
33 |
Correct |
254 ms |
40036 KB |
Output is correct |
34 |
Correct |
165 ms |
33700 KB |
Output is correct |
35 |
Correct |
171 ms |
39404 KB |
Output is correct |
36 |
Correct |
161 ms |
33636 KB |
Output is correct |
37 |
Correct |
212 ms |
39208 KB |
Output is correct |
38 |
Correct |
226 ms |
34156 KB |
Output is correct |
39 |
Correct |
112 ms |
43756 KB |
Output is correct |
40 |
Correct |
207 ms |
38004 KB |
Output is correct |
41 |
Correct |
11 ms |
14700 KB |
Output is correct |
42 |
Correct |
10 ms |
14828 KB |
Output is correct |
43 |
Correct |
11 ms |
14828 KB |
Output is correct |
44 |
Correct |
12 ms |
14828 KB |
Output is correct |
45 |
Correct |
11 ms |
14700 KB |
Output is correct |
46 |
Correct |
11 ms |
14828 KB |
Output is correct |
47 |
Correct |
11 ms |
14828 KB |
Output is correct |
48 |
Correct |
11 ms |
14828 KB |
Output is correct |
49 |
Correct |
11 ms |
14828 KB |
Output is correct |
50 |
Correct |
11 ms |
14828 KB |
Output is correct |
51 |
Correct |
190 ms |
34608 KB |
Output is correct |
52 |
Correct |
116 ms |
44012 KB |
Output is correct |
53 |
Correct |
213 ms |
38436 KB |
Output is correct |
54 |
Correct |
143 ms |
33960 KB |
Output is correct |
55 |
Correct |
201 ms |
34340 KB |
Output is correct |
56 |
Correct |
111 ms |
44012 KB |
Output is correct |
57 |
Correct |
179 ms |
39276 KB |
Output is correct |
58 |
Correct |
170 ms |
33860 KB |
Output is correct |
59 |
Correct |
189 ms |
34660 KB |
Output is correct |
60 |
Correct |
113 ms |
44012 KB |
Output is correct |
61 |
Correct |
176 ms |
39228 KB |
Output is correct |
62 |
Correct |
167 ms |
33608 KB |
Output is correct |
63 |
Correct |
196 ms |
34104 KB |
Output is correct |
64 |
Correct |
115 ms |
44012 KB |
Output is correct |
65 |
Correct |
210 ms |
39076 KB |
Output is correct |
66 |
Correct |
150 ms |
33960 KB |
Output is correct |
67 |
Correct |
197 ms |
34144 KB |
Output is correct |
68 |
Correct |
119 ms |
44012 KB |
Output is correct |
69 |
Correct |
182 ms |
37740 KB |
Output is correct |
70 |
Correct |
170 ms |
34000 KB |
Output is correct |