#include "books.h"
#include <bits/stdc++.h>
using namespace std;
int vis[1000001],r[1000001],g[1000001],nxt[1000001];
long long minimum_walk(vector <int> p, int s){
int n=p.size(),lo=n,hi=0;
iota(nxt,nxt+n,0);
long long d=0;
for (int i=0;i<n;i++)
if (!vis[i]){
int x=i;
while (!vis[x]){
vis[x]=1;
g[x]=i;
r[i]=max(r[i],x);
d+=abs(x-p[x]);
x=p[x];
}
if (r[i]!=i||i==s){
lo=min(lo,i);
hi=max(hi,r[i]);
}
}
int L=g[s],R=r[g[s]];
while (L>lo||R<hi){
if (nxt[L]!=R){
int j=nxt[L]+1;
nxt[L]=nxt[j];
L=min(L,g[j]);
R=max(R,r[g[j]]);
continue;
}
int tmpl=L,tmpr=R,ch=0;
long long d2=d;
while (L>lo||R<hi){
d+=2;
d2+=((L>lo)+(R<hi))*2;
L=max(L-1,lo);
R=min(R+1,hi);
int newL=g[L],newR=r[g[R]];
for (;L>=newL;newL=min(newL,g[L]),L--)
if (r[g[L]]>tmpr){
R=r[g[L]];
L=g[L];
ch=1;
break;
}
if (ch)
break;
L++;
for (;R<=newR;newR=max(newR,r[g[R]]),R++)
if (g[R]<tmpl){
L=g[R];
R=r[g[R]];
ch=1;
break;
}
if (ch)
break;
R--;
}
if (!ch)
d=d2;
}
return d;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2496 KB |
Output is correct |
13 |
Correct |
1 ms |
2496 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2496 KB |
Output is correct |
13 |
Correct |
1 ms |
2496 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2392 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2392 KB |
Output is correct |
28 |
Correct |
1 ms |
2392 KB |
Output is correct |
29 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2496 KB |
Output is correct |
13 |
Correct |
1 ms |
2496 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2392 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2392 KB |
Output is correct |
28 |
Correct |
1 ms |
2392 KB |
Output is correct |
29 |
Correct |
1 ms |
2392 KB |
Output is correct |
30 |
Correct |
93 ms |
26604 KB |
Output is correct |
31 |
Correct |
96 ms |
26708 KB |
Output is correct |
32 |
Correct |
78 ms |
30548 KB |
Output is correct |
33 |
Correct |
88 ms |
30408 KB |
Output is correct |
34 |
Correct |
89 ms |
30444 KB |
Output is correct |
35 |
Correct |
93 ms |
30384 KB |
Output is correct |
36 |
Correct |
86 ms |
30444 KB |
Output is correct |
37 |
Correct |
81 ms |
30548 KB |
Output is correct |
38 |
Correct |
83 ms |
30052 KB |
Output is correct |
39 |
Correct |
82 ms |
29804 KB |
Output is correct |
40 |
Correct |
84 ms |
27624 KB |
Output is correct |
41 |
Correct |
96 ms |
26748 KB |
Output is correct |
42 |
Correct |
93 ms |
26944 KB |
Output is correct |
43 |
Correct |
83 ms |
30540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2496 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2436 KB |
Output is correct |
13 |
Correct |
1 ms |
2648 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2496 KB |
Output is correct |
13 |
Correct |
1 ms |
2496 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2392 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2392 KB |
Output is correct |
28 |
Correct |
1 ms |
2392 KB |
Output is correct |
29 |
Correct |
1 ms |
2392 KB |
Output is correct |
30 |
Correct |
93 ms |
26604 KB |
Output is correct |
31 |
Correct |
96 ms |
26708 KB |
Output is correct |
32 |
Correct |
78 ms |
30548 KB |
Output is correct |
33 |
Correct |
88 ms |
30408 KB |
Output is correct |
34 |
Correct |
89 ms |
30444 KB |
Output is correct |
35 |
Correct |
93 ms |
30384 KB |
Output is correct |
36 |
Correct |
86 ms |
30444 KB |
Output is correct |
37 |
Correct |
81 ms |
30548 KB |
Output is correct |
38 |
Correct |
83 ms |
30052 KB |
Output is correct |
39 |
Correct |
82 ms |
29804 KB |
Output is correct |
40 |
Correct |
84 ms |
27624 KB |
Output is correct |
41 |
Correct |
96 ms |
26748 KB |
Output is correct |
42 |
Correct |
93 ms |
26944 KB |
Output is correct |
43 |
Correct |
83 ms |
30540 KB |
Output is correct |
44 |
Correct |
1 ms |
2396 KB |
Output is correct |
45 |
Correct |
1 ms |
2396 KB |
Output is correct |
46 |
Correct |
1 ms |
2396 KB |
Output is correct |
47 |
Correct |
1 ms |
2396 KB |
Output is correct |
48 |
Correct |
1 ms |
2396 KB |
Output is correct |
49 |
Correct |
1 ms |
2396 KB |
Output is correct |
50 |
Correct |
1 ms |
2396 KB |
Output is correct |
51 |
Correct |
1 ms |
2396 KB |
Output is correct |
52 |
Correct |
1 ms |
2496 KB |
Output is correct |
53 |
Correct |
1 ms |
2396 KB |
Output is correct |
54 |
Correct |
1 ms |
2396 KB |
Output is correct |
55 |
Correct |
1 ms |
2436 KB |
Output is correct |
56 |
Correct |
1 ms |
2648 KB |
Output is correct |
57 |
Correct |
1 ms |
2396 KB |
Output is correct |
58 |
Correct |
1 ms |
2396 KB |
Output is correct |
59 |
Correct |
1 ms |
2396 KB |
Output is correct |
60 |
Correct |
1 ms |
2396 KB |
Output is correct |
61 |
Correct |
1 ms |
2396 KB |
Output is correct |
62 |
Correct |
1 ms |
2396 KB |
Output is correct |
63 |
Correct |
1 ms |
2396 KB |
Output is correct |
64 |
Correct |
82 ms |
30588 KB |
Output is correct |
65 |
Correct |
86 ms |
30480 KB |
Output is correct |
66 |
Correct |
81 ms |
30440 KB |
Output is correct |
67 |
Correct |
86 ms |
30572 KB |
Output is correct |
68 |
Correct |
9 ms |
8796 KB |
Output is correct |
69 |
Correct |
9 ms |
8792 KB |
Output is correct |
70 |
Correct |
9 ms |
8792 KB |
Output is correct |
71 |
Correct |
9 ms |
8792 KB |
Output is correct |
72 |
Correct |
9 ms |
8848 KB |
Output is correct |
73 |
Correct |
9 ms |
8540 KB |
Output is correct |
74 |
Correct |
86 ms |
30584 KB |
Output is correct |
75 |
Correct |
94 ms |
30612 KB |
Output is correct |
76 |
Correct |
84 ms |
30544 KB |
Output is correct |
77 |
Correct |
83 ms |
30548 KB |
Output is correct |
78 |
Correct |
90 ms |
30544 KB |
Output is correct |
79 |
Correct |
89 ms |
30544 KB |
Output is correct |
80 |
Correct |
78 ms |
30548 KB |
Output is correct |
81 |
Correct |
84 ms |
29324 KB |
Output is correct |
82 |
Correct |
84 ms |
29264 KB |
Output is correct |
83 |
Correct |
96 ms |
30544 KB |
Output is correct |
84 |
Correct |
104 ms |
30716 KB |
Output is correct |
85 |
Correct |
89 ms |
30440 KB |
Output is correct |
86 |
Correct |
107 ms |
30444 KB |
Output is correct |
87 |
Correct |
98 ms |
30568 KB |
Output is correct |
88 |
Correct |
92 ms |
30548 KB |
Output is correct |
89 |
Correct |
91 ms |
30548 KB |
Output is correct |
90 |
Correct |
94 ms |
30440 KB |
Output is correct |
91 |
Correct |
88 ms |
30544 KB |
Output is correct |
92 |
Correct |
90 ms |
27984 KB |
Output is correct |