//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>",
            "#define Re register int",
            "#define LL long long",
            "using namespace std;",
            "const int N=1e5+3;",
            "int n,x,y,T;char op;",
            "inline void in(Re &x){",
            "    int 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 Link_Cut_Tree{",
            "\t#define pl tr[p].ps[0]",
            "\t#define pr tr[p].ps[1]",
            "\t#define pf tr[p].fa",
            "\tint Q[N];",
            "\tstruct QAQ{int fa,si,ans,tag,ps[2];}tr[N];",
            "\tinline void pushup(Re p){",
            "\t\ttr[p].ans=1+tr[p].si+tr[pl].ans+tr[pr].ans;",
            "\t}",
            "\tinline void updata(Re p){swap(pl,pr),tr[p].tag^=1;}",
            "\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}",
            "\tinline int nort(Re p){return tr[pf].ps[0]==p||tr[pf].ps[1]==p;}",
            "\tinline int which(Re p){return 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),tr[p].si+=tr[pr].ans,tr[p].si-=tr[pr=son].ans,pushup(p);",
            "\t}",
            "\tinline void makeroot(Re p){access(p),splay(p),updata(p);}",
            "\tinline int findroot(Re p){",
            "\t\taccess(p),splay(p),pushdown(p);",
            "\t\twhile(pl)pushdown(p=pl);",
            "\t\treturn p;",
            "\t}",
            "\tinline void split(Re x,Re y){makeroot(x),access(y),splay(y);}",
            "\tinline void link(Re x,Re y){",
            "\t\tmakeroot(x);",
            "\t\tif(findroot(y)!=x)tr[tr[x].fa=y].si+=tr[x].ans,pushup(y);",
            "\t}",
            "\tinline void cut(Re x,Re y){",
            "\t\tmakeroot(x);",
            "\t\tif(findroot(y)==x&&tr[x].fa==y&&!tr[x].ps[1])",
            "\t\t\ttr[x].fa=tr[y].ps[0]=0,pushup(y);",
            "\t}",
            "\tinline int ask(Re x,Re y){split(x,y);return (tr[x].si+1)*(tr[y].si+1);}",
            "}LCT;",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(T);",
            "\twhile(T--){",
            "\t\tscanf(\" %c\",&op),in(x),in(y);",
            "\t\tif(op=='A')LCT.link(x,y);",
            "\t\tif(op=='Q')printf(\"%d\\n\",LCT.ask(x,y));",
            "\t}",
            "}",
            ""
        ],
        "description": "插入 大融合"
    }
}