#include <bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;
// #define int long long
#define ll long long
// #define double long double
#define all(x) x.begin(), x.end()
#define debug(x) do{auto _x = x; cerr << #x << " = " << _x << endl;} while(0)
#define f first
#define s second
// #define endl '\n'
using pii = pair<int, int>;
const int inf = 1 << 30;
// const ll inf = 1ll << 62;
pii t[262144];
int n;
void init(int s) {
n = 1;
while(n < s) n *= 2;
}
void update(int i, pii v) {
for(t[i += n] = v; i > 1; i /= 2) {
t[i / 2] = max(t[i], t[i ^ 1]);
}
}
pii query(int l, int r) {
pii res = {-1, -1};
for(l += n, r += n; l < r; l /= 2, r /= 2) {
if(l & 1) res = max(res, t[l++]);
if(r & 1) res = max(res, t[--r]);
}
return res;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
vector<int> c(n);
for(int i = 0; i < n; i++) {
cin >> c[i];
}
vector<pii> el;
vector<vector<int>> ch(n);
for(int i = 0; i < n - 1; i++) {
int a, b; cin >> a >> b; a--; b--;
ch[a].push_back(b);
el.push_back({a, b});
}
vector<int> d(n);
vector<vector<int>> jmp(n, vector<int>(ceil(log2(n))));
vector<int> tin(n), tout(n);
int time = 0;
function<void(int)> dfs = [&](int cur) {
tin[cur] = time++;
for(int i = 1; i < log2(n); i++) {
jmp[cur][i] = jmp[jmp[cur][i - 1]][i - 1];
}
for(auto i : ch[cur]) {
jmp[i][0] = cur;
d[i] = d[cur] + 1;
dfs(i);
}
tout[cur] = time;
};
dfs(0);
init(n);
update(0, {-1, c[0]});
for(int i = 0; i < n - 1; i++) {
vector<pii> v;
int cur = el[i].f;
while(true) {
int od = d[cur];
pii curv = query(tin[cur], tout[cur]);
for(int i = ceil(log2(n)) - 1; i >= 0; i--) {
int nxt = jmp[cur][i];
if(query(tin[nxt], tout[nxt]).f == curv.f) {
cur = nxt;
}
}
v.push_back({curv.s, od - d[cur] + 1});
if(cur == 0) break;
cur = jmp[cur][0];
}
int ans = 0;
function<vector<pii>(int, int)> dnc = [&](int l, int r) {
if(l + 1 == r) return vector<pii>{v[l]};
int m = (l + r) / 2;
auto lv = dnc(l, m), rv = dnc(m, r);
vector<pii> res;
int i = 0, j = 0, x = 0;
while(j < rv.size()) {
if(i < lv.size() && lv[i].f < rv[j].f) {
res.push_back(lv[i]);
x += lv[i].s;
i++;
} else {
res.push_back(rv[j]);
ans += x * rv[j].s;
j++;
}
}
while(i < lv.size()) {
res.push_back(lv[i]);
i++;
}
return res;
};
dnc(0, v.size());
cout << ans << '\n';
update(tin[el[i].s], {i, c[el[i].s]});
}
}
Compilation message
construction.cpp: In lambda function:
construction.cpp:96:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | while(j < rv.size()) {
| ~~^~~~~~~~~~~
construction.cpp:97:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | if(i < lv.size() && lv[i].f < rv[j].f) {
| ~~^~~~~~~~~~~
construction.cpp:107:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | while(i < lv.size()) {
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
3 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
568 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
504 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
3 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
568 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
504 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
2 ms |
604 KB |
Output is correct |
46 |
Correct |
13 ms |
1116 KB |
Output is correct |
47 |
Correct |
13 ms |
1116 KB |
Output is correct |
48 |
Correct |
14 ms |
1116 KB |
Output is correct |
49 |
Correct |
5 ms |
1628 KB |
Output is correct |
50 |
Correct |
5 ms |
1628 KB |
Output is correct |
51 |
Correct |
5 ms |
1628 KB |
Output is correct |
52 |
Correct |
7 ms |
1320 KB |
Output is correct |
53 |
Correct |
7 ms |
1372 KB |
Output is correct |
54 |
Correct |
7 ms |
1372 KB |
Output is correct |
55 |
Correct |
7 ms |
1372 KB |
Output is correct |
56 |
Correct |
7 ms |
1372 KB |
Output is correct |
57 |
Correct |
26 ms |
1200 KB |
Output is correct |
58 |
Correct |
27 ms |
1116 KB |
Output is correct |
59 |
Correct |
27 ms |
1116 KB |
Output is correct |
60 |
Correct |
32 ms |
1064 KB |
Output is correct |
61 |
Correct |
7 ms |
1368 KB |
Output is correct |
62 |
Correct |
7 ms |
1372 KB |
Output is correct |
63 |
Correct |
7 ms |
1396 KB |
Output is correct |
64 |
Correct |
13 ms |
1184 KB |
Output is correct |
65 |
Correct |
13 ms |
1116 KB |
Output is correct |
66 |
Correct |
13 ms |
1184 KB |
Output is correct |
67 |
Correct |
13 ms |
1116 KB |
Output is correct |
68 |
Correct |
5 ms |
1416 KB |
Output is correct |
69 |
Correct |
6 ms |
1368 KB |
Output is correct |
70 |
Correct |
7 ms |
1372 KB |
Output is correct |
71 |
Correct |
9 ms |
1368 KB |
Output is correct |
72 |
Correct |
26 ms |
1192 KB |
Output is correct |
73 |
Correct |
27 ms |
1168 KB |
Output is correct |
74 |
Correct |
7 ms |
1372 KB |
Output is correct |
75 |
Correct |
6 ms |
1116 KB |
Output is correct |
76 |
Correct |
6 ms |
1260 KB |
Output is correct |
77 |
Correct |
8 ms |
1112 KB |
Output is correct |
78 |
Correct |
7 ms |
1112 KB |
Output is correct |
79 |
Correct |
8 ms |
1116 KB |
Output is correct |
80 |
Correct |
7 ms |
1220 KB |
Output is correct |
81 |
Correct |
8 ms |
1280 KB |
Output is correct |
82 |
Correct |
10 ms |
1112 KB |
Output is correct |
83 |
Correct |
8 ms |
1116 KB |
Output is correct |
84 |
Correct |
8 ms |
1116 KB |
Output is correct |
85 |
Correct |
8 ms |
1236 KB |
Output is correct |
86 |
Correct |
8 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
3 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
568 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
504 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
2 ms |
604 KB |
Output is correct |
46 |
Correct |
13 ms |
1116 KB |
Output is correct |
47 |
Correct |
13 ms |
1116 KB |
Output is correct |
48 |
Correct |
14 ms |
1116 KB |
Output is correct |
49 |
Correct |
5 ms |
1628 KB |
Output is correct |
50 |
Correct |
5 ms |
1628 KB |
Output is correct |
51 |
Correct |
5 ms |
1628 KB |
Output is correct |
52 |
Correct |
7 ms |
1320 KB |
Output is correct |
53 |
Correct |
7 ms |
1372 KB |
Output is correct |
54 |
Correct |
7 ms |
1372 KB |
Output is correct |
55 |
Correct |
7 ms |
1372 KB |
Output is correct |
56 |
Correct |
7 ms |
1372 KB |
Output is correct |
57 |
Correct |
26 ms |
1200 KB |
Output is correct |
58 |
Correct |
27 ms |
1116 KB |
Output is correct |
59 |
Correct |
27 ms |
1116 KB |
Output is correct |
60 |
Correct |
32 ms |
1064 KB |
Output is correct |
61 |
Correct |
7 ms |
1368 KB |
Output is correct |
62 |
Correct |
7 ms |
1372 KB |
Output is correct |
63 |
Correct |
7 ms |
1396 KB |
Output is correct |
64 |
Correct |
13 ms |
1184 KB |
Output is correct |
65 |
Correct |
13 ms |
1116 KB |
Output is correct |
66 |
Correct |
13 ms |
1184 KB |
Output is correct |
67 |
Correct |
13 ms |
1116 KB |
Output is correct |
68 |
Correct |
5 ms |
1416 KB |
Output is correct |
69 |
Correct |
6 ms |
1368 KB |
Output is correct |
70 |
Correct |
7 ms |
1372 KB |
Output is correct |
71 |
Correct |
9 ms |
1368 KB |
Output is correct |
72 |
Correct |
26 ms |
1192 KB |
Output is correct |
73 |
Correct |
27 ms |
1168 KB |
Output is correct |
74 |
Correct |
7 ms |
1372 KB |
Output is correct |
75 |
Correct |
6 ms |
1116 KB |
Output is correct |
76 |
Correct |
6 ms |
1260 KB |
Output is correct |
77 |
Correct |
8 ms |
1112 KB |
Output is correct |
78 |
Correct |
7 ms |
1112 KB |
Output is correct |
79 |
Correct |
8 ms |
1116 KB |
Output is correct |
80 |
Correct |
7 ms |
1220 KB |
Output is correct |
81 |
Correct |
8 ms |
1280 KB |
Output is correct |
82 |
Correct |
10 ms |
1112 KB |
Output is correct |
83 |
Correct |
8 ms |
1116 KB |
Output is correct |
84 |
Correct |
8 ms |
1116 KB |
Output is correct |
85 |
Correct |
8 ms |
1236 KB |
Output is correct |
86 |
Correct |
8 ms |
1116 KB |
Output is correct |
87 |
Correct |
39 ms |
2344 KB |
Output is correct |
88 |
Correct |
178 ms |
6520 KB |
Output is correct |
89 |
Correct |
707 ms |
20956 KB |
Output is correct |
90 |
Correct |
704 ms |
20936 KB |
Output is correct |
91 |
Correct |
731 ms |
20972 KB |
Output is correct |
92 |
Correct |
217 ms |
31688 KB |
Output is correct |
93 |
Correct |
255 ms |
31972 KB |
Output is correct |
94 |
Correct |
207 ms |
31508 KB |
Output is correct |
95 |
Correct |
281 ms |
26824 KB |
Output is correct |
96 |
Correct |
321 ms |
27336 KB |
Output is correct |
97 |
Correct |
288 ms |
27076 KB |
Output is correct |
98 |
Correct |
356 ms |
27340 KB |
Output is correct |
99 |
Correct |
316 ms |
25544 KB |
Output is correct |
100 |
Correct |
1617 ms |
21016 KB |
Output is correct |
101 |
Correct |
1602 ms |
21484 KB |
Output is correct |
102 |
Correct |
1595 ms |
21472 KB |
Output is correct |
103 |
Correct |
1614 ms |
21444 KB |
Output is correct |
104 |
Correct |
371 ms |
25412 KB |
Output is correct |
105 |
Correct |
348 ms |
25484 KB |
Output is correct |
106 |
Correct |
348 ms |
25624 KB |
Output is correct |
107 |
Correct |
723 ms |
20120 KB |
Output is correct |
108 |
Correct |
713 ms |
20172 KB |
Output is correct |
109 |
Correct |
763 ms |
20328 KB |
Output is correct |
110 |
Correct |
239 ms |
30916 KB |
Output is correct |
111 |
Correct |
299 ms |
26756 KB |
Output is correct |
112 |
Correct |
339 ms |
26560 KB |
Output is correct |
113 |
Correct |
279 ms |
24788 KB |
Output is correct |
114 |
Correct |
1589 ms |
20928 KB |
Output is correct |
115 |
Correct |
1626 ms |
20808 KB |
Output is correct |
116 |
Correct |
361 ms |
24772 KB |
Output is correct |
117 |
Correct |
285 ms |
22888 KB |
Output is correct |
118 |
Correct |
266 ms |
21956 KB |
Output is correct |
119 |
Correct |
268 ms |
21700 KB |
Output is correct |
120 |
Correct |
291 ms |
22476 KB |
Output is correct |
121 |
Correct |
283 ms |
21964 KB |
Output is correct |
122 |
Correct |
264 ms |
21444 KB |
Output is correct |
123 |
Correct |
425 ms |
22984 KB |
Output is correct |
124 |
Correct |
433 ms |
22232 KB |
Output is correct |
125 |
Correct |
417 ms |
21788 KB |
Output is correct |
126 |
Correct |
428 ms |
22544 KB |
Output is correct |
127 |
Correct |
434 ms |
21704 KB |
Output is correct |
128 |
Correct |
421 ms |
21444 KB |
Output is correct |