//#pragma optimization_level 3
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordset;
*/
#define fr first
#define sc second
#define vec vector
#define pb push_back
#define pii pair<int, int>
#define forn(x,y) for(ll x = 1 ; x <= y ; ++x)
#define all(x) (x).begin(),(x).end()
#define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef pair<ll,ll> pll;
const int nmax = 100005;
const ll linf = 1e17;
const ll mod = 1e9+7;
const int inf = 1e9+7;
const ll lim = 1e4;
int n,m;
vec <int> g[nmax];
int tin[nmax], tout[nmax],tt=1;
int up[nmax][20];
vec < pair < pii , ll > > pth[nmax];
vec < int > vc;
void dfs(int nod, int par){
up[nod][0] = par;
tin[nod] = ++tt;
for(int j : g[nod]){
if(j != par)
dfs(j,nod);
}
tout[nod] = ++tt;
}
bool anc(int a, int b){
return ( tin[a] <= tin[b] && tout[a] >= tout[b]);
}
int lca(int a, int b){
if(anc(a,b))return a;
int p = a;
for(int i = 16 ; i >= 0; i--){
if(!anc(up[p][i],b)) p = up[p][i];
}
return up[p][0];
}
struct {
ll bit[2*nmax];
ll query(int pos){
ll r = 0;
for(;pos > 0; pos -= pos&-pos){
r += bit[pos];
}
return r;
}
void update(int pos , int val){
for(;pos <= tt ; pos += pos&-pos){
bit[pos] += val;
}
}
ll range_query(int l , int r){
return query(r) - query(l-1);
}
}bt1,bt2;
ll dp[nmax];
void dfs2(int nod, int par){
for(int j : g[nod]){
if(j != par)
dfs2(j,nod);
}
ll r = 0;
for(int j : g[nod]){
if(j == par)continue;
r += dp[j];
}
ll rez = r;
bt1.update(tin[nod],r);
bt2.update(tout[nod],r);
for(auto path : pth[nod]){
rez = max( rez, bt1.range_query(tin[nod],tin[path.fr.fr]) - bt2.range_query(tin[nod],tin[path.fr.fr])
+bt1.range_query(tin[nod],tin[path.fr.sc]) - bt2.range_query(tin[nod],tin[path.fr.sc]) - r + path.sc );
}
dp[nod] = rez;
bt1.update(tin[nod],-dp[nod]);
bt2.update(tout[nod],-dp[nod]);
}
int main(){
fast;
cin >> n;
forn(i,n-1){
int a, b;
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
dfs(1,1);
for(int i = 1 ; i < 20 ; i++){
forn(ii,n){
up[ii][i] = up[up[ii][i-1]][i-1];
}
}
cin >> m;
forn(i,m){
int a, b, c;
cin >> a >> b >> c;
pth[lca(a,b)].pb({{a,b},c});
}
dfs2(1,1);
cout << dp[1] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5248 KB |
Output is correct |
5 |
Correct |
117 ms |
21368 KB |
Output is correct |
6 |
Correct |
69 ms |
27616 KB |
Output is correct |
7 |
Correct |
86 ms |
25848 KB |
Output is correct |
8 |
Correct |
79 ms |
21624 KB |
Output is correct |
9 |
Correct |
96 ms |
24568 KB |
Output is correct |
10 |
Correct |
88 ms |
21752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5024 KB |
Output is correct |
3 |
Correct |
8 ms |
5248 KB |
Output is correct |
4 |
Correct |
149 ms |
29560 KB |
Output is correct |
5 |
Correct |
166 ms |
30076 KB |
Output is correct |
6 |
Correct |
134 ms |
30200 KB |
Output is correct |
7 |
Correct |
152 ms |
30072 KB |
Output is correct |
8 |
Correct |
145 ms |
30200 KB |
Output is correct |
9 |
Correct |
146 ms |
30112 KB |
Output is correct |
10 |
Correct |
149 ms |
30072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5024 KB |
Output is correct |
3 |
Correct |
8 ms |
5248 KB |
Output is correct |
4 |
Correct |
149 ms |
29560 KB |
Output is correct |
5 |
Correct |
166 ms |
30076 KB |
Output is correct |
6 |
Correct |
134 ms |
30200 KB |
Output is correct |
7 |
Correct |
152 ms |
30072 KB |
Output is correct |
8 |
Correct |
145 ms |
30200 KB |
Output is correct |
9 |
Correct |
146 ms |
30112 KB |
Output is correct |
10 |
Correct |
149 ms |
30072 KB |
Output is correct |
11 |
Correct |
18 ms |
6144 KB |
Output is correct |
12 |
Correct |
153 ms |
30284 KB |
Output is correct |
13 |
Correct |
149 ms |
30072 KB |
Output is correct |
14 |
Correct |
135 ms |
30072 KB |
Output is correct |
15 |
Correct |
143 ms |
30072 KB |
Output is correct |
16 |
Correct |
139 ms |
30100 KB |
Output is correct |
17 |
Correct |
142 ms |
30072 KB |
Output is correct |
18 |
Correct |
142 ms |
30072 KB |
Output is correct |
19 |
Correct |
133 ms |
30072 KB |
Output is correct |
20 |
Correct |
145 ms |
30016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
161 ms |
22896 KB |
Output is correct |
2 |
Correct |
153 ms |
30176 KB |
Output is correct |
3 |
Correct |
242 ms |
28512 KB |
Output is correct |
4 |
Correct |
131 ms |
23784 KB |
Output is correct |
5 |
Correct |
201 ms |
27984 KB |
Output is correct |
6 |
Correct |
133 ms |
23644 KB |
Output is correct |
7 |
Correct |
205 ms |
27584 KB |
Output is correct |
8 |
Correct |
205 ms |
23584 KB |
Output is correct |
9 |
Correct |
127 ms |
30072 KB |
Output is correct |
10 |
Correct |
204 ms |
26344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5248 KB |
Output is correct |
5 |
Correct |
117 ms |
21368 KB |
Output is correct |
6 |
Correct |
69 ms |
27616 KB |
Output is correct |
7 |
Correct |
86 ms |
25848 KB |
Output is correct |
8 |
Correct |
79 ms |
21624 KB |
Output is correct |
9 |
Correct |
96 ms |
24568 KB |
Output is correct |
10 |
Correct |
88 ms |
21752 KB |
Output is correct |
11 |
Correct |
9 ms |
5248 KB |
Output is correct |
12 |
Correct |
8 ms |
5376 KB |
Output is correct |
13 |
Correct |
9 ms |
5248 KB |
Output is correct |
14 |
Correct |
9 ms |
5248 KB |
Output is correct |
15 |
Correct |
9 ms |
5248 KB |
Output is correct |
16 |
Correct |
8 ms |
5248 KB |
Output is correct |
17 |
Correct |
8 ms |
5248 KB |
Output is correct |
18 |
Correct |
8 ms |
5376 KB |
Output is correct |
19 |
Correct |
9 ms |
5248 KB |
Output is correct |
20 |
Correct |
8 ms |
5376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5248 KB |
Output is correct |
5 |
Correct |
117 ms |
21368 KB |
Output is correct |
6 |
Correct |
69 ms |
27616 KB |
Output is correct |
7 |
Correct |
86 ms |
25848 KB |
Output is correct |
8 |
Correct |
79 ms |
21624 KB |
Output is correct |
9 |
Correct |
96 ms |
24568 KB |
Output is correct |
10 |
Correct |
88 ms |
21752 KB |
Output is correct |
11 |
Correct |
7 ms |
5120 KB |
Output is correct |
12 |
Correct |
8 ms |
5024 KB |
Output is correct |
13 |
Correct |
8 ms |
5248 KB |
Output is correct |
14 |
Correct |
149 ms |
29560 KB |
Output is correct |
15 |
Correct |
166 ms |
30076 KB |
Output is correct |
16 |
Correct |
134 ms |
30200 KB |
Output is correct |
17 |
Correct |
152 ms |
30072 KB |
Output is correct |
18 |
Correct |
145 ms |
30200 KB |
Output is correct |
19 |
Correct |
146 ms |
30112 KB |
Output is correct |
20 |
Correct |
149 ms |
30072 KB |
Output is correct |
21 |
Correct |
18 ms |
6144 KB |
Output is correct |
22 |
Correct |
153 ms |
30284 KB |
Output is correct |
23 |
Correct |
149 ms |
30072 KB |
Output is correct |
24 |
Correct |
135 ms |
30072 KB |
Output is correct |
25 |
Correct |
143 ms |
30072 KB |
Output is correct |
26 |
Correct |
139 ms |
30100 KB |
Output is correct |
27 |
Correct |
142 ms |
30072 KB |
Output is correct |
28 |
Correct |
142 ms |
30072 KB |
Output is correct |
29 |
Correct |
133 ms |
30072 KB |
Output is correct |
30 |
Correct |
145 ms |
30016 KB |
Output is correct |
31 |
Correct |
161 ms |
22896 KB |
Output is correct |
32 |
Correct |
153 ms |
30176 KB |
Output is correct |
33 |
Correct |
242 ms |
28512 KB |
Output is correct |
34 |
Correct |
131 ms |
23784 KB |
Output is correct |
35 |
Correct |
201 ms |
27984 KB |
Output is correct |
36 |
Correct |
133 ms |
23644 KB |
Output is correct |
37 |
Correct |
205 ms |
27584 KB |
Output is correct |
38 |
Correct |
205 ms |
23584 KB |
Output is correct |
39 |
Correct |
127 ms |
30072 KB |
Output is correct |
40 |
Correct |
204 ms |
26344 KB |
Output is correct |
41 |
Correct |
9 ms |
5248 KB |
Output is correct |
42 |
Correct |
8 ms |
5376 KB |
Output is correct |
43 |
Correct |
9 ms |
5248 KB |
Output is correct |
44 |
Correct |
9 ms |
5248 KB |
Output is correct |
45 |
Correct |
9 ms |
5248 KB |
Output is correct |
46 |
Correct |
8 ms |
5248 KB |
Output is correct |
47 |
Correct |
8 ms |
5248 KB |
Output is correct |
48 |
Correct |
8 ms |
5376 KB |
Output is correct |
49 |
Correct |
9 ms |
5248 KB |
Output is correct |
50 |
Correct |
8 ms |
5376 KB |
Output is correct |
51 |
Correct |
184 ms |
23668 KB |
Output is correct |
52 |
Correct |
152 ms |
30112 KB |
Output is correct |
53 |
Correct |
202 ms |
26600 KB |
Output is correct |
54 |
Correct |
136 ms |
23796 KB |
Output is correct |
55 |
Correct |
166 ms |
23276 KB |
Output is correct |
56 |
Correct |
180 ms |
30072 KB |
Output is correct |
57 |
Correct |
186 ms |
27252 KB |
Output is correct |
58 |
Correct |
139 ms |
23660 KB |
Output is correct |
59 |
Correct |
189 ms |
23792 KB |
Output is correct |
60 |
Correct |
146 ms |
30072 KB |
Output is correct |
61 |
Correct |
262 ms |
27252 KB |
Output is correct |
62 |
Correct |
161 ms |
23460 KB |
Output is correct |
63 |
Correct |
199 ms |
23072 KB |
Output is correct |
64 |
Correct |
157 ms |
30072 KB |
Output is correct |
65 |
Correct |
208 ms |
26988 KB |
Output is correct |
66 |
Correct |
141 ms |
23824 KB |
Output is correct |
67 |
Correct |
168 ms |
23348 KB |
Output is correct |
68 |
Correct |
145 ms |
30048 KB |
Output is correct |
69 |
Correct |
217 ms |
26100 KB |
Output is correct |
70 |
Correct |
162 ms |
23588 KB |
Output is correct |