#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1<<18;
int n, q; ll ans[N];
vector<array<int, 3>> adj[N];
namespace Task1{
ll sum, cur;
void dfs1(int v, int p){
for(auto [u, d1, d2] : adj[v]){
sum += d2;
if(u==p) continue;
cur += d2;
dfs1(u, v);
}
}
void dfs2(int v, int p){
ans[1] = min(ans[1], sum-cur);
for(auto [u, d1, d2] : adj[v]){
if(u==p) continue;
cur -= d2; cur += d1;
dfs2(u, v);
cur += d2; cur -= d1;
}
}
void solve(){
dfs1(0, 0);
dfs2(0, 0);
}
}
namespace Task2{
ll sum, tot, dp[N], best[N];
int best1, best2;
void dfs1(int v, int p){
for(auto [u, d1, d2] : adj[v]){
sum += d2;
if(u==p) continue;
tot += d2;
dfs1(u, v);
}
}
void dfs2(int v, int p, ll val1, ll val2){
ll tmp = tot-val1+val2;
array<ll, 2> mx1 = {0, v}, mx2 = {0, v};
best[v] = v;
for(auto [u, d1, d2] : adj[v]){
if(u==p) continue;
dfs2(u, v, val1+d2, val2+d1);
if(dp[u]+d1>dp[v])
dp[v] = dp[u]+d1, best[v] = best[u];
if(array<ll, 2>{dp[u]+d1, best[u]}>mx1)
mx2 = mx1, mx1 = {dp[u]+d1, best[u]};
else if(array<ll, 2>{dp[u]+d1, best[u]}>mx2)
mx2 = {dp[u]+d1, best[u]};
}
if(sum-tmp-mx1[0]-mx2[0]<ans[2])
best1 = mx1[1], best2 = mx2[1], ans[2] = sum-tmp-mx1[0]-mx2[0];
}
void solve(){
dfs1(0, 0);
dfs2(0, 0, 0, 0);
}
}
namespace Task3{
int rt;
array<ll, 2> tree[N<<1]; ll lazy[N<<1];
int fa[N], idx[N];
int tin[N], tout[N], tid;
ll sum, cur, up[N];
bool vis[N];
void push(int v, int tl, int tr){
tree[v][0] += lazy[v];
if(tl!=tr)
lazy[v<<1] += lazy[v],
lazy[v<<1|1] += lazy[v];
lazy[v] = 0;
}
void upd(int l, int r, ll val, int v = 1, int tl = 0, int tr = N-1){
push(v, tl, tr);
if(tr<l || tl>r) return;
if(l<=tl && tr<=r){
lazy[v] += val;
push(v, tl, tr);
return;
}
int tm = (tl+tr)>>1;
upd(l, r, val, v<<1, tl, tm);
upd(l, r, val, v<<1|1, tm+1, tr);
tree[v] = max(tree[v<<1], tree[v<<1|1]);
}
void dfs1(int v, int p, ll dep){
fa[v] = p;
tin[v] = ++tid;
idx[tid] = v;
upd(tin[v], tin[v], dep);
for(auto [u, d1, d2] : adj[v]){
sum += d2;
if(u==p) continue;
cur += d2;
up[u] = d1;
dfs1(u, v, dep+d1);
}
tout[v] = tid;
}
void solve(){
for(int i = N; i<2*N; ++i)
tree[i][1] = i-N;
dfs1(rt, rt, 0);
vis[rt] = 1;
for(int i = 2; i<=n; ++i){
ans[i] = i==2?sum-cur:ans[i-1];
int v = idx[tree[1][1]];
ans[i] -= tree[1][0];
while(!vis[v]){
vis[v] = 1;
upd(tin[v], tout[v], -up[v]);
v = fa[v];
}
}
}
}
int main(){
// freopen("a.in", "r", stdin);
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i = 0; i<n-1; ++i){
int a, b, c, d;
cin >> a >> b >> c >> d;
--a; --b;
adj[a].push_back({b, c, d});
adj[b].push_back({a, d, c});
}
memset(ans, 63, sizeof ans);
Task1::solve();
Task2::solve();
Task3::rt = Task2::best1;
Task3::solve();
cin >> q;
while(q--){
int x; cin >> x;
cout << ans[x] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12748 KB |
Output is correct |
2 |
Correct |
6 ms |
12748 KB |
Output is correct |
3 |
Correct |
6 ms |
12748 KB |
Output is correct |
4 |
Correct |
6 ms |
12724 KB |
Output is correct |
5 |
Correct |
7 ms |
12728 KB |
Output is correct |
6 |
Correct |
6 ms |
12748 KB |
Output is correct |
7 |
Correct |
6 ms |
12748 KB |
Output is correct |
8 |
Correct |
6 ms |
12748 KB |
Output is correct |
9 |
Correct |
6 ms |
12748 KB |
Output is correct |
10 |
Correct |
6 ms |
12748 KB |
Output is correct |
11 |
Correct |
6 ms |
12748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12748 KB |
Output is correct |
2 |
Correct |
423 ms |
35712 KB |
Output is correct |
3 |
Correct |
535 ms |
56664 KB |
Output is correct |
4 |
Correct |
386 ms |
35796 KB |
Output is correct |
5 |
Correct |
467 ms |
35504 KB |
Output is correct |
6 |
Correct |
450 ms |
38840 KB |
Output is correct |
7 |
Correct |
372 ms |
35436 KB |
Output is correct |
8 |
Correct |
502 ms |
57496 KB |
Output is correct |
9 |
Correct |
346 ms |
34544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12752 KB |
Output is correct |
2 |
Correct |
435 ms |
35836 KB |
Output is correct |
3 |
Correct |
575 ms |
60612 KB |
Output is correct |
4 |
Correct |
429 ms |
35864 KB |
Output is correct |
5 |
Correct |
418 ms |
35704 KB |
Output is correct |
6 |
Correct |
453 ms |
39492 KB |
Output is correct |
7 |
Correct |
333 ms |
35916 KB |
Output is correct |
8 |
Correct |
504 ms |
50060 KB |
Output is correct |
9 |
Correct |
320 ms |
34540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12748 KB |
Output is correct |
2 |
Correct |
6 ms |
12748 KB |
Output is correct |
3 |
Correct |
6 ms |
12748 KB |
Output is correct |
4 |
Correct |
6 ms |
12724 KB |
Output is correct |
5 |
Correct |
7 ms |
12728 KB |
Output is correct |
6 |
Correct |
6 ms |
12748 KB |
Output is correct |
7 |
Correct |
6 ms |
12748 KB |
Output is correct |
8 |
Correct |
6 ms |
12748 KB |
Output is correct |
9 |
Correct |
6 ms |
12748 KB |
Output is correct |
10 |
Correct |
6 ms |
12748 KB |
Output is correct |
11 |
Correct |
6 ms |
12748 KB |
Output is correct |
12 |
Correct |
6 ms |
12748 KB |
Output is correct |
13 |
Correct |
8 ms |
13004 KB |
Output is correct |
14 |
Correct |
8 ms |
13072 KB |
Output is correct |
15 |
Correct |
9 ms |
13016 KB |
Output is correct |
16 |
Correct |
8 ms |
13004 KB |
Output is correct |
17 |
Correct |
8 ms |
13004 KB |
Output is correct |
18 |
Correct |
9 ms |
13004 KB |
Output is correct |
19 |
Correct |
11 ms |
12964 KB |
Output is correct |
20 |
Correct |
10 ms |
13024 KB |
Output is correct |
21 |
Correct |
9 ms |
12944 KB |
Output is correct |
22 |
Correct |
8 ms |
12912 KB |
Output is correct |
23 |
Correct |
8 ms |
13004 KB |
Output is correct |
24 |
Correct |
8 ms |
13004 KB |
Output is correct |
25 |
Correct |
11 ms |
13152 KB |
Output is correct |
26 |
Correct |
8 ms |
13004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12748 KB |
Output is correct |
2 |
Correct |
423 ms |
35712 KB |
Output is correct |
3 |
Correct |
535 ms |
56664 KB |
Output is correct |
4 |
Correct |
386 ms |
35796 KB |
Output is correct |
5 |
Correct |
467 ms |
35504 KB |
Output is correct |
6 |
Correct |
450 ms |
38840 KB |
Output is correct |
7 |
Correct |
372 ms |
35436 KB |
Output is correct |
8 |
Correct |
502 ms |
57496 KB |
Output is correct |
9 |
Correct |
346 ms |
34544 KB |
Output is correct |
10 |
Correct |
6 ms |
12752 KB |
Output is correct |
11 |
Correct |
435 ms |
35836 KB |
Output is correct |
12 |
Correct |
575 ms |
60612 KB |
Output is correct |
13 |
Correct |
429 ms |
35864 KB |
Output is correct |
14 |
Correct |
418 ms |
35704 KB |
Output is correct |
15 |
Correct |
453 ms |
39492 KB |
Output is correct |
16 |
Correct |
333 ms |
35916 KB |
Output is correct |
17 |
Correct |
504 ms |
50060 KB |
Output is correct |
18 |
Correct |
320 ms |
34540 KB |
Output is correct |
19 |
Correct |
5 ms |
12748 KB |
Output is correct |
20 |
Correct |
470 ms |
35872 KB |
Output is correct |
21 |
Correct |
483 ms |
66676 KB |
Output is correct |
22 |
Correct |
410 ms |
40288 KB |
Output is correct |
23 |
Correct |
440 ms |
41712 KB |
Output is correct |
24 |
Correct |
461 ms |
40448 KB |
Output is correct |
25 |
Correct |
455 ms |
41668 KB |
Output is correct |
26 |
Correct |
436 ms |
40420 KB |
Output is correct |
27 |
Correct |
453 ms |
41520 KB |
Output is correct |
28 |
Correct |
476 ms |
44544 KB |
Output is correct |
29 |
Correct |
468 ms |
41636 KB |
Output is correct |
30 |
Correct |
406 ms |
40792 KB |
Output is correct |
31 |
Correct |
393 ms |
41480 KB |
Output is correct |
32 |
Correct |
590 ms |
56604 KB |
Output is correct |
33 |
Correct |
344 ms |
40536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12748 KB |
Output is correct |
2 |
Correct |
6 ms |
12748 KB |
Output is correct |
3 |
Correct |
6 ms |
12748 KB |
Output is correct |
4 |
Correct |
6 ms |
12724 KB |
Output is correct |
5 |
Correct |
7 ms |
12728 KB |
Output is correct |
6 |
Correct |
6 ms |
12748 KB |
Output is correct |
7 |
Correct |
6 ms |
12748 KB |
Output is correct |
8 |
Correct |
6 ms |
12748 KB |
Output is correct |
9 |
Correct |
6 ms |
12748 KB |
Output is correct |
10 |
Correct |
6 ms |
12748 KB |
Output is correct |
11 |
Correct |
6 ms |
12748 KB |
Output is correct |
12 |
Correct |
6 ms |
12748 KB |
Output is correct |
13 |
Correct |
423 ms |
35712 KB |
Output is correct |
14 |
Correct |
535 ms |
56664 KB |
Output is correct |
15 |
Correct |
386 ms |
35796 KB |
Output is correct |
16 |
Correct |
467 ms |
35504 KB |
Output is correct |
17 |
Correct |
450 ms |
38840 KB |
Output is correct |
18 |
Correct |
372 ms |
35436 KB |
Output is correct |
19 |
Correct |
502 ms |
57496 KB |
Output is correct |
20 |
Correct |
346 ms |
34544 KB |
Output is correct |
21 |
Correct |
6 ms |
12752 KB |
Output is correct |
22 |
Correct |
435 ms |
35836 KB |
Output is correct |
23 |
Correct |
575 ms |
60612 KB |
Output is correct |
24 |
Correct |
429 ms |
35864 KB |
Output is correct |
25 |
Correct |
418 ms |
35704 KB |
Output is correct |
26 |
Correct |
453 ms |
39492 KB |
Output is correct |
27 |
Correct |
333 ms |
35916 KB |
Output is correct |
28 |
Correct |
504 ms |
50060 KB |
Output is correct |
29 |
Correct |
320 ms |
34540 KB |
Output is correct |
30 |
Correct |
6 ms |
12748 KB |
Output is correct |
31 |
Correct |
8 ms |
13004 KB |
Output is correct |
32 |
Correct |
8 ms |
13072 KB |
Output is correct |
33 |
Correct |
9 ms |
13016 KB |
Output is correct |
34 |
Correct |
8 ms |
13004 KB |
Output is correct |
35 |
Correct |
8 ms |
13004 KB |
Output is correct |
36 |
Correct |
9 ms |
13004 KB |
Output is correct |
37 |
Correct |
11 ms |
12964 KB |
Output is correct |
38 |
Correct |
10 ms |
13024 KB |
Output is correct |
39 |
Correct |
9 ms |
12944 KB |
Output is correct |
40 |
Correct |
8 ms |
12912 KB |
Output is correct |
41 |
Correct |
8 ms |
13004 KB |
Output is correct |
42 |
Correct |
8 ms |
13004 KB |
Output is correct |
43 |
Correct |
11 ms |
13152 KB |
Output is correct |
44 |
Correct |
8 ms |
13004 KB |
Output is correct |
45 |
Correct |
5 ms |
12748 KB |
Output is correct |
46 |
Correct |
470 ms |
35872 KB |
Output is correct |
47 |
Correct |
483 ms |
66676 KB |
Output is correct |
48 |
Correct |
410 ms |
40288 KB |
Output is correct |
49 |
Correct |
440 ms |
41712 KB |
Output is correct |
50 |
Correct |
461 ms |
40448 KB |
Output is correct |
51 |
Correct |
455 ms |
41668 KB |
Output is correct |
52 |
Correct |
436 ms |
40420 KB |
Output is correct |
53 |
Correct |
453 ms |
41520 KB |
Output is correct |
54 |
Correct |
476 ms |
44544 KB |
Output is correct |
55 |
Correct |
468 ms |
41636 KB |
Output is correct |
56 |
Correct |
406 ms |
40792 KB |
Output is correct |
57 |
Correct |
393 ms |
41480 KB |
Output is correct |
58 |
Correct |
590 ms |
56604 KB |
Output is correct |
59 |
Correct |
344 ms |
40536 KB |
Output is correct |
60 |
Correct |
6 ms |
12748 KB |
Output is correct |
61 |
Correct |
491 ms |
44336 KB |
Output is correct |
62 |
Correct |
535 ms |
64364 KB |
Output is correct |
63 |
Correct |
423 ms |
42932 KB |
Output is correct |
64 |
Correct |
514 ms |
44252 KB |
Output is correct |
65 |
Correct |
458 ms |
42840 KB |
Output is correct |
66 |
Correct |
578 ms |
44376 KB |
Output is correct |
67 |
Correct |
504 ms |
42916 KB |
Output is correct |
68 |
Correct |
508 ms |
44268 KB |
Output is correct |
69 |
Correct |
531 ms |
46980 KB |
Output is correct |
70 |
Correct |
537 ms |
44056 KB |
Output is correct |
71 |
Correct |
525 ms |
43540 KB |
Output is correct |
72 |
Correct |
433 ms |
44804 KB |
Output is correct |
73 |
Correct |
535 ms |
56612 KB |
Output is correct |
74 |
Correct |
407 ms |
44636 KB |
Output is correct |