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 pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, P[200005];
struct node{
int s, e, m, val, pos;
node *l, *r;
node(int _s, int _e){
s = _s, e = _e, m = (s + e) >> 1;
if(s != e){
l = new node(s, m), r = new node(m+1, e);
val = max(l->val, r->val);
if(l->val >= r->val)pos = l->pos;
else pos = r->pos;
}
else pos = s, val = P[s];
}
pi qry(int a, int b){
if(s == a && b == e)return {val, pos};
if(b <= m)return l->qry(a, b);
if(a > m)return r->qry(a, b);
return max(l->qry(a, m), r->qry(m+1, b));
}
}*root;
int grr(int l, int r){
if(l >= r)return 0;
pi tmp = root->qry(l, r);
int ans = 0;
if(tmp.se != l){
pi tmp2 = root->qry(l, tmp.se - 1);
ans = max(ans, grr(l, tmp.se - 1) + tmp.se - tmp2.se);
}
if(tmp.se != r){
pi tmp2 = root->qry(tmp.se + 1, r);
ans = max(ans, grr(tmp.se + 1, r) + tmp2.se - tmp.se);
}
return ans;
}
void solve(){
cin >> n;
for(int i = 1; i <= n; i++)cin >> P[i];
root = new node(1, n);
for(int i = 1; i < n; i++){
int a, b; cin >> a >> b;
}
cout << grr(1, n);
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message (stderr)
Main.cpp:61:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
61 | main(){
| ^~~~
# | 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... |