#include "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, s, ts, ans, a[1001000], cycL[1001000], cycR[1001000], L[1001000], R[1001000], dp[1001000];
bool visited[1001000];
ll cycsum;
struct Seg{
int tree[2002000], sz;
void init(int n){
sz = n;
fill(tree, tree+sz*2, 1e9);
}
void update(int p, int x){
for (tree[p+=sz]=x;p>1;p>>=1) tree[p>>1] = min(tree[p], tree[p^1]);
}
int query(int l, int r){
int ret = 1e9;
++r;
for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
if (l&1) ret = min(ret, tree[l++]);
if (r&1) ret = min(ret, tree[--r]);
}
return ret;
}
}tree1, tree2;
ll myabs(ll x){
if (x<0) return -x;
return x;
}
void dfs(int l, int r){
//printf("dfs: %d %d\n", l, r);
if (l==L[ts] && r==R[ts]) {ans += dp[L[ts]]; return;}
vector<pair<int, int>> child;
for (int i=l+1;i<r;){
if (R[i]<r){
child.emplace_back(i, R[i]);
i = R[i] + 1;
}
else i++;
}
vector<int> gl = {1}, gr = {1};
for (int i=1;i<(int)child.size();i++){
if (child[i-1].second+1==child[i].first) gl.push_back(gl.back()+1);
else gl.push_back(1);
}
for (int i=(int)child.size()-2;i>=0;i--){
if (child[i].second+1==child[i+1].first) gr.push_back(gr.back()+1);
else gr.push_back(1);
}
reverse(gr.begin(), gr.end());
for (int i=0;i<(int)child.size();i++) dp[child[i].first] = min(gl[i], gr[i]) + dp[l];
if (l==0) for (int i=0;i<(int)child.size();i++) dp[child[i].first] = (int)child.size()-1;
for (auto &[nl, nr]:child) dfs(nl, nr);
}
map<pair<int, int>, int> mp;
void naive(int s){
int ptl = s, ptr = s;
while(true){
int nl = tree1.query(ptl, ptr);
int nr = -tree2.query(ptl, ptr);
if (nl==ptl && nr==ptr) {L[s] = nl, R[s] = nr; break;}
if (mp.find(make_pair(nl, nr))!=mp.end()) {
int tmp = mp[make_pair(nl, nr)];
L[s] = L[tmp], R[s] = R[tmp];
break;
}
mp[make_pair(nl, nr)] = s;
ptl = nl, ptr = nr;
}
}
long long minimum_walk(std::vector<int> P, int S) {
int si = 0, ei = (int)P.size()-1;
for(;si<(int)P.size() && P[si]==si;si++);
for(;ei>=0 && P[ei]==ei;ei--);
if (si > ei) return 0;
n = ei-si+1;
for (int i=si;i<=ei;i++) a[i-si+1] = P[i] - si + 1;
s = S-si+1;
tree1.init(n+1);
tree2.init(n+1);
for (int i=1;i<=n;i++) if (!visited[i]){
vector<int> cyc;
for (int j=i;!visited[j];j=a[j]){
cyc.push_back(j);
visited[j] = 1;
cycR[i] = max(cycR[i], j);
cycsum += myabs(j - a[j]);
}
cycL[cycR[i]] = i;
for (auto &x:cyc){
tree1.update(x, i);
tree2.update(x, -cycR[i]);
}
}
///calc range
for (int i=1;i<=n;i++) naive(i);
/*for (int i=1;i<=n;i++) printf("(%d, %d) ", cycL[i], cycR[i]);
printf("\n");
for (int i=1;i<=n;i++) printf("(%d, %d) ", L[i], R[i]);
printf("\n");*/
if (s<=0) ts = 1, ans = 1-s;
else if (s>n) ts = n, ans = s-n;
else ts = s, ans = 0;
//printf("ok n = %d, s = %d\n", n, s);
dfs(0, n+1);
//printf("%lld + %d * 2\n", cycsum, ans);
return cycsum + ans * 2;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
308 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
304 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
308 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
304 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
436 KB |
Output is correct |
25 |
Correct |
1 ms |
436 KB |
Output is correct |
26 |
Correct |
1 ms |
308 KB |
Output is correct |
27 |
Correct |
1 ms |
308 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
308 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
304 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
436 KB |
Output is correct |
25 |
Correct |
1 ms |
436 KB |
Output is correct |
26 |
Correct |
1 ms |
308 KB |
Output is correct |
27 |
Correct |
1 ms |
308 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
389 ms |
39692 KB |
Output is correct |
31 |
Correct |
400 ms |
39924 KB |
Output is correct |
32 |
Correct |
85 ms |
9308 KB |
Output is correct |
33 |
Correct |
339 ms |
76136 KB |
Output is correct |
34 |
Correct |
337 ms |
76104 KB |
Output is correct |
35 |
Correct |
343 ms |
73336 KB |
Output is correct |
36 |
Correct |
333 ms |
63936 KB |
Output is correct |
37 |
Correct |
247 ms |
51532 KB |
Output is correct |
38 |
Correct |
249 ms |
47820 KB |
Output is correct |
39 |
Correct |
247 ms |
46996 KB |
Output is correct |
40 |
Correct |
210 ms |
40692 KB |
Output is correct |
41 |
Correct |
260 ms |
38844 KB |
Output is correct |
42 |
Correct |
245 ms |
39396 KB |
Output is correct |
43 |
Correct |
366 ms |
61092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
432 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
472 KB |
Output is correct |
18 |
Correct |
1 ms |
472 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
308 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
304 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
436 KB |
Output is correct |
25 |
Correct |
1 ms |
436 KB |
Output is correct |
26 |
Correct |
1 ms |
308 KB |
Output is correct |
27 |
Correct |
1 ms |
308 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
389 ms |
39692 KB |
Output is correct |
31 |
Correct |
400 ms |
39924 KB |
Output is correct |
32 |
Correct |
85 ms |
9308 KB |
Output is correct |
33 |
Correct |
339 ms |
76136 KB |
Output is correct |
34 |
Correct |
337 ms |
76104 KB |
Output is correct |
35 |
Correct |
343 ms |
73336 KB |
Output is correct |
36 |
Correct |
333 ms |
63936 KB |
Output is correct |
37 |
Correct |
247 ms |
51532 KB |
Output is correct |
38 |
Correct |
249 ms |
47820 KB |
Output is correct |
39 |
Correct |
247 ms |
46996 KB |
Output is correct |
40 |
Correct |
210 ms |
40692 KB |
Output is correct |
41 |
Correct |
260 ms |
38844 KB |
Output is correct |
42 |
Correct |
245 ms |
39396 KB |
Output is correct |
43 |
Correct |
366 ms |
61092 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
432 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
468 KB |
Output is correct |
55 |
Correct |
1 ms |
468 KB |
Output is correct |
56 |
Correct |
2 ms |
340 KB |
Output is correct |
57 |
Correct |
2 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
472 KB |
Output is correct |
61 |
Correct |
1 ms |
472 KB |
Output is correct |
62 |
Correct |
1 ms |
344 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
64 |
Correct |
311 ms |
62812 KB |
Output is correct |
65 |
Correct |
307 ms |
61924 KB |
Output is correct |
66 |
Correct |
244 ms |
56528 KB |
Output is correct |
67 |
Correct |
243 ms |
55788 KB |
Output is correct |
68 |
Correct |
27 ms |
6604 KB |
Output is correct |
69 |
Correct |
25 ms |
6144 KB |
Output is correct |
70 |
Correct |
27 ms |
6324 KB |
Output is correct |
71 |
Correct |
28 ms |
6660 KB |
Output is correct |
72 |
Correct |
26 ms |
6484 KB |
Output is correct |
73 |
Correct |
21 ms |
5088 KB |
Output is correct |
74 |
Correct |
328 ms |
81624 KB |
Output is correct |
75 |
Correct |
336 ms |
81628 KB |
Output is correct |
76 |
Correct |
424 ms |
70780 KB |
Output is correct |
77 |
Correct |
435 ms |
70636 KB |
Output is correct |
78 |
Correct |
512 ms |
78076 KB |
Output is correct |
79 |
Correct |
525 ms |
77936 KB |
Output is correct |
80 |
Correct |
89 ms |
14836 KB |
Output is correct |
81 |
Correct |
629 ms |
179956 KB |
Output is correct |
82 |
Correct |
643 ms |
180004 KB |
Output is correct |
83 |
Correct |
394 ms |
84216 KB |
Output is correct |
84 |
Correct |
363 ms |
78452 KB |
Output is correct |
85 |
Correct |
303 ms |
65120 KB |
Output is correct |
86 |
Correct |
267 ms |
58828 KB |
Output is correct |
87 |
Correct |
440 ms |
117516 KB |
Output is correct |
88 |
Correct |
482 ms |
110052 KB |
Output is correct |
89 |
Correct |
525 ms |
90220 KB |
Output is correct |
90 |
Correct |
256 ms |
56452 KB |
Output is correct |
91 |
Correct |
294 ms |
55400 KB |
Output is correct |
92 |
Correct |
214 ms |
46808 KB |
Output is correct |