#include <bits/stdc++.h>
#include "books.h"
using namespace std;
const long long inf=1e18;
long long n,a[1000069],fh[2][1000069],nr[1000069],dsu[1000069];
bitset<1000069> vtd,vtd2;
queue<long long> q;
long long fd(long long x)
{
if(dsu[x]!=x)
{
dsu[x]=fd(dsu[x]);
}
return dsu[x];
}
void ad(long long x,long long w)
{
long long i,p,sz;
vector<long long> v;
for(p=fh[0][x];fd(p)<=fh[1][x];dsu[fd(p)]=fd(fd(p)+1))
{
q.push(fd(p));
nr[fd(p)]=w;
v.push_back(fd(p));
}
sz=v.size();
for(i=0;i<sz;i++)
{
ad(v[i],w);
}
}
long long minimum_walk(vector<int> aa,int st)
{
long long i,ii,u,k,p,mn,mx,z=0;
st++;
n=aa.size();
for(i=1;i<=n;i++)
{
a[i]=aa[i-1]+1;
z+=abs(i-a[i]);
nr[i]=inf;
}
for(i=1;i<=n;i++)
{
if(!vtd[i])
{
mn=i;
mx=i;
for(p=i;!vtd[p];p=a[p])
{
vtd[p]=1;
mn=min(mn,p);
mx=max(mx,p);
}
for(p=i;!vtd2[p];p=a[p])
{
vtd2[p]=1;
fh[0][p]=mn;
fh[1][p]=mx;
}
}
}
for(i=1;i<=n+1;i++)
{
dsu[i]=i;
}
ad(st,0);
mx=0;
for(;!q.empty();)
{
k=q.front();
q.pop();
if(fh[0][k]<=st&&fh[1][k]>=st)
{
mx=nr[k];
}
for(ii=0;ii<2;ii++)
{
u=!ii*2-1;
if(k+u&&k+u<=n)
{
ad(k+u,nr[k]+1);
}
}
}
z+=mx*2;
mn=st;
mx=st;
for(i=1;i<=n;i++)
{
if(a[i]!=i)
{
mn=min(mn,i);
mx=max(mx,i);
}
}
k=0;
for(i=mn;i<mx;i++)
{
k=max(k,fh[1][i]);
z+=2*(k==i);
}
return z;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10692 KB |
Output is correct |
12 |
Correct |
1 ms |
10584 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10596 KB |
Output is correct |
17 |
Correct |
1 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10692 KB |
Output is correct |
12 |
Correct |
1 ms |
10584 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10596 KB |
Output is correct |
17 |
Correct |
1 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
2 ms |
10588 KB |
Output is correct |
23 |
Correct |
2 ms |
10712 KB |
Output is correct |
24 |
Correct |
1 ms |
10588 KB |
Output is correct |
25 |
Correct |
1 ms |
10588 KB |
Output is correct |
26 |
Correct |
2 ms |
10584 KB |
Output is correct |
27 |
Correct |
2 ms |
10584 KB |
Output is correct |
28 |
Correct |
2 ms |
10584 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10692 KB |
Output is correct |
12 |
Correct |
1 ms |
10584 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10596 KB |
Output is correct |
17 |
Correct |
1 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
2 ms |
10588 KB |
Output is correct |
23 |
Correct |
2 ms |
10712 KB |
Output is correct |
24 |
Correct |
1 ms |
10588 KB |
Output is correct |
25 |
Correct |
1 ms |
10588 KB |
Output is correct |
26 |
Correct |
2 ms |
10584 KB |
Output is correct |
27 |
Correct |
2 ms |
10584 KB |
Output is correct |
28 |
Correct |
2 ms |
10584 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
219 ms |
81928 KB |
Output is correct |
31 |
Correct |
269 ms |
81956 KB |
Output is correct |
32 |
Correct |
136 ms |
54140 KB |
Output is correct |
33 |
Correct |
159 ms |
54352 KB |
Output is correct |
34 |
Correct |
151 ms |
54352 KB |
Output is correct |
35 |
Correct |
144 ms |
54372 KB |
Output is correct |
36 |
Correct |
145 ms |
54376 KB |
Output is correct |
37 |
Correct |
132 ms |
54656 KB |
Output is correct |
38 |
Correct |
125 ms |
54524 KB |
Output is correct |
39 |
Correct |
124 ms |
54612 KB |
Output is correct |
40 |
Correct |
128 ms |
55752 KB |
Output is correct |
41 |
Correct |
156 ms |
69324 KB |
Output is correct |
42 |
Correct |
180 ms |
65212 KB |
Output is correct |
43 |
Correct |
146 ms |
82364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10584 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10584 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10692 KB |
Output is correct |
12 |
Correct |
1 ms |
10584 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10596 KB |
Output is correct |
17 |
Correct |
1 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
2 ms |
10588 KB |
Output is correct |
23 |
Correct |
2 ms |
10712 KB |
Output is correct |
24 |
Correct |
1 ms |
10588 KB |
Output is correct |
25 |
Correct |
1 ms |
10588 KB |
Output is correct |
26 |
Correct |
2 ms |
10584 KB |
Output is correct |
27 |
Correct |
2 ms |
10584 KB |
Output is correct |
28 |
Correct |
2 ms |
10584 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
219 ms |
81928 KB |
Output is correct |
31 |
Correct |
269 ms |
81956 KB |
Output is correct |
32 |
Correct |
136 ms |
54140 KB |
Output is correct |
33 |
Correct |
159 ms |
54352 KB |
Output is correct |
34 |
Correct |
151 ms |
54352 KB |
Output is correct |
35 |
Correct |
144 ms |
54372 KB |
Output is correct |
36 |
Correct |
145 ms |
54376 KB |
Output is correct |
37 |
Correct |
132 ms |
54656 KB |
Output is correct |
38 |
Correct |
125 ms |
54524 KB |
Output is correct |
39 |
Correct |
124 ms |
54612 KB |
Output is correct |
40 |
Correct |
128 ms |
55752 KB |
Output is correct |
41 |
Correct |
156 ms |
69324 KB |
Output is correct |
42 |
Correct |
180 ms |
65212 KB |
Output is correct |
43 |
Correct |
146 ms |
82364 KB |
Output is correct |
44 |
Correct |
2 ms |
10584 KB |
Output is correct |
45 |
Correct |
2 ms |
10588 KB |
Output is correct |
46 |
Correct |
2 ms |
10588 KB |
Output is correct |
47 |
Correct |
1 ms |
10584 KB |
Output is correct |
48 |
Correct |
2 ms |
10588 KB |
Output is correct |
49 |
Correct |
2 ms |
10588 KB |
Output is correct |
50 |
Correct |
2 ms |
10588 KB |
Output is correct |
51 |
Correct |
2 ms |
10588 KB |
Output is correct |
52 |
Correct |
2 ms |
10588 KB |
Output is correct |
53 |
Correct |
2 ms |
10588 KB |
Output is correct |
54 |
Correct |
2 ms |
10588 KB |
Output is correct |
55 |
Correct |
2 ms |
10588 KB |
Output is correct |
56 |
Correct |
2 ms |
10588 KB |
Output is correct |
57 |
Correct |
2 ms |
10588 KB |
Output is correct |
58 |
Correct |
2 ms |
10588 KB |
Output is correct |
59 |
Correct |
2 ms |
10584 KB |
Output is correct |
60 |
Correct |
2 ms |
10588 KB |
Output is correct |
61 |
Correct |
2 ms |
10588 KB |
Output is correct |
62 |
Correct |
2 ms |
10588 KB |
Output is correct |
63 |
Correct |
2 ms |
10588 KB |
Output is correct |
64 |
Correct |
138 ms |
79184 KB |
Output is correct |
65 |
Correct |
130 ms |
74776 KB |
Output is correct |
66 |
Correct |
133 ms |
57948 KB |
Output is correct |
67 |
Correct |
127 ms |
67624 KB |
Output is correct |
68 |
Correct |
15 ms |
18868 KB |
Output is correct |
69 |
Correct |
14 ms |
17088 KB |
Output is correct |
70 |
Correct |
15 ms |
18736 KB |
Output is correct |
71 |
Correct |
14 ms |
18256 KB |
Output is correct |
72 |
Correct |
14 ms |
16476 KB |
Output is correct |
73 |
Correct |
15 ms |
18072 KB |
Output is correct |
74 |
Correct |
145 ms |
54352 KB |
Output is correct |
75 |
Correct |
165 ms |
54212 KB |
Output is correct |
76 |
Correct |
156 ms |
82128 KB |
Output is correct |
77 |
Correct |
139 ms |
82356 KB |
Output is correct |
78 |
Correct |
134 ms |
61632 KB |
Output is correct |
79 |
Correct |
144 ms |
62380 KB |
Output is correct |
80 |
Correct |
125 ms |
54352 KB |
Output is correct |
81 |
Correct |
133 ms |
56536 KB |
Output is correct |
82 |
Correct |
135 ms |
56008 KB |
Output is correct |
83 |
Correct |
144 ms |
54356 KB |
Output is correct |
84 |
Correct |
143 ms |
54372 KB |
Output is correct |
85 |
Correct |
127 ms |
54392 KB |
Output is correct |
86 |
Correct |
123 ms |
54388 KB |
Output is correct |
87 |
Correct |
182 ms |
54248 KB |
Output is correct |
88 |
Correct |
141 ms |
54364 KB |
Output is correct |
89 |
Correct |
138 ms |
54356 KB |
Output is correct |
90 |
Correct |
127 ms |
54392 KB |
Output is correct |
91 |
Correct |
126 ms |
54380 KB |
Output is correct |
92 |
Correct |
130 ms |
55588 KB |
Output is correct |