#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#include "books.h"
VI L, R, C;
void extend(int& l, int& r) {
int ll = min(L[C[l]], L[C[r]]);
int rr = max(R[C[l]], R[C[r]]);
while (ll < l || r < rr) {
if (ll < l) {
l--;
setmin(ll, L[C[l]]);
setmax(rr, R[C[l]]);
} else if (r < rr) {
r++;
setmin(ll, L[C[r]]);
setmax(rr, R[C[r]]);
}
}
}
ll minimum_walk(VI p, int s) {
int n = p.size();
C = VI(n, -1);
int c = 0;
ll ans = 0;
int entl = +2e9, entr = -2e9;
rep(i, n) if (C[i] == -1) {
int u = i;
int l = +2e9, r = -2e9;
do {
C[u] = c;
setmin(l, u);
setmax(r, u);
ans += abs(p[u]-u);
u = p[u];
} while (u != i);
L.pb(l);
R.pb(r);
if (i != p[i]) {
setmin(entl, l);
setmax(entr, r);
}
c++;
}
int l = s;
int r = s;
while (true) {
extend(l, r);
int nl, nr;
bool ardyoq = false;
int cr = 0;
int maxr = r;
replr(i, r+1, entr) {
if (i > maxr) cr += 2;
setmax(maxr, R[C[i]]);
if (L[C[i]] < s) {
nr = i;
ardyoq = true;
break;
}
}
int cl = 0;
int minl = l;
reprl(i, l-1, entl) {
if (i < minl) cl += 2;
setmin(minl, L[C[i]]);
if (R[C[i]] > s) {
nl = i;
ardyoq = true;
break;
}
}
if (!ardyoq) {
ans += cl + cr;
break;
}
ans += min(cl, cr);
l = nl;
r = nr;
}
return ans;
}
Compilation message
books.cpp: In function 'll minimum_walk(VI, int)':
books.cpp:112:11: warning: 'nr' may be used uninitialized in this function [-Wmaybe-uninitialized]
112 | r = nr;
| ~~^~~~
books.cpp:111:11: warning: 'nl' may be used uninitialized in this function [-Wmaybe-uninitialized]
111 | l = nl;
| ~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
436 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
356 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
360 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
436 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
356 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
360 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
436 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
436 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
356 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
360 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
436 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
88 ms |
17860 KB |
Output is correct |
31 |
Correct |
87 ms |
17972 KB |
Output is correct |
32 |
Correct |
73 ms |
26032 KB |
Output is correct |
33 |
Correct |
72 ms |
24428 KB |
Output is correct |
34 |
Correct |
75 ms |
25544 KB |
Output is correct |
35 |
Correct |
85 ms |
27040 KB |
Output is correct |
36 |
Correct |
78 ms |
20328 KB |
Output is correct |
37 |
Correct |
71 ms |
18236 KB |
Output is correct |
38 |
Correct |
66 ms |
17748 KB |
Output is correct |
39 |
Correct |
73 ms |
17856 KB |
Output is correct |
40 |
Correct |
78 ms |
17768 KB |
Output is correct |
41 |
Correct |
83 ms |
17744 KB |
Output is correct |
42 |
Correct |
86 ms |
17648 KB |
Output is correct |
43 |
Correct |
78 ms |
24436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
388 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
376 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
440 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
436 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
356 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
360 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
436 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
88 ms |
17860 KB |
Output is correct |
31 |
Correct |
87 ms |
17972 KB |
Output is correct |
32 |
Correct |
73 ms |
26032 KB |
Output is correct |
33 |
Correct |
72 ms |
24428 KB |
Output is correct |
34 |
Correct |
75 ms |
25544 KB |
Output is correct |
35 |
Correct |
85 ms |
27040 KB |
Output is correct |
36 |
Correct |
78 ms |
20328 KB |
Output is correct |
37 |
Correct |
71 ms |
18236 KB |
Output is correct |
38 |
Correct |
66 ms |
17748 KB |
Output is correct |
39 |
Correct |
73 ms |
17856 KB |
Output is correct |
40 |
Correct |
78 ms |
17768 KB |
Output is correct |
41 |
Correct |
83 ms |
17744 KB |
Output is correct |
42 |
Correct |
86 ms |
17648 KB |
Output is correct |
43 |
Correct |
78 ms |
24436 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
344 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
388 KB |
Output is correct |
50 |
Correct |
0 ms |
344 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
344 KB |
Output is correct |
53 |
Correct |
1 ms |
344 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
0 ms |
376 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
1 ms |
440 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
76 ms |
24756 KB |
Output is correct |
65 |
Correct |
87 ms |
24292 KB |
Output is correct |
66 |
Correct |
75 ms |
18256 KB |
Output is correct |
67 |
Correct |
68 ms |
18000 KB |
Output is correct |
68 |
Correct |
8 ms |
2392 KB |
Output is correct |
69 |
Correct |
7 ms |
2244 KB |
Output is correct |
70 |
Correct |
7 ms |
2652 KB |
Output is correct |
71 |
Correct |
7 ms |
3072 KB |
Output is correct |
72 |
Correct |
8 ms |
3032 KB |
Output is correct |
73 |
Correct |
13 ms |
2140 KB |
Output is correct |
74 |
Correct |
85 ms |
23872 KB |
Output is correct |
75 |
Correct |
87 ms |
23832 KB |
Output is correct |
76 |
Correct |
85 ms |
23720 KB |
Output is correct |
77 |
Correct |
84 ms |
23736 KB |
Output is correct |
78 |
Correct |
78 ms |
23856 KB |
Output is correct |
79 |
Correct |
82 ms |
23728 KB |
Output is correct |
80 |
Correct |
75 ms |
25516 KB |
Output is correct |
81 |
Correct |
73 ms |
21604 KB |
Output is correct |
82 |
Correct |
88 ms |
21420 KB |
Output is correct |
83 |
Correct |
93 ms |
20848 KB |
Output is correct |
84 |
Correct |
94 ms |
20764 KB |
Output is correct |
85 |
Correct |
85 ms |
19168 KB |
Output is correct |
86 |
Correct |
79 ms |
17952 KB |
Output is correct |
87 |
Correct |
92 ms |
24260 KB |
Output is correct |
88 |
Correct |
93 ms |
23984 KB |
Output is correct |
89 |
Correct |
116 ms |
21176 KB |
Output is correct |
90 |
Correct |
82 ms |
17652 KB |
Output is correct |
91 |
Correct |
78 ms |
17592 KB |
Output is correct |
92 |
Correct |
102 ms |
17456 KB |
Output is correct |