#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int N = 2e5 + 10;
int n;
ll total;
ll ps[N], cnt[N], par[N], up[N];
int st[N], fn[N], id[N], tim;
ll ans[N];
bool mark[N];
vector<pair<int, int> > adj[N];
void preDFS(int x, int p = 0) {
for(auto j : adj[x]) {
if(j.first == p) {
up[x] = up[p] + j.second;
break;
}
}
for(auto j : adj[x]) {
if(j.first == p) {
continue;
}
cnt[j.first] = j.second;
ps[j.first] = ps[x] + j.second;
preDFS(j.first, x);
}
}
void DFS(int x, int p = 0) {
id[tim] = x;
st[x] = tim++;
par[x] = p;
for(auto j : adj[x]) {
if(j.first == p) {
continue;
}
total += j.second;
DFS(j.first, x);
}
fn[x] = tim;
}
struct node {
ll cnt, id, lz;
node() {
cnt = id = lz = 0;
}
} x[N << 2];
void build(int l = 0, int r = n, int v = 1) {
if(r - l < 2) {
x[v].cnt = ps[id[l]];
x[v].id = id[l];
return;
}
int mid = (l + r) >> 1;
build(l, mid, 2 * v), build(mid, r, 2 * v + 1);
x[v].cnt = max(x[2 * v].cnt, x[2 * v + 1].cnt);
x[v].id = (x[v].cnt == x[2 * v].cnt)? (x[2 * v].id): (x[2 * v + 1].id);
}
void put(int l, int r, int v) {
if(x[v].lz == 0) return;
x[2 * v].cnt -= x[v].lz, x[2 * v + 1].cnt -= x[v].lz;
x[2 * v].lz += x[v].lz, x[2 * v + 1].lz += x[v].lz;
x[v].lz = 0;
}
void change(int s, int e, int val, int l = 0, int r = n, int v = 1) {
if(r <= s || l >= e) return;
if(l >= s && r <= e) {
x[v].cnt -= val;
x[v].lz += val;
return;
}
put(l, r, v);
int mid = (l + r) >> 1;
change(s, e, val, l, mid, 2 * v), change(s, e, val, mid, r, 2 * v + 1);
x[v].cnt = max(x[2 * v].cnt, x[2 * v + 1].cnt);
x[v].id = (x[v].cnt == x[2 * v].cnt)? (x[2 * v].id): (x[2 * v + 1].id);
}
int main() {
cin >>n;
for(int i = 0; i < n - 1; i++) {
int u, v, w, wp; cin >>u >>v >>w >>wp;
adj[u].push_back({v, w}), adj[v].push_back({u, wp});
}
preDFS(1);
int root = max_element(ps + 1, ps + n + 1) - ps;
ps[root] = up[root] = 0;
preDFS(root);
DFS(root);
mark[root] = 1;
for(int i = 1; i <= n; i++) ans[1] = min(ans[1], up[i] - ps[i]);
build();
ans[1] += total;
for(int i = 2; i <= n; i++) {
int v = x[1].id;
while(!mark[v]) {
mark[v] = 1;
total -= cnt[v];
change(st[v], fn[v], cnt[v]);
v = par[v];
}
ans[i] = total;
}
int q; cin >>q;
for(int i = 0; i < q; i++) {
int nu; cin >>nu;
cout<<ans[nu] <<endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23732 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
11 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23800 KB |
Output is correct |
6 |
Correct |
10 ms |
23792 KB |
Output is correct |
7 |
Correct |
12 ms |
23804 KB |
Output is correct |
8 |
Correct |
11 ms |
23760 KB |
Output is correct |
9 |
Correct |
14 ms |
23764 KB |
Output is correct |
10 |
Correct |
11 ms |
23784 KB |
Output is correct |
11 |
Correct |
10 ms |
23792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23832 KB |
Output is correct |
2 |
Correct |
524 ms |
41396 KB |
Output is correct |
3 |
Correct |
549 ms |
52940 KB |
Output is correct |
4 |
Correct |
439 ms |
41488 KB |
Output is correct |
5 |
Correct |
541 ms |
41404 KB |
Output is correct |
6 |
Correct |
538 ms |
42920 KB |
Output is correct |
7 |
Correct |
520 ms |
41500 KB |
Output is correct |
8 |
Correct |
579 ms |
52320 KB |
Output is correct |
9 |
Correct |
450 ms |
41988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23836 KB |
Output is correct |
2 |
Correct |
541 ms |
41428 KB |
Output is correct |
3 |
Correct |
527 ms |
59316 KB |
Output is correct |
4 |
Correct |
498 ms |
46460 KB |
Output is correct |
5 |
Correct |
552 ms |
47820 KB |
Output is correct |
6 |
Correct |
562 ms |
49808 KB |
Output is correct |
7 |
Correct |
452 ms |
48116 KB |
Output is correct |
8 |
Correct |
572 ms |
55412 KB |
Output is correct |
9 |
Correct |
459 ms |
48248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23732 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
11 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23800 KB |
Output is correct |
6 |
Correct |
10 ms |
23792 KB |
Output is correct |
7 |
Correct |
12 ms |
23804 KB |
Output is correct |
8 |
Correct |
11 ms |
23760 KB |
Output is correct |
9 |
Correct |
14 ms |
23764 KB |
Output is correct |
10 |
Correct |
11 ms |
23784 KB |
Output is correct |
11 |
Correct |
10 ms |
23792 KB |
Output is correct |
12 |
Correct |
11 ms |
23796 KB |
Output is correct |
13 |
Correct |
20 ms |
24028 KB |
Output is correct |
14 |
Correct |
17 ms |
24168 KB |
Output is correct |
15 |
Correct |
19 ms |
24064 KB |
Output is correct |
16 |
Correct |
18 ms |
24020 KB |
Output is correct |
17 |
Correct |
17 ms |
24020 KB |
Output is correct |
18 |
Correct |
17 ms |
24020 KB |
Output is correct |
19 |
Correct |
19 ms |
24092 KB |
Output is correct |
20 |
Correct |
18 ms |
24020 KB |
Output is correct |
21 |
Correct |
17 ms |
24060 KB |
Output is correct |
22 |
Correct |
20 ms |
23988 KB |
Output is correct |
23 |
Correct |
18 ms |
24064 KB |
Output is correct |
24 |
Correct |
17 ms |
24072 KB |
Output is correct |
25 |
Correct |
17 ms |
24136 KB |
Output is correct |
26 |
Correct |
17 ms |
24092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23832 KB |
Output is correct |
2 |
Correct |
524 ms |
41396 KB |
Output is correct |
3 |
Correct |
549 ms |
52940 KB |
Output is correct |
4 |
Correct |
439 ms |
41488 KB |
Output is correct |
5 |
Correct |
541 ms |
41404 KB |
Output is correct |
6 |
Correct |
538 ms |
42920 KB |
Output is correct |
7 |
Correct |
520 ms |
41500 KB |
Output is correct |
8 |
Correct |
579 ms |
52320 KB |
Output is correct |
9 |
Correct |
450 ms |
41988 KB |
Output is correct |
10 |
Correct |
13 ms |
23836 KB |
Output is correct |
11 |
Correct |
541 ms |
41428 KB |
Output is correct |
12 |
Correct |
527 ms |
59316 KB |
Output is correct |
13 |
Correct |
498 ms |
46460 KB |
Output is correct |
14 |
Correct |
552 ms |
47820 KB |
Output is correct |
15 |
Correct |
562 ms |
49808 KB |
Output is correct |
16 |
Correct |
452 ms |
48116 KB |
Output is correct |
17 |
Correct |
572 ms |
55412 KB |
Output is correct |
18 |
Correct |
459 ms |
48248 KB |
Output is correct |
19 |
Correct |
11 ms |
23764 KB |
Output is correct |
20 |
Correct |
553 ms |
47804 KB |
Output is correct |
21 |
Correct |
498 ms |
59268 KB |
Output is correct |
22 |
Correct |
472 ms |
46436 KB |
Output is correct |
23 |
Correct |
539 ms |
47824 KB |
Output is correct |
24 |
Correct |
519 ms |
46648 KB |
Output is correct |
25 |
Correct |
534 ms |
47768 KB |
Output is correct |
26 |
Correct |
480 ms |
46700 KB |
Output is correct |
27 |
Correct |
560 ms |
47768 KB |
Output is correct |
28 |
Correct |
585 ms |
49536 KB |
Output is correct |
29 |
Correct |
577 ms |
47840 KB |
Output is correct |
30 |
Correct |
485 ms |
46968 KB |
Output is correct |
31 |
Correct |
474 ms |
48036 KB |
Output is correct |
32 |
Correct |
571 ms |
55796 KB |
Output is correct |
33 |
Correct |
472 ms |
48432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23732 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
11 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23800 KB |
Output is correct |
6 |
Correct |
10 ms |
23792 KB |
Output is correct |
7 |
Correct |
12 ms |
23804 KB |
Output is correct |
8 |
Correct |
11 ms |
23760 KB |
Output is correct |
9 |
Correct |
14 ms |
23764 KB |
Output is correct |
10 |
Correct |
11 ms |
23784 KB |
Output is correct |
11 |
Correct |
10 ms |
23792 KB |
Output is correct |
12 |
Correct |
11 ms |
23832 KB |
Output is correct |
13 |
Correct |
524 ms |
41396 KB |
Output is correct |
14 |
Correct |
549 ms |
52940 KB |
Output is correct |
15 |
Correct |
439 ms |
41488 KB |
Output is correct |
16 |
Correct |
541 ms |
41404 KB |
Output is correct |
17 |
Correct |
538 ms |
42920 KB |
Output is correct |
18 |
Correct |
520 ms |
41500 KB |
Output is correct |
19 |
Correct |
579 ms |
52320 KB |
Output is correct |
20 |
Correct |
450 ms |
41988 KB |
Output is correct |
21 |
Correct |
13 ms |
23836 KB |
Output is correct |
22 |
Correct |
541 ms |
41428 KB |
Output is correct |
23 |
Correct |
527 ms |
59316 KB |
Output is correct |
24 |
Correct |
498 ms |
46460 KB |
Output is correct |
25 |
Correct |
552 ms |
47820 KB |
Output is correct |
26 |
Correct |
562 ms |
49808 KB |
Output is correct |
27 |
Correct |
452 ms |
48116 KB |
Output is correct |
28 |
Correct |
572 ms |
55412 KB |
Output is correct |
29 |
Correct |
459 ms |
48248 KB |
Output is correct |
30 |
Correct |
11 ms |
23796 KB |
Output is correct |
31 |
Correct |
20 ms |
24028 KB |
Output is correct |
32 |
Correct |
17 ms |
24168 KB |
Output is correct |
33 |
Correct |
19 ms |
24064 KB |
Output is correct |
34 |
Correct |
18 ms |
24020 KB |
Output is correct |
35 |
Correct |
17 ms |
24020 KB |
Output is correct |
36 |
Correct |
17 ms |
24020 KB |
Output is correct |
37 |
Correct |
19 ms |
24092 KB |
Output is correct |
38 |
Correct |
18 ms |
24020 KB |
Output is correct |
39 |
Correct |
17 ms |
24060 KB |
Output is correct |
40 |
Correct |
20 ms |
23988 KB |
Output is correct |
41 |
Correct |
18 ms |
24064 KB |
Output is correct |
42 |
Correct |
17 ms |
24072 KB |
Output is correct |
43 |
Correct |
17 ms |
24136 KB |
Output is correct |
44 |
Correct |
17 ms |
24092 KB |
Output is correct |
45 |
Correct |
11 ms |
23764 KB |
Output is correct |
46 |
Correct |
553 ms |
47804 KB |
Output is correct |
47 |
Correct |
498 ms |
59268 KB |
Output is correct |
48 |
Correct |
472 ms |
46436 KB |
Output is correct |
49 |
Correct |
539 ms |
47824 KB |
Output is correct |
50 |
Correct |
519 ms |
46648 KB |
Output is correct |
51 |
Correct |
534 ms |
47768 KB |
Output is correct |
52 |
Correct |
480 ms |
46700 KB |
Output is correct |
53 |
Correct |
560 ms |
47768 KB |
Output is correct |
54 |
Correct |
585 ms |
49536 KB |
Output is correct |
55 |
Correct |
577 ms |
47840 KB |
Output is correct |
56 |
Correct |
485 ms |
46968 KB |
Output is correct |
57 |
Correct |
474 ms |
48036 KB |
Output is correct |
58 |
Correct |
571 ms |
55796 KB |
Output is correct |
59 |
Correct |
472 ms |
48432 KB |
Output is correct |
60 |
Correct |
11 ms |
23796 KB |
Output is correct |
61 |
Correct |
871 ms |
50432 KB |
Output is correct |
62 |
Correct |
838 ms |
60988 KB |
Output is correct |
63 |
Correct |
751 ms |
49068 KB |
Output is correct |
64 |
Correct |
872 ms |
50516 KB |
Output is correct |
65 |
Correct |
842 ms |
49196 KB |
Output is correct |
66 |
Correct |
873 ms |
50580 KB |
Output is correct |
67 |
Correct |
813 ms |
49152 KB |
Output is correct |
68 |
Correct |
812 ms |
50592 KB |
Output is correct |
69 |
Correct |
866 ms |
52136 KB |
Output is correct |
70 |
Correct |
839 ms |
50260 KB |
Output is correct |
71 |
Correct |
818 ms |
49696 KB |
Output is correct |
72 |
Correct |
831 ms |
51252 KB |
Output is correct |
73 |
Correct |
860 ms |
57960 KB |
Output is correct |
74 |
Correct |
780 ms |
52648 KB |
Output is correct |