#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#include "grader.cpp"
#else
#include "books.h"
#endif
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
#define lpos pos*2
#define rpos pos*2+1
const int INF = 1e9;
const int SIZE = 5e6 + 5;
int n, L, R;
int p[SIZE];
bool vs[SIZE];
int l[SIZE], r[SIZE];
int d[SIZE], in[SIZE], out[SIZE], dis[SIZE], pre[SIZE], g[SIZE];
int sz, id[SIZE];
deque<int> q;
pair<int, int> e[SIZE];
void build(int pos, int l, int r) {
id[pos] = (l == r ? l : ++sz);
debug(l, r, id[pos]);
if (l < r) {
int mid = (l + r) / 2;
build(lpos, l, mid);
build(rpos, mid + 1, r);
e[id[pos]] = {id[lpos], id[rpos]};
}
}
void add(int pos, int l, int r, int L, int R, int d, int k) {
if (l == L && r == R) {
if (d < dis[id[pos]]) {
dis[id[pos]] = d;
q.emplace_front(id[pos]);
pre[id[pos]] = k;
}
return;
}
int mid = (L + R) / 2;
if (r <= mid) add(lpos, l, r, L, mid, d, k);
else if (l > mid) add(rpos, l, r, mid + 1, R, d, k);
else {
add(lpos, l, mid, L, mid, d, k);
add(rpos, mid + 1, r, mid + 1, R, d, k);
}
}
ll minimum_walk(vector<int> p_, int s) {
n = p_.size();
s++;
FOR (i, 1, n) {
p[i] = p_[i - 1] + 1;
in[i] = out[i] = vs[i] = pre[i] = 0;
}
FOR (i, 1, n) {
if (i < p[i]) {
out[i]++, out[p[i]]--;
in[i + 1]++, in[p[i] + 1]--;
d[i]++, d[p[i]]--;
} else {
out[p[i] + 1]++, out[i + 1]--;
in[p[i]]++, in[i]--;
d[p[i]]++, d[i]--;
}
}
FOR (i, 1, n) {
in[i] += in[i - 1];
out[i] += out[i - 1];
d[i] += d[i - 1];
}
L = 1, R = n;
while (R > s && d[R - 1] == 0) R--;
while (L < s && d[L] == 0) L++;
if (L == R) return 0;
int cnt = 0;
FOR (i, L, R) if (vs[i] == 0) {
int mn = n, mx = 1, x = i;
vector<int> v;
while (vs[x] == 0) {
mn = min(mn, x);
mx = max(mx, x);
v.pb(x), vs[x] = 1, x = p[x];
}
cnt++;
for (int x : v) l[x] = mn, r[x] = mx, g[x] = cnt;
}
ll ans = 0;
FOR (i, L, R) ans += out[i];
sz = n;
build(1, 1, n);
fill(dis + 1, dis + sz + 1, INF);
fill(vs + 1, vs + sz + 1, 0);
q.clear();
dis[s] = 0, q.pb(s);
while (q.size()) {
int pos = q.front();
q.pop_front();
if (vs[pos]) continue;
vs[pos] = 1;
debug(pos, dis[pos]);
auto upd = [&](int i, int d, int t) {
if (d < dis[i]) {
dis[i] = d;
if (t == 0) q.emplace_front(i);
else q.pb(i);
pre[i] = pos;
}
};
if (pos > n) {
auto [x, y] = e[pos];
upd(x, dis[pos], 0);
upd(y, dis[pos], 0);
continue;
}
add(1, l[pos], r[pos], 1, n, dis[pos], pos);
if (pos - 1 >= L) upd(pos - 1, dis[pos] + 1, 1);
if (pos + 1 <= R) upd(pos + 1, dis[pos] + 1, 1);
}
debug("here");
int x = L, y = R;
while (pre[x] && dis[pre[x]] == dis[x]) x = pre[x];
while (pre[y] && dis[pre[y]] == dis[y]) y = pre[y];
while (g[x] != g[y]) {
debug(x, y, g[x], g[y]);
if (dis[x] < dis[y]) swap(x, y);
x = pre[x];
ans += 2;
while (pre[x] && dis[pre[x]] == dis[x]) x = pre[x];
}
ans += 2 * dis[x];
return ans;
}
/*
in1
4 0
0 2 3 1
out1
6
*/
Compilation message
books.cpp: In function 'void build(int, int, int)':
books.cpp:18:20: warning: statement has no effect [-Wunused-value]
18 | #define debug(...) 7122
| ^~~~
books.cpp:45:5: note: in expansion of macro 'debug'
45 | debug(l, r, id[pos]);
| ^~~~~
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:76:41: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
76 | in[i] = out[i] = vs[i] = pre[i] = 0;
| ~~~~~~~^~~
books.cpp:18:20: warning: statement has no effect [-Wunused-value]
18 | #define debug(...) 7122
| ^~~~
books.cpp:129:9: note: in expansion of macro 'debug'
129 | debug(pos, dis[pos]);
| ^~~~~
books.cpp:18:20: warning: statement has no effect [-Wunused-value]
18 | #define debug(...) 7122
| ^~~~
books.cpp:148:5: note: in expansion of macro 'debug'
148 | debug("here");
| ^~~~~
books.cpp:18:20: warning: statement has no effect [-Wunused-value]
18 | #define debug(...) 7122
| ^~~~
books.cpp:153:9: note: in expansion of macro 'debug'
153 | debug(x, y, g[x], g[y]);
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
22876 KB |
Output is correct |
2 |
Correct |
2 ms |
22876 KB |
Output is correct |
3 |
Correct |
2 ms |
22876 KB |
Output is correct |
4 |
Correct |
2 ms |
22876 KB |
Output is correct |
5 |
Correct |
2 ms |
22876 KB |
Output is correct |
6 |
Correct |
2 ms |
22876 KB |
Output is correct |
7 |
Correct |
2 ms |
22876 KB |
Output is correct |
8 |
Correct |
2 ms |
22876 KB |
Output is correct |
9 |
Correct |
1 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
22876 KB |
Output is correct |
11 |
Correct |
1 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
23004 KB |
Output is correct |
13 |
Correct |
2 ms |
22872 KB |
Output is correct |
14 |
Correct |
1 ms |
14684 KB |
Output is correct |
15 |
Correct |
2 ms |
22876 KB |
Output is correct |
16 |
Correct |
2 ms |
22876 KB |
Output is correct |
17 |
Correct |
2 ms |
22876 KB |
Output is correct |
18 |
Correct |
2 ms |
22876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
22876 KB |
Output is correct |
2 |
Correct |
2 ms |
22876 KB |
Output is correct |
3 |
Correct |
2 ms |
22876 KB |
Output is correct |
4 |
Correct |
2 ms |
22876 KB |
Output is correct |
5 |
Correct |
2 ms |
22876 KB |
Output is correct |
6 |
Correct |
2 ms |
22876 KB |
Output is correct |
7 |
Correct |
2 ms |
22876 KB |
Output is correct |
8 |
Correct |
2 ms |
22876 KB |
Output is correct |
9 |
Correct |
1 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
22876 KB |
Output is correct |
11 |
Correct |
1 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
23004 KB |
Output is correct |
13 |
Correct |
2 ms |
22872 KB |
Output is correct |
14 |
Correct |
1 ms |
14684 KB |
Output is correct |
15 |
Correct |
2 ms |
22876 KB |
Output is correct |
16 |
Correct |
2 ms |
22876 KB |
Output is correct |
17 |
Correct |
2 ms |
22876 KB |
Output is correct |
18 |
Correct |
2 ms |
22876 KB |
Output is correct |
19 |
Correct |
2 ms |
23032 KB |
Output is correct |
20 |
Correct |
2 ms |
22876 KB |
Output is correct |
21 |
Correct |
1 ms |
14684 KB |
Output is correct |
22 |
Correct |
2 ms |
22876 KB |
Output is correct |
23 |
Correct |
2 ms |
22876 KB |
Output is correct |
24 |
Correct |
2 ms |
22876 KB |
Output is correct |
25 |
Correct |
2 ms |
22876 KB |
Output is correct |
26 |
Correct |
2 ms |
22876 KB |
Output is correct |
27 |
Correct |
2 ms |
22876 KB |
Output is correct |
28 |
Correct |
2 ms |
22876 KB |
Output is correct |
29 |
Correct |
2 ms |
22876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
22876 KB |
Output is correct |
2 |
Correct |
2 ms |
22876 KB |
Output is correct |
3 |
Correct |
2 ms |
22876 KB |
Output is correct |
4 |
Correct |
2 ms |
22876 KB |
Output is correct |
5 |
Correct |
2 ms |
22876 KB |
Output is correct |
6 |
Correct |
2 ms |
22876 KB |
Output is correct |
7 |
Correct |
2 ms |
22876 KB |
Output is correct |
8 |
Correct |
2 ms |
22876 KB |
Output is correct |
9 |
Correct |
1 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
22876 KB |
Output is correct |
11 |
Correct |
1 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
23004 KB |
Output is correct |
13 |
Correct |
2 ms |
22872 KB |
Output is correct |
14 |
Correct |
1 ms |
14684 KB |
Output is correct |
15 |
Correct |
2 ms |
22876 KB |
Output is correct |
16 |
Correct |
2 ms |
22876 KB |
Output is correct |
17 |
Correct |
2 ms |
22876 KB |
Output is correct |
18 |
Correct |
2 ms |
22876 KB |
Output is correct |
19 |
Correct |
2 ms |
23032 KB |
Output is correct |
20 |
Correct |
2 ms |
22876 KB |
Output is correct |
21 |
Correct |
1 ms |
14684 KB |
Output is correct |
22 |
Correct |
2 ms |
22876 KB |
Output is correct |
23 |
Correct |
2 ms |
22876 KB |
Output is correct |
24 |
Correct |
2 ms |
22876 KB |
Output is correct |
25 |
Correct |
2 ms |
22876 KB |
Output is correct |
26 |
Correct |
2 ms |
22876 KB |
Output is correct |
27 |
Correct |
2 ms |
22876 KB |
Output is correct |
28 |
Correct |
2 ms |
22876 KB |
Output is correct |
29 |
Correct |
2 ms |
22876 KB |
Output is correct |
30 |
Correct |
183 ms |
96568 KB |
Output is correct |
31 |
Correct |
234 ms |
96412 KB |
Output is correct |
32 |
Correct |
57 ms |
43092 KB |
Output is correct |
33 |
Correct |
146 ms |
94436 KB |
Output is correct |
34 |
Correct |
154 ms |
94292 KB |
Output is correct |
35 |
Correct |
180 ms |
94288 KB |
Output is correct |
36 |
Correct |
152 ms |
94444 KB |
Output is correct |
37 |
Correct |
149 ms |
94204 KB |
Output is correct |
38 |
Correct |
152 ms |
94292 KB |
Output is correct |
39 |
Correct |
151 ms |
94288 KB |
Output is correct |
40 |
Correct |
205 ms |
94580 KB |
Output is correct |
41 |
Correct |
172 ms |
96012 KB |
Output is correct |
42 |
Correct |
198 ms |
95288 KB |
Output is correct |
43 |
Correct |
179 ms |
96592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
23128 KB |
Output is correct |
2 |
Correct |
2 ms |
22876 KB |
Output is correct |
3 |
Correct |
2 ms |
22876 KB |
Output is correct |
4 |
Correct |
2 ms |
22876 KB |
Output is correct |
5 |
Correct |
2 ms |
22876 KB |
Output is correct |
6 |
Correct |
2 ms |
22876 KB |
Output is correct |
7 |
Correct |
2 ms |
22872 KB |
Output is correct |
8 |
Correct |
2 ms |
22876 KB |
Output is correct |
9 |
Correct |
2 ms |
22876 KB |
Output is correct |
10 |
Correct |
1 ms |
14684 KB |
Output is correct |
11 |
Correct |
2 ms |
22876 KB |
Output is correct |
12 |
Correct |
2 ms |
22876 KB |
Output is correct |
13 |
Correct |
2 ms |
22872 KB |
Output is correct |
14 |
Correct |
2 ms |
22876 KB |
Output is correct |
15 |
Correct |
2 ms |
22872 KB |
Output is correct |
16 |
Correct |
2 ms |
22876 KB |
Output is correct |
17 |
Correct |
2 ms |
22988 KB |
Output is correct |
18 |
Correct |
2 ms |
22876 KB |
Output is correct |
19 |
Correct |
2 ms |
22876 KB |
Output is correct |
20 |
Correct |
2 ms |
22876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
22876 KB |
Output is correct |
2 |
Correct |
2 ms |
22876 KB |
Output is correct |
3 |
Correct |
2 ms |
22876 KB |
Output is correct |
4 |
Correct |
2 ms |
22876 KB |
Output is correct |
5 |
Correct |
2 ms |
22876 KB |
Output is correct |
6 |
Correct |
2 ms |
22876 KB |
Output is correct |
7 |
Correct |
2 ms |
22876 KB |
Output is correct |
8 |
Correct |
2 ms |
22876 KB |
Output is correct |
9 |
Correct |
1 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
22876 KB |
Output is correct |
11 |
Correct |
1 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
23004 KB |
Output is correct |
13 |
Correct |
2 ms |
22872 KB |
Output is correct |
14 |
Correct |
1 ms |
14684 KB |
Output is correct |
15 |
Correct |
2 ms |
22876 KB |
Output is correct |
16 |
Correct |
2 ms |
22876 KB |
Output is correct |
17 |
Correct |
2 ms |
22876 KB |
Output is correct |
18 |
Correct |
2 ms |
22876 KB |
Output is correct |
19 |
Correct |
2 ms |
23032 KB |
Output is correct |
20 |
Correct |
2 ms |
22876 KB |
Output is correct |
21 |
Correct |
1 ms |
14684 KB |
Output is correct |
22 |
Correct |
2 ms |
22876 KB |
Output is correct |
23 |
Correct |
2 ms |
22876 KB |
Output is correct |
24 |
Correct |
2 ms |
22876 KB |
Output is correct |
25 |
Correct |
2 ms |
22876 KB |
Output is correct |
26 |
Correct |
2 ms |
22876 KB |
Output is correct |
27 |
Correct |
2 ms |
22876 KB |
Output is correct |
28 |
Correct |
2 ms |
22876 KB |
Output is correct |
29 |
Correct |
2 ms |
22876 KB |
Output is correct |
30 |
Correct |
183 ms |
96568 KB |
Output is correct |
31 |
Correct |
234 ms |
96412 KB |
Output is correct |
32 |
Correct |
57 ms |
43092 KB |
Output is correct |
33 |
Correct |
146 ms |
94436 KB |
Output is correct |
34 |
Correct |
154 ms |
94292 KB |
Output is correct |
35 |
Correct |
180 ms |
94288 KB |
Output is correct |
36 |
Correct |
152 ms |
94444 KB |
Output is correct |
37 |
Correct |
149 ms |
94204 KB |
Output is correct |
38 |
Correct |
152 ms |
94292 KB |
Output is correct |
39 |
Correct |
151 ms |
94288 KB |
Output is correct |
40 |
Correct |
205 ms |
94580 KB |
Output is correct |
41 |
Correct |
172 ms |
96012 KB |
Output is correct |
42 |
Correct |
198 ms |
95288 KB |
Output is correct |
43 |
Correct |
179 ms |
96592 KB |
Output is correct |
44 |
Correct |
2 ms |
23128 KB |
Output is correct |
45 |
Correct |
2 ms |
22876 KB |
Output is correct |
46 |
Correct |
2 ms |
22876 KB |
Output is correct |
47 |
Correct |
2 ms |
22876 KB |
Output is correct |
48 |
Correct |
2 ms |
22876 KB |
Output is correct |
49 |
Correct |
2 ms |
22876 KB |
Output is correct |
50 |
Correct |
2 ms |
22872 KB |
Output is correct |
51 |
Correct |
2 ms |
22876 KB |
Output is correct |
52 |
Correct |
2 ms |
22876 KB |
Output is correct |
53 |
Correct |
1 ms |
14684 KB |
Output is correct |
54 |
Correct |
2 ms |
22876 KB |
Output is correct |
55 |
Correct |
2 ms |
22876 KB |
Output is correct |
56 |
Correct |
2 ms |
22872 KB |
Output is correct |
57 |
Correct |
2 ms |
22876 KB |
Output is correct |
58 |
Correct |
2 ms |
22872 KB |
Output is correct |
59 |
Correct |
2 ms |
22876 KB |
Output is correct |
60 |
Correct |
2 ms |
22988 KB |
Output is correct |
61 |
Correct |
2 ms |
22876 KB |
Output is correct |
62 |
Correct |
2 ms |
22876 KB |
Output is correct |
63 |
Correct |
2 ms |
22876 KB |
Output is correct |
64 |
Correct |
154 ms |
102992 KB |
Output is correct |
65 |
Correct |
162 ms |
102508 KB |
Output is correct |
66 |
Correct |
171 ms |
101712 KB |
Output is correct |
67 |
Correct |
150 ms |
102720 KB |
Output is correct |
68 |
Correct |
22 ms |
28504 KB |
Output is correct |
69 |
Correct |
16 ms |
28508 KB |
Output is correct |
70 |
Correct |
16 ms |
28692 KB |
Output is correct |
71 |
Correct |
17 ms |
28508 KB |
Output is correct |
72 |
Correct |
18 ms |
28500 KB |
Output is correct |
73 |
Correct |
16 ms |
28508 KB |
Output is correct |
74 |
Correct |
165 ms |
101104 KB |
Output is correct |
75 |
Correct |
156 ms |
100952 KB |
Output is correct |
76 |
Correct |
176 ms |
103252 KB |
Output is correct |
77 |
Correct |
180 ms |
103252 KB |
Output is correct |
78 |
Correct |
180 ms |
101764 KB |
Output is correct |
79 |
Correct |
192 ms |
101632 KB |
Output is correct |
80 |
Correct |
58 ms |
49748 KB |
Output is correct |
81 |
Correct |
203 ms |
101108 KB |
Output is correct |
82 |
Correct |
195 ms |
101056 KB |
Output is correct |
83 |
Correct |
171 ms |
100944 KB |
Output is correct |
84 |
Correct |
165 ms |
100976 KB |
Output is correct |
85 |
Correct |
154 ms |
100992 KB |
Output is correct |
86 |
Correct |
147 ms |
100948 KB |
Output is correct |
87 |
Correct |
181 ms |
100976 KB |
Output is correct |
88 |
Correct |
176 ms |
100944 KB |
Output is correct |
89 |
Correct |
170 ms |
100948 KB |
Output is correct |
90 |
Correct |
146 ms |
101076 KB |
Output is correct |
91 |
Correct |
147 ms |
100944 KB |
Output is correct |
92 |
Correct |
155 ms |
101204 KB |
Output is correct |