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

{
    "Qtree7": {
        "prefix": "acm-4.3LCT维护同色连通块最大点权",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#include<queue>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=1e5+3,inf=2e9;",
            "int n,m,x,y,o,T,op,A[N],co[N],fa[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 Queue{",
            "\tpriority_queue<int>Q,D;",
            "\tinline void insert(Re x){Q.push(x);}",
            "\tinline void erase(Re x){if(Q.top()==x)Q.pop();else D.push(x);}",
            "\tinline int top(){",
            "\t\twhile(!Q.empty()&&!D.empty()&&Q.top()==D.top())Q.pop(),D.pop();",
            "\t\treturn Q.empty()?-inf:Q.top();",
            "\t}",
            "};",
            "struct Link_Cut_Tree{",
            "\t#define pl tr[p].ps[0]",
            "\t#define pr tr[p].ps[1]",
            "\t#define pf tr[p].fa",
            "\tstruct QAQ{int fa,max,ps[2];}tr[N];Queue si[N];",
            "\tinline void pushup(Re p){",
            "\t\ttr[p].max=max(max(A[p],si[p].top()),max(tr[pl].max,tr[pr].max));",
            "\t}",
            "\tinline int which(Re p){return tr[pf].ps[1]==p;}",
            "\tinline int nort(Re p){return tr[pf].ps[0]==p||tr[pf].ps[1]==p;}",
            "\tinline void connect(Re p,Re fa,Re o){tr[pf=fa].ps[o]=p;}",
            "\tinline void rotate(Re p){",
            "\t\tRe fa=pf,fas=which(p);",
            "\t\tRe pa=tr[fa].fa,pas=which(fa);",
            "\t\tRe x=tr[p].ps[fas^1];",
            "\t\tif(nort(fa))tr[pa].ps[pas]=p;pf=pa;",
            "\t\tconnect(x,fa,fas),connect(fa,p,fas^1);",
            "\t\tpushup(fa),pushup(p);",
            "\t}",
            "\tinline void splay(Re p){",
            "\t\tfor(Re fa;nort(p);rotate(p))",
            "\t\t\tif(nort(fa=pf))rotate(which(p)==which(fa)?fa:p);",
            "\t}",
            "\tinline void access(Re p){",
            "\t\tfor(Re son=0;p;son=p,p=pf)",
            "\t\t\tsplay(p),si[p].insert(tr[pr].max),pr=son,si[p].erase(tr[pr].max),pushup(p);",
            "\t}",
            "\tinline void link(Re p,Re fa){",
            "\t\taccess(p),splay(p),access(fa),splay(fa),si[pf=fa].insert(tr[p].max),pushup(fa);",
            "\t}",
            "\tinline void cut(Re p,Re fa){",
            "\t\taccess(p),splay(p),splay(fa),pf=tr[fa].ps[1]=0,pushup(fa);",
            "\t}",
            "\tinline int ask(Re p){",
            "\t\taccess(p),splay(p);while(pl)p=pl;splay(p);return tr[pr].max;",
            "\t}",
            "\tinline void change(Re p,Re v){",
            "\t\taccess(p),splay(p),A[p]=v,pushup(p);",
            "\t}",
            "}LCT[2];",
            "inline void dfs(Re x,Re Fa){",
            "\tLCT[0].tr[x].max=LCT[1].tr[x].max=A[x];",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif((to=a[i].to)!=Fa)dfs(to,x);",
            "\tLCT[co[x]].link(x,fa[x]=Fa);",
            "}",
            "int main(){",
            "//    freopen(\"123.txt\",\"r\",stdin);",
            "    in(n),m=n-1,LCT[0].tr[0].max=LCT[1].tr[0].max=-inf;",
            "    while(m--)in(x),in(y),add(x,y),add(y,x);",
            "    for(Re i=1;i<=n;++i)in(co[i]);",
            "    for(Re i=1;i<=n;++i)in(A[i]);",
            "    in(T),dfs(1,n+1);",
            "    while(T--){",
            "    \tin(op),in(x);",
            "    \tif(op<1)printf(\"%d\\n\",LCT[co[x]].ask(x));",
            "    \telse if(op<2)LCT[co[x]].cut(x,fa[x]),LCT[co[x]^=1].link(x,fa[x]);",
            "    \telse in(y),LCT[co[x]].change(x,y);",
            "\t}",
            "}",
            ""
        ],
        "description": "插入 Qtree7"
    }
}