//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.12静态区间第K小",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define Re register int",
            "#define LL long long",
            "using namespace std;",
            "const int N=2e5+3,M=2e5+3,inf=1e9;",
            "int n,x,y,z,T,cnt,Ans[M];char op;",
            "struct QAQ{int l,r,k,op,id;}Q[N+M],Q1[N+M],Q2[N+M];",
            "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 BIT{",
            "\tint C[N];",
            "\tinline void add(Re x,Re v){while(x<=n)C[x]+=v,x+=x&-x;}",
            "\tinline int ask_(Re x){Re ans=0;while(x)ans+=C[x],x-=x&-x;return ans;}",
            "\tinline int ask(Re l,Re r){return ask_(r)-ask_(l-1);}",
            "}T1;",
            "inline void sakura(Re l,Re r,Re L,Re R){",
            "\tif(L>R)return;",
            "\tif(l==r){",
            "\t\tfor(Re i=L;i<=R;++i)if(Q[i].op>1)Ans[Q[i].id]=l;",
            "\t\treturn;",
            "\t}",
            "\tRe mid=l+r>>1,m1=0,m2=0;",
            "\tfor(Re i=L;i<=R;++i)",
            "\t\tif(Q[i].op&1){",
            "\t\t\tif(Q[i].k<=mid)Q1[++m1]=Q[i],T1.add(Q[i].id,1);",
            "\t\t\telse Q2[++m2]=Q[i];",
            "\t\t}",
            "\t\telse{",
            "\t\t\tRe tmp=T1.ask(Q[i].l,Q[i].r);",
            "\t\t\tif(Q[i].k<=tmp)Q1[++m1]=Q[i];",
            "\t\t\telse Q[i].k-=tmp,Q2[++m2]=Q[i];",
            "\t\t}",
            "\tfor(Re i=1;i<=m1;++i)if(Q1[i].op&1)T1.add(Q1[i].id,-1);",
            "\tfor(Re i=1;i<=m1;++i)Q[L+i-1]=Q1[i];",
            "\tfor(Re i=1;i<=m2;++i)Q[L+m1+i-1]=Q2[i];",
            "\tsakura(l,mid,L,L+m1-1);",
            "\tsakura(mid+1,r,L+m1,R);",
            "}",
            "int main(){",
            "\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(T);",
            "\tfor(Re i=1;i<=n;++i)in(x),Q[++cnt]=(QAQ){0,0,x,1,i};",
            "\tfor(Re i=1;i<=T;++i)in(x),in(y),in(z),Q[++cnt]=(QAQ){x,y,z,2,i};",
            "\tsakura(-inf,inf,1,cnt);",
            "\tfor(Re i=1;i<=T;++i)printf(\"%d\\n\",Ans[i]);",
            "}",
            ""
        ],
        "description": "插入 整体二分"
    }
}