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

{
    "点分树-在线求原树距离-Seg": {
        "prefix": "acm-4.3求距离小于等于K的点权值之和 震波",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=1e5+3,inf=2e9,logN=17;",
            "int n,m,o,x,y,T,op,lastans,A[N],deep[N],head[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 LCA{",
            "\tint fa[N],top[N],son[N],size[N];",
            "\tinline void dfs1(Re x,Re Fa){",
            "\t\tsize[x]=1,deep[x]=deep[fa[x]=Fa]+1;",
            "\t\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\t\tif((to=a[i].to)!=Fa){",
            "\t\t\t\tdfs1(to,x),size[x]+=size[to];",
            "\t\t\t\tif(size[son[x]]<size[to])son[x]=to;",
            "\t\t\t}",
            "\t}",
            "\tinline void dfs2(Re x,Re rt){",
            "\t\ttop[x]=rt;",
            "\t\tif(!son[x])return;",
            "\t\tdfs2(son[x],rt);",
            "\t\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\t\tif((to=a[i].to)!=fa[x]&&to!=son[x])dfs2(to,to);",
            "\t}",
            "\tinline void build(){dfs1(1,0),dfs2(1,1);}",
            "\tinline int lca(Re x,Re y){",
            "\t\twhile(top[x]!=top[y]){",
            "\t\t\tif(deep[top[x]]<deep[top[y]])swap(x,y);",
            "\t\t\tx=fa[top[x]];",
            "\t\t}",
            "\t\tif(deep[x]>deep[y])swap(x,y);",
            "\t\treturn x;",
            "\t}",
            "}T1;",
            "inline int Dis(Re x,Re y){return deep[x]+deep[y]-(deep[T1.lca(x,y)]<<1);}",
            "int pt[N<<1];",
            "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 S,lp,rp;}tr[N*60];",
            "\tinline void change(Re &p,Re L,Re R,Re x,Re v){",
            "\t\tif(!p)p=++O;",
            "\t\tif(L==R){tr[p].S+=v;return;}",
            "\t\tif(x<=mid)change(pl,L,mid,x,v);",
            "\t\telse change(pr,mid+1,R,x,v);",
            "\t\ttr[p].S=tr[pl].S+tr[pr].S;",
            "\t}",
            "\tinline int ask(Re p,Re L,Re R,Re l,Re r){",
            "\t\tif(!p)return 0;",
            "\t\tif(l<=L&&R<=r)return tr[p].S;",
            "\t\tRe ans=0;",
            "\t\tif(l<=mid)ans+=ask(pl,L,mid,l,r);",
            "\t\tif(r>mid)ans+=ask(pr,mid+1,R,l,r);",
            "\t\treturn ans;",
            "\t}",
            "}TR;",
            "int rt,sum,fa[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 sakura(Re x,Re Fa){",
            "\tRe now=sum;vis[x]=1,fa[x]=Fa;",
            "\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),sakura(rt,x);",
            "}",
            "inline void change(Re x,Re v){",
            "\tTR.change(pt[x],0,n,0,v);",
            "\tfor(Re i=x;fa[i];i=fa[i]){",
            "\t\tRe tmp=Dis(x,fa[i]);",
            "\t\tTR.change(pt[fa[i]],0,n,tmp,v);",
            "\t\tTR.change(pt[i+n],0,n,tmp,v);",
            "\t}",
            "}",
            "inline int ask(Re x,Re K){",
            "\tRe ans=TR.ask(pt[x],0,n,0,K);",
            "\tfor(Re i=x;fa[i];i=fa[i]){",
            "\t\tRe tmp=Dis(x,fa[i]);if(tmp>K)continue;",
            "\t\tans+=TR.ask(pt[fa[i]],0,n,0,K-tmp);",
            "\t\tans-=TR.ask(pt[i+n],0,n,0,K-tmp);",
            "\t}",
            "\treturn ans;",
            "}",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(T),m=n-1;",
            "\tfor(Re i=1;i<=n;++i)in(A[i]);",
            "\twhile(m--)in(x),in(y),add(x,y),add(y,x);",
            "\tT1.build(),sum=n,maxp[rt=0]=inf,getrt(1,0),sakura(rt,0);",
            "\tfor(Re i=1;i<=n;++i)change(i,A[i]);",
            "\twhile(T--){",
            "\t\tin(op),in(x),in(y),x^=lastans,y^=lastans;",
            "\t\tif(op)change(x,y-A[x]),A[x]=y;",
            "\t\telse printf(\"%d\\n\",lastans=ask(x,y));",
            "\t}",
            "}",
            ""
        ],
        "description": "插入 点分树-在线求原树距离-Seg"
    }
}