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

{
    "主席树-区间第k小": {
        "prefix": "acm-2.6mid",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=2e5+3,logN=18;",
            "int n,m,x,y,z,T,b[N],A[N],pt[N];",
            "inline void in(Re &x){",
            "    int f=0;x=0;char c=getchar();",
            "    while(c<'0'||c>'9')f|=c=='-',c=getchar();",
            "    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();",
            "    x=f?-x:x;",
            "}",
            "struct President_Tree{",
            "    #define pl tr[p].lp",
            "    #define pr tr[p].rp",
            "    #define mid (L+R>>1)",
            "    int O;",
            "    struct QAQ{int S,lp,rp;}tr[N*(logN+1)];",
            "    inline void creat(Re &p,Re q,Re L,Re R,Re w){",
            "        p=++O,tr[p]=tr[q],++tr[p].S;",
            "        if(L==R)return;",
            "        if(w<=mid)creat(pl,tr[q].lp,L,mid,w);",
            "        else creat(pr,tr[q].rp,mid+1,R,w);",
            "    }",
            "    inline int ask(Re p,Re q,Re L,Re R,Re k){",
            "        if(L==R)return L;",
            "        Re tmp=tr[pl].S-tr[tr[q].lp].S;",
            "        if(tmp>=k)return ask(pl,tr[q].lp,L,mid,k);",
            "        else return ask(pr,tr[q].rp,mid+1,R,k-tmp);",
            "    }",
            "}T1;",
            "int main(){",
            "//    freopen(\"123.txt\",\"r\",stdin);",
            "    in(n),in(T);",
            "    for(Re i=1;i<=n;++i)in(A[i]),b[i]=A[i];",
            "    sort(b+1,b+n+1),m=unique(b+1,b+n+1)-b-1;",
            "    for(Re i=1;i<=n;++i)T1.creat(pt[i],pt[i-1],1,m,lower_bound(b+1,b+m+1,A[i])-b);",
            "    while(T--)in(x),in(y),in(z),printf(\"%d\\n\",b[T1.ask(pt[y],pt[x-1],1,m,z)]);",
            "}",
            ""
        ],
        "description": "插入 主席树-区间第k小"
    }
}