//Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and 
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
// same ids are connected.
// prefix 的格式要求是 acm-[一级标题编号].[二级标题编号][该代码片段的英文简称]
// description 的格式要求是 "插入 [该代码片段的中文名]"

{
    "Lightning Conductor": {
        "prefix": "acm-5.1二分栈",
        "body": [
            "#include<algorithm>",
            "#include<cstdio>",
            "#include<cmath>",
            "#define Re register int",
            "using namespace std;",
            "const int N=5e5+3;",
            "int i,j,n,h,t,a[N],Q[N],Poi[N];",
            "double p[N],sqr[N];",
            "inline void in(Re &x){",
            "    int f=0;x=0;char c=getchar();",
            "    while(c<'0'||c>'9')f|=c=='-',c=getchar();",
            "    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();",
            "    x=f?-x:x;",
            "}",
            "inline double Y(Re i,Re j){return a[j]+sqr[i-j];}",
            "inline int find_Poi(int j1,int j2){// 找到两条直线的交点i ",
            "    Re l=j2,r=n,mid,ans=n+1;// 为了防止两条直线没有交点，预设一个最大边界值",
            "    while(l<=r){",
            "        mid=l+r>>1;",
            "        if(Y(mid,j1)<=Y(mid,j2))ans=mid,r=mid-1;",
            "// 当前位置i使用直线j1的值小于直线j2的值，说明交点i在当前位置右侧，因此右边界要缩小",
            "        else l=mid+1;",
            "    }",
            "    return ans;",
            "}",
            "inline void sakura(){",
            "    h=1,t=0;",
            "    for(i=1;i<=n;++i){",
            "    \twhile(h<t&&Poi[t-1]>=find_Poi(Q[t],i))--t;",
            "    \tPoi[t]=find_Poi(Q[t],i),Q[++t]=i;",
            "    \twhile(h<t&&Poi[h]<=i)++h;",
            "// 找到位置j使得直线Q[j]与直线Q[j+1]的交点大于i",
            "// 那么直线Q[j]是在i之前的最优直线，记录答案，自己画图模拟一下就懂了",
            "    \tp[i]=max(p[i],Y(i,Q[h]));",
            "    }",
            "}",
            "int main(){",
            "    in(n);",
            "    for(Re i=1;i<=n;++i)in(a[i]),sqr[i]=sqrt(i);",
            "    sakura();",
            "    for(Re i=1;i<n-i+1;++i)swap(a[i],a[n-i+1]),swap(p[i],p[n-i+1]);",
            "    sakura();",
            "    for(Re i=n;i;--i)printf(\"%d\\n\",(int)ceil(p[i])-a[i]);",
            "}",
            ""
        ],
        "description": "插入 Lightning Conductor"
    }
}