This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |