#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(),x.end()
#define abcorz ios_base::sync_with_stdio(false);cin.tie(0);
const int N=400005;
const int K=20;
int n;
vector<int> v(N),deep(N);
struct sparse_table{
vector<vector<int>> mx;
int cmp(int i,int j){
return (v[i]>v[j]?i:j);
}
void init(vector<int> &track){
mx.resize(K,vector<int>(N));
int n=track.size();
for(int i=0;i<n;i++){
mx[0][i]=track[i];
}
for(int i=1;i<K;i++){
for(int j=0;j+(1<<i)<=n;j++){
mx[i][j]=cmp(mx[i-1][j],mx[i-1][j+(1<<(i-1))]);
}
}
}
int ask(int l,int r){
int p=__lg(r-l);
return cmp(mx[p][l],mx[p][r-(1<<p)]);
}
}sp;
struct sparse_table1{
vector<vector<int>> mx;
int cmp(int i,int j){
return (deep[i]<deep[j]?i:j);
}
void init(vector<int> &track){
mx.resize(K,vector<int>(N));
int n=track.size();
for(int i=0;i<n;i++){
mx[0][i]=track[i];
}
for(int i=1;i<K;i++){
for(int j=0;j+(1<<i)<=n;j++){
mx[i][j]=cmp(mx[i-1][j],mx[i-1][j+(1<<(i-1))]);
}
}
}
int ask(int l,int r){
int p=__lg(r-l);
return cmp(mx[p][l],mx[p][r-(1<<p)]);
}
}sp1;
vector<int> adj[N];
vector<int> track,in(N);
vector<int> tr[N];
void dfs(int k,int pa){
in[k]=track.size();
tr[k].push_back(track.size());
track.push_back(k);
for(int j:adj[k]){
if(j==pa)continue;
deep[j]=deep[k]+1;
dfs(j,k);
tr[k].push_back(track.size());
track.push_back(k);
}
}
int dis(int a,int b){
if(in[a]>in[b])swap(a,b);
int lca=sp1.ask(in[a],in[b]+1);
return deep[a]+deep[b]-2*deep[lca];
}
int dc(int l,int r,int rec){
if(r<=l){
return 0;
}
int p=sp.ask(l,r);
if(rec==-1)rec=p;
int mx=0;
int last=l-1;
for(int j:tr[p]){
if(j<l||j>r){
continue;
}
mx=max(mx,dc(last+1,j,p));
last=j;
}
mx=max(mx,dc(last+1,r,p));
return dis(rec,p)+mx;
}
int32_t main(){
abcorz;
cin>>n;
for(int i=1;i<=n;i++){
cin>>v[i];
}
for(int i=1;i<n;i++){
int a,b;cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1,1);
sp.init(track);
sp1.init(track);
cout<<dc(0,track.size(),-1)<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
156956 KB |
Output is correct |
2 |
Correct |
50 ms |
156956 KB |
Output is correct |
3 |
Correct |
54 ms |
156956 KB |
Output is correct |
4 |
Correct |
52 ms |
156952 KB |
Output is correct |
5 |
Correct |
52 ms |
156956 KB |
Output is correct |
6 |
Correct |
50 ms |
157020 KB |
Output is correct |
7 |
Correct |
51 ms |
156960 KB |
Output is correct |
8 |
Correct |
56 ms |
157016 KB |
Output is correct |
9 |
Correct |
51 ms |
156952 KB |
Output is correct |
10 |
Correct |
51 ms |
156920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
156956 KB |
Output is correct |
2 |
Correct |
50 ms |
156956 KB |
Output is correct |
3 |
Correct |
54 ms |
156956 KB |
Output is correct |
4 |
Correct |
52 ms |
156952 KB |
Output is correct |
5 |
Correct |
52 ms |
156956 KB |
Output is correct |
6 |
Correct |
50 ms |
157020 KB |
Output is correct |
7 |
Correct |
51 ms |
156960 KB |
Output is correct |
8 |
Correct |
56 ms |
157016 KB |
Output is correct |
9 |
Correct |
51 ms |
156952 KB |
Output is correct |
10 |
Correct |
51 ms |
156920 KB |
Output is correct |
11 |
Correct |
52 ms |
157208 KB |
Output is correct |
12 |
Correct |
52 ms |
156952 KB |
Output is correct |
13 |
Correct |
52 ms |
156952 KB |
Output is correct |
14 |
Correct |
51 ms |
156952 KB |
Output is correct |
15 |
Correct |
56 ms |
156984 KB |
Output is correct |
16 |
Correct |
52 ms |
157140 KB |
Output is correct |
17 |
Correct |
53 ms |
156956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
156956 KB |
Output is correct |
2 |
Correct |
50 ms |
156956 KB |
Output is correct |
3 |
Correct |
54 ms |
156956 KB |
Output is correct |
4 |
Correct |
52 ms |
156952 KB |
Output is correct |
5 |
Correct |
52 ms |
156956 KB |
Output is correct |
6 |
Correct |
50 ms |
157020 KB |
Output is correct |
7 |
Correct |
51 ms |
156960 KB |
Output is correct |
8 |
Correct |
56 ms |
157016 KB |
Output is correct |
9 |
Correct |
51 ms |
156952 KB |
Output is correct |
10 |
Correct |
51 ms |
156920 KB |
Output is correct |
11 |
Correct |
52 ms |
157208 KB |
Output is correct |
12 |
Correct |
52 ms |
156952 KB |
Output is correct |
13 |
Correct |
52 ms |
156952 KB |
Output is correct |
14 |
Correct |
51 ms |
156952 KB |
Output is correct |
15 |
Correct |
56 ms |
156984 KB |
Output is correct |
16 |
Correct |
52 ms |
157140 KB |
Output is correct |
17 |
Correct |
53 ms |
156956 KB |
Output is correct |
18 |
Correct |
55 ms |
158232 KB |
Output is correct |
19 |
Correct |
56 ms |
157984 KB |
Output is correct |
20 |
Correct |
54 ms |
157976 KB |
Output is correct |
21 |
Correct |
55 ms |
157972 KB |
Output is correct |
22 |
Correct |
53 ms |
157980 KB |
Output is correct |
23 |
Correct |
54 ms |
157976 KB |
Output is correct |
24 |
Correct |
61 ms |
157876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
156956 KB |
Output is correct |
2 |
Correct |
50 ms |
156956 KB |
Output is correct |
3 |
Correct |
54 ms |
156956 KB |
Output is correct |
4 |
Correct |
52 ms |
156952 KB |
Output is correct |
5 |
Correct |
52 ms |
156956 KB |
Output is correct |
6 |
Correct |
50 ms |
157020 KB |
Output is correct |
7 |
Correct |
51 ms |
156960 KB |
Output is correct |
8 |
Correct |
56 ms |
157016 KB |
Output is correct |
9 |
Correct |
51 ms |
156952 KB |
Output is correct |
10 |
Correct |
51 ms |
156920 KB |
Output is correct |
11 |
Correct |
52 ms |
157208 KB |
Output is correct |
12 |
Correct |
52 ms |
156952 KB |
Output is correct |
13 |
Correct |
52 ms |
156952 KB |
Output is correct |
14 |
Correct |
51 ms |
156952 KB |
Output is correct |
15 |
Correct |
56 ms |
156984 KB |
Output is correct |
16 |
Correct |
52 ms |
157140 KB |
Output is correct |
17 |
Correct |
53 ms |
156956 KB |
Output is correct |
18 |
Correct |
55 ms |
158232 KB |
Output is correct |
19 |
Correct |
56 ms |
157984 KB |
Output is correct |
20 |
Correct |
54 ms |
157976 KB |
Output is correct |
21 |
Correct |
55 ms |
157972 KB |
Output is correct |
22 |
Correct |
53 ms |
157980 KB |
Output is correct |
23 |
Correct |
54 ms |
157976 KB |
Output is correct |
24 |
Correct |
61 ms |
157876 KB |
Output is correct |
25 |
Correct |
51 ms |
156952 KB |
Output is correct |
26 |
Correct |
55 ms |
157720 KB |
Output is correct |
27 |
Correct |
56 ms |
157724 KB |
Output is correct |
28 |
Correct |
56 ms |
157724 KB |
Output is correct |
29 |
Correct |
55 ms |
157704 KB |
Output is correct |
30 |
Incorrect |
55 ms |
157464 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
156956 KB |
Output is correct |
2 |
Correct |
50 ms |
156956 KB |
Output is correct |
3 |
Correct |
54 ms |
156956 KB |
Output is correct |
4 |
Correct |
52 ms |
156952 KB |
Output is correct |
5 |
Correct |
52 ms |
156956 KB |
Output is correct |
6 |
Correct |
50 ms |
157020 KB |
Output is correct |
7 |
Correct |
51 ms |
156960 KB |
Output is correct |
8 |
Correct |
56 ms |
157016 KB |
Output is correct |
9 |
Correct |
51 ms |
156952 KB |
Output is correct |
10 |
Correct |
51 ms |
156920 KB |
Output is correct |
11 |
Correct |
52 ms |
157208 KB |
Output is correct |
12 |
Correct |
52 ms |
156952 KB |
Output is correct |
13 |
Correct |
52 ms |
156952 KB |
Output is correct |
14 |
Correct |
51 ms |
156952 KB |
Output is correct |
15 |
Correct |
56 ms |
156984 KB |
Output is correct |
16 |
Correct |
52 ms |
157140 KB |
Output is correct |
17 |
Correct |
53 ms |
156956 KB |
Output is correct |
18 |
Correct |
55 ms |
158232 KB |
Output is correct |
19 |
Correct |
56 ms |
157984 KB |
Output is correct |
20 |
Correct |
54 ms |
157976 KB |
Output is correct |
21 |
Correct |
55 ms |
157972 KB |
Output is correct |
22 |
Correct |
53 ms |
157980 KB |
Output is correct |
23 |
Correct |
54 ms |
157976 KB |
Output is correct |
24 |
Correct |
61 ms |
157876 KB |
Output is correct |
25 |
Correct |
169 ms |
191620 KB |
Output is correct |
26 |
Correct |
172 ms |
191356 KB |
Output is correct |
27 |
Correct |
174 ms |
191416 KB |
Output is correct |
28 |
Correct |
186 ms |
191364 KB |
Output is correct |
29 |
Correct |
202 ms |
192176 KB |
Output is correct |
30 |
Correct |
186 ms |
192424 KB |
Output is correct |
31 |
Correct |
205 ms |
191572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
156980 KB |
Output is correct |
2 |
Incorrect |
51 ms |
156956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
156956 KB |
Output is correct |
2 |
Correct |
50 ms |
156956 KB |
Output is correct |
3 |
Correct |
54 ms |
156956 KB |
Output is correct |
4 |
Correct |
52 ms |
156952 KB |
Output is correct |
5 |
Correct |
52 ms |
156956 KB |
Output is correct |
6 |
Correct |
50 ms |
157020 KB |
Output is correct |
7 |
Correct |
51 ms |
156960 KB |
Output is correct |
8 |
Correct |
56 ms |
157016 KB |
Output is correct |
9 |
Correct |
51 ms |
156952 KB |
Output is correct |
10 |
Correct |
51 ms |
156920 KB |
Output is correct |
11 |
Correct |
52 ms |
157208 KB |
Output is correct |
12 |
Correct |
52 ms |
156952 KB |
Output is correct |
13 |
Correct |
52 ms |
156952 KB |
Output is correct |
14 |
Correct |
51 ms |
156952 KB |
Output is correct |
15 |
Correct |
56 ms |
156984 KB |
Output is correct |
16 |
Correct |
52 ms |
157140 KB |
Output is correct |
17 |
Correct |
53 ms |
156956 KB |
Output is correct |
18 |
Correct |
55 ms |
158232 KB |
Output is correct |
19 |
Correct |
56 ms |
157984 KB |
Output is correct |
20 |
Correct |
54 ms |
157976 KB |
Output is correct |
21 |
Correct |
55 ms |
157972 KB |
Output is correct |
22 |
Correct |
53 ms |
157980 KB |
Output is correct |
23 |
Correct |
54 ms |
157976 KB |
Output is correct |
24 |
Correct |
61 ms |
157876 KB |
Output is correct |
25 |
Correct |
51 ms |
156952 KB |
Output is correct |
26 |
Correct |
55 ms |
157720 KB |
Output is correct |
27 |
Correct |
56 ms |
157724 KB |
Output is correct |
28 |
Correct |
56 ms |
157724 KB |
Output is correct |
29 |
Correct |
55 ms |
157704 KB |
Output is correct |
30 |
Incorrect |
55 ms |
157464 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |