#include "books.h"
#include<bits/extc++.h>
using namespace std;
const int maxn = 1<<20;
using ll = long long;
int vis[maxn], a[maxn], col[maxn], n, id = 0;
int L, R, pL, pR, rL = 1<<30, rR = -1<<30, left = 0;
vector<int> cycle[maxn];
ll ans = 0;
vector<array<int, 2>> segs;
void touch(int p) {
if(vis[p]) return;
++id;
int l = p, r = p, lst = p;
while(!vis[p]) {
vis[p] = 1;
l = min(l, p);
r = max(r, p);
col[p] = id;
lst = p;
p = a[p];
ans += abs(p-lst);
}
}
void upd(int i) {
pL = min(pL, cycle[col[i]][0]);
pR = max(pR, cycle[col[i]].back());
}
void expand() {
while(pL < L || pR > R) {
while(L > pL) upd(--L);
while(R < pR) upd(++R);
}
}
ll minimum_walk(vector<int> _a, int s) {
n = _a.size();
for(int i = 0; i < n; i++) a[i] = _a[i];
for(int i = 0; i < n; i++) {
if(i == s || a[i] != i) {
rR = max(rR, i);
rL = min(rL, i);
}
touch(i);
}
for(int i = 0; i < n; i++) cycle[col[i]].push_back(i);
memset(vis, 0, sizeof vis);
pL = pR = R = s, L = s+1;
expand();
while(L > rL || R < rR) {
ans += 2;
if(L > rL) pL--;
else pR++;
expand();
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
29056 KB |
Output is correct |
2 |
Correct |
17 ms |
29056 KB |
Output is correct |
3 |
Correct |
17 ms |
29056 KB |
Output is correct |
4 |
Correct |
17 ms |
29056 KB |
Output is correct |
5 |
Correct |
16 ms |
29056 KB |
Output is correct |
6 |
Correct |
17 ms |
29056 KB |
Output is correct |
7 |
Correct |
16 ms |
29056 KB |
Output is correct |
8 |
Correct |
17 ms |
29056 KB |
Output is correct |
9 |
Correct |
17 ms |
29056 KB |
Output is correct |
10 |
Correct |
17 ms |
29056 KB |
Output is correct |
11 |
Correct |
16 ms |
29056 KB |
Output is correct |
12 |
Correct |
17 ms |
29184 KB |
Output is correct |
13 |
Correct |
19 ms |
29056 KB |
Output is correct |
14 |
Correct |
17 ms |
29056 KB |
Output is correct |
15 |
Correct |
17 ms |
29056 KB |
Output is correct |
16 |
Correct |
17 ms |
29056 KB |
Output is correct |
17 |
Correct |
17 ms |
29056 KB |
Output is correct |
18 |
Correct |
18 ms |
29056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
29056 KB |
Output is correct |
2 |
Correct |
17 ms |
29056 KB |
Output is correct |
3 |
Correct |
17 ms |
29056 KB |
Output is correct |
4 |
Correct |
17 ms |
29056 KB |
Output is correct |
5 |
Correct |
16 ms |
29056 KB |
Output is correct |
6 |
Correct |
17 ms |
29056 KB |
Output is correct |
7 |
Correct |
16 ms |
29056 KB |
Output is correct |
8 |
Correct |
17 ms |
29056 KB |
Output is correct |
9 |
Correct |
17 ms |
29056 KB |
Output is correct |
10 |
Correct |
17 ms |
29056 KB |
Output is correct |
11 |
Correct |
16 ms |
29056 KB |
Output is correct |
12 |
Correct |
17 ms |
29184 KB |
Output is correct |
13 |
Correct |
19 ms |
29056 KB |
Output is correct |
14 |
Correct |
17 ms |
29056 KB |
Output is correct |
15 |
Correct |
17 ms |
29056 KB |
Output is correct |
16 |
Correct |
17 ms |
29056 KB |
Output is correct |
17 |
Correct |
17 ms |
29056 KB |
Output is correct |
18 |
Correct |
18 ms |
29056 KB |
Output is correct |
19 |
Correct |
17 ms |
29056 KB |
Output is correct |
20 |
Correct |
17 ms |
29084 KB |
Output is correct |
21 |
Correct |
17 ms |
29056 KB |
Output is correct |
22 |
Correct |
17 ms |
29056 KB |
Output is correct |
23 |
Correct |
16 ms |
29056 KB |
Output is correct |
24 |
Correct |
17 ms |
29056 KB |
Output is correct |
25 |
Correct |
17 ms |
28928 KB |
Output is correct |
26 |
Correct |
17 ms |
29056 KB |
Output is correct |
27 |
Correct |
17 ms |
29056 KB |
Output is correct |
28 |
Correct |
17 ms |
29056 KB |
Output is correct |
29 |
Correct |
17 ms |
29056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
29056 KB |
Output is correct |
2 |
Correct |
17 ms |
29056 KB |
Output is correct |
3 |
Correct |
17 ms |
29056 KB |
Output is correct |
4 |
Correct |
17 ms |
29056 KB |
Output is correct |
5 |
Correct |
16 ms |
29056 KB |
Output is correct |
6 |
Correct |
17 ms |
29056 KB |
Output is correct |
7 |
Correct |
16 ms |
29056 KB |
Output is correct |
8 |
Correct |
17 ms |
29056 KB |
Output is correct |
9 |
Correct |
17 ms |
29056 KB |
Output is correct |
10 |
Correct |
17 ms |
29056 KB |
Output is correct |
11 |
Correct |
16 ms |
29056 KB |
Output is correct |
12 |
Correct |
17 ms |
29184 KB |
Output is correct |
13 |
Correct |
19 ms |
29056 KB |
Output is correct |
14 |
Correct |
17 ms |
29056 KB |
Output is correct |
15 |
Correct |
17 ms |
29056 KB |
Output is correct |
16 |
Correct |
17 ms |
29056 KB |
Output is correct |
17 |
Correct |
17 ms |
29056 KB |
Output is correct |
18 |
Correct |
18 ms |
29056 KB |
Output is correct |
19 |
Correct |
17 ms |
29056 KB |
Output is correct |
20 |
Correct |
17 ms |
29084 KB |
Output is correct |
21 |
Correct |
17 ms |
29056 KB |
Output is correct |
22 |
Correct |
17 ms |
29056 KB |
Output is correct |
23 |
Correct |
16 ms |
29056 KB |
Output is correct |
24 |
Correct |
17 ms |
29056 KB |
Output is correct |
25 |
Correct |
17 ms |
28928 KB |
Output is correct |
26 |
Correct |
17 ms |
29056 KB |
Output is correct |
27 |
Correct |
17 ms |
29056 KB |
Output is correct |
28 |
Correct |
17 ms |
29056 KB |
Output is correct |
29 |
Correct |
17 ms |
29056 KB |
Output is correct |
30 |
Correct |
284 ms |
50276 KB |
Output is correct |
31 |
Correct |
278 ms |
55636 KB |
Output is correct |
32 |
Correct |
216 ms |
82808 KB |
Output is correct |
33 |
Correct |
226 ms |
73336 KB |
Output is correct |
34 |
Correct |
234 ms |
73336 KB |
Output is correct |
35 |
Correct |
248 ms |
69240 KB |
Output is correct |
36 |
Correct |
235 ms |
61944 KB |
Output is correct |
37 |
Correct |
211 ms |
57592 KB |
Output is correct |
38 |
Correct |
192 ms |
57208 KB |
Output is correct |
39 |
Correct |
193 ms |
57080 KB |
Output is correct |
40 |
Correct |
214 ms |
56168 KB |
Output is correct |
41 |
Correct |
236 ms |
56044 KB |
Output is correct |
42 |
Correct |
215 ms |
56184 KB |
Output is correct |
43 |
Correct |
224 ms |
74616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
29056 KB |
Output is correct |
2 |
Correct |
16 ms |
29056 KB |
Output is correct |
3 |
Correct |
21 ms |
29056 KB |
Output is correct |
4 |
Correct |
17 ms |
29056 KB |
Output is correct |
5 |
Correct |
16 ms |
29056 KB |
Output is correct |
6 |
Incorrect |
17 ms |
29056 KB |
3rd lines differ - on the 1st token, expected: '22140', found: '22144' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
29056 KB |
Output is correct |
2 |
Correct |
17 ms |
29056 KB |
Output is correct |
3 |
Correct |
17 ms |
29056 KB |
Output is correct |
4 |
Correct |
17 ms |
29056 KB |
Output is correct |
5 |
Correct |
16 ms |
29056 KB |
Output is correct |
6 |
Correct |
17 ms |
29056 KB |
Output is correct |
7 |
Correct |
16 ms |
29056 KB |
Output is correct |
8 |
Correct |
17 ms |
29056 KB |
Output is correct |
9 |
Correct |
17 ms |
29056 KB |
Output is correct |
10 |
Correct |
17 ms |
29056 KB |
Output is correct |
11 |
Correct |
16 ms |
29056 KB |
Output is correct |
12 |
Correct |
17 ms |
29184 KB |
Output is correct |
13 |
Correct |
19 ms |
29056 KB |
Output is correct |
14 |
Correct |
17 ms |
29056 KB |
Output is correct |
15 |
Correct |
17 ms |
29056 KB |
Output is correct |
16 |
Correct |
17 ms |
29056 KB |
Output is correct |
17 |
Correct |
17 ms |
29056 KB |
Output is correct |
18 |
Correct |
18 ms |
29056 KB |
Output is correct |
19 |
Correct |
17 ms |
29056 KB |
Output is correct |
20 |
Correct |
17 ms |
29084 KB |
Output is correct |
21 |
Correct |
17 ms |
29056 KB |
Output is correct |
22 |
Correct |
17 ms |
29056 KB |
Output is correct |
23 |
Correct |
16 ms |
29056 KB |
Output is correct |
24 |
Correct |
17 ms |
29056 KB |
Output is correct |
25 |
Correct |
17 ms |
28928 KB |
Output is correct |
26 |
Correct |
17 ms |
29056 KB |
Output is correct |
27 |
Correct |
17 ms |
29056 KB |
Output is correct |
28 |
Correct |
17 ms |
29056 KB |
Output is correct |
29 |
Correct |
17 ms |
29056 KB |
Output is correct |
30 |
Correct |
284 ms |
50276 KB |
Output is correct |
31 |
Correct |
278 ms |
55636 KB |
Output is correct |
32 |
Correct |
216 ms |
82808 KB |
Output is correct |
33 |
Correct |
226 ms |
73336 KB |
Output is correct |
34 |
Correct |
234 ms |
73336 KB |
Output is correct |
35 |
Correct |
248 ms |
69240 KB |
Output is correct |
36 |
Correct |
235 ms |
61944 KB |
Output is correct |
37 |
Correct |
211 ms |
57592 KB |
Output is correct |
38 |
Correct |
192 ms |
57208 KB |
Output is correct |
39 |
Correct |
193 ms |
57080 KB |
Output is correct |
40 |
Correct |
214 ms |
56168 KB |
Output is correct |
41 |
Correct |
236 ms |
56044 KB |
Output is correct |
42 |
Correct |
215 ms |
56184 KB |
Output is correct |
43 |
Correct |
224 ms |
74616 KB |
Output is correct |
44 |
Correct |
16 ms |
29056 KB |
Output is correct |
45 |
Correct |
16 ms |
29056 KB |
Output is correct |
46 |
Correct |
21 ms |
29056 KB |
Output is correct |
47 |
Correct |
17 ms |
29056 KB |
Output is correct |
48 |
Correct |
16 ms |
29056 KB |
Output is correct |
49 |
Incorrect |
17 ms |
29056 KB |
3rd lines differ - on the 1st token, expected: '22140', found: '22144' |
50 |
Halted |
0 ms |
0 KB |
- |