#include "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;
#define INF 1000000000000000000LL
#define ff first
#define ss second
lld n;
lld comps;
lld cyc[1000100];
vector<vector<lld>> cycles;
pair<pair<lld,lld>,pair<lld,lld>> reach[1000100];
lld lfinal, rfinal;
lld par[1000100], sz[1000100];
lld find(lld x){
if(x==par[x]) return x;
return par[x] = find(par[x]);
}
void uni(lld a, lld b){
a = find(a);
b = find(b);
if(a==b) return;
if(sz[a]>sz[b]) swap(a,b);
sz[b] += sz[a];
par[a] = b;
}
bool cmp(lld comp1, lld comp2){
return reach[comp1].ss.ff<reach[comp2].ss.ff;
}
void debug(){
for(lld i=0;i<n;i++) cout << find(cyc[i]) << " \n"[i==n-1];
for(lld i=0;i<comps;i++)
if((lld)cycles[i].size()>1) cout << i << ": " << reach[i].ff.ff << " " << reach[i].ff.ss << " " << reach[i].ss.ff << " " << reach[i].ss.ss << "\n";
}
lld minimum_walk(vector<int> p, int s){
n = (lld) p.size();
lld ans = 0;
for(lld i=0;i<n;i++) ans+=abs(p[i]-i);
for(lld i=0;i<n;i++){
par[i] = i;
sz[i] = 1;
}
vector<bool> vis(n, false);
comps = 0;
lfinal = s, rfinal = s;
for(lld i=0;i<n;i++){
if(!vis[i]){
vector<lld> tmp;
lld x = i;
while(true){
if(vis[x]) break;
tmp.push_back(x);
vis[x] = true;
cyc[x] = comps;
x = p[x];
}
sort(tmp.begin(), tmp.end());
cycles.push_back(tmp);
if((int)tmp.size()>1){
if(tmp.front()>=s) reach[comps] = {{tmp.front(),-1},{tmp.front(),tmp.back()}};
else if(tmp.back()<=s) reach[comps] = {{tmp.front(),tmp.back()},{INF,tmp.back()}};
else{
auto it = lower_bound(tmp.begin(), tmp.end(), s);
reach[comps] = {{tmp.front(),*prev(it)},{*it,tmp.back()}};
}
lfinal = min(lfinal, reach[comps].ff.ff);
rfinal = max(rfinal, reach[comps].ss.ss);
}else{
reach[comps] = {{tmp[0],tmp[0]},{tmp[0],tmp[0]}};
}
comps++;
}
}
lld cur_comp = find(cyc[s]);
lld lblock = s, rblock = s;
lld lblocknew = reach[cur_comp].ff.ff, rblocknew = reach[cur_comp].ss.ss;
while(lblocknew!=lblock||rblocknew!=rblock){
lld lblocknewnew, rblocknewnew;
for(lld i=lblocknew;i<=lblock;i++){
if((lld)cycles[find(cyc[i])].size()==1) continue;
reach[cur_comp].ff.ff = min(reach[cur_comp].ff.ff, reach[find(cyc[i])].ff.ff);
reach[cur_comp].ff.ss = max(reach[cur_comp].ff.ss, reach[find(cyc[i])].ff.ss);
reach[cur_comp].ss.ff = min(reach[cur_comp].ss.ff, reach[find(cyc[i])].ss.ff);
reach[cur_comp].ss.ss = max(reach[cur_comp].ss.ss, reach[find(cyc[i])].ss.ss);
lblocknewnew = reach[cur_comp].ff.ff;
rblocknewnew = reach[cur_comp].ss.ss;
uni(cyc[i], cur_comp);
cyc[i] = cur_comp;
}
for(lld i=rblock;i<=rblocknew;i++){
if((lld)cycles[find(cyc[i])].size()==1) continue;
reach[cur_comp].ff.ff = min(reach[cur_comp].ff.ff, reach[find(cyc[i])].ff.ff);
reach[cur_comp].ff.ss = max(reach[cur_comp].ff.ss, reach[find(cyc[i])].ff.ss);
reach[cur_comp].ss.ff = min(reach[cur_comp].ss.ff, reach[find(cyc[i])].ss.ff);
reach[cur_comp].ss.ss = max(reach[cur_comp].ss.ss, reach[find(cyc[i])].ss.ss);
lblocknewnew = reach[cur_comp].ff.ff;
rblocknewnew = reach[cur_comp].ss.ss;
uni(cyc[i], cur_comp);
cyc[i] = cur_comp;
}
lblock = lblocknew; rblock = rblocknew;
lblocknew = lblocknewnew; rblocknew = rblocknewnew;
}
lld l = INF;
for(lld i=lblock-1;i>=lfinal;i--){
if((lld)cycles[find(cyc[i])].size()==1) continue;
if(i<l){
cur_comp = find(cyc[i]);
l = reach[cur_comp].ff.ff;
}else{
reach[cur_comp].ff.ff = min(reach[cur_comp].ff.ff, reach[find(cyc[i])].ff.ff);
reach[cur_comp].ff.ss = max(reach[cur_comp].ff.ss, reach[find(cyc[i])].ff.ss);
reach[cur_comp].ss.ff = min(reach[cur_comp].ss.ff, reach[find(cyc[i])].ss.ff);
reach[cur_comp].ss.ss = max(reach[cur_comp].ss.ss, reach[find(cyc[i])].ss.ss);
l = reach[cur_comp].ff.ff;
uni(cyc[i], cur_comp);
cyc[i] = cur_comp;
}
}
lld r = -1;
for(lld i=rblock+1;i<=rfinal;i++){
if((lld)cycles[find(cyc[i])].size()==1) continue;
if(i>r){
cur_comp = find(cyc[i]);
r = reach[cur_comp].ss.ss;
}else{
reach[cur_comp].ff.ff = min(reach[cur_comp].ff.ff, reach[find(cyc[i])].ff.ff);
reach[cur_comp].ff.ss = max(reach[cur_comp].ff.ss, reach[find(cyc[i])].ff.ss);
reach[cur_comp].ss.ff = min(reach[cur_comp].ss.ff, reach[find(cyc[i])].ss.ff);
reach[cur_comp].ss.ss = max(reach[cur_comp].ss.ss, reach[find(cyc[i])].ss.ss);
r = reach[cur_comp].ss.ss;
uni(cyc[i], cur_comp);
cyc[i] = cur_comp;
}
}
//debug();
set<lld> brid;
for(lld i=0;i<n;i++){
if((lld)cycles[find(cyc[i])].size()==1) continue;
if(reach[find(cyc[i])].ff.ff<s&&reach[find(cyc[i])].ss.ss>s) brid.insert(find(cyc[i]));
}
vector<lld> bridges;
for(lld xxx:brid) bridges.push_back(xxx);
sort(bridges.begin(), bridges.end(), cmp);
for(lld i=1;i<(lld)bridges.size();i++){
if(reach[bridges[i-1]].ss.ss>reach[bridges[i]].ss.ff||reach[bridges[i-1]].ff.ff<reach[bridges[i]].ff.ss){
reach[find(bridges[i-1])].ff.ff = min(reach[find(bridges[i-1])].ff.ff, reach[find(bridges[i])].ff.ff);
reach[find(bridges[i-1])].ff.ss = max(reach[find(bridges[i-1])].ff.ss, reach[find(bridges[i])].ff.ss);
reach[find(bridges[i-1])].ss.ff = min(reach[find(bridges[i-1])].ss.ff, reach[find(bridges[i])].ss.ff);
reach[find(bridges[i-1])].ss.ss = max(reach[find(bridges[i-1])].ss.ss, reach[find(bridges[i])].ss.ss);
reach[find(bridges[i])] = reach[find(bridges[i-1])];
uni(bridges[i-1], bridges[i]);
}
}
l = lblock, r = rblock;
while(l>lfinal||r<rfinal){
lld i, j;
lld lpath = (l-1<lfinal?INF:0), rpath = (r+1>rfinal?INF:0);
bool found_bridge = false;
for(i=l-1;i>=lfinal;i--){
if(reach[find(cyc[i])].ff.ff<s&&reach[find(cyc[i])].ss.ss>s){
lpath+=2;
found_bridge = true;
break;
}
if(find(cyc[i])!=find(cyc[i+1])) lpath+=2;
i = reach[find(cyc[i])].ff.ff;
if(i==lfinal) break;
}
for(j=r+1;j<=rfinal;j++){
if(reach[find(cyc[j])].ff.ff<s&&reach[find(cyc[j])].ss.ss>s){
found_bridge = true;
rpath+=2;
break;
}
if(find(cyc[j])!=find(cyc[j-1])) rpath+=2;
j = reach[find(cyc[j])].ss.ss;
if(j==rfinal) break;
}
if(found_bridge) ans += min(lpath, rpath);
else ans += (lpath==INF?0:lpath)+(rpath==INF?0:rpath);
if(l-1>=lfinal)l = reach[find(cyc[i])].ff.ff;
if(r+1<=rfinal)r = reach[find(cyc[j])].ss.ss;
//printf("------------\n%lld %lld\n--------------\n",l,r);
}
return ans;
}
Compilation message
books.cpp: In function 'lld minimum_walk(std::vector<int>, int)':
books.cpp:83:36: warning: 'rblocknewnew' may be used uninitialized in this function [-Wmaybe-uninitialized]
83 | while(lblocknew!=lblock||rblocknew!=rblock){
| ~~~~~~~~~^~~~~~~~
books.cpp:83:17: warning: 'lblocknewnew' may be used uninitialized in this function [-Wmaybe-uninitialized]
83 | while(lblocknew!=lblock||rblocknew!=rblock){
| ~~~~~~~~~^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
316 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
316 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
316 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Correct |
1 ms |
316 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
316 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
316 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Correct |
1 ms |
316 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
320 KB |
Output is correct |
30 |
Correct |
295 ms |
50732 KB |
Output is correct |
31 |
Correct |
295 ms |
47948 KB |
Output is correct |
32 |
Correct |
270 ms |
124528 KB |
Output is correct |
33 |
Correct |
251 ms |
98528 KB |
Output is correct |
34 |
Correct |
241 ms |
98572 KB |
Output is correct |
35 |
Correct |
259 ms |
96748 KB |
Output is correct |
36 |
Correct |
239 ms |
64636 KB |
Output is correct |
37 |
Correct |
198 ms |
48920 KB |
Output is correct |
38 |
Correct |
220 ms |
46788 KB |
Output is correct |
39 |
Correct |
222 ms |
46696 KB |
Output is correct |
40 |
Correct |
218 ms |
46376 KB |
Output is correct |
41 |
Correct |
273 ms |
47132 KB |
Output is correct |
42 |
Correct |
220 ms |
47064 KB |
Output is correct |
43 |
Correct |
244 ms |
102220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
320 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 |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
440 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 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 |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
316 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
316 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Correct |
1 ms |
316 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
320 KB |
Output is correct |
30 |
Correct |
295 ms |
50732 KB |
Output is correct |
31 |
Correct |
295 ms |
47948 KB |
Output is correct |
32 |
Correct |
270 ms |
124528 KB |
Output is correct |
33 |
Correct |
251 ms |
98528 KB |
Output is correct |
34 |
Correct |
241 ms |
98572 KB |
Output is correct |
35 |
Correct |
259 ms |
96748 KB |
Output is correct |
36 |
Correct |
239 ms |
64636 KB |
Output is correct |
37 |
Correct |
198 ms |
48920 KB |
Output is correct |
38 |
Correct |
220 ms |
46788 KB |
Output is correct |
39 |
Correct |
222 ms |
46696 KB |
Output is correct |
40 |
Correct |
218 ms |
46376 KB |
Output is correct |
41 |
Correct |
273 ms |
47132 KB |
Output is correct |
42 |
Correct |
220 ms |
47064 KB |
Output is correct |
43 |
Correct |
244 ms |
102220 KB |
Output is correct |
44 |
Correct |
1 ms |
320 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
320 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 |
316 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
440 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
316 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 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 |
340 KB |
Output is correct |
61 |
Correct |
1 ms |
340 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
316 KB |
Output is correct |
64 |
Correct |
245 ms |
102312 KB |
Output is correct |
65 |
Correct |
243 ms |
108504 KB |
Output is correct |
66 |
Correct |
191 ms |
48936 KB |
Output is correct |
67 |
Correct |
183 ms |
47568 KB |
Output is correct |
68 |
Correct |
21 ms |
6904 KB |
Output is correct |
69 |
Correct |
20 ms |
5836 KB |
Output is correct |
70 |
Correct |
21 ms |
7948 KB |
Output is correct |
71 |
Correct |
26 ms |
11268 KB |
Output is correct |
72 |
Correct |
23 ms |
11388 KB |
Output is correct |
73 |
Correct |
20 ms |
4816 KB |
Output is correct |
74 |
Correct |
244 ms |
98572 KB |
Output is correct |
75 |
Correct |
234 ms |
98540 KB |
Output is correct |
76 |
Correct |
241 ms |
101276 KB |
Output is correct |
77 |
Correct |
298 ms |
101932 KB |
Output is correct |
78 |
Correct |
233 ms |
94544 KB |
Output is correct |
79 |
Correct |
258 ms |
94660 KB |
Output is correct |
80 |
Correct |
216 ms |
124528 KB |
Output is correct |
81 |
Correct |
351 ms |
103816 KB |
Output is correct |
82 |
Correct |
390 ms |
103912 KB |
Output is correct |
83 |
Correct |
221 ms |
75096 KB |
Output is correct |
84 |
Correct |
224 ms |
69952 KB |
Output is correct |
85 |
Correct |
186 ms |
57992 KB |
Output is correct |
86 |
Correct |
190 ms |
50296 KB |
Output is correct |
87 |
Correct |
292 ms |
121852 KB |
Output is correct |
88 |
Correct |
259 ms |
105076 KB |
Output is correct |
89 |
Correct |
246 ms |
82132 KB |
Output is correct |
90 |
Correct |
175 ms |
47680 KB |
Output is correct |
91 |
Correct |
184 ms |
46844 KB |
Output is correct |
92 |
Correct |
192 ms |
46584 KB |
Output is correct |