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

{
    "高速公路": {
        "prefix": "acm-5.1x单调 k不单调，树上转移",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LD double",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const LL N=1e6+3,inf=4e18;",
            "int n,m,x,y,z,o,h,t,P[N],Q[N],A[N],Fa[N],head[N];LL ans,dp[N],dis[N];",
            "struct QAQ{int w,to,next;}a[N];",
            "inline void add(Re x,Re y,Re z){a[++o].w=z,a[o].to=y,a[o].next=head[x],head[x]=o;}",
            "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[j]+P[i]*(dis[i]-dis[j])+A[i])",
            "//dp[i]=dp[j]+P[i]*dis[i]-P[i]*dis[j]+A[i]",
            "//P[i]*dis[j]+dp[i]-P[i]*dis[i]-A[i]=dp[j]",
            "//  k *  x   +          b           =  y",
            "inline LL Y(Re j){return dp[j];}",
            "inline LL X(Re j){return dis[j];}",
            "inline LD slope(Re i,Re j){return (LD)(Y(j)-Y(i))/(X(j)-X(i));}",
            "inline void dfs(Re x,Re fa){",
            "\tdp[x]=inf;Re h0=h,t0=t;",
            "\tRe l=h,r=t;",
            "\twhile(l<r){",
            "\t\tRe mid=l+r>>1;",
            "\t\tif(slope(Q[mid],Q[mid+1])<=P[x])l=mid+1;",
            "\t\telse r=mid;",
            "\t}",
            "\th=r;",
            "\tdp[x]=dp[Q[h]]+(LL)P[x]*(dis[x]-dis[Q[h]])+A[x];",
            "\tl=h,r=t;",
            "\twhile(l<r){",
            "\t\tRe mid=l+r+1>>1;",
            "\t\tif(slope(Q[mid-1],x)<=slope(Q[mid-1],Q[mid]))r=mid-1;",
            "\t\telse l=mid;",
            "\t}",
            "\tRe Qx0=Q[r+1];t=r,Q[++t]=x;",
            "\tfor(Re i=head[x],to;i;i=a[i].next)dis[to=a[i].to]=dis[x]+a[i].w,dfs(to,x);",
            "\th=h0,Q[t]=Qx0,t=t0;",
            "}",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n);",
            "\tfor(Re i=2;i<=n;++i)in(x),in(z),in(P[i]),in(A[i]),add(x,i,z),Fa[i]=x;",
            "\tdp[1]=0,h=1,t=0,Q[++t]=1;",
            "\tfor(Re i=head[1],to;i;i=a[i].next)",
            "\t\tdis[to=a[i].to]=dis[1]+a[i].w,dfs(to,1);",
            "\tfor(Re i=2;i<=n;++i)printf(\"%lld\\n\",dp[i]);",
            "}",
            ""
        ],
        "description": "插入 高速公路"
    }
}