//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-4.3LCT维护原子树信息",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#include<set>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=1e5+3,inf=2e9;",
            "int n,x,y,z,T,op,root,X[N],Y[N];",
            "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 Link_Cut_Tree{",
            "\t#define pl tr[p].ps[0]",
            "\t#define pr tr[p].ps[1]",
            "\t#define pf tr[p].fa",
            "\t#define pv tr[p].v",
            "\tint Q[N];",
            "\tstruct QAQ{int v,fa,si,shi,ans,add,tag,ans0,ps[2];multiset<int>SI;}tr[N];",
            "\tinline void pushup(Re p){",
            "\t\ttr[p].shi=min(pv,min(tr[pl].shi,tr[pr].shi));",
            "\t\ttr[p].si=min(*tr[p].SI.begin(),min(tr[pl].si,tr[pr].si));",
            "\t\ttr[p].ans=min(tr[p].shi,tr[p].si);",
            "\t}",
            "\tinline void updata(Re p){tr[p].tag^=1,swap(pl,pr);}",
            "\tinline void updata(Re p,Re v){tr[p].add=1,tr[p].shi=pv=v,tr[p].ans=min(tr[p].shi,tr[p].si);}",
            "\tinline void pushdown(Re p){",
            "\t\tif(tr[p].tag){",
            "\t\t\tif(pl)updata(pl);",
            "\t\t\tif(pr)updata(pr);",
            "\t\t\ttr[p].tag=0;",
            "\t\t}",
            "\t\tif(tr[p].add){",
            "\t\t\tif(pl)updata(pl,pv);",
            "\t\t\tif(pr)updata(pr,pv);",
            "\t\t\ttr[p].add=0;",
            "\t\t}",
            "\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\tRe x=p,t=0;Q[++t]=x;",
            "\t\twhile(nort(x))Q[++t]=x=tr[x].fa;",
            "\t\twhile(t)pushdown(Q[t--]);",
            "\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);",
            "\t\t\tif(pr)tr[p].SI.insert(tr[pr].ans);",
            "\t\t\tpr=son;",
            "\t\t\tif(pr)tr[p].SI.erase(tr[p].SI.lower_bound(tr[pr].ans));",
            "\t\t\tpushup(p);",
            "\t\t}",
            "\t}",
            "\tinline void makeroot(Re p){access(p),splay(p),updata(p);}",
            "\tinline void link(Re x,Re y){makeroot(x),access(y),splay(y),tr[tr[x].fa=y].SI.insert(tr[x].ans);}",
            "\tinline void change(Re x,Re y,Re z){makeroot(x),access(y),splay(y),updata(y,z);}",
            "\tinline int ask_son(Re p){access(p),splay(p);return min(pv,*tr[p].SI.begin());}",
            "}T1;",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(T),T1.tr[0].shi=T1.tr[0].si=inf;",
            "\tfor(Re i=1;i<n;++i)in(X[i]),in(Y[i]);",
            "\tfor(Re i=1;i<=n;++i)in(T1.tr[i].v),T1.tr[i].SI.insert(inf),T1.pushup(i);",
            "\tfor(Re i=1;i<n;++i)T1.link(X[i],Y[i]);",
            "\tin(root),T1.makeroot(root);",
            "\twhile(T--){",
            "\t\tin(op),in(x);",
            "\t\tif(op<2)T1.makeroot(root=x);",
            "\t\telse if(op<3)in(y),in(z),T1.change(x,y,z),T1.makeroot(root);",
            "\t\telse printf(\"%d\\n\",T1.ask_son(x));",
            "\t}",
            "}",
            ""
        ],
        "description": "插入 遥远的国度"
    }
}