//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.9Count on a tree II",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#include<cmath>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=4e4+3,M=1e5+3,INF=1e6+3,logN=16;",
            "int n,m,o,x,y,T,BL,id_O,b[N],A[N],B[N<<1],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;",
            "struct Query{",
            "\tint l,r,id,lca;",
            "\tinline bool operator<(const Query &O)const{",
            "\t\tRe B1=l/BL,B2=O.l/BL;",
            "\t\treturn B1!=B2?B1<B2:((B1&1)?r<O.r:r>O.r);",
            "\t}",
            "}Q[M];",
            "int ans,vis[N<<1],Ans[M],cnt[INF];",
            "inline void change(Re x){",
            "\tvis[x]?ans-=!--cnt[A[x]]:ans+=!cnt[A[x]]++,vis[x]^=1;",
            "}",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(T),BL=sqrt(n<<1);",
            "\tfor(Re i=1;i<=n;++i)in(A[i]),b[i]=A[i];",
            "\tsort(b+1,b+n+1),m=unique(b+1,b+n+1)-b-1;",
            "\tfor(Re i=1;i<=n;++i)A[i]=lower_bound(b+1,b+m+1,A[i])-b;",
            "\tm=n-1;while(m--)in(x),in(y),add(x,y),add(y,x);",
            "\tT1.dfs(1,0);",
            "\tfor(Re i=1;i<=T;++i){",
            "\t\tin(x),in(y);Re lca=T1.lca(x,y);",
            "\t\tif(first[x]>first[y])swap(x,y);",
            "\t\tif(lca==x)Q[i]=(Query){first[x],first[y],i,0};",
            "\t\telse Q[i]=(Query){last[x],first[y],i,lca};",
            "\t}",
            "\tsort(Q+1,Q+T+1);",
            "\tRe nowL=1,nowR=0;",
            "\tfor(Re i=1;i<=T;++i){",
            "\t\tRe L=Q[i].l,R=Q[i].r,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\tif(lca)change(lca);",
            "\t\tAns[Q[i].id]=ans;",
            "\t\tif(lca)change(lca);",
            "\t}",
            "\tfor(Re i=1;i<=T;++i)printf(\"%d\\n\",Ans[i]);",
            "}",
            ""
        ],
        "description": "插入 树上静态莫队"
    }
}