#include "books.h"
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,popcnt,sse4")
#define pii pair<int,int>
#define ll long long
const int mxn = 1e6+10;
const int LEN = mxn*20;
int N;
int nid[LEN],to[LEN],wei[LEN],head[LEN];
int d1[LEN],d2[LEN];
ll ans = 0;
bitset<LEN> vis;
int ecnt,vcnt;
int seg[mxn*4];
void add_edge(int a,int b,int w){
//cerr<<"ADD_EDGE: "<<a<<' '<<b<<' '<<w<<endl;
ecnt++;
nid[ecnt] = head[a];
head[a] = ecnt;
to[ecnt] = b;
wei[ecnt] = w;
return;
}
void build(int now,int l,int r){
if(l == r){
seg[now] = l;
return;
}
seg[now] = vcnt++;
int mid = (l+r)>>1;
build(now*2+1,l,mid);
build(now*2+2,mid+1,r);
add_edge(seg[now*2+1],seg[now],0);
add_edge(seg[now*2+2],seg[now],0);
//cerr<<l<<' '<<r<<"::"<<seg[now]<<":"<<seg[now*2+1]<<' '<<seg[now*2+2]<<endl;
return;
}
void add(int now,int l,int r,int s,int e,int p){
if(l>=s&&e>=r){
//cerr<<l<<' '<<r<<":"<<p<<endl;
add_edge(seg[now],p,0);
return;
}
int mid = (l+r)>>1;
if(mid>=s)add(now*2+1,l,mid,s,e,p);
if(mid<e)add(now*2+2,mid+1,r,s,e,p);
return;
}
vector<int> bucket[mxn*2];
void DIJKSTRA(int dist[]){
for(auto &i:bucket)i.clear();
for(int i = 0;i<vcnt;i++){
bucket[dist[i]].push_back(i);
}
vis.reset();
for(int d = 0;d<mxn*2;d++){
while(!bucket[d].empty()){
auto now = bucket[d].back();bucket[d].pop_back();
if(vis[now])continue;
vis[now] = true;
for(int eid = head[now];eid;eid = nid[eid]){
int nxt = to[eid],w = wei[eid];
if(dist[nxt]>dist[now]+w){
dist[nxt] = dist[now]+w;
bucket[dist[nxt]].push_back(nxt);
}
}
}
}
return;
}
long long minimum_walk(std::vector<int> p, int s) {
N = p.size();
vcnt = N;
for(int i = 0;i+1<N;i++){
add_edge(i,i+1,1);
add_edge(i+1,i,1);
}
build(0,0,N-1);
for(int i = 0;i<p.size();i++){
ans += abs(i-p[i]);
if(vis[i])continue;
int now = i;
int lp = i,rp = i;
do{
lp = min(lp,now);
rp = max(rp,now);
vis[now] = true;
now = p[now];
}while(now != i);
int tar = vcnt++;
add(0,0,N-1,lp,rp,tar);
do{
add_edge(tar,now,0);
now = p[now];
}while(now != i);
}
fill(d1,d1+LEN,mxn*2-1);
fill(d2,d2+LEN,mxn*2-1);
int tl = s,tr = s;
for(int i = 0;i<N;i++){
if(p[i] != i)tl = min(tl,i),tr = max(tr,i);
}
//cerr<<tl<<','<<tr<<endl;
d1[tl] = d2[tr] = 0;
DIJKSTRA(d1);
DIJKSTRA(d2);
//for(int i = 0;i<vcnt;i++)cerr<<d1[i]<<' ';cerr<<endl;
//for(int i = 0;i<vcnt;i++)cerr<<d2[i]<<' ';cerr<<endl;
for(int i = 0;i<vcnt;i++)d1[i] += d2[i];
DIJKSTRA(d1);
//for(int i = 0;i<vcnt;i++)cerr<<d1[i]<<' ';cerr<<endl;
return ans+d1[s]*2;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:89:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for(int i = 0;i<p.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
213812 KB |
Output is correct |
2 |
Correct |
60 ms |
213588 KB |
Output is correct |
3 |
Correct |
66 ms |
213596 KB |
Output is correct |
4 |
Correct |
61 ms |
213840 KB |
Output is correct |
5 |
Correct |
61 ms |
213584 KB |
Output is correct |
6 |
Correct |
62 ms |
213584 KB |
Output is correct |
7 |
Correct |
62 ms |
213588 KB |
Output is correct |
8 |
Correct |
71 ms |
213656 KB |
Output is correct |
9 |
Correct |
61 ms |
213728 KB |
Output is correct |
10 |
Correct |
61 ms |
213588 KB |
Output is correct |
11 |
Correct |
61 ms |
213584 KB |
Output is correct |
12 |
Correct |
61 ms |
213632 KB |
Output is correct |
13 |
Correct |
68 ms |
213644 KB |
Output is correct |
14 |
Correct |
62 ms |
213668 KB |
Output is correct |
15 |
Correct |
73 ms |
213584 KB |
Output is correct |
16 |
Correct |
63 ms |
213588 KB |
Output is correct |
17 |
Correct |
60 ms |
213840 KB |
Output is correct |
18 |
Correct |
67 ms |
213584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
213812 KB |
Output is correct |
2 |
Correct |
60 ms |
213588 KB |
Output is correct |
3 |
Correct |
66 ms |
213596 KB |
Output is correct |
4 |
Correct |
61 ms |
213840 KB |
Output is correct |
5 |
Correct |
61 ms |
213584 KB |
Output is correct |
6 |
Correct |
62 ms |
213584 KB |
Output is correct |
7 |
Correct |
62 ms |
213588 KB |
Output is correct |
8 |
Correct |
71 ms |
213656 KB |
Output is correct |
9 |
Correct |
61 ms |
213728 KB |
Output is correct |
10 |
Correct |
61 ms |
213588 KB |
Output is correct |
11 |
Correct |
61 ms |
213584 KB |
Output is correct |
12 |
Correct |
61 ms |
213632 KB |
Output is correct |
13 |
Correct |
68 ms |
213644 KB |
Output is correct |
14 |
Correct |
62 ms |
213668 KB |
Output is correct |
15 |
Correct |
73 ms |
213584 KB |
Output is correct |
16 |
Correct |
63 ms |
213588 KB |
Output is correct |
17 |
Correct |
60 ms |
213840 KB |
Output is correct |
18 |
Correct |
67 ms |
213584 KB |
Output is correct |
19 |
Correct |
63 ms |
213840 KB |
Output is correct |
20 |
Correct |
60 ms |
213840 KB |
Output is correct |
21 |
Correct |
62 ms |
213840 KB |
Output is correct |
22 |
Correct |
61 ms |
213840 KB |
Output is correct |
23 |
Correct |
62 ms |
213840 KB |
Output is correct |
24 |
Correct |
64 ms |
213840 KB |
Output is correct |
25 |
Correct |
64 ms |
213840 KB |
Output is correct |
26 |
Correct |
60 ms |
213840 KB |
Output is correct |
27 |
Correct |
64 ms |
213840 KB |
Output is correct |
28 |
Correct |
62 ms |
213840 KB |
Output is correct |
29 |
Correct |
61 ms |
213844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
213812 KB |
Output is correct |
2 |
Correct |
60 ms |
213588 KB |
Output is correct |
3 |
Correct |
66 ms |
213596 KB |
Output is correct |
4 |
Correct |
61 ms |
213840 KB |
Output is correct |
5 |
Correct |
61 ms |
213584 KB |
Output is correct |
6 |
Correct |
62 ms |
213584 KB |
Output is correct |
7 |
Correct |
62 ms |
213588 KB |
Output is correct |
8 |
Correct |
71 ms |
213656 KB |
Output is correct |
9 |
Correct |
61 ms |
213728 KB |
Output is correct |
10 |
Correct |
61 ms |
213588 KB |
Output is correct |
11 |
Correct |
61 ms |
213584 KB |
Output is correct |
12 |
Correct |
61 ms |
213632 KB |
Output is correct |
13 |
Correct |
68 ms |
213644 KB |
Output is correct |
14 |
Correct |
62 ms |
213668 KB |
Output is correct |
15 |
Correct |
73 ms |
213584 KB |
Output is correct |
16 |
Correct |
63 ms |
213588 KB |
Output is correct |
17 |
Correct |
60 ms |
213840 KB |
Output is correct |
18 |
Correct |
67 ms |
213584 KB |
Output is correct |
19 |
Correct |
63 ms |
213840 KB |
Output is correct |
20 |
Correct |
60 ms |
213840 KB |
Output is correct |
21 |
Correct |
62 ms |
213840 KB |
Output is correct |
22 |
Correct |
61 ms |
213840 KB |
Output is correct |
23 |
Correct |
62 ms |
213840 KB |
Output is correct |
24 |
Correct |
64 ms |
213840 KB |
Output is correct |
25 |
Correct |
64 ms |
213840 KB |
Output is correct |
26 |
Correct |
60 ms |
213840 KB |
Output is correct |
27 |
Correct |
64 ms |
213840 KB |
Output is correct |
28 |
Correct |
62 ms |
213840 KB |
Output is correct |
29 |
Correct |
61 ms |
213844 KB |
Output is correct |
30 |
Correct |
532 ms |
311216 KB |
Output is correct |
31 |
Correct |
523 ms |
314984 KB |
Output is correct |
32 |
Correct |
410 ms |
377496 KB |
Output is correct |
33 |
Correct |
368 ms |
350556 KB |
Output is correct |
34 |
Correct |
417 ms |
350460 KB |
Output is correct |
35 |
Correct |
375 ms |
344220 KB |
Output is correct |
36 |
Correct |
324 ms |
333464 KB |
Output is correct |
37 |
Correct |
273 ms |
321644 KB |
Output is correct |
38 |
Correct |
268 ms |
319088 KB |
Output is correct |
39 |
Correct |
270 ms |
318812 KB |
Output is correct |
40 |
Correct |
327 ms |
321196 KB |
Output is correct |
41 |
Correct |
365 ms |
321708 KB |
Output is correct |
42 |
Correct |
357 ms |
322636 KB |
Output is correct |
43 |
Correct |
888 ms |
345244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
213844 KB |
Output is correct |
2 |
Correct |
64 ms |
213844 KB |
Output is correct |
3 |
Correct |
62 ms |
213840 KB |
Output is correct |
4 |
Correct |
79 ms |
213844 KB |
Output is correct |
5 |
Correct |
65 ms |
213892 KB |
Output is correct |
6 |
Correct |
65 ms |
213840 KB |
Output is correct |
7 |
Correct |
61 ms |
213840 KB |
Output is correct |
8 |
Correct |
67 ms |
213840 KB |
Output is correct |
9 |
Correct |
70 ms |
213860 KB |
Output is correct |
10 |
Correct |
64 ms |
213880 KB |
Output is correct |
11 |
Correct |
63 ms |
213840 KB |
Output is correct |
12 |
Correct |
63 ms |
213832 KB |
Output is correct |
13 |
Correct |
64 ms |
213840 KB |
Output is correct |
14 |
Correct |
73 ms |
213840 KB |
Output is correct |
15 |
Correct |
62 ms |
213840 KB |
Output is correct |
16 |
Correct |
66 ms |
213840 KB |
Output is correct |
17 |
Correct |
64 ms |
213840 KB |
Output is correct |
18 |
Correct |
62 ms |
213840 KB |
Output is correct |
19 |
Correct |
63 ms |
213760 KB |
Output is correct |
20 |
Correct |
63 ms |
213696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
213812 KB |
Output is correct |
2 |
Correct |
60 ms |
213588 KB |
Output is correct |
3 |
Correct |
66 ms |
213596 KB |
Output is correct |
4 |
Correct |
61 ms |
213840 KB |
Output is correct |
5 |
Correct |
61 ms |
213584 KB |
Output is correct |
6 |
Correct |
62 ms |
213584 KB |
Output is correct |
7 |
Correct |
62 ms |
213588 KB |
Output is correct |
8 |
Correct |
71 ms |
213656 KB |
Output is correct |
9 |
Correct |
61 ms |
213728 KB |
Output is correct |
10 |
Correct |
61 ms |
213588 KB |
Output is correct |
11 |
Correct |
61 ms |
213584 KB |
Output is correct |
12 |
Correct |
61 ms |
213632 KB |
Output is correct |
13 |
Correct |
68 ms |
213644 KB |
Output is correct |
14 |
Correct |
62 ms |
213668 KB |
Output is correct |
15 |
Correct |
73 ms |
213584 KB |
Output is correct |
16 |
Correct |
63 ms |
213588 KB |
Output is correct |
17 |
Correct |
60 ms |
213840 KB |
Output is correct |
18 |
Correct |
67 ms |
213584 KB |
Output is correct |
19 |
Correct |
63 ms |
213840 KB |
Output is correct |
20 |
Correct |
60 ms |
213840 KB |
Output is correct |
21 |
Correct |
62 ms |
213840 KB |
Output is correct |
22 |
Correct |
61 ms |
213840 KB |
Output is correct |
23 |
Correct |
62 ms |
213840 KB |
Output is correct |
24 |
Correct |
64 ms |
213840 KB |
Output is correct |
25 |
Correct |
64 ms |
213840 KB |
Output is correct |
26 |
Correct |
60 ms |
213840 KB |
Output is correct |
27 |
Correct |
64 ms |
213840 KB |
Output is correct |
28 |
Correct |
62 ms |
213840 KB |
Output is correct |
29 |
Correct |
61 ms |
213844 KB |
Output is correct |
30 |
Correct |
532 ms |
311216 KB |
Output is correct |
31 |
Correct |
523 ms |
314984 KB |
Output is correct |
32 |
Correct |
410 ms |
377496 KB |
Output is correct |
33 |
Correct |
368 ms |
350556 KB |
Output is correct |
34 |
Correct |
417 ms |
350460 KB |
Output is correct |
35 |
Correct |
375 ms |
344220 KB |
Output is correct |
36 |
Correct |
324 ms |
333464 KB |
Output is correct |
37 |
Correct |
273 ms |
321644 KB |
Output is correct |
38 |
Correct |
268 ms |
319088 KB |
Output is correct |
39 |
Correct |
270 ms |
318812 KB |
Output is correct |
40 |
Correct |
327 ms |
321196 KB |
Output is correct |
41 |
Correct |
365 ms |
321708 KB |
Output is correct |
42 |
Correct |
357 ms |
322636 KB |
Output is correct |
43 |
Correct |
888 ms |
345244 KB |
Output is correct |
44 |
Correct |
62 ms |
213844 KB |
Output is correct |
45 |
Correct |
64 ms |
213844 KB |
Output is correct |
46 |
Correct |
62 ms |
213840 KB |
Output is correct |
47 |
Correct |
79 ms |
213844 KB |
Output is correct |
48 |
Correct |
65 ms |
213892 KB |
Output is correct |
49 |
Correct |
65 ms |
213840 KB |
Output is correct |
50 |
Correct |
61 ms |
213840 KB |
Output is correct |
51 |
Correct |
67 ms |
213840 KB |
Output is correct |
52 |
Correct |
70 ms |
213860 KB |
Output is correct |
53 |
Correct |
64 ms |
213880 KB |
Output is correct |
54 |
Correct |
63 ms |
213840 KB |
Output is correct |
55 |
Correct |
63 ms |
213832 KB |
Output is correct |
56 |
Correct |
64 ms |
213840 KB |
Output is correct |
57 |
Correct |
73 ms |
213840 KB |
Output is correct |
58 |
Correct |
62 ms |
213840 KB |
Output is correct |
59 |
Correct |
66 ms |
213840 KB |
Output is correct |
60 |
Correct |
64 ms |
213840 KB |
Output is correct |
61 |
Correct |
62 ms |
213840 KB |
Output is correct |
62 |
Correct |
63 ms |
213760 KB |
Output is correct |
63 |
Correct |
63 ms |
213696 KB |
Output is correct |
64 |
Correct |
492 ms |
342936 KB |
Output is correct |
65 |
Correct |
489 ms |
345800 KB |
Output is correct |
66 |
Correct |
320 ms |
319868 KB |
Output is correct |
67 |
Correct |
310 ms |
319052 KB |
Output is correct |
68 |
Correct |
96 ms |
220996 KB |
Output is correct |
69 |
Correct |
90 ms |
220608 KB |
Output is correct |
70 |
Correct |
98 ms |
222152 KB |
Output is correct |
71 |
Correct |
108 ms |
221632 KB |
Output is correct |
72 |
Correct |
104 ms |
222464 KB |
Output is correct |
73 |
Correct |
98 ms |
221476 KB |
Output is correct |
74 |
Correct |
357 ms |
357276 KB |
Output is correct |
75 |
Correct |
364 ms |
357272 KB |
Output is correct |
76 |
Correct |
1126 ms |
349600 KB |
Output is correct |
77 |
Correct |
1108 ms |
346524 KB |
Output is correct |
78 |
Correct |
669 ms |
346056 KB |
Output is correct |
79 |
Correct |
666 ms |
345428 KB |
Output is correct |
80 |
Correct |
453 ms |
381616 KB |
Output is correct |
81 |
Correct |
571 ms |
425116 KB |
Output is correct |
82 |
Correct |
562 ms |
425112 KB |
Output is correct |
83 |
Correct |
488 ms |
345244 KB |
Output is correct |
84 |
Correct |
451 ms |
340288 KB |
Output is correct |
85 |
Correct |
357 ms |
331516 KB |
Output is correct |
86 |
Correct |
355 ms |
322360 KB |
Output is correct |
87 |
Correct |
610 ms |
388908 KB |
Output is correct |
88 |
Correct |
553 ms |
367924 KB |
Output is correct |
89 |
Correct |
502 ms |
348356 KB |
Output is correct |
90 |
Correct |
336 ms |
323276 KB |
Output is correct |
91 |
Correct |
298 ms |
316872 KB |
Output is correct |
92 |
Correct |
370 ms |
317360 KB |
Output is correct |