#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
#define ff first
#define ss second
int N, Q, dfn[200005], dfnn, efn[200005], par[200005];
bool used[200005];
vector<pair<int, pii> > edge[200005];
ll ans[200005], D[200005];
ll dfs1(int x, int p) {
ll ret = 0;
for (auto i : edge[x]) {
if (i.ff == p) continue;
ret += dfs1(i.ff, x) + i.ss.ff;
}
return ret;
}
ll dfs2(int x, int p, ll v) {
ll ret = v;
for (auto i : edge[x]) {
if (i.ff == p) continue;
ret = min(ret, dfs2(i.ff, x, v - i.ss.ff + i.ss.ss));
}
return ret;
}
int dfs3(int x, int p) {
if (x == p) D[x] = 0;
dfn[x] = ++dfnn;
par[x] = p;
int ret = x;
for (auto i : edge[x]) {
if (i.ff == p) continue;
D[i.ff] = D[x] + i.ss.ff;
int t = dfs3(i.ff, x);
if (D[ret] < D[t]) ret = t;
}
efn[x] = dfnn;
return ret;
}
const int SIZE = 1 << 18;
struct ST {
struct Node {
pair<ll, int> v;
ll l;
} A[2 * SIZE];
void init() {
for (int i = 1; i <= N; ++i) A[dfn[i] + SIZE].v = {D[i], i};
for (int i = SIZE - 1; i; --i) A[i].v = max(A[i << 1].v, A[i << 1 | 1].v);
}
void busy(int nn) {
A[nn << 1].v.ff += A[nn].l;
A[nn << 1].l += A[nn].l;
A[nn << 1 | 1].v.ff += A[nn].l;
A[nn << 1 | 1].l += A[nn].l;
A[nn].l = 0;
}
void update(int nn, int ns, int ne, int s, int e, int v) {
if (ns > e || ne < s) return;
if (s <= ns && ne <= e) { A[nn].v.ff += v;
A[nn].l += v;
return;
}
int m = ns + ne >> 1;
busy(nn);
update(nn << 1, ns, m, s, e, v);
update(nn << 1 | 1, m + 1, ne, s, e, v);
A[nn].v = max(A[nn << 1].v, A[nn << 1 | 1].v);
}
pair<ll, int> query(int nn, int ns, int ne, int s, int e) {
if (ns > e || ne < s) return {0, 0};
if (s <= ns && ne <= e) return A[nn].v;
busy(nn);
int m = ns + ne >> 1;
return max(query(nn << 1, ns, m, s, e), query(nn << 1 | 1, m + 1, ne, s, e));
}
void update(int s, int e, int v) { update(1, 0, SIZE - 1, s, e, v); }
pair<ll, int> query(int s, int e) { return query(1, 0, SIZE - 1, s, e); }
} seg;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> N;
for (int i = 1; i < N; ++i) {
int a, b, c, d; cin >> a >> b >> c >> d;
edge[a].emplace_back(b, pii(c, d));
edge[b].emplace_back(a, pii(d, c));
}
ans[0] = dfs2(1, 1, dfs1(1, 1));
int X = dfs3(1, 1);
used[X] = 1;
dfnn = 0;
dfs3(X, X);
ans[1] = dfs1(X, X);
seg.init();
vector<int> vec;
for (int i = 2; i <= N; ++i) {
auto t = seg.query(1, dfnn);
if (t.ff == 0) {
while (i <= N) ans[i++] = ans[i - 1];
break;
}
ans[i] = ans[i - 1] - t.ff;
for (int x = t.ss; !used[x]; x = par[x]) vec.push_back(x);
while (vec.size()) {
int a = vec.back(); vec.pop_back();
used[a] = 1;
seg.update(dfn[a], efn[a], D[par[a]] - D[a]);
}
}
cin >> Q;
while (Q--) {
int x; cin >> x;
if (x == 1) x--;
printf("%lld\n", ans[x]);
}
return 0;
}
Compilation message
designated_cities.cpp: In member function 'void ST::update(int, int, int, int, int, int)':
designated_cities.cpp:69:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = ns + ne >> 1;
~~~^~~~
designated_cities.cpp: In member function 'std::pair<long long int, int> ST::query(int, int, int, int, int)':
designated_cities.cpp:79:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = ns + ne >> 1;
~~~^~~~
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:106:24: warning: operation on 'i' may be undefined [-Wsequence-point]
while (i <= N) ans[i++] = ans[i - 1];
~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
17400 KB |
Output is correct |
2 |
Correct |
19 ms |
17400 KB |
Output is correct |
3 |
Correct |
19 ms |
17400 KB |
Output is correct |
4 |
Correct |
18 ms |
17272 KB |
Output is correct |
5 |
Correct |
18 ms |
17400 KB |
Output is correct |
6 |
Correct |
18 ms |
17400 KB |
Output is correct |
7 |
Correct |
21 ms |
17528 KB |
Output is correct |
8 |
Correct |
18 ms |
17404 KB |
Output is correct |
9 |
Correct |
18 ms |
17400 KB |
Output is correct |
10 |
Correct |
20 ms |
17332 KB |
Output is correct |
11 |
Correct |
18 ms |
17400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
17400 KB |
Output is correct |
2 |
Correct |
571 ms |
37736 KB |
Output is correct |
3 |
Correct |
666 ms |
49180 KB |
Output is correct |
4 |
Correct |
535 ms |
36344 KB |
Output is correct |
5 |
Correct |
564 ms |
37648 KB |
Output is correct |
6 |
Correct |
609 ms |
39544 KB |
Output is correct |
7 |
Correct |
499 ms |
37552 KB |
Output is correct |
8 |
Correct |
688 ms |
48700 KB |
Output is correct |
9 |
Correct |
458 ms |
38332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
17400 KB |
Output is correct |
2 |
Correct |
582 ms |
37800 KB |
Output is correct |
3 |
Correct |
669 ms |
49152 KB |
Output is correct |
4 |
Correct |
555 ms |
36352 KB |
Output is correct |
5 |
Correct |
596 ms |
37600 KB |
Output is correct |
6 |
Correct |
586 ms |
39928 KB |
Output is correct |
7 |
Correct |
483 ms |
37804 KB |
Output is correct |
8 |
Correct |
623 ms |
45852 KB |
Output is correct |
9 |
Correct |
482 ms |
37740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
17400 KB |
Output is correct |
2 |
Correct |
19 ms |
17400 KB |
Output is correct |
3 |
Correct |
19 ms |
17400 KB |
Output is correct |
4 |
Correct |
18 ms |
17272 KB |
Output is correct |
5 |
Correct |
18 ms |
17400 KB |
Output is correct |
6 |
Correct |
18 ms |
17400 KB |
Output is correct |
7 |
Correct |
21 ms |
17528 KB |
Output is correct |
8 |
Correct |
18 ms |
17404 KB |
Output is correct |
9 |
Correct |
18 ms |
17400 KB |
Output is correct |
10 |
Correct |
20 ms |
17332 KB |
Output is correct |
11 |
Correct |
18 ms |
17400 KB |
Output is correct |
12 |
Correct |
19 ms |
17400 KB |
Output is correct |
13 |
Correct |
21 ms |
17684 KB |
Output is correct |
14 |
Correct |
23 ms |
17656 KB |
Output is correct |
15 |
Correct |
21 ms |
17656 KB |
Output is correct |
16 |
Correct |
22 ms |
17656 KB |
Output is correct |
17 |
Correct |
21 ms |
17540 KB |
Output is correct |
18 |
Correct |
22 ms |
17656 KB |
Output is correct |
19 |
Correct |
22 ms |
17592 KB |
Output is correct |
20 |
Correct |
22 ms |
17656 KB |
Output is correct |
21 |
Correct |
24 ms |
17656 KB |
Output is correct |
22 |
Correct |
23 ms |
17656 KB |
Output is correct |
23 |
Correct |
22 ms |
17656 KB |
Output is correct |
24 |
Correct |
22 ms |
17656 KB |
Output is correct |
25 |
Correct |
22 ms |
17656 KB |
Output is correct |
26 |
Correct |
24 ms |
17656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
17400 KB |
Output is correct |
2 |
Correct |
571 ms |
37736 KB |
Output is correct |
3 |
Correct |
666 ms |
49180 KB |
Output is correct |
4 |
Correct |
535 ms |
36344 KB |
Output is correct |
5 |
Correct |
564 ms |
37648 KB |
Output is correct |
6 |
Correct |
609 ms |
39544 KB |
Output is correct |
7 |
Correct |
499 ms |
37552 KB |
Output is correct |
8 |
Correct |
688 ms |
48700 KB |
Output is correct |
9 |
Correct |
458 ms |
38332 KB |
Output is correct |
10 |
Correct |
19 ms |
17400 KB |
Output is correct |
11 |
Correct |
582 ms |
37800 KB |
Output is correct |
12 |
Correct |
669 ms |
49152 KB |
Output is correct |
13 |
Correct |
555 ms |
36352 KB |
Output is correct |
14 |
Correct |
596 ms |
37600 KB |
Output is correct |
15 |
Correct |
586 ms |
39928 KB |
Output is correct |
16 |
Correct |
483 ms |
37804 KB |
Output is correct |
17 |
Correct |
623 ms |
45852 KB |
Output is correct |
18 |
Correct |
482 ms |
37740 KB |
Output is correct |
19 |
Correct |
19 ms |
17400 KB |
Output is correct |
20 |
Correct |
657 ms |
37752 KB |
Output is correct |
21 |
Correct |
664 ms |
49120 KB |
Output is correct |
22 |
Correct |
562 ms |
36376 KB |
Output is correct |
23 |
Correct |
610 ms |
37752 KB |
Output is correct |
24 |
Correct |
590 ms |
36528 KB |
Output is correct |
25 |
Correct |
584 ms |
37756 KB |
Output is correct |
26 |
Correct |
597 ms |
36612 KB |
Output is correct |
27 |
Correct |
637 ms |
37556 KB |
Output is correct |
28 |
Correct |
605 ms |
39760 KB |
Output is correct |
29 |
Correct |
575 ms |
37752 KB |
Output is correct |
30 |
Correct |
530 ms |
36880 KB |
Output is correct |
31 |
Correct |
478 ms |
37672 KB |
Output is correct |
32 |
Correct |
701 ms |
46104 KB |
Output is correct |
33 |
Correct |
470 ms |
38308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
17400 KB |
Output is correct |
2 |
Correct |
19 ms |
17400 KB |
Output is correct |
3 |
Correct |
19 ms |
17400 KB |
Output is correct |
4 |
Correct |
18 ms |
17272 KB |
Output is correct |
5 |
Correct |
18 ms |
17400 KB |
Output is correct |
6 |
Correct |
18 ms |
17400 KB |
Output is correct |
7 |
Correct |
21 ms |
17528 KB |
Output is correct |
8 |
Correct |
18 ms |
17404 KB |
Output is correct |
9 |
Correct |
18 ms |
17400 KB |
Output is correct |
10 |
Correct |
20 ms |
17332 KB |
Output is correct |
11 |
Correct |
18 ms |
17400 KB |
Output is correct |
12 |
Correct |
18 ms |
17400 KB |
Output is correct |
13 |
Correct |
571 ms |
37736 KB |
Output is correct |
14 |
Correct |
666 ms |
49180 KB |
Output is correct |
15 |
Correct |
535 ms |
36344 KB |
Output is correct |
16 |
Correct |
564 ms |
37648 KB |
Output is correct |
17 |
Correct |
609 ms |
39544 KB |
Output is correct |
18 |
Correct |
499 ms |
37552 KB |
Output is correct |
19 |
Correct |
688 ms |
48700 KB |
Output is correct |
20 |
Correct |
458 ms |
38332 KB |
Output is correct |
21 |
Correct |
19 ms |
17400 KB |
Output is correct |
22 |
Correct |
582 ms |
37800 KB |
Output is correct |
23 |
Correct |
669 ms |
49152 KB |
Output is correct |
24 |
Correct |
555 ms |
36352 KB |
Output is correct |
25 |
Correct |
596 ms |
37600 KB |
Output is correct |
26 |
Correct |
586 ms |
39928 KB |
Output is correct |
27 |
Correct |
483 ms |
37804 KB |
Output is correct |
28 |
Correct |
623 ms |
45852 KB |
Output is correct |
29 |
Correct |
482 ms |
37740 KB |
Output is correct |
30 |
Correct |
19 ms |
17400 KB |
Output is correct |
31 |
Correct |
21 ms |
17684 KB |
Output is correct |
32 |
Correct |
23 ms |
17656 KB |
Output is correct |
33 |
Correct |
21 ms |
17656 KB |
Output is correct |
34 |
Correct |
22 ms |
17656 KB |
Output is correct |
35 |
Correct |
21 ms |
17540 KB |
Output is correct |
36 |
Correct |
22 ms |
17656 KB |
Output is correct |
37 |
Correct |
22 ms |
17592 KB |
Output is correct |
38 |
Correct |
22 ms |
17656 KB |
Output is correct |
39 |
Correct |
24 ms |
17656 KB |
Output is correct |
40 |
Correct |
23 ms |
17656 KB |
Output is correct |
41 |
Correct |
22 ms |
17656 KB |
Output is correct |
42 |
Correct |
22 ms |
17656 KB |
Output is correct |
43 |
Correct |
22 ms |
17656 KB |
Output is correct |
44 |
Correct |
24 ms |
17656 KB |
Output is correct |
45 |
Correct |
19 ms |
17400 KB |
Output is correct |
46 |
Correct |
657 ms |
37752 KB |
Output is correct |
47 |
Correct |
664 ms |
49120 KB |
Output is correct |
48 |
Correct |
562 ms |
36376 KB |
Output is correct |
49 |
Correct |
610 ms |
37752 KB |
Output is correct |
50 |
Correct |
590 ms |
36528 KB |
Output is correct |
51 |
Correct |
584 ms |
37756 KB |
Output is correct |
52 |
Correct |
597 ms |
36612 KB |
Output is correct |
53 |
Correct |
637 ms |
37556 KB |
Output is correct |
54 |
Correct |
605 ms |
39760 KB |
Output is correct |
55 |
Correct |
575 ms |
37752 KB |
Output is correct |
56 |
Correct |
530 ms |
36880 KB |
Output is correct |
57 |
Correct |
478 ms |
37672 KB |
Output is correct |
58 |
Correct |
701 ms |
46104 KB |
Output is correct |
59 |
Correct |
470 ms |
38308 KB |
Output is correct |
60 |
Correct |
19 ms |
17400 KB |
Output is correct |
61 |
Correct |
669 ms |
40596 KB |
Output is correct |
62 |
Correct |
815 ms |
50904 KB |
Output is correct |
63 |
Correct |
624 ms |
39160 KB |
Output is correct |
64 |
Correct |
895 ms |
40384 KB |
Output is correct |
65 |
Correct |
642 ms |
39164 KB |
Output is correct |
66 |
Correct |
656 ms |
40560 KB |
Output is correct |
67 |
Correct |
589 ms |
39160 KB |
Output is correct |
68 |
Correct |
597 ms |
40576 KB |
Output is correct |
69 |
Correct |
655 ms |
42208 KB |
Output is correct |
70 |
Correct |
658 ms |
40312 KB |
Output is correct |
71 |
Correct |
610 ms |
39724 KB |
Output is correct |
72 |
Correct |
514 ms |
41144 KB |
Output is correct |
73 |
Correct |
713 ms |
48340 KB |
Output is correct |
74 |
Correct |
499 ms |
42420 KB |
Output is correct |