#include "books.h"
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
using namespace std;
using ll = long long;
bool seen[1000005];
vector<int> P;
vector<int> t;
struct Node
{
int mn, mx;
};
Node nodes[8000005];
void upd(int i, int l, int r, int p, int mn, int mx)
{
auto &node = nodes[i];
if (p < l or p > r)
return;
if (l == r)
{
node.mn = mn;
node.mx = mx;
return;
}
int m = (l + r) / 2;
upd(2 * i, l, m, p, mn, mx);
upd(2 * i + 1, m + 1, r, p, mn, mx);
node.mn = min(nodes[2 * i].mn, nodes[2 * i + 1].mn);
node.mx = max(nodes[2 * i].mx, nodes[2 * i + 1].mx);
}
pair<int, int> query(int i, int l, int r, int ql, int qr)
{
auto &node = nodes[i];
if (qr < l or ql > r)
return {1e9, 0};
if (ql <= l and qr >= r)
return {node.mn, node.mx};
int m = (l + r) / 2;
auto A = query(2 * i, l, m, ql, qr);
auto B = query(2 * i + 1, m + 1, r, ql, qr);
return {min(A.first, B.first), max(A.second, B.second)};
}
ll dfs(int i)
{
if (seen[i])
return 0;
seen[i] = true;
t.push_back(i);
return abs(i - P[i]) + dfs(P[i]);
}
ll minimum_walk(vector<int> p, int s)
{
P = p;
ll sum = 0;
int n = p.size();
vector<pair<int, int>> q;
q.push_back({s, s});
bool lol = false;
for (auto &n : nodes)
n.mn = 1e9;
for (int i = 0; i < n; i++)
{
if (seen[i] or i == p[i])
continue;
t.clear();
sum += dfs(i);
sort(t.begin(), t.end());
q.push_back({t.front(), t.back()});
auto [a, b] = q.back();
for (int x : t)
upd(1, 0, n, x, a, b);
// if (a <= s and s <= b)
// lol = true;
}
sort(q.begin(), q.end());
int st = 0;
int lt = -1;
int pl, pr;
for (auto [a, b] : q)
{
if (lt == -1)
{
st = a;
lt = b;
}
if (a > lt)
{
if (st <= s and s <= lt)
{
lol = true;
pl = st, pr = lt;
}
sum += 2 * (a - lt);
lt = b;
st = a;
}
else
{
lt = max(lt, b);
}
}
if (st <= s and s <= lt)
{
lol = true;
pl = st, pr = lt;
}
if (pl == pr)
lol = false;
for (int p = 0; p < 21; p++)
{
for (int i = 0; i < n; i++)
{
auto [a, b] = query(1, 0, n, i, i);
auto [x, y] = query(1, 0, n, a, b);
// printf("%d %d %d\n", i, x, y);
upd(1, 0, n, i, x, y);
}
}
int mn = 1e9;
for (int i = 0; i < n; i++)
{
auto [a, b] = query(1, 0, n, i, i);
if (a <= pl and pr <= b)
mn = min(mn, abs(i - s));
}
#ifdef DEBUG
printf("%d %d\n", lol, mn);
#endif
if (lol)
sum += 2 * mn;
return sum;
}
Compilation message
books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:119:2: warning: 'pr' may be used uninitialized in this function [-Wmaybe-uninitialized]
119 | if (pl == pr)
| ^~
books.cpp:119:2: warning: 'pl' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
62820 KB |
Output is correct |
2 |
Correct |
32 ms |
62892 KB |
Output is correct |
3 |
Correct |
26 ms |
62912 KB |
Output is correct |
4 |
Correct |
27 ms |
62928 KB |
Output is correct |
5 |
Correct |
26 ms |
62812 KB |
Output is correct |
6 |
Correct |
27 ms |
62804 KB |
Output is correct |
7 |
Correct |
26 ms |
62896 KB |
Output is correct |
8 |
Correct |
27 ms |
62868 KB |
Output is correct |
9 |
Correct |
26 ms |
62788 KB |
Output is correct |
10 |
Correct |
26 ms |
62892 KB |
Output is correct |
11 |
Correct |
25 ms |
62860 KB |
Output is correct |
12 |
Correct |
26 ms |
62804 KB |
Output is correct |
13 |
Correct |
26 ms |
62796 KB |
Output is correct |
14 |
Correct |
27 ms |
62932 KB |
Output is correct |
15 |
Correct |
25 ms |
62792 KB |
Output is correct |
16 |
Correct |
26 ms |
62804 KB |
Output is correct |
17 |
Correct |
30 ms |
63008 KB |
Output is correct |
18 |
Correct |
35 ms |
62804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
62820 KB |
Output is correct |
2 |
Correct |
32 ms |
62892 KB |
Output is correct |
3 |
Correct |
26 ms |
62912 KB |
Output is correct |
4 |
Correct |
27 ms |
62928 KB |
Output is correct |
5 |
Correct |
26 ms |
62812 KB |
Output is correct |
6 |
Correct |
27 ms |
62804 KB |
Output is correct |
7 |
Correct |
26 ms |
62896 KB |
Output is correct |
8 |
Correct |
27 ms |
62868 KB |
Output is correct |
9 |
Correct |
26 ms |
62788 KB |
Output is correct |
10 |
Correct |
26 ms |
62892 KB |
Output is correct |
11 |
Correct |
25 ms |
62860 KB |
Output is correct |
12 |
Correct |
26 ms |
62804 KB |
Output is correct |
13 |
Correct |
26 ms |
62796 KB |
Output is correct |
14 |
Correct |
27 ms |
62932 KB |
Output is correct |
15 |
Correct |
25 ms |
62792 KB |
Output is correct |
16 |
Correct |
26 ms |
62804 KB |
Output is correct |
17 |
Correct |
30 ms |
63008 KB |
Output is correct |
18 |
Correct |
35 ms |
62804 KB |
Output is correct |
19 |
Correct |
31 ms |
62952 KB |
Output is correct |
20 |
Correct |
30 ms |
62872 KB |
Output is correct |
21 |
Correct |
29 ms |
62908 KB |
Output is correct |
22 |
Correct |
33 ms |
62932 KB |
Output is correct |
23 |
Correct |
32 ms |
62832 KB |
Output is correct |
24 |
Correct |
31 ms |
62932 KB |
Output is correct |
25 |
Correct |
31 ms |
62856 KB |
Output is correct |
26 |
Correct |
33 ms |
62932 KB |
Output is correct |
27 |
Correct |
31 ms |
62840 KB |
Output is correct |
28 |
Correct |
33 ms |
62932 KB |
Output is correct |
29 |
Correct |
31 ms |
62944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
62820 KB |
Output is correct |
2 |
Correct |
32 ms |
62892 KB |
Output is correct |
3 |
Correct |
26 ms |
62912 KB |
Output is correct |
4 |
Correct |
27 ms |
62928 KB |
Output is correct |
5 |
Correct |
26 ms |
62812 KB |
Output is correct |
6 |
Correct |
27 ms |
62804 KB |
Output is correct |
7 |
Correct |
26 ms |
62896 KB |
Output is correct |
8 |
Correct |
27 ms |
62868 KB |
Output is correct |
9 |
Correct |
26 ms |
62788 KB |
Output is correct |
10 |
Correct |
26 ms |
62892 KB |
Output is correct |
11 |
Correct |
25 ms |
62860 KB |
Output is correct |
12 |
Correct |
26 ms |
62804 KB |
Output is correct |
13 |
Correct |
26 ms |
62796 KB |
Output is correct |
14 |
Correct |
27 ms |
62932 KB |
Output is correct |
15 |
Correct |
25 ms |
62792 KB |
Output is correct |
16 |
Correct |
26 ms |
62804 KB |
Output is correct |
17 |
Correct |
30 ms |
63008 KB |
Output is correct |
18 |
Correct |
35 ms |
62804 KB |
Output is correct |
19 |
Correct |
31 ms |
62952 KB |
Output is correct |
20 |
Correct |
30 ms |
62872 KB |
Output is correct |
21 |
Correct |
29 ms |
62908 KB |
Output is correct |
22 |
Correct |
33 ms |
62932 KB |
Output is correct |
23 |
Correct |
32 ms |
62832 KB |
Output is correct |
24 |
Correct |
31 ms |
62932 KB |
Output is correct |
25 |
Correct |
31 ms |
62856 KB |
Output is correct |
26 |
Correct |
33 ms |
62932 KB |
Output is correct |
27 |
Correct |
31 ms |
62840 KB |
Output is correct |
28 |
Correct |
33 ms |
62932 KB |
Output is correct |
29 |
Correct |
31 ms |
62944 KB |
Output is correct |
30 |
Execution timed out |
2068 ms |
115436 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
62804 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3590' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
62820 KB |
Output is correct |
2 |
Correct |
32 ms |
62892 KB |
Output is correct |
3 |
Correct |
26 ms |
62912 KB |
Output is correct |
4 |
Correct |
27 ms |
62928 KB |
Output is correct |
5 |
Correct |
26 ms |
62812 KB |
Output is correct |
6 |
Correct |
27 ms |
62804 KB |
Output is correct |
7 |
Correct |
26 ms |
62896 KB |
Output is correct |
8 |
Correct |
27 ms |
62868 KB |
Output is correct |
9 |
Correct |
26 ms |
62788 KB |
Output is correct |
10 |
Correct |
26 ms |
62892 KB |
Output is correct |
11 |
Correct |
25 ms |
62860 KB |
Output is correct |
12 |
Correct |
26 ms |
62804 KB |
Output is correct |
13 |
Correct |
26 ms |
62796 KB |
Output is correct |
14 |
Correct |
27 ms |
62932 KB |
Output is correct |
15 |
Correct |
25 ms |
62792 KB |
Output is correct |
16 |
Correct |
26 ms |
62804 KB |
Output is correct |
17 |
Correct |
30 ms |
63008 KB |
Output is correct |
18 |
Correct |
35 ms |
62804 KB |
Output is correct |
19 |
Correct |
31 ms |
62952 KB |
Output is correct |
20 |
Correct |
30 ms |
62872 KB |
Output is correct |
21 |
Correct |
29 ms |
62908 KB |
Output is correct |
22 |
Correct |
33 ms |
62932 KB |
Output is correct |
23 |
Correct |
32 ms |
62832 KB |
Output is correct |
24 |
Correct |
31 ms |
62932 KB |
Output is correct |
25 |
Correct |
31 ms |
62856 KB |
Output is correct |
26 |
Correct |
33 ms |
62932 KB |
Output is correct |
27 |
Correct |
31 ms |
62840 KB |
Output is correct |
28 |
Correct |
33 ms |
62932 KB |
Output is correct |
29 |
Correct |
31 ms |
62944 KB |
Output is correct |
30 |
Execution timed out |
2068 ms |
115436 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |