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

{
    "点分治  (长度小于等于K的路径数 Tree)": {
        "prefix": "acm-4.3容斥",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=4e4+3,inf=2e9;",
            "int n,m,o,x,y,z,K,head[N];LL Ans;",
            "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;",
            "}",
            "int rt,sum,cnt,tmp[N],dis[N],vis[N],maxp[N],size[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 fa,Re w){",
            "\tif(dis[fa]+w>K)return;",
            "\ttmp[++cnt]=dis[x]=dis[fa]+w;",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif(!vis[to=a[i].to]&&to!=fa)getdis(to,x,a[i].w);",
            "}",
            "inline int calc(Re x,Re d){",
            "\tRe ans=0;cnt=0,tmp[++cnt]=dis[x]=d;",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif(!vis[to=a[i].to])getdis(to,x,a[i].w);",
            "\tsort(tmp+1,tmp+cnt+1);",
            "\tRe R=cnt;",
            "\tfor(Re i=1;i<=cnt&&i<R;++i){",
            "\t\twhile(i<R&tmp[i]+tmp[R]>K)--R;",
            "\t\tif(i<R)ans+=(R-(i+1)+1);",
            "\t}",
            "\treturn ans;",
            "}",
            "inline void sakura(Re x){",
            "\tRe now=sum;vis[x]=1,Ans+=calc(x,0);",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif(!vis[to=a[i].to])",
            "\t\t\tAns-=calc(to,a[i].w),sum=size[to]>size[x]?now-size[x]:size[to],maxp[rt=0]=inf,getrt(to,x),sakura(rt);",
            "}",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),m=n-1;",
            "\twhile(m--)in(x),in(y),in(z),add(x,y,z),add(y,x,z);",
            "\tin(K);",
            "\tsum=n,maxp[rt=0]=inf,getrt(1,0),sakura(rt);",
            "\tprintf(\"%lld\\n\",Ans);",
            "}",
            ""
        ],
        "description": "插入 点分治  (长度小于等于K的路径数 Tree)"
    }
}