For this Question: You are given an array A = {a<sub>1</sub>, a<sub>2</sub>, ... , a<sub>n</sub>} of n integers.

Let's define a function over an array f(A).

where f(A) = |a<sub>1</sub>- a<sub>2</sub>| + |a<sub>2</sub>- a<sub>3</sub>|  + .... + |a<sub>n-1</sub> - a<sub>n</sub>|. For each <i>i</i>, answer of the following question:

Find the minimum possible value of f(A) after performing <b>at most one</b> operation of the following type:
<ul>
<li> Swap a<sub>i</sub> with a<sub>j</sub>, where j != i
</ul>The first line contains a single integer n, where n is the size of the array.

The second line contains n integers a<sub>1</sub>, a<sub>2</sub>,...,a<sub>n</sub>.

<b>Constraints:</b>
1 <= n <= 7*10<sup>4</sup>
1 <= a<sub>i</sub> <= 10<sup>8</sup>Print n lines, <i>i</i><sup>th</sup> of them containing a single integer, the minimum value of f(A) for the index i after performing the operation or not.Sample Input 1:
4
5 5 5 5

Sample Output 1:
0
0
0
0

Sample Input 2:
6
1 2 1 2 1 2

Sample Output 2:
2
1
2
2
1
2, I have written this Solution Code: #include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stdio.h>
using namespace std;
const double inf=1e18;
double ans[55555],sum;
struct pp
{
    int id,value;
    bool operator <(const pp &temp)const
    {
        return value<temp.value;
    }
};
pp uh[55555];
int pos[55555];
struct segment_tree
{
    int l,r;
    double min_value[3];
    int iden[3];
};
segment_tree tree[140000];
struct xx
{
    int h,flag,id;
    bool operator <(const xx &temp)const
    {
        if(h==temp.h)
           return flag<temp.flag;
        return h<temp.h;
    }
};
xx x[310000];
struct segment
{
    int l,r,h;
};
int n,h[55555],cnt_x,cnt;
void build(int left,int right,int id)
{
    int i,l=2*id+1,r=2*id+2,mid=(left+right)>>1;
    tree[id].l=left;
    tree[id].r=right;
    for(i=0;i<3;i++)
       tree[id].min_value[i]=inf;
    if(left==right)
       return;
    build(left,mid,l);
    build(mid+1,right,r);
}
void insert(int pos,double value[3],int ip,int flag,int id)
{
    int i,j,s,p,q,l=2*id+1,r=2*id+2;
    if(tree[id].l==tree[id].r)
    {
        for(i=0;i<3;i++)
        {
            if(flag<0)
            {
                if(tree[id].min_value[i]>value[i])
                {
                    tree[id].min_value[i]=value[i];
                    tree[id].iden[i]=ip;
                }
                
            }
            else
               tree[id].min_value[i]=inf;   
        }
        return;
    }
    if(tree[l].r<pos)
       insert(pos,value,ip,flag,r);
    else
       insert(pos,value,ip,flag,l);
    for(i=0;i<3;i++)
    {
       tree[id].min_value[i]=min(tree[l].min_value[i],tree[r].min_value[i]);
       tree[id].iden[i]=tree[r].iden[i];
       if(tree[id].min_value[i]==tree[l].min_value[i])
          tree[id].iden[i]=tree[l].iden[i];
    }
}
void query(int left,int right,int ipn,int ip,int id,double &rt)
{
    int l=2*id+1,r=2*id+2;
    if(tree[id].l==left&&tree[id].r==right)
    {
        if(abs(tree[id].iden[ip]-ipn)>1)
        {
            rt=min(rt,tree[id].min_value[ip]);
            return;
        }
    }
    if(tree[id].l==tree[id].r)
       return;
    if(tree[l].r<left)
        query(left,right,ipn,ip,r,rt);
    else if(tree[r].l>right)
        query(left,right,ipn,ip,l,rt);
    else
    {
        query(left,tree[l].r,ipn,ip,l,rt);
        query(tree[r].l,right,ipn,ip,r,rt);
    }
}
int lower_bound(pp uh[],int x)
{
    int left,right,mid;
    left=0;
    right=cnt-1;
    while(left<=right)
    {
        mid=(left+right)>>1;
        if(uh[mid].value<x)
           left=mid+1;
        else
           right=mid-1;
    }
    return left;
}
int upper_bound(pp uh[],int x)
{
    int left,right,mid;
    left=0;
    right=cnt-1;
    while(left<=right)
    {
        mid=(left+right)>>1;
        if(uh[mid].value>x)
           right=mid-1;
        else
           left=mid+1;
    }
    return right;
}
void solve(int flag)
{
    int i,j,s,p,q,id;
    build(0,cnt-1,0);
    cnt_x=0;
    if(flag==0)
    {
        for(i=1;i<n-1;i++)
        {
            x[cnt_x].flag=-1;
            x[cnt_x].id=i;
            x[cnt_x++].h=min(h[i-1],h[i+1]);
            x[cnt_x].flag=0;
            x[cnt_x].id=i;
            x[cnt_x++].h=h[i];
            x[cnt_x].flag=1;
            x[cnt_x].id=i;
            x[cnt_x++].h=max(h[i-1],h[i+1]);
        }
    }
    else if(flag<0)
    {
        for(i=1;i<n-1;i++)
        {
            x[cnt_x].flag=-1;
            x[cnt_x].id=i;
            x[cnt_x++].h=uh[0].value;
            x[cnt_x].flag=1;
            x[cnt_x].id=i;
            x[cnt_x++].h=min(h[i-1],h[i+1]);
            x[cnt_x].flag=0;
            x[cnt_x].id=i;
            x[cnt_x++].h=h[i];
        }
    }
    else
    {
        for(i=1;i<n-1;i++)
        {
            x[cnt_x].flag=-1;
            x[cnt_x].id=i;
            x[cnt_x++].h=max(h[i-1],h[i+1]);
            x[cnt_x].flag=1;
            x[cnt_x].id=i;
            x[cnt_x++].h=uh[cnt-1].value;
            x[cnt_x].flag=0;
            x[cnt_x].id=i;
            x[cnt_x++].h=h[i];
        }
    }
    sort(x,x+cnt_x);
    for(i=0;i<cnt_x;i++)
    {
        double value,vv[3];
        if(flag<0)
           value=h[x[i].id+1]+h[x[i].id-1];
        else if(flag==0)
           value=abs(h[x[i].id+1]-h[x[i].id-1]);
        else
           value=-(h[x[i].id+1]+h[x[i].id-1]);
        value-=abs(h[x[i].id+1]-h[x[i].id])+abs(h[x[i].id-1]-h[x[i].id]);
        vv[0]=vv[1]=vv[2]=value;
        vv[0]-=2*h[x[i].id];
        vv[2]+=2*h[x[i].id];
        if(x[i].flag!=0)
           insert(pos[x[i].id],vv,x[i].id,x[i].flag,0);
        else
        {
            double rt;
            int ls,rs;
            ls=min(h[x[i].id-1],h[x[i].id+1]);
            rs=max(h[x[i].id-1],h[x[i].id+1]);
            ls=lower_bound(uh,ls);
            rs=upper_bound(uh,rs);
            rt=inf;
            query(ls,rs,x[i].id,1,0,rt);
            id=x[i].id;
            rt=rt-abs(h[x[i].id]-h[x[i].id+1])-abs(h[x[i].id]-h[x[i].id-1])+abs(h[x[i].id-1]-h[x[i].id+1]);
            rt+=2*flag*h[x[i].id];
            if(ans[id]>sum+rt)
               ans[id]=sum+rt;
            rt=inf;
            ls=min(h[x[i].id-1],h[x[i].id+1]);
            ls=upper_bound(uh,ls);
            query(0,ls,x[i].id,0,0,rt);
            rt=rt-abs(h[x[i].id]-h[x[i].id+1])-abs(h[x[i].id]-h[x[i].id-1])+h[x[i].id-1]+h[x[i].id+1];
            rt+=2*flag*h[x[i].id];
            if(ans[id]>sum+rt)
                ans[id]=sum+rt;
            rt=inf;
            rs=max(h[x[i].id-1],h[x[i].id+1]);
            rs=lower_bound(uh,rs);
            query(rs,cnt-1,x[i].id,2,0,rt);
            rt=rt-abs(h[x[i].id]-h[x[i].id+1])-abs(h[x[i].id]-h[x[i].id-1])-h[x[i].id-1]-h[x[i].id+1];
            rt+=2*flag*h[x[i].id];
            if(ans[id]>sum+rt)
                ans[id]=sum+rt;
        }
    }
}
int main()
{
    int i,j,s,p,q;
    double rt;
    scanf("%d",&n);
    cnt_x=cnt=0;
    for(i=0;i<n;i++)
    {
      scanf("%d",&h[i]);
       uh[cnt].id=i;
       uh[cnt++].value=h[i];
    }
    sum=0;
    for(i=0;i<n-1;i++)
       sum+=abs(h[i]-h[i+1]);
    for(i=0;i<n;i++) 
        ans[i]=sum;
    sort(uh,uh+cnt);
    for(i=0;i<cnt;i++)
       pos[uh[i].id]=i;
    solve(0);
    solve(-1);
    solve(1);
    if(n>=3&&ans[0]>sum+abs(h[2]-h[0])-abs(h[2]-h[1]))
       ans[0]=sum+abs(h[2]-h[0])-abs(h[2]-h[1]);
    for(i=2;i<n;i++)
    {
        rt=abs(h[i-1]-h[0])-abs(h[i-1]-h[i]);
        if(i+1<n)
           rt+=abs(h[i+1]-h[0])-abs(h[i+1]-h[i]);
        rt+=abs(h[1]-h[i])-abs(h[1]-h[0]);
        if(ans[0]>sum+rt)
           ans[0]=sum+rt;
    }
    if(n>=3&&ans[n-1]>sum+abs(h[n-3]-h[n-1])-abs(h[n-3]-h[n-2]))
       ans[n-1]=sum+abs(h[n-3]-h[n-1])-abs(h[n-3]-h[n-2]);
    for(i=0;i<n-2;i++)
    {
        rt=abs(h[i+1]-h[n-1])-abs(h[i+1]-h[i]);
        if(i)
           rt+=abs(h[i-1]-h[n-1])-abs(h[i-1]-h[i]);
        rt+=abs(h[n-2]-h[i])-abs(h[n-2]-h[n-1]);
        if(ans[n-1]>sum+rt)
           ans[n-1]=sum+rt;
    }
    for(i=1;i<n-1;i++)
    {
        if(i>1)
        {
            rt=abs(h[i]-h[1])-abs(h[0]-h[1]);
            if(i>0)
               rt+=abs(h[i-1]-h[0])-abs(h[i-1]-h[i]);
            if(i+1<n)
               rt+=abs(h[i+1]-h[0])-abs(h[i+1]-h[i]);
            if(ans[i]>sum+rt)
               ans[i]=sum+rt;
        }
        if(i<n-2)
        {
            rt=abs(h[n-2]-h[i])-abs(h[n-2]-h[n-1]);
            if(i>0)
               rt+=abs(h[i-1]-h[n-1])-abs(h[i-1]-h[i]);
            if(i+1<n)
               rt+=abs(h[i+1]-h[n-1])-abs(h[i+1]-h[i]);
            if(ans[i]>sum+rt)
               ans[i]=sum+rt;
        }
        if(i>0)
        {
            rt=0;
            if(i-2>=0)
              rt+=abs(h[i-2]-h[i])-abs(h[i-2]-h[i-1]);
            if(i+1<n)
              rt+=abs(h[i-1]-h[i+1])-abs(h[i]-h[i+1]);
            if(ans[i]>sum+rt)
               ans[i]=sum+rt;
        } 
        if(i+1<n)
        {
            rt=0;
            if(i)
               rt+=abs(h[i-1]-h[i+1])-abs(h[i-1]-h[i]);
            if(i+2<n)
               rt+=abs(h[i]-h[i+2])-abs(h[i+1]-h[i+2]);
            if(ans[i]>sum+rt)
               ans[i]=sum+rt;  
        }
    }
    for(i=0;i<n;i++)
       printf("%.0f\n",ans[i]);
    return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not?