#include "books.h"
#include <bits/stdc++.h>
using namespace std;
const int N=1e6, LG=20;
int tl, tr;
int vis[N], n, stmin[LG][N], stmax[LG][N];
pair<int, int> ll[N], rr[N];
int check[N];
long long ans=0;
int getmin(int l, int r){
int lg=__lg(r-l+1);
return min(stmin[lg][l], stmin[lg][r-(1<<lg)+1]);
}
int getmax(int l, int r){
int lg=__lg(r-l+1);
return max(stmax[lg][l], stmax[lg][r-(1<<lg)+1]);
}
void extend(int &l, int &r){
while (1){
int nl=getmin(l, r), nr=getmax(l, r);
if (nl==l && nr==r) break;
l=nl; r=nr;
}
}
void dp(int l, int r){
extend(l, r);
if (l==tl && r==tr) return;
int l1=tl, r1=r, l2=l, r2=tr;
if (ll[l].first || ll[l].second){
l1=ll[l].first;
}
if (rr[r].first || rr[r].second){
r2=rr[r].second;
}
int c1=check[l]-check[l1];
int c2=check[r2]-check[r];
if (l1==l) c1=1e9;
if (r2==r) c2=1e9;
ans+=min(c1, c2)*2;
if (c1<=c2) return dp(l1, r1);
return dp(l2, r2);
}
long long minimum_walk(vector<int> p, int s) {
n=p.size();
for (int i=0; i<n; ++i) if (!vis[i]){
vector<int> cc;
int u=i;
stmin[0][i]=i; stmax[0][i]=i;
int maxl=-1, minr=-1;
while (!vis[u]){
if (u<s){
if (maxl==-1 || maxl<u) maxl=u;
}
if (u>s){
if (minr==-1 || minr>u) minr=u;
}
vis[u]=1;
cc.push_back(u);
stmin[0][i]=min(stmin[0][i], u);
stmax[0][i]=max(stmax[0][i], u);
ans+=abs(u-p[u]);
u=p[u];
}
if (stmin[0][i]<s && s<stmax[0][i]){
ll[maxl+1]=rr[minr-1]={maxl, minr};
}else{
int l=stmin[0][i], r=stmax[0][i];
++check[l]; --check[r];
}
for (int j:cc) stmin[0][j]=stmin[0][i], stmax[0][j]=stmax[0][i];
}
partial_sum(check, check+n, check);
for (int i=n-1; i>0; --i) check[i]=!check[i-1];
check[0]=0;
partial_sum(check, check+n, check);
for (int i=1; i<=s; ++i){
if (ll[i].first==0 && ll[i].second==0) ll[i]=ll[i-1];
}
for (int i=n-2; i>=s; --i){
if (rr[i].first==0 && rr[i].second==0) rr[i]=rr[i+1];
}
for (int i=1; i<LG; ++i) for (int j=0; j+(1<<i)-1<n; ++j){
stmin[i][j]=min(stmin[i-1][j], stmin[i-1][j+(1<<(i-1))]);
stmax[i][j]=max(stmax[i-1][j], stmax[i-1][j+(1<<(i-1))]);
}
tl=0, tr=n-1;
while (tl<s && stmin[0][tl]==tl && stmax[0][tl]==tl) ++tl;
while (tr>s && stmin[0][tr]==tr && stmax[0][tr]==tr) --tr;
dp(s, s);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
1 ms |
14684 KB |
Output is correct |
3 |
Correct |
1 ms |
14684 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
14684 KB |
Output is correct |
6 |
Correct |
1 ms |
14684 KB |
Output is correct |
7 |
Correct |
1 ms |
14684 KB |
Output is correct |
8 |
Correct |
1 ms |
14680 KB |
Output is correct |
9 |
Correct |
1 ms |
14684 KB |
Output is correct |
10 |
Correct |
1 ms |
14684 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
14684 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
14936 KB |
Output is correct |
16 |
Correct |
1 ms |
14684 KB |
Output is correct |
17 |
Correct |
2 ms |
14684 KB |
Output is correct |
18 |
Correct |
1 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
1 ms |
14684 KB |
Output is correct |
3 |
Correct |
1 ms |
14684 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
14684 KB |
Output is correct |
6 |
Correct |
1 ms |
14684 KB |
Output is correct |
7 |
Correct |
1 ms |
14684 KB |
Output is correct |
8 |
Correct |
1 ms |
14680 KB |
Output is correct |
9 |
Correct |
1 ms |
14684 KB |
Output is correct |
10 |
Correct |
1 ms |
14684 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
14684 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
14936 KB |
Output is correct |
16 |
Correct |
1 ms |
14684 KB |
Output is correct |
17 |
Correct |
2 ms |
14684 KB |
Output is correct |
18 |
Correct |
1 ms |
14684 KB |
Output is correct |
19 |
Correct |
4 ms |
43356 KB |
Output is correct |
20 |
Correct |
4 ms |
43352 KB |
Output is correct |
21 |
Correct |
4 ms |
43356 KB |
Output is correct |
22 |
Correct |
4 ms |
43356 KB |
Output is correct |
23 |
Correct |
4 ms |
43356 KB |
Output is correct |
24 |
Correct |
4 ms |
43356 KB |
Output is correct |
25 |
Correct |
4 ms |
43356 KB |
Output is correct |
26 |
Correct |
4 ms |
43356 KB |
Output is correct |
27 |
Correct |
4 ms |
43356 KB |
Output is correct |
28 |
Correct |
4 ms |
43356 KB |
Output is correct |
29 |
Correct |
4 ms |
43356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
1 ms |
14684 KB |
Output is correct |
3 |
Correct |
1 ms |
14684 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
14684 KB |
Output is correct |
6 |
Correct |
1 ms |
14684 KB |
Output is correct |
7 |
Correct |
1 ms |
14684 KB |
Output is correct |
8 |
Correct |
1 ms |
14680 KB |
Output is correct |
9 |
Correct |
1 ms |
14684 KB |
Output is correct |
10 |
Correct |
1 ms |
14684 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
14684 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
14936 KB |
Output is correct |
16 |
Correct |
1 ms |
14684 KB |
Output is correct |
17 |
Correct |
2 ms |
14684 KB |
Output is correct |
18 |
Correct |
1 ms |
14684 KB |
Output is correct |
19 |
Correct |
4 ms |
43356 KB |
Output is correct |
20 |
Correct |
4 ms |
43352 KB |
Output is correct |
21 |
Correct |
4 ms |
43356 KB |
Output is correct |
22 |
Correct |
4 ms |
43356 KB |
Output is correct |
23 |
Correct |
4 ms |
43356 KB |
Output is correct |
24 |
Correct |
4 ms |
43356 KB |
Output is correct |
25 |
Correct |
4 ms |
43356 KB |
Output is correct |
26 |
Correct |
4 ms |
43356 KB |
Output is correct |
27 |
Correct |
4 ms |
43356 KB |
Output is correct |
28 |
Correct |
4 ms |
43356 KB |
Output is correct |
29 |
Correct |
4 ms |
43356 KB |
Output is correct |
30 |
Correct |
128 ms |
183244 KB |
Output is correct |
31 |
Correct |
139 ms |
183436 KB |
Output is correct |
32 |
Correct |
123 ms |
181332 KB |
Output is correct |
33 |
Correct |
126 ms |
181332 KB |
Output is correct |
34 |
Correct |
129 ms |
181256 KB |
Output is correct |
35 |
Correct |
131 ms |
181328 KB |
Output is correct |
36 |
Correct |
122 ms |
181236 KB |
Output is correct |
37 |
Correct |
121 ms |
181252 KB |
Output is correct |
38 |
Correct |
114 ms |
181332 KB |
Output is correct |
39 |
Correct |
118 ms |
181256 KB |
Output is correct |
40 |
Correct |
141 ms |
181628 KB |
Output is correct |
41 |
Correct |
135 ms |
182112 KB |
Output is correct |
42 |
Correct |
136 ms |
182320 KB |
Output is correct |
43 |
Correct |
131 ms |
181328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
47452 KB |
Output is correct |
2 |
Correct |
4 ms |
47452 KB |
Output is correct |
3 |
Correct |
4 ms |
47452 KB |
Output is correct |
4 |
Correct |
4 ms |
43356 KB |
Output is correct |
5 |
Correct |
4 ms |
43356 KB |
Output is correct |
6 |
Correct |
4 ms |
47452 KB |
Output is correct |
7 |
Correct |
4 ms |
47452 KB |
Output is correct |
8 |
Correct |
5 ms |
47452 KB |
Output is correct |
9 |
Correct |
4 ms |
47452 KB |
Output is correct |
10 |
Correct |
4 ms |
43356 KB |
Output is correct |
11 |
Correct |
5 ms |
47648 KB |
Output is correct |
12 |
Correct |
4 ms |
47452 KB |
Output is correct |
13 |
Correct |
4 ms |
47452 KB |
Output is correct |
14 |
Correct |
4 ms |
47452 KB |
Output is correct |
15 |
Correct |
4 ms |
47452 KB |
Output is correct |
16 |
Correct |
4 ms |
47452 KB |
Output is correct |
17 |
Correct |
4 ms |
47596 KB |
Output is correct |
18 |
Correct |
4 ms |
47452 KB |
Output is correct |
19 |
Correct |
5 ms |
47452 KB |
Output is correct |
20 |
Correct |
4 ms |
47596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
1 ms |
14684 KB |
Output is correct |
3 |
Correct |
1 ms |
14684 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
14684 KB |
Output is correct |
6 |
Correct |
1 ms |
14684 KB |
Output is correct |
7 |
Correct |
1 ms |
14684 KB |
Output is correct |
8 |
Correct |
1 ms |
14680 KB |
Output is correct |
9 |
Correct |
1 ms |
14684 KB |
Output is correct |
10 |
Correct |
1 ms |
14684 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
14684 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
14936 KB |
Output is correct |
16 |
Correct |
1 ms |
14684 KB |
Output is correct |
17 |
Correct |
2 ms |
14684 KB |
Output is correct |
18 |
Correct |
1 ms |
14684 KB |
Output is correct |
19 |
Correct |
4 ms |
43356 KB |
Output is correct |
20 |
Correct |
4 ms |
43352 KB |
Output is correct |
21 |
Correct |
4 ms |
43356 KB |
Output is correct |
22 |
Correct |
4 ms |
43356 KB |
Output is correct |
23 |
Correct |
4 ms |
43356 KB |
Output is correct |
24 |
Correct |
4 ms |
43356 KB |
Output is correct |
25 |
Correct |
4 ms |
43356 KB |
Output is correct |
26 |
Correct |
4 ms |
43356 KB |
Output is correct |
27 |
Correct |
4 ms |
43356 KB |
Output is correct |
28 |
Correct |
4 ms |
43356 KB |
Output is correct |
29 |
Correct |
4 ms |
43356 KB |
Output is correct |
30 |
Correct |
128 ms |
183244 KB |
Output is correct |
31 |
Correct |
139 ms |
183436 KB |
Output is correct |
32 |
Correct |
123 ms |
181332 KB |
Output is correct |
33 |
Correct |
126 ms |
181332 KB |
Output is correct |
34 |
Correct |
129 ms |
181256 KB |
Output is correct |
35 |
Correct |
131 ms |
181328 KB |
Output is correct |
36 |
Correct |
122 ms |
181236 KB |
Output is correct |
37 |
Correct |
121 ms |
181252 KB |
Output is correct |
38 |
Correct |
114 ms |
181332 KB |
Output is correct |
39 |
Correct |
118 ms |
181256 KB |
Output is correct |
40 |
Correct |
141 ms |
181628 KB |
Output is correct |
41 |
Correct |
135 ms |
182112 KB |
Output is correct |
42 |
Correct |
136 ms |
182320 KB |
Output is correct |
43 |
Correct |
131 ms |
181328 KB |
Output is correct |
44 |
Correct |
4 ms |
47452 KB |
Output is correct |
45 |
Correct |
4 ms |
47452 KB |
Output is correct |
46 |
Correct |
4 ms |
47452 KB |
Output is correct |
47 |
Correct |
4 ms |
43356 KB |
Output is correct |
48 |
Correct |
4 ms |
43356 KB |
Output is correct |
49 |
Correct |
4 ms |
47452 KB |
Output is correct |
50 |
Correct |
4 ms |
47452 KB |
Output is correct |
51 |
Correct |
5 ms |
47452 KB |
Output is correct |
52 |
Correct |
4 ms |
47452 KB |
Output is correct |
53 |
Correct |
4 ms |
43356 KB |
Output is correct |
54 |
Correct |
5 ms |
47648 KB |
Output is correct |
55 |
Correct |
4 ms |
47452 KB |
Output is correct |
56 |
Correct |
4 ms |
47452 KB |
Output is correct |
57 |
Correct |
4 ms |
47452 KB |
Output is correct |
58 |
Correct |
4 ms |
47452 KB |
Output is correct |
59 |
Correct |
4 ms |
47452 KB |
Output is correct |
60 |
Correct |
4 ms |
47596 KB |
Output is correct |
61 |
Correct |
4 ms |
47452 KB |
Output is correct |
62 |
Correct |
5 ms |
47452 KB |
Output is correct |
63 |
Correct |
4 ms |
47596 KB |
Output is correct |
64 |
Correct |
141 ms |
188756 KB |
Output is correct |
65 |
Correct |
130 ms |
188756 KB |
Output is correct |
66 |
Correct |
122 ms |
190804 KB |
Output is correct |
67 |
Correct |
115 ms |
190804 KB |
Output is correct |
68 |
Correct |
21 ms |
92244 KB |
Output is correct |
69 |
Correct |
17 ms |
92244 KB |
Output is correct |
70 |
Correct |
18 ms |
92252 KB |
Output is correct |
71 |
Correct |
19 ms |
92232 KB |
Output is correct |
72 |
Correct |
18 ms |
90204 KB |
Output is correct |
73 |
Correct |
19 ms |
92252 KB |
Output is correct |
74 |
Correct |
138 ms |
188504 KB |
Output is correct |
75 |
Correct |
135 ms |
188500 KB |
Output is correct |
76 |
Correct |
132 ms |
190860 KB |
Output is correct |
77 |
Correct |
144 ms |
190808 KB |
Output is correct |
78 |
Correct |
132 ms |
190804 KB |
Output is correct |
79 |
Correct |
134 ms |
190804 KB |
Output is correct |
80 |
Correct |
128 ms |
187384 KB |
Output is correct |
81 |
Correct |
140 ms |
198740 KB |
Output is correct |
82 |
Correct |
138 ms |
198896 KB |
Output is correct |
83 |
Correct |
137 ms |
191732 KB |
Output is correct |
84 |
Correct |
137 ms |
191472 KB |
Output is correct |
85 |
Correct |
124 ms |
191060 KB |
Output is correct |
86 |
Correct |
118 ms |
190984 KB |
Output is correct |
87 |
Correct |
142 ms |
195692 KB |
Output is correct |
88 |
Correct |
135 ms |
193620 KB |
Output is correct |
89 |
Correct |
136 ms |
192036 KB |
Output is correct |
90 |
Correct |
114 ms |
190804 KB |
Output is correct |
91 |
Correct |
129 ms |
190804 KB |
Output is correct |
92 |
Correct |
121 ms |
190836 KB |
Output is correct |