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

{
    "最近公共祖先LCA": {
        "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=5e5+3;",
            "int n,m,x,y,T,root;",
            "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,tag,ps[2];}tr[N];",
            "\tinline void pushup(Re p){}",
            "\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 int access(Re p){",
            "\t\tRe son=0;",
            "\t\tfor(;p;son=p,p=pf)",
            "\t\t\tsplay(p),pr=son,pushup(p);",
            "\t\treturn son;",
            "\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){makeroot(x),tr[x].fa=y;}",
            "\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 lca(Re x,Re y){access(x);return access(y);}",
            "}LCT;",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(T),in(root),m=n-1;",
            "\twhile(m--)in(x),in(y),LCT.link(x,y);LCT.makeroot(root);",
            "\twhile(T--)in(x),in(y),printf(\"%d\\n\",LCT.lca(x,y));",
            "}",
            ""
        ],
        "description": "插入 最近公共祖先LCA"
    }
}