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

{
    "Escape Through Leaf": {
        "prefix": "acm-2.6超哥线段树+线段树合并",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=1e5+3;",
            "int n,m,x,y,o,cnt,k[N],A[N],B[N],pt[N],head[N];LL b[N],dp[N];",
            "struct QAQ{int to,next;}a[N<<1];",
            "inline void add(Re x,Re y){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;",
            "}",
            "struct Segment_Tree{",
            "\t#define pl tr[p].lp",
            "\t#define pr tr[p].rp",
            "\t#define mid ((L+R)>>1)",
            "\tint O;",
            "\tstruct QAQ{int id,lp,rp;}tr[N*18*2*2];",
            "\tinline LL y(Re id,Re x){return (LL)k[id]*x+b[id];}",
            "\tinline void change(Re &p,Re L,Re R,Re id){",
            "\t\tif(!p){tr[p=++O].id=id;return;}",
            "\t\tif(L==R){if(y(id,L)<y(tr[p].id,L))tr[p].id=id;return;}",
            "\t\tif(k[id]>k[tr[p].id]){",
            "\t\t\tif(y(id,mid)<y(tr[p].id,mid))change(pr,mid+1,R,tr[p].id),tr[p].id=id;",
            "\t\t\telse change(pl,L,mid,id);",
            "\t\t}",
            "\t\telse{",
            "\t\t\tif(y(id,mid)<y(tr[p].id,mid))change(pl,L,mid,tr[p].id),tr[p].id=id;",
            "\t\t\telse change(pr,mid+1,R,id);",
            "\t\t}",
            "\t}",
            "\tinline LL ask(Re p,Re L,Re R,Re x){",
            "\t\tif(!p)return 1e18;",
            "\t\tif(L==R)return y(tr[p].id,x);",
            "\t\tLL ans=y(tr[p].id,x);",
            "\t\tif(x<=mid)return min(ans,ask(pl,L,mid,x));",
            "\t\telse return min(ans,ask(pr,mid+1,R,x));",
            "\t}",
            "\tinline int merge(Re p,Re q,Re L,Re R){",
            "\t\tif(!p||!q)return p+q;",
            "\t\tif(L==R){if(y(tr[p].id,L)>y(tr[q].id,L))tr[p].id=tr[q].id;return p;}",
            "\t\tpl=merge(pl,tr[q].lp,L,mid);",
            "\t\tpr=merge(pr,tr[q].rp,mid+1,R);",
            "\t\tchange(p,L,R,tr[q].id);",
            "\t\treturn p;",
            "\t}",
            "}TR;",
            "inline void dfs(Re x,Re fa){",
            "\tRe son=0;",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif((to=a[i].to)!=fa)son=1,dfs(to,x),pt[x]=TR.merge(pt[x],pt[to],-N,N);",
            "\tk[++cnt]=B[x],dp[x]=b[cnt]=son?TR.ask(pt[x],-N,N,A[x]):0;",
            "\tTR.change(pt[x],-N,N,cnt);",
            "}",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),m=n-1;",
            "\tfor(Re i=1;i<=n;++i)in(A[i]);",
            "\tfor(Re i=1;i<=n;++i)in(B[i]);",
            "\twhile(m--)in(x),in(y),add(x,y),add(y,x);",
            "\tdfs(1,0);",
            "\tfor(Re i=1;i<=n;++i)printf(\"%lld\\n\",dp[i]);",
            "}",
            ""
        ],
        "description": "插入 Escape Through Leaf"
    }
}