//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.3点分树",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#include<vector>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=1e5+3,inf=2e9;",
            "int n,m,o,x,y,z,T,root,A[N],head[N];",
            "struct QAQ{int w,to,next;}a[N<<1];",
            "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;",
            "}",
            "struct TREE{",
            "\tint o,head[N];",
            "\tstruct QAQ{int rt,to,next;}a[N];",
            "\tinline void add(Re x,Re y,Re z){a[++o].rt=z,a[o].to=y,a[o].next=head[x],head[x]=o;}",
            "}T0;",
            "int rt,sum,Sv[N],gs[N],vis[N],size[N],maxp[N],frt[N][20],fdis[N][20];LL Sdv1[N],Sdv2[N];",
            "inline void getrt(Re x,Re fa){",
            "\tsize[x]=1,maxp[x]=0;",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif(!vis[to=a[i].to]&&to!=fa)",
            "\t\t\tgetrt(to,x),size[x]+=size[to],maxp[x]=max(maxp[x],size[to]);",
            "\tmaxp[x]=max(maxp[x],sum-size[x]);",
            "\tif(maxp[x]<maxp[rt])rt=x;",
            "}",
            "inline void getdis(Re x,Re rt,Re fa,Re d){",
            "\tif(x!=rt)frt[x][++gs[x]]=rt,fdis[x][gs[x]]=d;",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif(!vis[to=a[i].to]&&to!=fa)getdis(to,rt,x,d+a[i].w);",
            "}",
            "inline void sakura(Re x){",
            "\tRe now=sum;vis[x]=1,getdis(x,x,0,0);",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif(!vis[to=a[i].to])",
            "\t\t\tsum=size[to]>size[x]?now-size[x]:size[to],maxp[rt=0]=inf,getrt(to,0),T0.add(x,to,rt),sakura(rt);",
            "}",
            "inline void change(Re x,Re v){",
            "\tSdv1[x]+=v*0,Sv[x]+=v;",
            "\tfor(Re i=gs[x];i>=2;--i){",
            "\t\tRe tmp=fdis[x][i-1];",
            "\t\tSdv1[frt[x][i-1]]+=(LL)v*tmp;",
            "\t\tSdv2[frt[x][i]]+=(LL)v*tmp;",
            "\t\tSv[frt[x][i-1]]+=v;",
            "\t}",
            "}",
            "inline LL ask(Re x){",
            "\tLL ans=Sdv1[x];",
            "\tfor(Re i=gs[x];i>=2;--i){",
            "\t\tRe tmp=fdis[x][i-1];",
            "\t\tans+=Sdv1[frt[x][i-1]];",
            "\t\tans-=Sdv2[frt[x][i]];",
            "\t\tans+=(LL)(Sv[frt[x][i-1]]-Sv[frt[x][i]])*tmp;",
            "\t}",
            "\treturn ans;",
            "}",
            "inline LL find(Re x){",
            "\tLL tmp=ask(x);",
            "\tfor(Re i=T0.head[x];i;i=T0.a[i].next)",
            "\t\tif(ask(T0.a[i].to)<tmp)return find(T0.a[i].rt);",
            "\treturn tmp;",
            "}",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(T),m=n-1;",
            "\twhile(m--)in(x),in(y),in(z),add(x,y,z),add(y,x,z);",
            "\tsum=n,maxp[rt=0]=inf,getrt(1,0),sakura(root=rt);",
            "\tfor(Re i=1;i<=n;++i)frt[i][++gs[i]]=i;",
            "\twhile(T--)in(x),in(y),change(x,y),printf(\"%lld\\n\",find(root));",
            "}",
            ""
        ],
        "description": "插入 幻想乡战略游戏"
    }
}