#include "books.h"
#include<bits/stdc++.h>
using namespace std;
int nr, N, S, p[1000009], prv[1000009], nxt[1000009], lg[1000009], mi[22][1000009], ma[22][1000009];
bool ap[1000009];
pair < int, int > segm[1000009];
pair < int, int > getMiMa (int l, int r)
{
int p = lg[r - l + 1];
return {min (mi[p][l], mi[p][r - (1 << p) + 1]), max (ma[p][l], ma[p][r - (1 << p) + 1])};
}
long long minimum_walk (vector < int > pp, int ss)
{
N = pp.size (), S = ss + 1;
for (int i=1; i<=N; i++)
p[i] = pp[i - 1] + 1;
for (int i=1; i<=N; i++)
if (ap[i] == 0)
{
int j = p[i], maxRight = i;
ap[i] = 1;
while (j != i)
ap[j] = 1, maxRight = max (maxRight, j), j = p[j];
nxt[i] = maxRight, prv[i] = i, j = p[i];
while (j != i)
nxt[j] = maxRight, prv[j] = i, j = p[j];
}
long long ans = 0;
for (int i=1; i<=N; i++)
{
int curr = i - p[i];
if (curr < 0) curr = -curr;
ans += curr;
}
for (int i=1; i<=N; i++)
mi[0][i] = prv[i], ma[0][i] = nxt[i];
for (int i=1; i<=N; i++)
{
lg[i] = lg[i - 1];
if ((2 << lg[i]) <= i) lg[i] ++;
}
for (int i=1; i<=lg[N]; i++)
for (int j=1; j + (1 << i) - 1 <= N; j ++)
mi[i][j] = min (mi[i - 1][j], mi[i - 1][j + (1 << (i - 1))]),
ma[i][j] = max (ma[i - 1][j], ma[i - 1][j + (1 << (i - 1))]);
for (int i=1; i<=N; i++)
if ((i == S || i != p[i]) && ap[i] == 1)
{
int l = i, r = i;
while (1)
{
auto curr = getMiMa (l, r);
if (l == curr.first && r == curr.second) break;
l = curr.first;
r = curr.second;
}
for (int j=l; j<=r; j++)
ap[j] = 0;
segm[++nr] = {l, r};
}
for (int i=1; i<nr; i++)
ans += 2LL * (segm[i + 1].first - segm[i].second);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
676 KB |
Output is correct |
4 |
Correct |
2 ms |
676 KB |
Output is correct |
5 |
Correct |
2 ms |
676 KB |
Output is correct |
6 |
Correct |
2 ms |
676 KB |
Output is correct |
7 |
Correct |
2 ms |
676 KB |
Output is correct |
8 |
Correct |
2 ms |
676 KB |
Output is correct |
9 |
Correct |
2 ms |
676 KB |
Output is correct |
10 |
Correct |
2 ms |
676 KB |
Output is correct |
11 |
Correct |
2 ms |
676 KB |
Output is correct |
12 |
Correct |
2 ms |
676 KB |
Output is correct |
13 |
Correct |
2 ms |
676 KB |
Output is correct |
14 |
Correct |
2 ms |
676 KB |
Output is correct |
15 |
Correct |
2 ms |
676 KB |
Output is correct |
16 |
Correct |
2 ms |
676 KB |
Output is correct |
17 |
Correct |
2 ms |
676 KB |
Output is correct |
18 |
Correct |
2 ms |
676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
676 KB |
Output is correct |
4 |
Correct |
2 ms |
676 KB |
Output is correct |
5 |
Correct |
2 ms |
676 KB |
Output is correct |
6 |
Correct |
2 ms |
676 KB |
Output is correct |
7 |
Correct |
2 ms |
676 KB |
Output is correct |
8 |
Correct |
2 ms |
676 KB |
Output is correct |
9 |
Correct |
2 ms |
676 KB |
Output is correct |
10 |
Correct |
2 ms |
676 KB |
Output is correct |
11 |
Correct |
2 ms |
676 KB |
Output is correct |
12 |
Correct |
2 ms |
676 KB |
Output is correct |
13 |
Correct |
2 ms |
676 KB |
Output is correct |
14 |
Correct |
2 ms |
676 KB |
Output is correct |
15 |
Correct |
2 ms |
676 KB |
Output is correct |
16 |
Correct |
2 ms |
676 KB |
Output is correct |
17 |
Correct |
2 ms |
676 KB |
Output is correct |
18 |
Correct |
2 ms |
676 KB |
Output is correct |
19 |
Correct |
2 ms |
800 KB |
Output is correct |
20 |
Correct |
2 ms |
804 KB |
Output is correct |
21 |
Correct |
2 ms |
848 KB |
Output is correct |
22 |
Correct |
2 ms |
872 KB |
Output is correct |
23 |
Correct |
2 ms |
1000 KB |
Output is correct |
24 |
Correct |
2 ms |
1000 KB |
Output is correct |
25 |
Correct |
2 ms |
1000 KB |
Output is correct |
26 |
Correct |
2 ms |
1000 KB |
Output is correct |
27 |
Correct |
2 ms |
1000 KB |
Output is correct |
28 |
Correct |
2 ms |
1000 KB |
Output is correct |
29 |
Correct |
2 ms |
1000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
676 KB |
Output is correct |
4 |
Correct |
2 ms |
676 KB |
Output is correct |
5 |
Correct |
2 ms |
676 KB |
Output is correct |
6 |
Correct |
2 ms |
676 KB |
Output is correct |
7 |
Correct |
2 ms |
676 KB |
Output is correct |
8 |
Correct |
2 ms |
676 KB |
Output is correct |
9 |
Correct |
2 ms |
676 KB |
Output is correct |
10 |
Correct |
2 ms |
676 KB |
Output is correct |
11 |
Correct |
2 ms |
676 KB |
Output is correct |
12 |
Correct |
2 ms |
676 KB |
Output is correct |
13 |
Correct |
2 ms |
676 KB |
Output is correct |
14 |
Correct |
2 ms |
676 KB |
Output is correct |
15 |
Correct |
2 ms |
676 KB |
Output is correct |
16 |
Correct |
2 ms |
676 KB |
Output is correct |
17 |
Correct |
2 ms |
676 KB |
Output is correct |
18 |
Correct |
2 ms |
676 KB |
Output is correct |
19 |
Correct |
2 ms |
800 KB |
Output is correct |
20 |
Correct |
2 ms |
804 KB |
Output is correct |
21 |
Correct |
2 ms |
848 KB |
Output is correct |
22 |
Correct |
2 ms |
872 KB |
Output is correct |
23 |
Correct |
2 ms |
1000 KB |
Output is correct |
24 |
Correct |
2 ms |
1000 KB |
Output is correct |
25 |
Correct |
2 ms |
1000 KB |
Output is correct |
26 |
Correct |
2 ms |
1000 KB |
Output is correct |
27 |
Correct |
2 ms |
1000 KB |
Output is correct |
28 |
Correct |
2 ms |
1000 KB |
Output is correct |
29 |
Correct |
2 ms |
1000 KB |
Output is correct |
30 |
Correct |
413 ms |
173512 KB |
Output is correct |
31 |
Correct |
423 ms |
173512 KB |
Output is correct |
32 |
Correct |
294 ms |
173520 KB |
Output is correct |
33 |
Correct |
308 ms |
174972 KB |
Output is correct |
34 |
Correct |
332 ms |
174972 KB |
Output is correct |
35 |
Correct |
324 ms |
174972 KB |
Output is correct |
36 |
Correct |
316 ms |
174972 KB |
Output is correct |
37 |
Correct |
308 ms |
174972 KB |
Output is correct |
38 |
Correct |
335 ms |
174972 KB |
Output is correct |
39 |
Correct |
304 ms |
174972 KB |
Output is correct |
40 |
Correct |
322 ms |
174972 KB |
Output is correct |
41 |
Correct |
370 ms |
174972 KB |
Output is correct |
42 |
Correct |
356 ms |
174972 KB |
Output is correct |
43 |
Correct |
323 ms |
174972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
174972 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
676 KB |
Output is correct |
4 |
Correct |
2 ms |
676 KB |
Output is correct |
5 |
Correct |
2 ms |
676 KB |
Output is correct |
6 |
Correct |
2 ms |
676 KB |
Output is correct |
7 |
Correct |
2 ms |
676 KB |
Output is correct |
8 |
Correct |
2 ms |
676 KB |
Output is correct |
9 |
Correct |
2 ms |
676 KB |
Output is correct |
10 |
Correct |
2 ms |
676 KB |
Output is correct |
11 |
Correct |
2 ms |
676 KB |
Output is correct |
12 |
Correct |
2 ms |
676 KB |
Output is correct |
13 |
Correct |
2 ms |
676 KB |
Output is correct |
14 |
Correct |
2 ms |
676 KB |
Output is correct |
15 |
Correct |
2 ms |
676 KB |
Output is correct |
16 |
Correct |
2 ms |
676 KB |
Output is correct |
17 |
Correct |
2 ms |
676 KB |
Output is correct |
18 |
Correct |
2 ms |
676 KB |
Output is correct |
19 |
Correct |
2 ms |
800 KB |
Output is correct |
20 |
Correct |
2 ms |
804 KB |
Output is correct |
21 |
Correct |
2 ms |
848 KB |
Output is correct |
22 |
Correct |
2 ms |
872 KB |
Output is correct |
23 |
Correct |
2 ms |
1000 KB |
Output is correct |
24 |
Correct |
2 ms |
1000 KB |
Output is correct |
25 |
Correct |
2 ms |
1000 KB |
Output is correct |
26 |
Correct |
2 ms |
1000 KB |
Output is correct |
27 |
Correct |
2 ms |
1000 KB |
Output is correct |
28 |
Correct |
2 ms |
1000 KB |
Output is correct |
29 |
Correct |
2 ms |
1000 KB |
Output is correct |
30 |
Correct |
413 ms |
173512 KB |
Output is correct |
31 |
Correct |
423 ms |
173512 KB |
Output is correct |
32 |
Correct |
294 ms |
173520 KB |
Output is correct |
33 |
Correct |
308 ms |
174972 KB |
Output is correct |
34 |
Correct |
332 ms |
174972 KB |
Output is correct |
35 |
Correct |
324 ms |
174972 KB |
Output is correct |
36 |
Correct |
316 ms |
174972 KB |
Output is correct |
37 |
Correct |
308 ms |
174972 KB |
Output is correct |
38 |
Correct |
335 ms |
174972 KB |
Output is correct |
39 |
Correct |
304 ms |
174972 KB |
Output is correct |
40 |
Correct |
322 ms |
174972 KB |
Output is correct |
41 |
Correct |
370 ms |
174972 KB |
Output is correct |
42 |
Correct |
356 ms |
174972 KB |
Output is correct |
43 |
Correct |
323 ms |
174972 KB |
Output is correct |
44 |
Incorrect |
2 ms |
174972 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |