//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 的格式要求是 "插入 [该代码片段的中文名]"

{
    "CDQ 斜率优化  (k x均不单调)": {
        "prefix": "acm-5.1Building Bridges",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LD long double",
            "#define LL long long",
            "#define Re register int",
            "#define S2(a) (1ll*(a)*(a))",
            "using namespace std;",
            "const LL N=1e5+3,inf=1e18;",
            "int n,H[N],W[N],Q[N];LL S[N],dp[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;",
            "}",
            "//dp[i]=min(dp[i],dp[j]+(H[i]-H[j])*(H[i]-H[j])+S[i-1]-S[j]);",
            "//dp[i]=dp[j]-2*H[i]*H[j]+H[j]*H[j]+H[i]*H[i]+S[i-1]-S[j]",
            "//(2*H[i]) * H[j] + (dp[i]-S[i-1]-H[i]*H[i]) = (dp[j]+H[j]*H[j]-S[j])",
            "//   k     *  x   +            b             =          y",
            "#define X(j) (a[j].x)",
            "#define Y(j) (a[j].y)",
            "struct QAQ{",
            "\tint k,x,id;LL y;",
            "\tinline bool operator<(const QAQ &O)const{return x!=O.x?x<O.x:y<O.y;}",
            "}a[N],b[N];",
            "inline bool cmp(QAQ A,QAQ B){return A.k<B.k;}",
            "inline LD slope(Re i,Re j){return X(i)==X(j)?(Y(j)>Y(i)?inf:-inf):(LD)(Y(j)-Y(i))/(X(j)-X(i));}",
            "inline void CDQ(Re L,Re R){",
            "\tif(L==R){Re j=a[L].id;a[L].y=dp[j]+(LL)H[j]*H[j]-S[j];return;}",
            "\tRe mid=L+R>>1,p1=L,p2=mid+1,h=1,t=0;",
            "\tfor(Re i=L;i<=R;++i)a[i].id<=mid?b[p1++]=a[i]:b[p2++]=a[i];",
            "\tfor(Re i=L;i<=R;++i)a[i]=b[i];",
            "\tCDQ(L,mid);",
            "\tfor(Re i=L;i<=mid;++i){",
            "\t\twhile(h<t&&slope(Q[t-1],Q[t])>=slope(Q[t-1],i))--t;",
            "\t\tQ[++t]=i;",
            "\t}",
            "\tfor(Re i=mid+1,j,id;i<=R;++i){",
            "\t\twhile(h<t&&slope(Q[h],Q[h+1])<=a[i].k)++h;",
            "\t\tif(h<=t)id=a[i].id,j=Q[h],dp[id]=min(dp[id],a[j].y-(LL)a[i].k*a[j].x+S[id-1]+(LL)H[id]*H[id]);",
            "\t}",
            "\tCDQ(mid+1,R);",
            "\tRe w=L-1;p1=L,p2=mid+1;",
            "\twhile(p1<=mid&&p2<=R)b[++w]=a[p1]<a[p2]?a[p1++]:a[p2++];",
            "\twhile(p1<=mid)b[++w]=a[p1++];while(p2<=R)b[++w]=a[p2++];",
            "\tfor(Re i=L;i<=R;++i)a[i]=b[i];",
            "}",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n);",
            "\tfor(Re i=1;i<=n;++i)in(H[i]);",
            "\tfor(Re i=1;i<=n;++i)in(W[i]);",
            "\tfor(Re i=1;i<=n;++i)S[i]=S[i-1]+W[i],dp[i]=inf;",
            "\tfor(Re i=1;i<=n;++i)a[i].k=(H[i]<<1),a[i].x=H[i],a[i].id=i;",
            "\tsort(a+1,a+n+1,cmp);",
            "\tdp[1]=0,CDQ(1,n);",
            "\tprintf(\"%lld\\n\",dp[n]);",
            "}",
            ""
        ],
        "description": "插入 CDQ 斜率优化  (k x均不单调)"
    }
}