#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 1000000000000000000
#define MOD 998244353
int p[200000], spt[200000][18];
vector<int> adj[200000];
int query(int x, int y) {
int k = (int)floor(log((double)y-x+1)/log(2.0));
if(p[spt[x][k]] > p[spt[y-(1<<k)+1][k]]) return spt[x][k];
else return spt[y-(1<<k)+1][k];
}
int dfs(int x, int l, int r) {
int ans = 0;
if(x != l) ans = max(ans, dfs(query(l, x-1), l, x-1)+x-query(l, x-1));
if(x != r) ans = max(ans, dfs(query(x+1, r), x+1, r)+query(x+1, r)-x);
return ans;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, x, y;
cin >> n;
for(int i = 0; i < n; i++) cin >> p[i];
for(int i = 0; i < n-1; i++) {
cin >> x >> y;
x--; y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
for(int i = 0; i < n; i++) spt[i][0] = i;
for(int j = 1; (1<<j) <= n; j++) {
for(int i = 0; i + (1<<j) - 1 < n; i++) {
if(p[spt[i][j-1]] > p[spt[i+(1<<(j-1))][j-1]]) {
spt[i][j] = spt[i][j-1];
} else spt[i][j] = spt[i+(1<<(j-1))][j-1];
}
}
cout << dfs(query(0, n-1), 0, n-1);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8840 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8840 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8792 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8796 KB |
Output is correct |
15 |
Correct |
1 ms |
8796 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8840 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8792 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8796 KB |
Output is correct |
15 |
Correct |
1 ms |
8796 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
4 ms |
9060 KB |
Output is correct |
19 |
Correct |
4 ms |
9052 KB |
Output is correct |
20 |
Correct |
3 ms |
9052 KB |
Output is correct |
21 |
Correct |
3 ms |
8792 KB |
Output is correct |
22 |
Correct |
3 ms |
8792 KB |
Output is correct |
23 |
Correct |
3 ms |
8796 KB |
Output is correct |
24 |
Correct |
3 ms |
8792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8840 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8792 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8796 KB |
Output is correct |
15 |
Correct |
1 ms |
8796 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
4 ms |
9060 KB |
Output is correct |
19 |
Correct |
4 ms |
9052 KB |
Output is correct |
20 |
Correct |
3 ms |
9052 KB |
Output is correct |
21 |
Correct |
3 ms |
8792 KB |
Output is correct |
22 |
Correct |
3 ms |
8792 KB |
Output is correct |
23 |
Correct |
3 ms |
8796 KB |
Output is correct |
24 |
Correct |
3 ms |
8792 KB |
Output is correct |
25 |
Correct |
1 ms |
8796 KB |
Output is correct |
26 |
Incorrect |
3 ms |
8796 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8840 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8792 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8796 KB |
Output is correct |
15 |
Correct |
1 ms |
8796 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
4 ms |
9060 KB |
Output is correct |
19 |
Correct |
4 ms |
9052 KB |
Output is correct |
20 |
Correct |
3 ms |
9052 KB |
Output is correct |
21 |
Correct |
3 ms |
8792 KB |
Output is correct |
22 |
Correct |
3 ms |
8792 KB |
Output is correct |
23 |
Correct |
3 ms |
8796 KB |
Output is correct |
24 |
Correct |
3 ms |
8792 KB |
Output is correct |
25 |
Correct |
98 ms |
50800 KB |
Output is correct |
26 |
Correct |
93 ms |
53372 KB |
Output is correct |
27 |
Correct |
97 ms |
53404 KB |
Output is correct |
28 |
Correct |
97 ms |
44924 KB |
Output is correct |
29 |
Correct |
93 ms |
44932 KB |
Output is correct |
30 |
Correct |
88 ms |
44888 KB |
Output is correct |
31 |
Correct |
96 ms |
44676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Incorrect |
1 ms |
8844 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8840 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8792 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8796 KB |
Output is correct |
15 |
Correct |
1 ms |
8796 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
4 ms |
9060 KB |
Output is correct |
19 |
Correct |
4 ms |
9052 KB |
Output is correct |
20 |
Correct |
3 ms |
9052 KB |
Output is correct |
21 |
Correct |
3 ms |
8792 KB |
Output is correct |
22 |
Correct |
3 ms |
8792 KB |
Output is correct |
23 |
Correct |
3 ms |
8796 KB |
Output is correct |
24 |
Correct |
3 ms |
8792 KB |
Output is correct |
25 |
Correct |
1 ms |
8796 KB |
Output is correct |
26 |
Incorrect |
3 ms |
8796 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |