//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-2.9糖果公园",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#include<cmath>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=1e5+3,M=1e5+3,logN=17;",
            "int n,m,x,y,o,T,op,id_O,A[N],V[N],W[N],B[N],head[N],last[N],first[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){",
            "    Re fu=0;x=0;char c=getchar();",
            "    while(c<'0'||c>'9')fu|=c=='-',c=getchar();",
            "    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();",
            "    x=fu?-x:x;",
            "}",
            "struct LCA{",
            "\tint deep[N],ant[N][20];",
            "\tinline void dfs(Re x,Re fa){",
            "\t\tfirst[B[++B[0]]=x]=++id_O;",
            "\t\tdeep[x]=deep[ant[x][0]=fa]+1;",
            "\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\tlast[B[++B[0]]=x]=++id_O;",
            "\t}",
            "\tinline int lca(Re x,Re y){",
            "\t\tif(deep[x]<deep[y])swap(x,y);",
            "\t\tfor(Re i=logN;i>=0;--i)if(deep[ant[x][i]]>=deep[y])x=ant[x][i];",
            "\t\tif(x==y)return x;",
            "\t\tfor(Re i=logN;i>=0;--i)if(ant[x][i]!=ant[y][i])x=ant[x][i],y=ant[y][i];",
            "\t\treturn ant[x][0];",
            "\t}",
            "}T1;",
            "int BL,TQ,TC,vis[N],cnt[N];LL ans,Ans[M];",
            "struct Query{",
            "\tint l,r,id,lca,time;",
            "\tinline bool operator<(const Query &O)const{",
            "\t\tRe B1=l/BL,B2=O.l/BL,B3=r/BL,B4=O.r/BL;",
            "\t\treturn B1!=B2?B1<B2:(B3!=B4?B3<B4:time<O.time);",
            "\t}",
            "}Q[M];",
            "struct Change{int w,co;}C[M];",
            "inline void change(Re x){",
            "\tvis[x]?ans-=(LL)V[A[x]]*W[cnt[A[x]]--]:ans+=(LL)V[A[x]]*W[++cnt[A[x]]],vis[x]^=1;",
            "}",
            "int main(){",
            "\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(m),in(T),BL=pow(n<<1,2.0/3.0);",
            "\tfor(Re i=1;i<=m;++i)in(V[i]);",
            "\tfor(Re i=1;i<=n;++i)in(W[i]);",
            "\tfor(Re i=1;i<n;++i)in(x),in(y),add(x,y),add(y,x);",
            "\tfor(Re i=1;i<=n;++i)in(A[i]);",
            "\tT1.dfs(1,0);",
            "\tfor(Re i=1;i<=T;++i){",
            "\t\tin(op),in(x),in(y);",
            "\t\tif(op){",
            "\t\t\tRe lca=T1.lca(x,y);",
            "\t\t\tif(first[x]>first[y])swap(x,y);",
            "\t\t\tif(lca==x)++TQ,Q[TQ]=(Query){first[x],first[y],TQ,0,TC};",
            "\t\t\telse ++TQ,Q[TQ]=(Query){last[x],first[y],TQ,lca,TC};",
            "\t\t}",
            "\t\telse C[++TC]=(Change){x,y};",
            "\t}",
            "\tsort(Q+1,Q+TQ+1);",
            "\tRe nowL=1,nowR=0,nowT=0;",
            "\tfor(Re i=1;i<=TQ;++i){",
            "\t\tRe L=Q[i].l,R=Q[i].r,T=Q[i].time,lca=Q[i].lca;",
            "\t\twhile(nowL<L)change(B[nowL++]);",
            "\t\twhile(nowL>L)change(B[--nowL]);",
            "\t\twhile(nowR>R)change(B[nowR--]);",
            "\t\twhile(nowR<R)change(B[++nowR]);",
            "\t\twhile(nowT<T){",
            "\t\t\tRe w=C[++nowT].w;",
            "\t\t\tif(vis[w])change(w),swap(A[w],C[nowT].co),change(w);",
            "\t\t\telse swap(A[w],C[nowT].co);",
            "\t\t}",
            "\t\twhile(nowT>T){",
            "\t\t\tRe w=C[nowT].w;",
            "\t\t\tif(vis[w])change(w),swap(A[w],C[nowT].co),change(w);",
            "\t\t\telse swap(A[w],C[nowT].co);",
            "\t\t\t--nowT;",
            "\t\t}",
            "\t\tif(lca)change(lca);",
            "\t\tAns[Q[i].id]=ans;",
            "\t\tif(lca)change(lca);",
            "\t}",
            "\tfor(Re i=1;i<=TQ;++i)printf(\"%lld\\n\",Ans[i]);",
            "}",
            ""
        ],
        "description": "插入 树上带修莫队"
    }
}