//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-4.3mid",
        "body": [
            "#include<algorithm>",
            "#include<cstdio>",
            "#define pl tr[p].lp ",
            "#define pr tr[p].rp",
            "#define mid (L+R>>1)",
            "#define Re register int",
            "const int N=3e5+3,logN=19;",
            "int x,y,n,m,T,o,lca,w[N],pt[N],ans[N],head[N],deep[N];",
            "struct QWQ{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){",
            "    x=0;char c=getchar();",
            "    while(c<'0'||c>'9')c=getchar();",
            "    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();",
            "}",
            "struct Segmemt_Tree{",
            "\tstruct QAQ{int g,lp,rp;}tr[N*36];int cnt;",
            "\tinline void change(Re &p,Re L,Re R,Re x,Re v){",
            "\t\tif(!p)p=++cnt;",
            "\t\tif(L==R){tr[p].g+=v;return;}",
            "\t\tif(x<=mid)change(pl,L,mid,x,v);",
            "\t    else change(pr,mid+1,R,x,v);",
            "\t}",
            "\tinline int merge(Re p,Re q){",
            "\t    if(!p)return q;if(!q)return p;",
            "\t    tr[p].g+=tr[q].g;",
            "\t    pl=merge(pl,tr[q].lp);",
            "\t\tpr=merge(pr,tr[q].rp);",
            "\t    return p;",
            "\t}",
            "\tinline int ask(Re p,Re L,Re R,Re x){",
            "\t    if(!p)return 0;",
            "\t    if(L==R)return tr[p].g;",
            "\t    if(x<=mid)return ask(pl,L,mid,x);",
            "\t    else return ask(pr,mid+1,R,x);",
            "\t}",
            "}T1;",
            "struct LCA{",
            "\tint ant[N][23];",
            "\tinline void dfs(Re x,Re fa){",
            "\t\tdeep[x]=deep[fa]+1,ant[x][0]=fa;",
            "\t\tfor(Re i=1;(1<<i)<=deep[x];++i)ant[x][i]=ant[ant[x][i-1]][i-1];",
            "\t\tfor(Re i=head[x];i;i=a[i].next)if(a[i].to!=fa)dfs(a[i].to,x);",
            "\t}",
            "\tinline int lca(Re x,Re y){",
            "\t\tif(deep[x]>deep[y])std::swap(x,y);",
            "\t\tfor(Re i=logN;i>=0;--i)if(deep[x]<=deep[y]-(1<<i))y=ant[y][i];",
            "\t\tif(x==y)return y;",
            "\t\tfor(Re i=logN;i>=0;--i)",
            "\t\t\tif(ant[x][i]==ant[y][i])continue;",
            "\t\t\telse x=ant[x][i],y=ant[y][i];",
            "\t\treturn ant[x][0];",
            "\t}",
            "}T2;",
            "inline void dfs(Re x,Re fa){",
            "    for(Re i=head[x],to;i;i=a[i].next)",
            "    \tif((to=a[i].to)!=fa)",
            "        \tdfs(to,x),pt[x]=T1.merge(pt[x],pt[to]);",
            "\tans[x]+=T1.ask(pt[x],1,n<<1,n+deep[x]-w[x]);",
            "    if(w[x]&&deep[x]+w[x]<=n)ans[x]+=T1.ask(pt[x],1,n<<1,n+deep[x]+w[x]);",
            "}",
            "int main(){",
            "\tin(n),in(T),m=n-1;",
            "\twhile(m--)in(x),in(y),add(x,y),add(y,x);",
            "\tfor(Re i=1;i<=n;++i)in(w[i]);",
            "\tT2.dfs(1,0);",
            "\twhile(T--){",
            "\t\tin(x),in(y),lca=T2.lca(x,y);",
            "\t\tT1.change(pt[x],1,n<<1,n+deep[x],1);",
            "        T1.change(pt[y],1,n<<1,n+deep[lca]*2-deep[x],1);",
            "        T1.change(pt[lca],1,n<<1,n+deep[x],-1);",
            "        T1.change(pt[T2.ant[lca][0]],1,n<<1,n+deep[lca]*2-deep[x],-1);",
            "\t}",
            "\tdfs(1,0);",
            "\tfor(Re i=1;i<=n;++i)printf(\"%d \",ans[i]);",
            "}",
            ""
        ],
        "description": "插入 天天爱跑步"
    }
}