#include <bits/stdc++.h>
#include "books.h"
using namespace std;
const int INF = 0x3f3f3f3f;
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,tune=native")
vector<vector<pair<int, int>>> adj;
int create()
{
adj.emplace_back();
return adj.size()-1;
}
int build(int ini, int fim)
{
if (ini == fim)
{
return ini;
}
int pos = create();
int m = (ini + fim) >> 1;
adj[pos].emplace_back(build(ini, m), 0);
adj[pos].emplace_back(build(m+1, fim), 0);
return pos;
}
void update(int pos, int ini, int fim, int p, int q, int val)
{
if (q < ini || p > fim) return;
if (p <= ini && fim <= q) {adj[val].emplace_back(pos, 0); return;}
int m = (ini + fim) >> 1;
int e = adj[pos][0].first, d = adj[pos][1].first;
update(e, ini, m, p, q, val);
update(d, m+1, fim, p, q, val);
}
long long minimum_walk(std::vector<int> p, int s) {
int N = p.size();
long long resp = 0;
for (int i = 0; i < N; i++) resp += abs(p[i]-i);
if (resp == 0) return 0;
vector<int> minSuf(N); minSuf.back() = p.back();
for (int i = N-2; i >= 0; i--) minSuf[i] = min(minSuf[i+1], p[i]);
vector<int> maxPref(N); maxPref[0] = p[0];
for (int i = 1; i < N; i++) maxPref[i] = max(maxPref[i-1], p[i]);
int lastS = 0; vector<pair<int, int>> comps;
for (int i = 1; i < N; i++) if (maxPref[i-1] < i && minSuf[i] >= i)
{
comps.emplace_back(lastS, i-1);
// resp += i - lastS;
lastS = i;
}
comps.emplace_back(lastS, N-1);
while (!comps.empty() && comps.back().first == comps.back().second) comps.pop_back();
reverse(comps.begin(), comps.end());
while (!comps.empty() && comps.back().first == comps.back().second) comps.pop_back();
reverse(comps.begin(), comps.end());
for (int i = 0; i+1 < comps.size(); i++) resp += 2*(comps[i+1].first - comps[i].second);
if (s < comps[0].first) resp += 2*(comps[0].first - s);
else if (s > comps.back().second) resp += 2*(s - comps.back().second);
else if (s != 0)
{
//Parte ruim
adj.resize(N);
int root = build(0, N-1);
for (int i = 0; i < N; i++)
{
int l = min(i, p[i]), r = max(i, p[i]);
update(root, 0, N-1, l, r, i);
if (l > 0) adj[i].emplace_back(l-1, +1);
if (r+1 < N) adj[i].emplace_back(r+1, +1);
}
queue<int> q1, q2; vector<int> Marc(adj.size()), Dist(adj.size(), INF);
q1.push(s); Dist[s] = 0;
while (!q1.empty() || !q2.empty())
{
int cur;
if (!q1.empty()) cur = q1.front(), q1.pop();
else cur = q2.front(), q2.pop();
if (Marc[cur]) continue;
Marc[cur] = 1;
for (auto [viz, p] : adj[cur])
{
if (Marc[viz]) continue;
if (Dist[viz] <= Dist[cur] + p) continue;
if (p == 0) q1.push(viz);
else q2.push(viz);
Dist[viz] = Dist[cur]+p;
}
}
for (auto [x, y] : comps)
{
if (x <= s && s <= y)
{
resp += 2*Dist[x];
break;
}
}
}
return resp;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:59:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (int i = 0; i+1 < comps.size(); i++) resp += 2*(comps[i+1].first - comps[i].second);
| ~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 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 |
1 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 |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 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 |
1 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 |
75 ms |
15952 KB |
Output is correct |
31 |
Correct |
73 ms |
22612 KB |
Output is correct |
32 |
Correct |
67 ms |
14828 KB |
Output is correct |
33 |
Correct |
84 ms |
31068 KB |
Output is correct |
34 |
Correct |
77 ms |
30920 KB |
Output is correct |
35 |
Correct |
89 ms |
26816 KB |
Output is correct |
36 |
Correct |
77 ms |
24772 KB |
Output is correct |
37 |
Correct |
71 ms |
22880 KB |
Output is correct |
38 |
Correct |
68 ms |
22612 KB |
Output is correct |
39 |
Correct |
72 ms |
22616 KB |
Output is correct |
40 |
Correct |
80 ms |
22764 KB |
Output is correct |
41 |
Correct |
79 ms |
22612 KB |
Output is correct |
42 |
Correct |
72 ms |
22768 KB |
Output is correct |
43 |
Correct |
66 ms |
22612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 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 |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
424 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 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 |
1 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 |
75 ms |
15952 KB |
Output is correct |
31 |
Correct |
73 ms |
22612 KB |
Output is correct |
32 |
Correct |
67 ms |
14828 KB |
Output is correct |
33 |
Correct |
84 ms |
31068 KB |
Output is correct |
34 |
Correct |
77 ms |
30920 KB |
Output is correct |
35 |
Correct |
89 ms |
26816 KB |
Output is correct |
36 |
Correct |
77 ms |
24772 KB |
Output is correct |
37 |
Correct |
71 ms |
22880 KB |
Output is correct |
38 |
Correct |
68 ms |
22612 KB |
Output is correct |
39 |
Correct |
72 ms |
22616 KB |
Output is correct |
40 |
Correct |
80 ms |
22764 KB |
Output is correct |
41 |
Correct |
79 ms |
22612 KB |
Output is correct |
42 |
Correct |
72 ms |
22768 KB |
Output is correct |
43 |
Correct |
66 ms |
22612 KB |
Output is correct |
44 |
Correct |
1 ms |
344 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
344 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
604 KB |
Output is correct |
51 |
Correct |
1 ms |
604 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
1 ms |
604 KB |
Output is correct |
55 |
Correct |
1 ms |
604 KB |
Output is correct |
56 |
Correct |
1 ms |
424 KB |
Output is correct |
57 |
Correct |
1 ms |
600 KB |
Output is correct |
58 |
Correct |
1 ms |
604 KB |
Output is correct |
59 |
Correct |
1 ms |
604 KB |
Output is correct |
60 |
Correct |
1 ms |
604 KB |
Output is correct |
61 |
Correct |
1 ms |
604 KB |
Output is correct |
62 |
Correct |
1 ms |
344 KB |
Output is correct |
63 |
Correct |
1 ms |
604 KB |
Output is correct |
64 |
Correct |
417 ms |
171128 KB |
Output is correct |
65 |
Correct |
418 ms |
169112 KB |
Output is correct |
66 |
Correct |
558 ms |
225316 KB |
Output is correct |
67 |
Correct |
612 ms |
237976 KB |
Output is correct |
68 |
Correct |
46 ms |
19240 KB |
Output is correct |
69 |
Correct |
62 ms |
20264 KB |
Output is correct |
70 |
Correct |
47 ms |
19240 KB |
Output is correct |
71 |
Correct |
39 ms |
17452 KB |
Output is correct |
72 |
Correct |
35 ms |
16688 KB |
Output is correct |
73 |
Correct |
69 ms |
28164 KB |
Output is correct |
74 |
Correct |
422 ms |
170236 KB |
Output is correct |
75 |
Correct |
502 ms |
170228 KB |
Output is correct |
76 |
Correct |
535 ms |
181536 KB |
Output is correct |
77 |
Correct |
487 ms |
183844 KB |
Output is correct |
78 |
Correct |
486 ms |
175876 KB |
Output is correct |
79 |
Correct |
478 ms |
176024 KB |
Output is correct |
80 |
Correct |
69 ms |
14944 KB |
Output is correct |
81 |
Correct |
631 ms |
303768 KB |
Output is correct |
82 |
Correct |
617 ms |
303928 KB |
Output is correct |
83 |
Correct |
529 ms |
182712 KB |
Output is correct |
84 |
Correct |
571 ms |
183192 KB |
Output is correct |
85 |
Correct |
506 ms |
188308 KB |
Output is correct |
86 |
Correct |
564 ms |
201280 KB |
Output is correct |
87 |
Correct |
550 ms |
226852 KB |
Output is correct |
88 |
Correct |
516 ms |
201112 KB |
Output is correct |
89 |
Correct |
524 ms |
183980 KB |
Output is correct |
90 |
Correct |
597 ms |
223384 KB |
Output is correct |
91 |
Correct |
645 ms |
240412 KB |
Output is correct |
92 |
Correct |
808 ms |
266160 KB |
Output is correct |