#include "books.h"
#include <bits/stdc++.h>
using namespace std;
int vis[1000001],r[1000001],g[1000001],nxt[1000001];
vector <pair <int, pair <int, int>>> e;
long long minimum_walk(vector <int> p, int s){
int n=p.size(),lo=n,hi=0;
iota(nxt,nxt+n,0);
vector <pair <int, int>> tmp;
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]];
while (L>=newL){
if (r[g[L]]>tmpr){
R=r[g[L]];
L=g[L];
ch=1;
break;
}
newL=min(newL,g[L]);
L--;
}
if (ch)
break;
L++;
while (R<=newR){
if (g[R]<tmpl){
L=g[R];
R=r[g[R]];
ch=1;
break;
}
newR=max(newR,r[g[R]]);
R++;
}
if (ch)
break;
R--;
}
if (!ch)
d=d2;
}
return d;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2548 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 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 |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2548 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 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 |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 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 |
2396 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 |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2548 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 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 |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 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 |
2396 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 |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
99 ms |
20008 KB |
Output is correct |
31 |
Correct |
94 ms |
20004 KB |
Output is correct |
32 |
Correct |
73 ms |
23888 KB |
Output is correct |
33 |
Correct |
83 ms |
23800 KB |
Output is correct |
34 |
Correct |
86 ms |
23824 KB |
Output is correct |
35 |
Correct |
84 ms |
23800 KB |
Output is correct |
36 |
Correct |
79 ms |
23892 KB |
Output is correct |
37 |
Correct |
76 ms |
23812 KB |
Output is correct |
38 |
Correct |
80 ms |
23288 KB |
Output is correct |
39 |
Correct |
85 ms |
23036 KB |
Output is correct |
40 |
Correct |
79 ms |
20820 KB |
Output is correct |
41 |
Correct |
89 ms |
20052 KB |
Output is correct |
42 |
Correct |
83 ms |
20308 KB |
Output is correct |
43 |
Correct |
78 ms |
23816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 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 |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 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 |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2548 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 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 |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 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 |
2396 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 |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
99 ms |
20008 KB |
Output is correct |
31 |
Correct |
94 ms |
20004 KB |
Output is correct |
32 |
Correct |
73 ms |
23888 KB |
Output is correct |
33 |
Correct |
83 ms |
23800 KB |
Output is correct |
34 |
Correct |
86 ms |
23824 KB |
Output is correct |
35 |
Correct |
84 ms |
23800 KB |
Output is correct |
36 |
Correct |
79 ms |
23892 KB |
Output is correct |
37 |
Correct |
76 ms |
23812 KB |
Output is correct |
38 |
Correct |
80 ms |
23288 KB |
Output is correct |
39 |
Correct |
85 ms |
23036 KB |
Output is correct |
40 |
Correct |
79 ms |
20820 KB |
Output is correct |
41 |
Correct |
89 ms |
20052 KB |
Output is correct |
42 |
Correct |
83 ms |
20308 KB |
Output is correct |
43 |
Correct |
78 ms |
23816 KB |
Output is correct |
44 |
Correct |
1 ms |
2392 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 |
2396 KB |
Output is correct |
53 |
Correct |
1 ms |
2392 KB |
Output is correct |
54 |
Correct |
1 ms |
2396 KB |
Output is correct |
55 |
Correct |
1 ms |
2396 KB |
Output is correct |
56 |
Correct |
1 ms |
2396 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 |
2652 KB |
Output is correct |
63 |
Correct |
1 ms |
2396 KB |
Output is correct |
64 |
Correct |
80 ms |
30464 KB |
Output is correct |
65 |
Correct |
77 ms |
30536 KB |
Output is correct |
66 |
Correct |
79 ms |
30584 KB |
Output is correct |
67 |
Correct |
83 ms |
30552 KB |
Output is correct |
68 |
Correct |
8 ms |
8796 KB |
Output is correct |
69 |
Correct |
11 ms |
9052 KB |
Output is correct |
70 |
Correct |
9 ms |
8796 KB |
Output is correct |
71 |
Correct |
10 ms |
8892 KB |
Output is correct |
72 |
Correct |
9 ms |
8796 KB |
Output is correct |
73 |
Correct |
9 ms |
8636 KB |
Output is correct |
74 |
Correct |
84 ms |
30548 KB |
Output is correct |
75 |
Correct |
83 ms |
30544 KB |
Output is correct |
76 |
Correct |
81 ms |
30460 KB |
Output is correct |
77 |
Correct |
78 ms |
30744 KB |
Output is correct |
78 |
Correct |
85 ms |
30548 KB |
Output is correct |
79 |
Correct |
86 ms |
30460 KB |
Output is correct |
80 |
Correct |
75 ms |
30584 KB |
Output is correct |
81 |
Correct |
82 ms |
29184 KB |
Output is correct |
82 |
Correct |
81 ms |
29184 KB |
Output is correct |
83 |
Correct |
86 ms |
30660 KB |
Output is correct |
84 |
Correct |
83 ms |
30584 KB |
Output is correct |
85 |
Correct |
83 ms |
30400 KB |
Output is correct |
86 |
Correct |
82 ms |
30548 KB |
Output is correct |
87 |
Correct |
86 ms |
30544 KB |
Output is correct |
88 |
Correct |
86 ms |
30656 KB |
Output is correct |
89 |
Correct |
95 ms |
30452 KB |
Output is correct |
90 |
Correct |
112 ms |
30456 KB |
Output is correct |
91 |
Correct |
83 ms |
30548 KB |
Output is correct |
92 |
Correct |
83 ms |
27876 KB |
Output is correct |