//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": [
            "#pragma GCC optimize(3,\"Ofast\",\"inline\")",
            "#pragma GCC optimize(2)",
            "#include<algorithm>",
            "#include<cstdio>",
            "#define Re register int",
            "#define pl tr[p].ps[0]",
            "#define pr tr[p].ps[1]",
            "#define pf tr[p].fa",
            "#define pv tr[p].v",
            "using namespace std;",
            "const int N=1e5+5,inf=2e9;",
            "int T,x,y,n,o,root,a[N];",
            "struct Splay{int v,fa,ps[2],tag,cnt,size;}tr[N];",
            "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;",
            "}",
            "inline void pushup(Re p){if(!p)return;tr[p].size=tr[p].cnt+tr[pl].size+tr[pr].size;}",
            "inline void connect(Re p,Re f,Re o){tr[tr[p].fa=f].ps[o]=p;}",
            "inline int which(Re p){return tr[pf].ps[1]==p;}",
            "inline void pushdown(Re p){",
            "\tif(tr[p].tag)tr[p].tag=0,swap(pl,pr),tr[pl].tag^=1,tr[pr].tag^=1;",
            "} ",
            "inline void rotate(Re p){",
            "\tRe y=pf,ys=which(p);",
            "\tRe R=tr[y].fa,Rs=which(y);",
            "\tRe B=tr[p].ps[ys^1];",
            "\tconnect(B,y,ys),connect(y,p,ys^1),connect(p,R,Rs);",
            "\tpushup(y),pushup(p);",
            "}",
            "inline void Splay(Re p,Re to){",
            "\tfor(Re fa;(fa=pf)!=to;rotate(p))",
            "\t\tif(tr[fa].fa!=to)rotate(which(p)==which(fa)?fa:p);",
            "\tif(!to)root=p;",
            "}",
            "inline void New(Re x,Re fa){",
            "\ttr[++o].v=x,tr[o].fa=fa,tr[o].cnt=tr[o].size=1;",
            "\tif(fa)tr[fa].ps[tr[fa].v<x]=o;",
            "}",
            "inline int ask_w(Re p,Re x){",
            "\tpushdown(p);",
            "\tif(x<=tr[pl].size)return ask_w(pl,x);",
            "\telse{",
            "\t\tRe tmp=tr[pl].size+tr[p].cnt;",
            "\t\treturn x<=tmp?p:ask_w(pr,x-tmp);",
            "\t}",
            "}",
            "inline void build(Re &p,Re L,Re R,Re fa){",
            "\tif(L>R)return;",
            "\tRe mid=L+R>>1;p=++o;",
            "\tpf=fa,pv=a[mid];",
            "\ttr[p].cnt=tr[p].size=1;",
            "\tbuild(pl,L,mid-1,p),build(pr,mid+1,R,p);",
            "\tpushup(p);",
            "}",
            "inline void reverse(Re l,Re r){",
            "\tl=ask_w(root,l-1),r=ask_w(root,r+1);",
            "\tSplay(l,0),Splay(r,l);",
            "\ttr[tr[r].ps[0]].tag^=1;",
            "}",
            "inline void print(Re p){",
            "\tpushdown(p);",
            "\tif(pl)print(pl);",
            "\tif(pv!=inf&&pv!=-inf)printf(\"%d \",pv);",
            "\tif(pr)print(pr);",
            "}",
            "int main(){",
            "\tin(n),in(T);",
            "\ta[1]=-inf,a[n+2]=inf;",
            "\tfor(Re i=2;i<=n+1;++i)a[i]=i-1;",
            "\tbuild(root,1,n+2,0);",
            "\twhile(T--)in(x),in(y),reverse(x+1,y+1);",
            "\tprint(root);",
            "}",
            ""
        ],
        "description": "插入 Splay (无注释)(文艺平衡树)"
    }
}