#include<bits/stdc++.h>
#define ll long long
#define ldb long double
#define fi first
#define se second
#define sza(a) (int)a.size()
#define pir pair<int,int>
#define pirll pair<ll,ll>
using namespace std;
const int maxn = 2e5 + 5;
vector <vector<int>> vec(maxn);
int a[maxn];
int input(int n){
int root = 0;
for (int i = 1 ; i <= n ; i++){
cin >> a[i];
if (a[i] == n) root = i;
}
for (int i = 1 ; i < n ; i++){
int u,v;
cin >> u >> v;
vec[u].push_back(v);
vec[v].push_back(u);
}
return root;
}
namespace sub_1_2_3_4{
bool check(int n){
return (n <= 5000);
}
const int N = 5090;
int node[N],dp[N];
int dfs(int u,int par,int len,int cap){
int res = dp[u] + len;
for (int v : vec[u])
if (v != par && a[v] < cap)
res = max(res,dfs(v,u,len + 1,cap));
return res;
}
ll solve(int n,int root){
for (int i = 1 ; i <= n ; i++)
node[a[i]] = i;
for (int i = 1 ; i <= n ; i++)
dp[node[i]] = dfs(node[i],node[i],0,i);
return dp[root];
}
}
namespace sub5{
const ll inf = 1e17;
struct segment_tree{
ll seg[4*maxn];
void init(int l,int r,int v){
seg[v] = -inf;
if (l == r) return;
int w = (l + r)/2;
init(l,w,2*v + 1);
init(w + 1,r,2*v + 2);
}
void update(int l,int r,int v,int p,ll val){
if (l > p || r < p) return;
if (l == r){
seg[v] = val;
return;
}
int w = (l + r)/2;
update(l,w,2*v + 1,p,val);
update(w + 1,r,2*v + 2,p,val);
seg[v] = max(seg[2*v + 1],seg[2*v + 2]);
}
ll calc(int l,int r,int v,int lx,int rx){
if (l > rx || r < lx) return -inf;
if (l >= lx && r <= rx) return seg[v];
int w = (l + r)/2;
return max(calc(l,w,2*v + 1,lx,rx),calc(w + 1,r,2*v + 2,lx,rx));
}
};
segment_tree U,D;
int node[maxn],nxt[maxn],lst[maxn];
ll dp[maxn];
void prepare(int n){
U.init(1,n,0);
D.init(1,n,0);
for (int i = 1 ; i <= n ; i++){
nxt[i] = n + 1;
node[a[i]] = i;
}
U.update(1,n,0,node[1],node[1]);
D.update(1,n,0,node[1],-node[1]);
deque <int> dq;
for (int i = 1 ; i <= n ; i++){
while (dq.size() && a[dq.back()] < a[i]) dq.pop_back();
lst[i] = (!dq.size()) ? 1 : (dq.back() + 1);
dq.push_back(i);
}
dq.clear();
for (int i = n ; i > 0 ; i--){
while (dq.size() && a[dq.back()] < a[i]) dq.pop_back();
nxt[i] = (!dq.size()) ? n : (dq.back() - 1);
dq.push_back(i);
}
}
ll solve(int n,int root){
prepare(n);
ll res = 0;
for (int i = 2 ; i <= n ; i++){
int p = node[i];
ll v = max(U.calc(1,n,0,p,nxt[p]) - p,D.calc(1,n,0,lst[p],p) + p);
v = max(v,0LL);
U.update(1,n,0,p,v + p);
D.update(1,n,0,p,v - p);
res = max(res,v);
}
return res;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
// freopen("CAT.inp","r",stdin);
// freopen("CAT.out","w",stdout);
int n;
cin >> n;
int root = input(n);
if (sub_1_2_3_4::check(n)){
cout << sub_1_2_3_4::solve(n,root);
return 0;
}
cout << sub5::solve(n,root);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5056 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5056 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
85 ms |
5776 KB |
Output is correct |
19 |
Correct |
76 ms |
5804 KB |
Output is correct |
20 |
Correct |
82 ms |
5904 KB |
Output is correct |
21 |
Correct |
4 ms |
5724 KB |
Output is correct |
22 |
Correct |
4 ms |
5468 KB |
Output is correct |
23 |
Correct |
4 ms |
5468 KB |
Output is correct |
24 |
Correct |
3 ms |
5468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5056 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
85 ms |
5776 KB |
Output is correct |
19 |
Correct |
76 ms |
5804 KB |
Output is correct |
20 |
Correct |
82 ms |
5904 KB |
Output is correct |
21 |
Correct |
4 ms |
5724 KB |
Output is correct |
22 |
Correct |
4 ms |
5468 KB |
Output is correct |
23 |
Correct |
4 ms |
5468 KB |
Output is correct |
24 |
Correct |
3 ms |
5468 KB |
Output is correct |
25 |
Correct |
2 ms |
5172 KB |
Output is correct |
26 |
Correct |
87 ms |
5632 KB |
Output is correct |
27 |
Correct |
92 ms |
5468 KB |
Output is correct |
28 |
Correct |
96 ms |
5716 KB |
Output is correct |
29 |
Correct |
90 ms |
5716 KB |
Output is correct |
30 |
Correct |
7 ms |
5464 KB |
Output is correct |
31 |
Correct |
10 ms |
5472 KB |
Output is correct |
32 |
Correct |
8 ms |
5464 KB |
Output is correct |
33 |
Correct |
8 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5056 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
85 ms |
5776 KB |
Output is correct |
19 |
Correct |
76 ms |
5804 KB |
Output is correct |
20 |
Correct |
82 ms |
5904 KB |
Output is correct |
21 |
Correct |
4 ms |
5724 KB |
Output is correct |
22 |
Correct |
4 ms |
5468 KB |
Output is correct |
23 |
Correct |
4 ms |
5468 KB |
Output is correct |
24 |
Correct |
3 ms |
5468 KB |
Output is correct |
25 |
Correct |
125 ms |
26960 KB |
Output is correct |
26 |
Correct |
117 ms |
26788 KB |
Output is correct |
27 |
Correct |
118 ms |
26772 KB |
Output is correct |
28 |
Correct |
167 ms |
26392 KB |
Output is correct |
29 |
Correct |
161 ms |
26444 KB |
Output is correct |
30 |
Correct |
166 ms |
26452 KB |
Output is correct |
31 |
Correct |
155 ms |
26436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Incorrect |
158 ms |
26212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5056 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
85 ms |
5776 KB |
Output is correct |
19 |
Correct |
76 ms |
5804 KB |
Output is correct |
20 |
Correct |
82 ms |
5904 KB |
Output is correct |
21 |
Correct |
4 ms |
5724 KB |
Output is correct |
22 |
Correct |
4 ms |
5468 KB |
Output is correct |
23 |
Correct |
4 ms |
5468 KB |
Output is correct |
24 |
Correct |
3 ms |
5468 KB |
Output is correct |
25 |
Correct |
2 ms |
5172 KB |
Output is correct |
26 |
Correct |
87 ms |
5632 KB |
Output is correct |
27 |
Correct |
92 ms |
5468 KB |
Output is correct |
28 |
Correct |
96 ms |
5716 KB |
Output is correct |
29 |
Correct |
90 ms |
5716 KB |
Output is correct |
30 |
Correct |
7 ms |
5464 KB |
Output is correct |
31 |
Correct |
10 ms |
5472 KB |
Output is correct |
32 |
Correct |
8 ms |
5464 KB |
Output is correct |
33 |
Correct |
8 ms |
5212 KB |
Output is correct |
34 |
Correct |
125 ms |
26960 KB |
Output is correct |
35 |
Correct |
117 ms |
26788 KB |
Output is correct |
36 |
Correct |
118 ms |
26772 KB |
Output is correct |
37 |
Correct |
167 ms |
26392 KB |
Output is correct |
38 |
Correct |
161 ms |
26444 KB |
Output is correct |
39 |
Correct |
166 ms |
26452 KB |
Output is correct |
40 |
Correct |
155 ms |
26436 KB |
Output is correct |
41 |
Correct |
2 ms |
5212 KB |
Output is correct |
42 |
Correct |
2 ms |
4956 KB |
Output is correct |
43 |
Incorrect |
158 ms |
26212 KB |
Output isn't correct |
44 |
Halted |
0 ms |
0 KB |
- |