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

{
    "Splay  (无注释)": {
        "prefix": "acm-2.7普通平衡树",
        "body": [
            "#include<algorithm>",
            "#include<cstdio>",
            "#include<queue>",
            "#define Re register int",
            "#define LL long long",
            "using namespace std;",
            "const int N=1e5+5;",
            "int x,T,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 Splay{",
            "\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 O,root;",
            "\tstruct QAQ{int v,fa,cnt,size,ps[2];}tr[N];",
            "\tinline void pushup(Re p){",
            "\t\tif(p)tr[p].size=tr[p].cnt+tr[pl].size+tr[pr].size;",
            "\t}",
            "\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\tconnect(x,fa,fas),connect(fa,p,fas^1),connect(p,pa,pas);",
            "\t\tpushup(fa),pushup(p);",
            "\t}",
            "\tinline void splay(Re p){",
            "\t\tfor(Re fa;fa=pf;rotate(p))",
            "\t\t\tif(tr[fa].fa)rotate(which(p)==which(fa)?fa:p);",
            "\t\troot=p;",
            "\t}",
            "\tinline int ask_level(Re p,Re x){",
            "\t\tif(x==pv){Re ans=tr[pl].size+1;splay(p);return ans;}",
            "\t\treturn x<pv?ask_level(pl,x):tr[pl].size+tr[p].cnt+ask_level(pr,x);",
            "\t}",
            "\tinline int ask_v(Re p,Re k){",
            "\t\tif(!p)return 0;",
            "\t\tif(k<=tr[pl].size)return ask_v(pl,k);",
            "\t\tRe tmp=tr[pl].size+tr[p].cnt;",
            "\t\treturn k<=tmp?pv:ask_v(pr,k-tmp);",
            "\t}",
            "\tinline int pre(Re p){p=pl;while(pr)p=pr;return p;}",
            "\tinline int nex(Re p){p=pr;while(pl)p=pl;return p;}",
            "\tinline void insert(Re x){",
            "\t\tRe p=root,fa=0;",
            "\t\twhile(p&&pv!=x)fa=p,p=tr[p].ps[x>pv];",
            "\t\tif(p)++tr[p].cnt,pushup(p);",
            "\t\telse{",
            "\t\t\ttr[p=++O]=(QAQ){x,fa,1,1};",
            "\t\t\tif(fa)tr[fa].ps[x>tr[fa].v]=p;",
            "\t\t}",
            "\t\tsplay(p);",
            "\t}",
            "\tinline void del(Re x){",
            "\t\task_level(root,x);Re p=root;",
            "\t\tif(tr[p].cnt>1){--tr[p].cnt,pushup(p);return;}",
            "\t\tif(!pl&&!pr){root=0;return;}",
            "\t\tif(!pl||!pr){tr[root=pl+pr].fa=0;return;}",
            "\t\tsplay(pre(p));",
            "\t\ttr[root].ps[1]=pr,tr[pr].fa=root;",
            "\t\tpushup(root);",
            "\t}",
            "\tinline int ask_pre(Re x){insert(x);Re ans=tr[pre(root)].v;del(x);return ans;}",
            "\tinline int ask_nex(Re x){insert(x);Re ans=tr[nex(root)].v;del(x);return ans;}",
            "}T1;",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(T);",
            "\twhile(T--){",
            "\t\tin(op),in(x);",
            "\t\tif(op==1)T1.insert(x);",
            "\t\tif(op==2)T1.del(x);",
            "\t\tif(op==3)printf(\"%d\\n\",T1.ask_level(T1.root,x));",
            "\t\tif(op==4)printf(\"%d\\n\",T1.ask_v(T1.root,x));",
            "\t\tif(op==5)printf(\"%d\\n\",T1.ask_pre(x));",
            "\t\tif(op==6)printf(\"%d\\n\",T1.ask_nex(x));",
            "\t}",
            "}",
            ""
        ],
        "description": "插入 Splay  (无注释)"
    }
}