#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 2e5 + 10;
int n, a[maxn], used[maxn], dp[maxn], pos[maxn];
vector < int > adj[maxn];
int rec(int val)
{
if (used[val])
return dp[val];
used[val] = 1;
int left = 0, right = n + 1;
for (int i = n; i > val; i --)
{
if (pos[i] > pos[val])
{
right = min(right, pos[i]);
}
else
{
left = max(left, pos[i]);
}
}
int mx = -1;
for (int i = pos[val] + 1; i < right; i ++)
{
if (a[i] > mx)
{
mx = a[i];
dp[val] = max(dp[val], i - pos[val] + rec(a[i]));
}
}
mx = -1;
for (int i = pos[val] - 1; i > left; i --)
{
if (a[i] > mx)
{
mx = a[i];
dp[val] = max(dp[val], pos[val] - i + rec(a[i]));
}
}
return dp[val];
}
int dfs(int v, int p, int mx, int dis, int val)
{
if (mx > val)
return 0;
int ans = 0;
if (a[v] == mx)
ans = max(ans, dis + dp[v]);
for (int u : adj[v])
{
if (u == p)
continue;
ans = max(ans, dfs(u, v, max(mx, a[u]), dis + 1, val));
}
return ans;
}
int bef[maxn], aft[maxn];
pair < int, int > tree[4 * maxn];
void build_tree(int root, int left, int right)
{
if (left == right)
{
tree[root] = {a[left], left};
return;
}
int mid = (left + right) / 2;
build_tree(root * 2, left, mid);
build_tree(root * 2 + 1, mid + 1, right);
tree[root] = max(tree[root * 2], tree[root * 2 + 1]);
}
pair < int, int > query(int root, int left, int right, int qleft, int qright)
{
if (left > qright || right < qleft)
return {-1, -1};
if (left >= qleft && right <= qright)
return tree[root];
int mid = (left + right) / 2;
return max(query(root * 2, left, mid, qleft, qright),
query(root * 2 + 1, mid + 1, right, qleft, qright));
}
void solve()
{
cin >> n;
for (int i = 1; i <= n; i ++)
{
cin >> a[i];
pos[a[i]] = i;
}
for (int i = 1; i < n; i ++)
{
int v, u;
cin >> v >> u;
adj[v].push_back(u);
adj[u].push_back(v);
}
if (n <= 5000)
{
for (int i = 1; i <= n; i ++)
{
int v = pos[i];
for (int u : adj[v])
{
dp[v] = max(dp[v], dfs(u, v, a[u], 1, a[v]));
}
///cout << v << " : " << dp[v] << endl;
}
cout << dp[pos[n]] << endl;
}
else
{
stack < int > st;
for (int i = 1; i <= n; i ++)
{
while(!st.empty() && a[st.top()] < a[i])
st.pop();
if (!st.empty())
bef[i] = st.top();
///cout << "stack " << i << " " << st.top() << endl;
st.push(i);
}
while(!st.empty())
st.pop();
for (int i = n; i > 0; i --)
{
while(!st.empty() && a[st.top()] < a[i])
st.pop();
if (!st.empty())
aft[i] = st.top();
else
aft[i] = n + 1;
st.push(i);
}
build_tree(1, 1, n);
set < int > to_right, to_left;
set < int > :: iterator it, pt;
for (int i = 1; i <= n; i ++)
{
int v = pos[i];
///cout << "now " << v << " " << bef[v] << " " <<aft[v] << endl;
pair < int, int > lf = query(1, 1, n, bef[v] + 1, v - 1);
pair < int, int > rf = query(1, 1, n, v + 1, aft[v] - 1);
///cout << v << " " << lf.second << " " << rf.second << endl;
if (lf.first != -1)
dp[v] = max(dp[v], dp[lf.first] + v - lf.second);
if (rf.first != -1)
dp[v] = max(dp[v], dp[rf.first] + rf.second - v);
}
cout << dp[pos[n]] << endl;
}
}
int main()
{
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
5040 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
5040 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
93 ms |
5528 KB |
Output is correct |
19 |
Correct |
84 ms |
5572 KB |
Output is correct |
20 |
Correct |
90 ms |
5696 KB |
Output is correct |
21 |
Correct |
6 ms |
5460 KB |
Output is correct |
22 |
Correct |
6 ms |
5460 KB |
Output is correct |
23 |
Correct |
6 ms |
5460 KB |
Output is correct |
24 |
Correct |
7 ms |
5332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
5040 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
93 ms |
5528 KB |
Output is correct |
19 |
Correct |
84 ms |
5572 KB |
Output is correct |
20 |
Correct |
90 ms |
5696 KB |
Output is correct |
21 |
Correct |
6 ms |
5460 KB |
Output is correct |
22 |
Correct |
6 ms |
5460 KB |
Output is correct |
23 |
Correct |
6 ms |
5460 KB |
Output is correct |
24 |
Correct |
7 ms |
5332 KB |
Output is correct |
25 |
Correct |
2 ms |
4948 KB |
Output is correct |
26 |
Correct |
85 ms |
5392 KB |
Output is correct |
27 |
Correct |
85 ms |
5476 KB |
Output is correct |
28 |
Correct |
88 ms |
5464 KB |
Output is correct |
29 |
Correct |
98 ms |
5524 KB |
Output is correct |
30 |
Correct |
11 ms |
5208 KB |
Output is correct |
31 |
Correct |
19 ms |
5252 KB |
Output is correct |
32 |
Correct |
12 ms |
5216 KB |
Output is correct |
33 |
Correct |
14 ms |
5244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
5040 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
93 ms |
5528 KB |
Output is correct |
19 |
Correct |
84 ms |
5572 KB |
Output is correct |
20 |
Correct |
90 ms |
5696 KB |
Output is correct |
21 |
Correct |
6 ms |
5460 KB |
Output is correct |
22 |
Correct |
6 ms |
5460 KB |
Output is correct |
23 |
Correct |
6 ms |
5460 KB |
Output is correct |
24 |
Correct |
7 ms |
5332 KB |
Output is correct |
25 |
Incorrect |
227 ms |
23332 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Incorrect |
271 ms |
22796 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
5040 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
93 ms |
5528 KB |
Output is correct |
19 |
Correct |
84 ms |
5572 KB |
Output is correct |
20 |
Correct |
90 ms |
5696 KB |
Output is correct |
21 |
Correct |
6 ms |
5460 KB |
Output is correct |
22 |
Correct |
6 ms |
5460 KB |
Output is correct |
23 |
Correct |
6 ms |
5460 KB |
Output is correct |
24 |
Correct |
7 ms |
5332 KB |
Output is correct |
25 |
Correct |
2 ms |
4948 KB |
Output is correct |
26 |
Correct |
85 ms |
5392 KB |
Output is correct |
27 |
Correct |
85 ms |
5476 KB |
Output is correct |
28 |
Correct |
88 ms |
5464 KB |
Output is correct |
29 |
Correct |
98 ms |
5524 KB |
Output is correct |
30 |
Correct |
11 ms |
5208 KB |
Output is correct |
31 |
Correct |
19 ms |
5252 KB |
Output is correct |
32 |
Correct |
12 ms |
5216 KB |
Output is correct |
33 |
Correct |
14 ms |
5244 KB |
Output is correct |
34 |
Incorrect |
227 ms |
23332 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |