思路:分类讨论下即可
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <stack>
#include <cstring>
#include <queue>
#include <algorithm>
#define ll long long
#define max_ 50
using namespace std;
int n,pos,l,r;
int main(int argc, char const *argv[]) {
cin>>n>>pos>>l>>r;
int ans;
if(l==1&&r==n)
{
printf("0\n" );
return 0;
}
if(l==1)
{
ans=abs(pos-r)+1;
printf("%d\n",ans );
return 0;
}
if(r==n)
{
ans=abs(pos-l)+1;
printf("%d\n",ans );
return 0;
}
if(pos>=l&&pos<=r)
ans=r-l+min(r-pos,pos-l)+2;
else
ans=r-l+min(abs(pos-r),abs(pos-l))+2;
printf("%d\n",ans );
return 0;
}