# 做法

#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],rk[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,rk[0],f[1]-sum[1]-(ll)x[0]);
change2(1,1,m+1,rk[0],f[1]-sum[1]+(ll)x[0]);
fo(i,2,m){
f[i]=query(1,1,m+1,1,rk[i])+(ll)x[i]+sum[i-1];
f[i]=min(f[i],query2(1,1,m+1,rk[i],m+1)-(ll)x[i]+sum[i-1]);
change(1,1,m+1,rk[i-1],f[i]-sum[i]-(ll)x[i-1]);
change2(1,1,m+1,rk[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) rk[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) rk[id[i]]=i+1;
work();
printf("%lld\n",ans);
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120