Language: C++

#include<cstdio>
#include<algorithm>
#include<cmath>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef long long ll;
const int maxn=200000+10;
const ll inf=1000000000000000;
ll t1[maxn*4],t2[maxn*4],sum[maxn],f[maxn];
int x[maxn],id[maxn],rank[maxn];
int i,j,k,l,t,n,m,a,b;
ll ans;
bool cmp(int a,int b){
	return x[a]<x[b];
}
void build(int p,int l,int r){
	t1[p]=t2[p]=inf;
	if (l==r) return;
	int mid=(l+r)/2;
	build(p*2,l,mid);build(p*2+1,mid+1,r);
}
void change(int p,int l,int r,int a,ll b){
	if (l==r){
		t1[p]=b;
		return;
	}
	int mid=(l+r)/2;
	if (a<=mid) change(p*2,l,mid,a,b);else change(p*2+1,mid+1,r,a,b);
	t1[p]=min(t1[p*2],t1[p*2+1]);
}
void change2(int p,int l,int r,int a,ll b){
	if (l==r){
		t2[p]=b;
		return;
	}
	int mid=(l+r)/2;
	if (a<=mid) change2(p*2,l,mid,a,b);else change2(p*2+1,mid+1,r,a,b);
	t2[p]=min(t2[p*2],t2[p*2+1]);
}
ll query(int p,int l,int r,int a,int b){
	if (l==a&&r==b) return t1[p];
	int mid=(l+r)/2;
	if (b<=mid) return query(p*2,l,mid,a,b);
	else if (a>mid) return query(p*2+1,mid+1,r,a,b);
	else return min(query(p*2,l,mid,a,mid),query(p*2+1,mid+1,r,mid+1,b));
}
ll query2(int p,int l,int r,int a,int b){
	if (l==a&&r==b) return t2[p];
	int mid=(l+r)/2;
	if (b<=mid) return query2(p*2,l,mid,a,b);
	else if (a>mid) return query2(p*2+1,mid+1,r,a,b);
	else return min(query2(p*2,l,mid,a,mid),query2(p*2+1,mid+1,r,mid+1,b));
}
void work(){
	build(1,1,m+1);
	change(1,1,m+1,rank[0],f[1]-sum[1]-(ll)x[0]);
	change2(1,1,m+1,rank[0],f[1]-sum[1]+(ll)x[0]);
	fo(i,2,m){
		f[i]=query(1,1,m+1,1,rank[i])+(ll)x[i]+sum[i-1];
		f[i]=min(f[i],query2(1,1,m+1,rank[i],m+1)-(ll)x[i]+sum[i-1]);
		change(1,1,m+1,rank[i-1],f[i]-sum[i]-(ll)x[i-1]);
		change2(1,1,m+1,rank[i-1],f[i]-sum[i]+(ll)x[i-1]);
	}
	fo(i,1,m) ans=min(ans,f[i]+sum[m]-sum[i]);
}
int main(){
	scanf("%d%d%d%d",&n,&m,&a,&b);
	fo(i,1,m){
		scanf("%d",&x[i]);
		if (i>1) sum[i]=abs(x[i]-x[i-1]);
	}
	fo(i,1,m) sum[i]+=sum[i-1];
	ans=inf;
	x[0]=a;
	f[1]=abs(x[1]-b);
	fo(i,0,m) id[i]=i;
	sort(id,id+m+1,cmp);
	fo(i,0,m) rank[id[i]]=i+1;
	work();
	x[0]=b;
	f[1]=abs(x[1]-a);
	fo(i,0,m) id[i]=i;
	sort(id,id+m+1,cmp);
	fo(i,0,m) rank[id[i]]=i+1;
	work();
	printf("%lld\n",ans);
}

Compiler output:
a.cc: In function 'void work()':
a.cc:56:24: error: reference to 'rank' is ambiguous
   56 |         change(1,1,m+1,rank[0],f[1]-sum[1]-(ll)x[0]);
      |                        ^~~~
In file included from /usr/include/c++/14/bits/stl_pair.h:60,
                 from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from a.cc:2:
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:10:22: note:                 'int rank [200010]'
   10 | int x[maxn],id[maxn],rank[maxn];
      |                      ^~~~
a.cc:57:25: error: reference to 'rank' is ambiguous
   57 |         change2(1,1,m+1,rank[0],f[1]-sum[1]+(ll)x[0]);
      |                         ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:10:22: note:                 'int rank [200010]'
   10 | int x[maxn],id[maxn],rank[maxn];
      |                      ^~~~
a.cc:59:38: error: reference to 'rank' is ambiguous
   59 |                 f[i]=query(1,1,m+1,1,rank[i])+(ll)x[i]+sum[i-1];
      |                                      ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:10:22: note:                 'int rank [200010]'
   10 | int x[maxn],id[maxn],rank[maxn];
      |                      ^~~~
a.cc:60:46: error: reference to 'rank' is ambiguous
   60 |                 f[i]=min(f[i],query2(1,1,m+1,rank[i],m+1)-(ll)x[i]+sum[i-1]);
      |                                              ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:10:22: note:                 'int rank [200010]'
   10 | int x[maxn],id[maxn],rank[maxn];
      |                      ^~~~
a.cc:61:32: error: reference to 'rank' is ambiguous
   61 |                 change(1,1,m+1,rank[i-1],f[i]-sum[i]-(ll)x[i-1]);
      |                                ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:10:22: note:                 'int rank [200010]'
   10 | int x[maxn],id[maxn],rank[maxn];
      |                      ^~~~
a.cc:62:33: error: reference to 'rank' is ambiguous
   62 |                 change2(1,1,m+1,rank[i-1],f[i]-sum[i]+(ll)x[i-1]);
      |                                 ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:10:22: note:                 'int rank [200010]'
   10 | int x[maxn],id[maxn],rank[maxn];
      |                      ^~~~
a.cc: In function 'int main()':
a.cc:78:19: error: reference to 'rank' is ambiguous
   78 |         fo(i,0,m) rank[id[i]]=i+1;
      |                   ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:10:22: note:                 'int rank [200010]'
   10 | int x[maxn],id[maxn],rank[maxn];
      |                      ^~~~
a.cc:84:19: error: reference to 'rank' is ambiguous
   84 |         fo(i,0,m) rank[id[i]]=i+1;
      |                   ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:10:22: note:                 'int rank [200010]'
   10 | int x[maxn],id[maxn],rank[maxn];
      |                      ^~~~