#pragma GCC optimize("O3", "Ofast", "unroll-loops", "fast-math")
// #pragma GCC target("avx", "avx2")
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5, maxlog = 18;
const long long inf = 1e16;
int a[maxn];
vector<int> g[maxn];
int sz[maxn], level[maxn], centPar[maxn][maxlog];
int dist[maxn][maxlog], mx[maxn][maxlog], idxNxt[maxn][maxlog];
vector<int> toutList;
void dfsSz(int v, int p) {
sz[v] = 1;
for (const int& u : g[v]) {
if (u != p && level[u] == -1) {
dfsSz(u, v);
sz[v] += sz[u];
}
}
toutList.emplace_back(v);
}
void dfsCalc(int v, int p, int j) {
int i = 0;
for (const int& u : g[v]) {
if (u != p && level[u] == -1) {
dist[u][j] = dist[v][j] + 1;
mx[u][j] = max(mx[v][j], a[u]);
centPar[u][j] = centPar[v][j];
if (p != -1) {
idxNxt[u][j] = idxNxt[v][j];
} else {
idxNxt[u][j] = i;
}
dfsCalc(u, v, j);
}
++i;
}
}
void findCentroid(int v, int j) {
toutList.clear();
dfsSz(v, -1);
for (const int& c : toutList) {
if (2 * sz[c] >= sz[v]) {
v = c;
break;
}
}
// cerr << "j: " << j << ", v: " << v << '\n';
level[v] = j;
dist[v][j] = 0;
mx[v][j] = a[v];
centPar[v][j] = v;
dfsCalc(v, -1, j);
for (const int& u : g[v]) {
if (level[u] == -1) {
findCentroid(u, j + 1);
}
}
}
struct SegTree {
int n, N;
vector<long long> st;
SegTree() {}
SegTree(int _n) {
n = _n;
N = 1;
while (N < n) {
N <<= 1;
}
st.resize(2 * N, -inf);
}
void maxEq(int i, long long x) {
int v = N + i;
st[v] = max(st[v], x);
v >>= 1;
while (v > 0) {
st[v] = max(st[2 * v], st[2 * v + 1]);
v >>= 1;
}
}
long long getMax(int l, int r) {
int vl = N + l, vr = N + r;
long long res = -inf;
while (vl < vr) {
if (vl & 1) {
res = max(res, st[vl]);
}
if ((vr ^ 1) & 1) {
res = max(res, st[vr]);
}
vl = (vl + 1) >> 1;
vr = (vr - 1) >> 1;
}
if (vl == vr) {
res = max(res, st[vl]);
}
return res;
}
~SegTree() {}
};
SegTree st[maxn];
vector<pair<int, int>> byVal[maxn];
void solve() {
int n;
cin >> n;
vector<int> posOf(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
--a[i];
posOf[a[i]] = i;
level[i] = -1;
}
for (int i = 0; i < n - 1; ++i) {
int v, u;
cin >> v >> u;
--v, --u;
g[v].emplace_back(u);
g[u].emplace_back(v);
}
for (int v = 0; v < n; ++v) {
st[v] = SegTree(g[v].size());
}
findCentroid(0, 0);
vector<long long> dp(n);
for (int i = 0; i < n; ++i) {
while (!byVal[i].empty()) {
auto [v, j] = byVal[i].back();
byVal[i].pop_back();
st[centPar[v][j]].maxEq(idxNxt[v][j], dp[a[v]] + dist[v][j]);
}
int v = posOf[i];
for (int j = level[v]; j >= 0; --j) {
if (mx[v][j] <= i) {
int u = centPar[v][j];
long long val;
if (j < level[v]) {
val = max(st[u].getMax(0, idxNxt[v][j] - 1), st[u].getMax(idxNxt[v][j] + 1, (int)g[u].size() - 1));
val = max(val, dp[a[u]]);
} else {
val = max(val, st[u].getMax(0, (int)g[u].size() - 1));
}
dp[i] = max(dp[i], val + dist[v][j]);
}
}
// cerr << "i: " << i << ", dp: " << dp[i] << '\n';
for (int j = level[v] - 1; j >= 0; --j) {
if (mx[v][j] <= i + 1) {
st[centPar[v][j]].maxEq(idxNxt[v][j], dp[i] + dist[v][j]);
} else {
byVal[mx[v][j]].emplace_back(v, j);
}
}
}
cout << dp[n - 1] << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int T = 1;
cin >> T;
while (T--) {
solve();
cerr << "-----------\n";
cout << "-----------\n";
}
#else
int T = 1;
// cin >> T;
while (T--) {
solve();
}
#endif
return 0;
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:142:27: warning: 'val' may be used uninitialized in this function [-Wmaybe-uninitialized]
142 | long long val;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27228 KB |
Output is correct |
2 |
Correct |
6 ms |
27304 KB |
Output is correct |
3 |
Correct |
6 ms |
27480 KB |
Output is correct |
4 |
Correct |
5 ms |
27228 KB |
Output is correct |
5 |
Correct |
5 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27300 KB |
Output is correct |
7 |
Correct |
6 ms |
27228 KB |
Output is correct |
8 |
Correct |
6 ms |
27224 KB |
Output is correct |
9 |
Correct |
6 ms |
27228 KB |
Output is correct |
10 |
Correct |
6 ms |
27228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27228 KB |
Output is correct |
2 |
Correct |
6 ms |
27304 KB |
Output is correct |
3 |
Correct |
6 ms |
27480 KB |
Output is correct |
4 |
Correct |
5 ms |
27228 KB |
Output is correct |
5 |
Correct |
5 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27300 KB |
Output is correct |
7 |
Correct |
6 ms |
27228 KB |
Output is correct |
8 |
Correct |
6 ms |
27224 KB |
Output is correct |
9 |
Correct |
6 ms |
27228 KB |
Output is correct |
10 |
Correct |
6 ms |
27228 KB |
Output is correct |
11 |
Correct |
7 ms |
29276 KB |
Output is correct |
12 |
Correct |
6 ms |
29324 KB |
Output is correct |
13 |
Correct |
7 ms |
29276 KB |
Output is correct |
14 |
Correct |
7 ms |
29276 KB |
Output is correct |
15 |
Correct |
6 ms |
29276 KB |
Output is correct |
16 |
Correct |
6 ms |
29296 KB |
Output is correct |
17 |
Correct |
6 ms |
29276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27228 KB |
Output is correct |
2 |
Correct |
6 ms |
27304 KB |
Output is correct |
3 |
Correct |
6 ms |
27480 KB |
Output is correct |
4 |
Correct |
5 ms |
27228 KB |
Output is correct |
5 |
Correct |
5 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27300 KB |
Output is correct |
7 |
Correct |
6 ms |
27228 KB |
Output is correct |
8 |
Correct |
6 ms |
27224 KB |
Output is correct |
9 |
Correct |
6 ms |
27228 KB |
Output is correct |
10 |
Correct |
6 ms |
27228 KB |
Output is correct |
11 |
Correct |
7 ms |
29276 KB |
Output is correct |
12 |
Correct |
6 ms |
29324 KB |
Output is correct |
13 |
Correct |
7 ms |
29276 KB |
Output is correct |
14 |
Correct |
7 ms |
29276 KB |
Output is correct |
15 |
Correct |
6 ms |
29276 KB |
Output is correct |
16 |
Correct |
6 ms |
29296 KB |
Output is correct |
17 |
Correct |
6 ms |
29276 KB |
Output is correct |
18 |
Correct |
10 ms |
32600 KB |
Output is correct |
19 |
Correct |
10 ms |
32348 KB |
Output is correct |
20 |
Correct |
10 ms |
32348 KB |
Output is correct |
21 |
Correct |
14 ms |
32600 KB |
Output is correct |
22 |
Correct |
15 ms |
32752 KB |
Output is correct |
23 |
Correct |
14 ms |
32600 KB |
Output is correct |
24 |
Correct |
11 ms |
32604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27228 KB |
Output is correct |
2 |
Correct |
6 ms |
27304 KB |
Output is correct |
3 |
Correct |
6 ms |
27480 KB |
Output is correct |
4 |
Correct |
5 ms |
27228 KB |
Output is correct |
5 |
Correct |
5 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27300 KB |
Output is correct |
7 |
Correct |
6 ms |
27228 KB |
Output is correct |
8 |
Correct |
6 ms |
27224 KB |
Output is correct |
9 |
Correct |
6 ms |
27228 KB |
Output is correct |
10 |
Correct |
6 ms |
27228 KB |
Output is correct |
11 |
Correct |
7 ms |
29276 KB |
Output is correct |
12 |
Correct |
6 ms |
29324 KB |
Output is correct |
13 |
Correct |
7 ms |
29276 KB |
Output is correct |
14 |
Correct |
7 ms |
29276 KB |
Output is correct |
15 |
Correct |
6 ms |
29276 KB |
Output is correct |
16 |
Correct |
6 ms |
29296 KB |
Output is correct |
17 |
Correct |
6 ms |
29276 KB |
Output is correct |
18 |
Correct |
10 ms |
32600 KB |
Output is correct |
19 |
Correct |
10 ms |
32348 KB |
Output is correct |
20 |
Correct |
10 ms |
32348 KB |
Output is correct |
21 |
Correct |
14 ms |
32600 KB |
Output is correct |
22 |
Correct |
15 ms |
32752 KB |
Output is correct |
23 |
Correct |
14 ms |
32600 KB |
Output is correct |
24 |
Correct |
11 ms |
32604 KB |
Output is correct |
25 |
Correct |
6 ms |
27480 KB |
Output is correct |
26 |
Correct |
11 ms |
32444 KB |
Output is correct |
27 |
Correct |
12 ms |
32348 KB |
Output is correct |
28 |
Correct |
11 ms |
32348 KB |
Output is correct |
29 |
Correct |
11 ms |
32440 KB |
Output is correct |
30 |
Correct |
11 ms |
32088 KB |
Output is correct |
31 |
Correct |
11 ms |
32092 KB |
Output is correct |
32 |
Correct |
12 ms |
32156 KB |
Output is correct |
33 |
Correct |
12 ms |
32092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27228 KB |
Output is correct |
2 |
Correct |
6 ms |
27304 KB |
Output is correct |
3 |
Correct |
6 ms |
27480 KB |
Output is correct |
4 |
Correct |
5 ms |
27228 KB |
Output is correct |
5 |
Correct |
5 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27300 KB |
Output is correct |
7 |
Correct |
6 ms |
27228 KB |
Output is correct |
8 |
Correct |
6 ms |
27224 KB |
Output is correct |
9 |
Correct |
6 ms |
27228 KB |
Output is correct |
10 |
Correct |
6 ms |
27228 KB |
Output is correct |
11 |
Correct |
7 ms |
29276 KB |
Output is correct |
12 |
Correct |
6 ms |
29324 KB |
Output is correct |
13 |
Correct |
7 ms |
29276 KB |
Output is correct |
14 |
Correct |
7 ms |
29276 KB |
Output is correct |
15 |
Correct |
6 ms |
29276 KB |
Output is correct |
16 |
Correct |
6 ms |
29296 KB |
Output is correct |
17 |
Correct |
6 ms |
29276 KB |
Output is correct |
18 |
Correct |
10 ms |
32600 KB |
Output is correct |
19 |
Correct |
10 ms |
32348 KB |
Output is correct |
20 |
Correct |
10 ms |
32348 KB |
Output is correct |
21 |
Correct |
14 ms |
32600 KB |
Output is correct |
22 |
Correct |
15 ms |
32752 KB |
Output is correct |
23 |
Correct |
14 ms |
32600 KB |
Output is correct |
24 |
Correct |
11 ms |
32604 KB |
Output is correct |
25 |
Correct |
232 ms |
125736 KB |
Output is correct |
26 |
Correct |
258 ms |
126008 KB |
Output is correct |
27 |
Correct |
236 ms |
126148 KB |
Output is correct |
28 |
Correct |
442 ms |
140228 KB |
Output is correct |
29 |
Correct |
471 ms |
144780 KB |
Output is correct |
30 |
Correct |
448 ms |
143784 KB |
Output is correct |
31 |
Correct |
448 ms |
143816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
27228 KB |
Output is correct |
2 |
Correct |
6 ms |
27228 KB |
Output is correct |
3 |
Correct |
397 ms |
122240 KB |
Output is correct |
4 |
Correct |
384 ms |
122568 KB |
Output is correct |
5 |
Correct |
398 ms |
122108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27228 KB |
Output is correct |
2 |
Correct |
6 ms |
27304 KB |
Output is correct |
3 |
Correct |
6 ms |
27480 KB |
Output is correct |
4 |
Correct |
5 ms |
27228 KB |
Output is correct |
5 |
Correct |
5 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27300 KB |
Output is correct |
7 |
Correct |
6 ms |
27228 KB |
Output is correct |
8 |
Correct |
6 ms |
27224 KB |
Output is correct |
9 |
Correct |
6 ms |
27228 KB |
Output is correct |
10 |
Correct |
6 ms |
27228 KB |
Output is correct |
11 |
Correct |
7 ms |
29276 KB |
Output is correct |
12 |
Correct |
6 ms |
29324 KB |
Output is correct |
13 |
Correct |
7 ms |
29276 KB |
Output is correct |
14 |
Correct |
7 ms |
29276 KB |
Output is correct |
15 |
Correct |
6 ms |
29276 KB |
Output is correct |
16 |
Correct |
6 ms |
29296 KB |
Output is correct |
17 |
Correct |
6 ms |
29276 KB |
Output is correct |
18 |
Correct |
10 ms |
32600 KB |
Output is correct |
19 |
Correct |
10 ms |
32348 KB |
Output is correct |
20 |
Correct |
10 ms |
32348 KB |
Output is correct |
21 |
Correct |
14 ms |
32600 KB |
Output is correct |
22 |
Correct |
15 ms |
32752 KB |
Output is correct |
23 |
Correct |
14 ms |
32600 KB |
Output is correct |
24 |
Correct |
11 ms |
32604 KB |
Output is correct |
25 |
Correct |
6 ms |
27480 KB |
Output is correct |
26 |
Correct |
11 ms |
32444 KB |
Output is correct |
27 |
Correct |
12 ms |
32348 KB |
Output is correct |
28 |
Correct |
11 ms |
32348 KB |
Output is correct |
29 |
Correct |
11 ms |
32440 KB |
Output is correct |
30 |
Correct |
11 ms |
32088 KB |
Output is correct |
31 |
Correct |
11 ms |
32092 KB |
Output is correct |
32 |
Correct |
12 ms |
32156 KB |
Output is correct |
33 |
Correct |
12 ms |
32092 KB |
Output is correct |
34 |
Correct |
232 ms |
125736 KB |
Output is correct |
35 |
Correct |
258 ms |
126008 KB |
Output is correct |
36 |
Correct |
236 ms |
126148 KB |
Output is correct |
37 |
Correct |
442 ms |
140228 KB |
Output is correct |
38 |
Correct |
471 ms |
144780 KB |
Output is correct |
39 |
Correct |
448 ms |
143784 KB |
Output is correct |
40 |
Correct |
448 ms |
143816 KB |
Output is correct |
41 |
Correct |
5 ms |
27228 KB |
Output is correct |
42 |
Correct |
6 ms |
27228 KB |
Output is correct |
43 |
Correct |
397 ms |
122240 KB |
Output is correct |
44 |
Correct |
384 ms |
122568 KB |
Output is correct |
45 |
Correct |
398 ms |
122108 KB |
Output is correct |
46 |
Correct |
547 ms |
124664 KB |
Output is correct |
47 |
Correct |
565 ms |
123592 KB |
Output is correct |
48 |
Correct |
576 ms |
125444 KB |
Output is correct |
49 |
Correct |
570 ms |
123484 KB |
Output is correct |
50 |
Correct |
584 ms |
119244 KB |
Output is correct |
51 |
Correct |
610 ms |
119168 KB |
Output is correct |
52 |
Correct |
603 ms |
118628 KB |
Output is correct |
53 |
Correct |
596 ms |
119780 KB |
Output is correct |