#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];
long long maxx = 0;
void solve(int l, int r, int loc, long long 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
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);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4432 KB |
Output is correct |
2 |
Correct |
2 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
2 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
2 ms |
4432 KB |
Output is correct |
10 |
Correct |
2 ms |
4432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4432 KB |
Output is correct |
2 |
Correct |
2 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
2 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
2 ms |
4432 KB |
Output is correct |
10 |
Correct |
2 ms |
4432 KB |
Output is correct |
11 |
Correct |
2 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
2 ms |
4432 KB |
Output is correct |
15 |
Correct |
2 ms |
4432 KB |
Output is correct |
16 |
Correct |
2 ms |
4432 KB |
Output is correct |
17 |
Correct |
1 ms |
4432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4432 KB |
Output is correct |
2 |
Correct |
2 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
2 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
2 ms |
4432 KB |
Output is correct |
10 |
Correct |
2 ms |
4432 KB |
Output is correct |
11 |
Correct |
2 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
2 ms |
4432 KB |
Output is correct |
15 |
Correct |
2 ms |
4432 KB |
Output is correct |
16 |
Correct |
2 ms |
4432 KB |
Output is correct |
17 |
Correct |
1 ms |
4432 KB |
Output is correct |
18 |
Correct |
3 ms |
4688 KB |
Output is correct |
19 |
Correct |
2 ms |
4688 KB |
Output is correct |
20 |
Correct |
5 ms |
4688 KB |
Output is correct |
21 |
Correct |
3 ms |
4600 KB |
Output is correct |
22 |
Correct |
3 ms |
4432 KB |
Output is correct |
23 |
Correct |
2 ms |
4432 KB |
Output is correct |
24 |
Correct |
3 ms |
4432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4432 KB |
Output is correct |
2 |
Correct |
2 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
2 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
2 ms |
4432 KB |
Output is correct |
10 |
Correct |
2 ms |
4432 KB |
Output is correct |
11 |
Correct |
2 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
2 ms |
4432 KB |
Output is correct |
15 |
Correct |
2 ms |
4432 KB |
Output is correct |
16 |
Correct |
2 ms |
4432 KB |
Output is correct |
17 |
Correct |
1 ms |
4432 KB |
Output is correct |
18 |
Correct |
3 ms |
4688 KB |
Output is correct |
19 |
Correct |
2 ms |
4688 KB |
Output is correct |
20 |
Correct |
5 ms |
4688 KB |
Output is correct |
21 |
Correct |
3 ms |
4600 KB |
Output is correct |
22 |
Correct |
3 ms |
4432 KB |
Output is correct |
23 |
Correct |
2 ms |
4432 KB |
Output is correct |
24 |
Correct |
3 ms |
4432 KB |
Output is correct |
25 |
Correct |
1 ms |
4432 KB |
Output is correct |
26 |
Incorrect |
3 ms |
4432 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4432 KB |
Output is correct |
2 |
Correct |
2 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
2 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
2 ms |
4432 KB |
Output is correct |
10 |
Correct |
2 ms |
4432 KB |
Output is correct |
11 |
Correct |
2 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
2 ms |
4432 KB |
Output is correct |
15 |
Correct |
2 ms |
4432 KB |
Output is correct |
16 |
Correct |
2 ms |
4432 KB |
Output is correct |
17 |
Correct |
1 ms |
4432 KB |
Output is correct |
18 |
Correct |
3 ms |
4688 KB |
Output is correct |
19 |
Correct |
2 ms |
4688 KB |
Output is correct |
20 |
Correct |
5 ms |
4688 KB |
Output is correct |
21 |
Correct |
3 ms |
4600 KB |
Output is correct |
22 |
Correct |
3 ms |
4432 KB |
Output is correct |
23 |
Correct |
2 ms |
4432 KB |
Output is correct |
24 |
Correct |
3 ms |
4432 KB |
Output is correct |
25 |
Correct |
104 ms |
16668 KB |
Output is correct |
26 |
Correct |
101 ms |
19532 KB |
Output is correct |
27 |
Correct |
106 ms |
19544 KB |
Output is correct |
28 |
Correct |
75 ms |
11088 KB |
Output is correct |
29 |
Correct |
80 ms |
11104 KB |
Output is correct |
30 |
Correct |
81 ms |
11184 KB |
Output is correct |
31 |
Correct |
70 ms |
11084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4432 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4432 KB |
Output is correct |
2 |
Correct |
2 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
2 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
2 ms |
4432 KB |
Output is correct |
10 |
Correct |
2 ms |
4432 KB |
Output is correct |
11 |
Correct |
2 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
2 ms |
4432 KB |
Output is correct |
15 |
Correct |
2 ms |
4432 KB |
Output is correct |
16 |
Correct |
2 ms |
4432 KB |
Output is correct |
17 |
Correct |
1 ms |
4432 KB |
Output is correct |
18 |
Correct |
3 ms |
4688 KB |
Output is correct |
19 |
Correct |
2 ms |
4688 KB |
Output is correct |
20 |
Correct |
5 ms |
4688 KB |
Output is correct |
21 |
Correct |
3 ms |
4600 KB |
Output is correct |
22 |
Correct |
3 ms |
4432 KB |
Output is correct |
23 |
Correct |
2 ms |
4432 KB |
Output is correct |
24 |
Correct |
3 ms |
4432 KB |
Output is correct |
25 |
Correct |
1 ms |
4432 KB |
Output is correct |
26 |
Incorrect |
3 ms |
4432 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |