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;
const int nmax = 2e5 + 1;
int v[nmax], n;
struct AINT {
int ma[nmax * 4], ai[nmax * 4];
void build(int nod = 1, int st = 1, int dr = n) {
if(st == dr) {
ma[nod] = v[st];
ai[nod] = st;
return;
}
int mid = (st + dr) / 2;
build(nod * 2, st, mid);
build(nod * 2 + 1, mid + 1, dr);
if(ma[nod * 2] > ma[nod * 2 + 1]) {
ma[nod] = ma[nod * 2];
ai[nod] = ai[nod * 2];
} else {
ma[nod] = ma[nod * 2 + 1];
ai[nod] = ai[nod * 2 + 1];
}
}
int query(int l, int r, int nod = 1, int st = 1, int dr = n) {
if(dr < l || r < st) {
return 0;
}
if(l <= st && dr <= r) {
return ai[nod];
}
int mid = (st + dr) / 2;
int i1 = query(l, r, nod * 2, st, mid);
int i2 = query(l, r, nod * 2 + 1, mid + 1, dr);
if(v[i1] > v[i2]) {
return i1;
}
return i2;
}
} st;
int d[nmax];
int maxx = 0;
void solve(int l, int r, int loc, int curr) {
if(l == r) {
maxx = max(maxx, curr);
return;
}
int x;
if(loc == 0) {
// stanga
x = st.query(l + 1, r);
solve(l + 1, x, 1, curr + x - l);
solve(x, r, 0, curr + x - l);
} else {
//dreapta
x = st.query(l, r - 1);
solve(l, x, 1, curr + r - x);
solve(x, r - 1, 0, curr + r - x);
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
int pn;
for(int i = 1; i <= n; i++) {
cin >> v[i];
if(v[i] == n) {
pn = i;
}
}
for(int i = 1; i < n; i++) {
int useless;
cin >> useless >> useless;
}
st.build();
solve(1, pn, 1, 0);
solve(pn, n, 0, 0);
cout << maxx;
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int32_t main()':
Main.cpp:86:8: warning: 'pn' may be used uninitialized in this function [-Wmaybe-uninitialized]
86 | solve(pn, n, 0, 0);
| ~~~~~^~~~~~~~~~~~~
# | 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... |