#include <bits/stdc++.h>
//#include "books.h"
using namespace std;
typedef long long int ll;
typedef complex<double> point;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
//#define endl '\n'
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout<<x<<'\n', 0;
const int N=1e6+10,inf=(ll)1e9;
ll vis[N];
pii seg[N*4];
void upd(ll nod,ll l,ll r,ll id,pii val){
if (r-l==1){
seg[nod]=val;
return ;
}
ll mid=(r+l)/2;
if (mid>id) upd(nod*2,l,mid,id,val);
else upd(nod*2+1,mid,r,id,val);
seg[nod].F=min(seg[nod*2].F,seg[nod*2+1].F);
seg[nod].S=max(seg[nod*2].S,seg[nod*2+1].S);
}
pii get(ll nod,ll l,ll r,ll L,ll R){
if (l>=R || L>=r) return {inf,0};
if (l>=L && r<=R) return seg[nod];
ll mid=(r+l)/2;
pii a=get(nod*2,l,mid,L,R);
pii b=get(nod*2+1,mid,r,L,R);
return {min(a.F,b.F),max(a.S,b.S)};
}
long long minimum_walk(std::vector<int32_t> p, int32_t s) {
memset(vis,0,sizeof vis);
vector <pii> baz;
ll n=p.size();
ll ans=0;
for (int i=0;i<n;i++){
// cout << i << " rf" << endl;
if (vis[i]) continue;
if (p[i]==i){
upd(1,0,n,i,{i,i});
continue;
}
ll mn=i,mx=i;
ll j=p[i];
ans+=abs(p[i]-i);
while(j!=i){
ans+=abs(p[j]-j);
// cout << j << " " << p[j] << endl;
mn=min(mn,j);
mx=max(mx,j);
vis[j]=1;
j=p[j];
}
j=p[i];
while(j!=i){
upd(1,0,n,j,{mn,mx});
j=p[j];
}
upd(1,0,n,i,{mn,mx});
vis[i]=1;
baz.pb({mn,-mx});
}
// cout << ans << endl;
sort(baz.begin(),baz.end());
vector <pii> a;
if (baz.size()){
a.pb(baz[0]);
for (int i=1;i<baz.size();i++){
pii p=baz[i],q=a.back();
if (-p.S<=-q.S) continue;
if (p.F<=-q.S){
a.pop_back();
q.S=p.S;
a.pb(q);
}
else
a.pb(baz[i]);
}
ll p1=0;
if (a[0].F>s){
ans+=(a[0].F-s)*2;
p1=1;
}
if (-a.back().S<s){
ans+=(s+a.back().S)*2;
p1=1;
}
if (p1==0){
pii b={s,s};
while(true){
while(true){
pii z=get(1,0,n,b.F,b.S+1);
if (z!=b) b=z;
else break;
}
ll z=lower_bound(a.begin(),a.end(),pii({b.F,-inf}))-a.begin();
if (z==a.size() || a[z].F!=b.F){
b.F--;
ans+=2;
}
else break;
}
}
for (int i=1;i<a.size();i++){
ans+=(a[i].F+a[i-1].S)*2;
}
}
return ans;
}
int32_t main() {
int32_t n, s;
assert(2 == scanf("%d %d", &n, &s));
vector<int32_t> p((unsigned) n);
for(int i = 0; i < n; i++)
assert(1 == scanf("%d", &p[(unsigned) i]));
long long res = minimum_walk(p, s);
printf("%lld\n", res);
return 0;
}
/*
4 0
0 2 3 1
4 2
1 3 2 0
*/
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int32_t)':
books.cpp:76:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for (int i=1;i<baz.size();i++){
| ~^~~~~~~~~~~
books.cpp:105:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | if (z==a.size() || a[z].F!=b.F){
| ~^~~~~~~~~~
books.cpp:112:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
112 | for (int i=1;i<a.size();i++){
| ~^~~~~~~~~
/tmp/ccHZTyUn.o: In function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'
/tmp/ccYhxQs0.o:books.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status