#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std;
#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair <int, int>
#define ull unsigned long long
#define int long long
// #define int unsigned long long
const ll inf = 1e9 + 7;
const ll weirdMod = 998244353;
struct node {
int mx, nd;
} t[800040];
pii rts;
bool used[200005];
vector<pair<int, pii>> G[200005];
int n, q, ans[200005], dep[200005], dp[200005], p[200005], val[200005], cst[200005], in[200005], out[200005], lazy[800040], rev[200005], mx, tim;
bool comp(int a, int b) {
return (val[a] + dep[a] > val[b] + dep[b]);
}
void prop(int v, int tl, int tr) {
t[v].mx += lazy[v];
if (tl != tr) {
lazy[2 * v] += lazy[v];
lazy[2 * v + 1] += lazy[v];
} lazy[v] = 0;
}
void build(int v, int tl, int tr) {
if (tl == tr) {
t[v].nd = tl;
return;
} int tm = (tl + tr) / 2;
build(2 * v, tl, tm);
build(2 * v + 1, tm + 1, tr);
}
void update(int v, int tl, int tr, int l, int r, int val) {
if (lazy[v])
prop(v, tl, tr);
if (r < tl || tr < l)
return;
if (l <= tl && tr <= r) {
t[v].mx += val;
if (tl != tr) {
lazy[2 * v] += val;
lazy[2 * v + 1] += val;
} return;
} int tm = (tl + tr) / 2;
update(2 * v, tl, tm, l, min(r, tm), val);
update(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, val);
if (t[2 * v].mx > t[2 * v + 1].mx) t[v] = t[2 * v];
else t[v] = t[2 * v + 1];
}
void subtract(int v) {
if (used[v]) return;
update(1, 1, n, in[v], out[v], -cst[v]);
used[v] = 1; subtract(p[v]);
}
void temp_dfs(int v, int par) {
for (auto u : G[v]) {
if (u.ff == par) continue;
val[1] += u.sc.sc;
temp_dfs(u.ff, v);
}
}
void reroot_dfs(int v, int par) {
for (auto u : G[v]) {
if (u.ff == par) continue;
val[u.ff] = val[v] - u.sc.sc + u.sc.ff;
reroot_dfs(u.ff, v);
}
}
void pair_dfs(int v, int par) {
vector<int> vec = {v};
for (auto u : G[v]) {
if (u.ff == par) continue;
dep[u.ff] = dep[v] + u.sc.ff + u.sc.sc;
pair_dfs(u.ff, v); vec.pb({dp[u.ff]});
} sort(vec.begin(), vec.end(), comp);
dp[v] = vec[0];
if (vec.size() == 1) return;
int cur = val[vec[0]] + val[vec[1]] + dep[vec[0]] + dep[vec[1]] - 2 * dep[v];
if (cur > mx) {
mx = cur; rts = {vec[0], vec[1]};
}
}
void build_dfs(int v, int par, int lst) {
p[v] = par;
cst[v] = lst;
in[v] = ++tim;
rev[in[v]] = v;
dep[v] = dep[par] + lst;
if (G[v].size() == 1 && v != rts.sc)
update(1, 1, n, in[v], in[v], dep[v]);
for (auto u : G[v]) {
if (u.ff == par) continue;
build_dfs(u.ff, v, u.sc.ff);
} out[v] = tim;
}
void solve() {
cin >> n;
int sum = 0;
for (int i = 1; i < n; i++) {
int a, b, x, y;
cin >> a >> b >> x >> y;
G[a].pb({b, {x, y}});
G[b].pb({a, {y, x}});
sum += x + y;
} temp_dfs(1, 0); reroot_dfs(1, 0); pair_dfs(1, 0);
ans[2] = mx / 2;
for (int i = 1; i <= n; i++)
ans[1] = max(ans[1], val[i]);
build(1, 1, n); build_dfs(rts.sc, 0, 0);
used[rts.sc] = 1; subtract(rts.ff);
for (int i = 3; i <= n; i++) {
ans[i] = ans[i - 1] + t[1].mx;
subtract(rev[t[1].nd]);
} int q; cin >> q;
while (q--) {
int x; cin >> x;
cout << sum - ans[x] << '\n';
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
cout << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
18780 KB |
Output is correct |
2 |
Correct |
2 ms |
18780 KB |
Output is correct |
3 |
Correct |
2 ms |
18780 KB |
Output is correct |
4 |
Correct |
2 ms |
18780 KB |
Output is correct |
5 |
Correct |
2 ms |
18780 KB |
Output is correct |
6 |
Correct |
2 ms |
18780 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
2 ms |
18776 KB |
Output is correct |
9 |
Correct |
2 ms |
18780 KB |
Output is correct |
10 |
Correct |
3 ms |
19036 KB |
Output is correct |
11 |
Correct |
2 ms |
18780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
18780 KB |
Output is correct |
2 |
Correct |
250 ms |
54232 KB |
Output is correct |
3 |
Correct |
271 ms |
79184 KB |
Output is correct |
4 |
Correct |
229 ms |
52816 KB |
Output is correct |
5 |
Correct |
267 ms |
54788 KB |
Output is correct |
6 |
Correct |
266 ms |
57680 KB |
Output is correct |
7 |
Correct |
291 ms |
55288 KB |
Output is correct |
8 |
Correct |
281 ms |
79724 KB |
Output is correct |
9 |
Correct |
243 ms |
55880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
18776 KB |
Output is correct |
2 |
Correct |
244 ms |
47744 KB |
Output is correct |
3 |
Correct |
289 ms |
76968 KB |
Output is correct |
4 |
Correct |
223 ms |
47952 KB |
Output is correct |
5 |
Correct |
233 ms |
48392 KB |
Output is correct |
6 |
Correct |
249 ms |
51796 KB |
Output is correct |
7 |
Correct |
230 ms |
49652 KB |
Output is correct |
8 |
Correct |
296 ms |
63752 KB |
Output is correct |
9 |
Correct |
222 ms |
49400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
18780 KB |
Output is correct |
2 |
Correct |
2 ms |
18780 KB |
Output is correct |
3 |
Correct |
2 ms |
18780 KB |
Output is correct |
4 |
Correct |
2 ms |
18780 KB |
Output is correct |
5 |
Correct |
2 ms |
18780 KB |
Output is correct |
6 |
Correct |
2 ms |
18780 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
2 ms |
18776 KB |
Output is correct |
9 |
Correct |
2 ms |
18780 KB |
Output is correct |
10 |
Correct |
3 ms |
19036 KB |
Output is correct |
11 |
Correct |
2 ms |
18780 KB |
Output is correct |
12 |
Correct |
2 ms |
18780 KB |
Output is correct |
13 |
Correct |
4 ms |
19036 KB |
Output is correct |
14 |
Correct |
4 ms |
19292 KB |
Output is correct |
15 |
Correct |
4 ms |
19192 KB |
Output is correct |
16 |
Correct |
4 ms |
19036 KB |
Output is correct |
17 |
Correct |
4 ms |
19220 KB |
Output is correct |
18 |
Correct |
6 ms |
19128 KB |
Output is correct |
19 |
Correct |
4 ms |
19036 KB |
Output is correct |
20 |
Correct |
4 ms |
19036 KB |
Output is correct |
21 |
Correct |
4 ms |
19036 KB |
Output is correct |
22 |
Correct |
4 ms |
19032 KB |
Output is correct |
23 |
Correct |
4 ms |
19032 KB |
Output is correct |
24 |
Correct |
4 ms |
19292 KB |
Output is correct |
25 |
Correct |
5 ms |
19292 KB |
Output is correct |
26 |
Correct |
5 ms |
19036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
18780 KB |
Output is correct |
2 |
Correct |
250 ms |
54232 KB |
Output is correct |
3 |
Correct |
271 ms |
79184 KB |
Output is correct |
4 |
Correct |
229 ms |
52816 KB |
Output is correct |
5 |
Correct |
267 ms |
54788 KB |
Output is correct |
6 |
Correct |
266 ms |
57680 KB |
Output is correct |
7 |
Correct |
291 ms |
55288 KB |
Output is correct |
8 |
Correct |
281 ms |
79724 KB |
Output is correct |
9 |
Correct |
243 ms |
55880 KB |
Output is correct |
10 |
Correct |
2 ms |
18776 KB |
Output is correct |
11 |
Correct |
244 ms |
47744 KB |
Output is correct |
12 |
Correct |
289 ms |
76968 KB |
Output is correct |
13 |
Correct |
223 ms |
47952 KB |
Output is correct |
14 |
Correct |
233 ms |
48392 KB |
Output is correct |
15 |
Correct |
249 ms |
51796 KB |
Output is correct |
16 |
Correct |
230 ms |
49652 KB |
Output is correct |
17 |
Correct |
296 ms |
63752 KB |
Output is correct |
18 |
Correct |
222 ms |
49400 KB |
Output is correct |
19 |
Correct |
2 ms |
18776 KB |
Output is correct |
20 |
Correct |
250 ms |
54292 KB |
Output is correct |
21 |
Correct |
255 ms |
83612 KB |
Output is correct |
22 |
Correct |
233 ms |
52956 KB |
Output is correct |
23 |
Correct |
233 ms |
54612 KB |
Output is correct |
24 |
Correct |
270 ms |
53344 KB |
Output is correct |
25 |
Correct |
252 ms |
54676 KB |
Output is correct |
26 |
Correct |
239 ms |
53328 KB |
Output is correct |
27 |
Correct |
238 ms |
54636 KB |
Output is correct |
28 |
Correct |
264 ms |
57684 KB |
Output is correct |
29 |
Correct |
238 ms |
54868 KB |
Output is correct |
30 |
Correct |
247 ms |
53588 KB |
Output is correct |
31 |
Correct |
242 ms |
54780 KB |
Output is correct |
32 |
Correct |
291 ms |
71380 KB |
Output is correct |
33 |
Correct |
225 ms |
55840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
18780 KB |
Output is correct |
2 |
Correct |
2 ms |
18780 KB |
Output is correct |
3 |
Correct |
2 ms |
18780 KB |
Output is correct |
4 |
Correct |
2 ms |
18780 KB |
Output is correct |
5 |
Correct |
2 ms |
18780 KB |
Output is correct |
6 |
Correct |
2 ms |
18780 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
2 ms |
18776 KB |
Output is correct |
9 |
Correct |
2 ms |
18780 KB |
Output is correct |
10 |
Correct |
3 ms |
19036 KB |
Output is correct |
11 |
Correct |
2 ms |
18780 KB |
Output is correct |
12 |
Correct |
2 ms |
18780 KB |
Output is correct |
13 |
Correct |
250 ms |
54232 KB |
Output is correct |
14 |
Correct |
271 ms |
79184 KB |
Output is correct |
15 |
Correct |
229 ms |
52816 KB |
Output is correct |
16 |
Correct |
267 ms |
54788 KB |
Output is correct |
17 |
Correct |
266 ms |
57680 KB |
Output is correct |
18 |
Correct |
291 ms |
55288 KB |
Output is correct |
19 |
Correct |
281 ms |
79724 KB |
Output is correct |
20 |
Correct |
243 ms |
55880 KB |
Output is correct |
21 |
Correct |
2 ms |
18776 KB |
Output is correct |
22 |
Correct |
244 ms |
47744 KB |
Output is correct |
23 |
Correct |
289 ms |
76968 KB |
Output is correct |
24 |
Correct |
223 ms |
47952 KB |
Output is correct |
25 |
Correct |
233 ms |
48392 KB |
Output is correct |
26 |
Correct |
249 ms |
51796 KB |
Output is correct |
27 |
Correct |
230 ms |
49652 KB |
Output is correct |
28 |
Correct |
296 ms |
63752 KB |
Output is correct |
29 |
Correct |
222 ms |
49400 KB |
Output is correct |
30 |
Correct |
2 ms |
18780 KB |
Output is correct |
31 |
Correct |
4 ms |
19036 KB |
Output is correct |
32 |
Correct |
4 ms |
19292 KB |
Output is correct |
33 |
Correct |
4 ms |
19192 KB |
Output is correct |
34 |
Correct |
4 ms |
19036 KB |
Output is correct |
35 |
Correct |
4 ms |
19220 KB |
Output is correct |
36 |
Correct |
6 ms |
19128 KB |
Output is correct |
37 |
Correct |
4 ms |
19036 KB |
Output is correct |
38 |
Correct |
4 ms |
19036 KB |
Output is correct |
39 |
Correct |
4 ms |
19036 KB |
Output is correct |
40 |
Correct |
4 ms |
19032 KB |
Output is correct |
41 |
Correct |
4 ms |
19032 KB |
Output is correct |
42 |
Correct |
4 ms |
19292 KB |
Output is correct |
43 |
Correct |
5 ms |
19292 KB |
Output is correct |
44 |
Correct |
5 ms |
19036 KB |
Output is correct |
45 |
Correct |
2 ms |
18776 KB |
Output is correct |
46 |
Correct |
250 ms |
54292 KB |
Output is correct |
47 |
Correct |
255 ms |
83612 KB |
Output is correct |
48 |
Correct |
233 ms |
52956 KB |
Output is correct |
49 |
Correct |
233 ms |
54612 KB |
Output is correct |
50 |
Correct |
270 ms |
53344 KB |
Output is correct |
51 |
Correct |
252 ms |
54676 KB |
Output is correct |
52 |
Correct |
239 ms |
53328 KB |
Output is correct |
53 |
Correct |
238 ms |
54636 KB |
Output is correct |
54 |
Correct |
264 ms |
57684 KB |
Output is correct |
55 |
Correct |
238 ms |
54868 KB |
Output is correct |
56 |
Correct |
247 ms |
53588 KB |
Output is correct |
57 |
Correct |
242 ms |
54780 KB |
Output is correct |
58 |
Correct |
291 ms |
71380 KB |
Output is correct |
59 |
Correct |
225 ms |
55840 KB |
Output is correct |
60 |
Correct |
2 ms |
18776 KB |
Output is correct |
61 |
Correct |
256 ms |
56912 KB |
Output is correct |
62 |
Correct |
259 ms |
80976 KB |
Output is correct |
63 |
Correct |
270 ms |
55636 KB |
Output is correct |
64 |
Correct |
268 ms |
57056 KB |
Output is correct |
65 |
Correct |
255 ms |
55648 KB |
Output is correct |
66 |
Correct |
271 ms |
57172 KB |
Output is correct |
67 |
Correct |
271 ms |
55784 KB |
Output is correct |
68 |
Correct |
267 ms |
57392 KB |
Output is correct |
69 |
Correct |
289 ms |
59728 KB |
Output is correct |
70 |
Correct |
265 ms |
57428 KB |
Output is correct |
71 |
Correct |
275 ms |
56576 KB |
Output is correct |
72 |
Correct |
267 ms |
58088 KB |
Output is correct |
73 |
Correct |
288 ms |
71000 KB |
Output is correct |
74 |
Correct |
265 ms |
60124 KB |
Output is correct |