#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5, maxlog = 18, inf = 1e9;
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<int> st;
SegTree() {}
SegTree(int _n) {
n = _n;
N = 1;
while (N < n) {
N <<= 1;
}
st.resize(2 * N, -inf);
}
void maxEq(int i, int 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;
}
}
int getMax(int l, int r) {
int vl = N + l, vr = N + r;
int 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];
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<int> dp(n);
set<tuple<int, int, int>> s;
for (int i = 0; i < n; ++i) {
while (!s.empty() && get<0>(*s.begin()) <= i) {
int v = get<1>(*s.begin()), j = get<2>(*s.begin());
s.erase(s.begin());
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];
int 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) {
s.emplace(mx[v][j], 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:139:21: warning: 'val' may be used uninitialized in this function [-Wmaybe-uninitialized]
139 | int val;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20072 KB |
Output is correct |
2 |
Correct |
4 ms |
20060 KB |
Output is correct |
3 |
Correct |
4 ms |
20060 KB |
Output is correct |
4 |
Correct |
4 ms |
20312 KB |
Output is correct |
5 |
Correct |
4 ms |
20060 KB |
Output is correct |
6 |
Correct |
4 ms |
20056 KB |
Output is correct |
7 |
Correct |
4 ms |
20060 KB |
Output is correct |
8 |
Correct |
4 ms |
20056 KB |
Output is correct |
9 |
Correct |
4 ms |
20056 KB |
Output is correct |
10 |
Correct |
4 ms |
20060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20072 KB |
Output is correct |
2 |
Correct |
4 ms |
20060 KB |
Output is correct |
3 |
Correct |
4 ms |
20060 KB |
Output is correct |
4 |
Correct |
4 ms |
20312 KB |
Output is correct |
5 |
Correct |
4 ms |
20060 KB |
Output is correct |
6 |
Correct |
4 ms |
20056 KB |
Output is correct |
7 |
Correct |
4 ms |
20060 KB |
Output is correct |
8 |
Correct |
4 ms |
20056 KB |
Output is correct |
9 |
Correct |
4 ms |
20056 KB |
Output is correct |
10 |
Correct |
4 ms |
20060 KB |
Output is correct |
11 |
Correct |
4 ms |
20060 KB |
Output is correct |
12 |
Correct |
4 ms |
20056 KB |
Output is correct |
13 |
Correct |
4 ms |
20060 KB |
Output is correct |
14 |
Correct |
6 ms |
20060 KB |
Output is correct |
15 |
Correct |
4 ms |
20060 KB |
Output is correct |
16 |
Correct |
7 ms |
20060 KB |
Output is correct |
17 |
Correct |
4 ms |
20060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20072 KB |
Output is correct |
2 |
Correct |
4 ms |
20060 KB |
Output is correct |
3 |
Correct |
4 ms |
20060 KB |
Output is correct |
4 |
Correct |
4 ms |
20312 KB |
Output is correct |
5 |
Correct |
4 ms |
20060 KB |
Output is correct |
6 |
Correct |
4 ms |
20056 KB |
Output is correct |
7 |
Correct |
4 ms |
20060 KB |
Output is correct |
8 |
Correct |
4 ms |
20056 KB |
Output is correct |
9 |
Correct |
4 ms |
20056 KB |
Output is correct |
10 |
Correct |
4 ms |
20060 KB |
Output is correct |
11 |
Correct |
4 ms |
20060 KB |
Output is correct |
12 |
Correct |
4 ms |
20056 KB |
Output is correct |
13 |
Correct |
4 ms |
20060 KB |
Output is correct |
14 |
Correct |
6 ms |
20060 KB |
Output is correct |
15 |
Correct |
4 ms |
20060 KB |
Output is correct |
16 |
Correct |
7 ms |
20060 KB |
Output is correct |
17 |
Correct |
4 ms |
20060 KB |
Output is correct |
18 |
Correct |
15 ms |
24924 KB |
Output is correct |
19 |
Correct |
13 ms |
25180 KB |
Output is correct |
20 |
Correct |
13 ms |
25180 KB |
Output is correct |
21 |
Correct |
18 ms |
26968 KB |
Output is correct |
22 |
Correct |
19 ms |
27016 KB |
Output is correct |
23 |
Correct |
18 ms |
26968 KB |
Output is correct |
24 |
Correct |
18 ms |
26972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20072 KB |
Output is correct |
2 |
Correct |
4 ms |
20060 KB |
Output is correct |
3 |
Correct |
4 ms |
20060 KB |
Output is correct |
4 |
Correct |
4 ms |
20312 KB |
Output is correct |
5 |
Correct |
4 ms |
20060 KB |
Output is correct |
6 |
Correct |
4 ms |
20056 KB |
Output is correct |
7 |
Correct |
4 ms |
20060 KB |
Output is correct |
8 |
Correct |
4 ms |
20056 KB |
Output is correct |
9 |
Correct |
4 ms |
20056 KB |
Output is correct |
10 |
Correct |
4 ms |
20060 KB |
Output is correct |
11 |
Correct |
4 ms |
20060 KB |
Output is correct |
12 |
Correct |
4 ms |
20056 KB |
Output is correct |
13 |
Correct |
4 ms |
20060 KB |
Output is correct |
14 |
Correct |
6 ms |
20060 KB |
Output is correct |
15 |
Correct |
4 ms |
20060 KB |
Output is correct |
16 |
Correct |
7 ms |
20060 KB |
Output is correct |
17 |
Correct |
4 ms |
20060 KB |
Output is correct |
18 |
Correct |
15 ms |
24924 KB |
Output is correct |
19 |
Correct |
13 ms |
25180 KB |
Output is correct |
20 |
Correct |
13 ms |
25180 KB |
Output is correct |
21 |
Correct |
18 ms |
26968 KB |
Output is correct |
22 |
Correct |
19 ms |
27016 KB |
Output is correct |
23 |
Correct |
18 ms |
26968 KB |
Output is correct |
24 |
Correct |
18 ms |
26972 KB |
Output is correct |
25 |
Correct |
4 ms |
20312 KB |
Output is correct |
26 |
Correct |
15 ms |
25540 KB |
Output is correct |
27 |
Correct |
15 ms |
25396 KB |
Output is correct |
28 |
Correct |
15 ms |
25436 KB |
Output is correct |
29 |
Correct |
15 ms |
25432 KB |
Output is correct |
30 |
Correct |
17 ms |
25944 KB |
Output is correct |
31 |
Correct |
15 ms |
25944 KB |
Output is correct |
32 |
Correct |
16 ms |
25948 KB |
Output is correct |
33 |
Correct |
16 ms |
25944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20072 KB |
Output is correct |
2 |
Correct |
4 ms |
20060 KB |
Output is correct |
3 |
Correct |
4 ms |
20060 KB |
Output is correct |
4 |
Correct |
4 ms |
20312 KB |
Output is correct |
5 |
Correct |
4 ms |
20060 KB |
Output is correct |
6 |
Correct |
4 ms |
20056 KB |
Output is correct |
7 |
Correct |
4 ms |
20060 KB |
Output is correct |
8 |
Correct |
4 ms |
20056 KB |
Output is correct |
9 |
Correct |
4 ms |
20056 KB |
Output is correct |
10 |
Correct |
4 ms |
20060 KB |
Output is correct |
11 |
Correct |
4 ms |
20060 KB |
Output is correct |
12 |
Correct |
4 ms |
20056 KB |
Output is correct |
13 |
Correct |
4 ms |
20060 KB |
Output is correct |
14 |
Correct |
6 ms |
20060 KB |
Output is correct |
15 |
Correct |
4 ms |
20060 KB |
Output is correct |
16 |
Correct |
7 ms |
20060 KB |
Output is correct |
17 |
Correct |
4 ms |
20060 KB |
Output is correct |
18 |
Correct |
15 ms |
24924 KB |
Output is correct |
19 |
Correct |
13 ms |
25180 KB |
Output is correct |
20 |
Correct |
13 ms |
25180 KB |
Output is correct |
21 |
Correct |
18 ms |
26968 KB |
Output is correct |
22 |
Correct |
19 ms |
27016 KB |
Output is correct |
23 |
Correct |
18 ms |
26968 KB |
Output is correct |
24 |
Correct |
18 ms |
26972 KB |
Output is correct |
25 |
Incorrect |
653 ms |
107560 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20060 KB |
Output is correct |
2 |
Correct |
5 ms |
20156 KB |
Output is correct |
3 |
Correct |
1311 ms |
201740 KB |
Output is correct |
4 |
Correct |
1254 ms |
200580 KB |
Output is correct |
5 |
Correct |
1334 ms |
200792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20072 KB |
Output is correct |
2 |
Correct |
4 ms |
20060 KB |
Output is correct |
3 |
Correct |
4 ms |
20060 KB |
Output is correct |
4 |
Correct |
4 ms |
20312 KB |
Output is correct |
5 |
Correct |
4 ms |
20060 KB |
Output is correct |
6 |
Correct |
4 ms |
20056 KB |
Output is correct |
7 |
Correct |
4 ms |
20060 KB |
Output is correct |
8 |
Correct |
4 ms |
20056 KB |
Output is correct |
9 |
Correct |
4 ms |
20056 KB |
Output is correct |
10 |
Correct |
4 ms |
20060 KB |
Output is correct |
11 |
Correct |
4 ms |
20060 KB |
Output is correct |
12 |
Correct |
4 ms |
20056 KB |
Output is correct |
13 |
Correct |
4 ms |
20060 KB |
Output is correct |
14 |
Correct |
6 ms |
20060 KB |
Output is correct |
15 |
Correct |
4 ms |
20060 KB |
Output is correct |
16 |
Correct |
7 ms |
20060 KB |
Output is correct |
17 |
Correct |
4 ms |
20060 KB |
Output is correct |
18 |
Correct |
15 ms |
24924 KB |
Output is correct |
19 |
Correct |
13 ms |
25180 KB |
Output is correct |
20 |
Correct |
13 ms |
25180 KB |
Output is correct |
21 |
Correct |
18 ms |
26968 KB |
Output is correct |
22 |
Correct |
19 ms |
27016 KB |
Output is correct |
23 |
Correct |
18 ms |
26968 KB |
Output is correct |
24 |
Correct |
18 ms |
26972 KB |
Output is correct |
25 |
Correct |
4 ms |
20312 KB |
Output is correct |
26 |
Correct |
15 ms |
25540 KB |
Output is correct |
27 |
Correct |
15 ms |
25396 KB |
Output is correct |
28 |
Correct |
15 ms |
25436 KB |
Output is correct |
29 |
Correct |
15 ms |
25432 KB |
Output is correct |
30 |
Correct |
17 ms |
25944 KB |
Output is correct |
31 |
Correct |
15 ms |
25944 KB |
Output is correct |
32 |
Correct |
16 ms |
25948 KB |
Output is correct |
33 |
Correct |
16 ms |
25944 KB |
Output is correct |
34 |
Incorrect |
653 ms |
107560 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |