//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-2.7线段树",
        "body": [
            "#pragma GCC optimize(2)",
            "#pragma GCC optimize(3,\"Ofast\",\"inline\")",
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define mid (L+R>>1)",
            "#define Re register int",
            "#define pl tree[p].lp",
            "#define pr tree[p].rp",
            "#define F(i,a,b) for(Re i=a;i<=b;++i)",
            "#define lo(o) lower_bound(b+1,b+m+1,o)-b",
            "using namespace std;",
            "const int N=1e5+3,inf=2147483647;",
            "int m,T,t,fu,cnt,root,a[N],b[N],Q[N],opt[N]; ",
            "struct QAQ{int g,lp,rp;}tree[N<<1];",
            "inline void in(Re &x){",
            "    x=fu=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 int ask_kth(Re p,Re L,Re R,Re k){",
            "\tif(L==R)return b[R];",
            "    Re tmp=tree[pl].g;",
            "    if(tmp>=k)return ask_kth(pl,L,mid,k);",
            "    else return ask_kth(pr,mid+1,R,k-tmp);",
            "}",
            "inline void add(Re &p,Re L,Re R,Re w,Re v){",
            "    if(!p)p=++cnt;tree[p].g+=v;",
            "    if(L==R)return;",
            "    if(w<=mid)add(pl,L,mid,w,v);",
            "    else add(pr,mid+1,R,w,v);",
            "}",
            "inline int ask_level(Re p,Re L,Re R,Re x){",
            "\tif(!x)return 0;",
            "\tif(L==R)return tree[p].g;",
            "\tif(x<=mid)return ask_level(pl,L,mid,x);",
            "\telse return tree[pl].g+ask_level(pr,mid+1,R,x);",
            "}",
            "int main(){",
            "\tin(T); ",
            "    F(i,1,T){",
            "    \tin(opt[i]),in(Q[i]);",
            "    \tif(opt[i]!=4)b[++m]=Q[i];",
            "    }",
            "    sort(b+1,b+m+1),m=unique(b+1,b+m+1)-b-1;",
            "    F(i,1,T){",
            "    \tif(opt[i]==1)add(root,1,m,lo(Q[i]),1);",
            "    \tif(opt[i]==2)add(root,1,m,lo(Q[i]),-1);",
            "\t\tif(opt[i]==3)printf(\"%d\\n\",ask_level(root,1,m,lo(Q[i])-1)+1);",
            "\t\tif(opt[i]==4)printf(\"%d\\n\",ask_kth(root,1,m,Q[i]));",
            "\t\tif(opt[i]==5){",
            "\t\t\tRe level=ask_level(root,1,m,lo(Q[i])-1);",
            "\t\t\tprintf(\"%d\\n\",ask_kth(root,1,m,level));",
            "\t\t}",
            "\t\tif(opt[i]==6){",
            "\t\t\tRe level=ask_level(root,1,m,lo(Q[i]));",
            "\t\t\tprintf(\"%d\\n\",ask_kth(root,1,m,level+1));",
            "\t\t}",
            "    }",
            "}",
            ""
        ],
        "description": "插入 普通平衡树"
    }
}