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>
#define loop(i,a,b) for(int i = (a); i < (b); i ++)
#define pb push_back
#define ins insert
#define pii pair<int,int>
#define ff first
#define ss second
#define op(x) cerr << #x << " = " << x << endl;
#define opa(x) cerr << #x << " = " << x << ", ";
#define spac cerr << ' ';
#define entr cerr << endl;
#define STL(x) cerr << #x << " : "; for(auto &qwe:x) cerr << qwe << ' '; cerr << endl;
#define ARR(x, nnn) cerr << #x << " : "; loop(qwe,0,nnn) cerr << x[qwe] << ' '; cerr << endl;
#define bye exit(0);
using namespace std;
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
ostream& operator<<(ostream& os, pii A){ os << "[" << A.ff << ", " << A.ss << "]"; }
const int mxn = (int)(2e5) + 10;
const int INF = (int)(1e9);
int a[mxn];
vector<vector<int>> seg;
inline int merg(int A, int B){
return (a[A] > a[B]) ? (A) : (B);
}
inline void init(int n){
seg.pb({});
loop(i,0,n) seg[0].pb(i);
for(int half_len = 1; half_len <= n; half_len <<= 1){
seg.pb({});
for(int i = 0; i + half_len * 2 - 1 < n; i ++){
seg.back().pb(merg(seg[seg.size() - 2][i], seg[seg.size() - 2][i + half_len]));
}
}
}
inline int qry(int l, int r){
if(l == r) return seg[0][l];
int lg_sz = __lg(r - l + 1);
return merg(seg[lg_sz][l], seg[lg_sz][r - (1 << lg_sz) + 1]);
}
long long run(int l, int r, int now){
long long ret = 0;
if(now != l){
int nxt = qry(l, now - 1);
ret = max(ret, run(l, now - 1, nxt) + abs(now - nxt));
}
if(now != r){
int nxt = qry(now + 1, r);
ret = max(ret, run(now + 1, r, nxt) + abs(nxt - now));
}
return ret;
}
int main(){
ios::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
loop(i,0,n) cin >> a[i];
init(n);
int tmp;
loop(i,0,n-1) cin >> tmp >> tmp;
long long ans = run(0, n - 1, qry(0, n - 1));
cout << ans << '\n';
}
/*
5
1 5 3 4 5
*/
Compilation message (stderr)
Main.cpp: In function 'std::ostream& operator<<(std::ostream&, std::pair<int, int>)':
Main.cpp:17:84: warning: no return statement in function returning non-void [-Wreturn-type]
17 | ostream& operator<<(ostream& os, pii A){ os << "[" << A.ff << ", " << A.ss << "]"; }
| ^
# | 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... |