//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.12P3332 K大数查询",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define Re register int",
            "#define LL long long",
            "using namespace std;",
            "const int N=5e4+3,M=5e4+3,inf=1e9;",
            "int n,x,T,Ans[M];",
            "struct QAQ{int l,r,op,id;LL k;}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 Segment_Tree{",
            "\t#define pl p<<1",
            "\t#define pr p<<1|1",
            "\t#define Mid (L+R>>1)",
            "\tstruct QAQ{int CL,add;LL S;}tr[N<<2];",
            "\tinline void pushup(Re p){tr[p].S=tr[pl].S+tr[pr].S;}",
            "\tinline void updata(Re p,Re L,Re R,Re v){tr[p].S+=v*(R-L+1),tr[p].add+=v;}",
            "\tinline void updata_(Re p){tr[p].add=tr[p].S=0,tr[p].CL=1;}",
            "\tinline void pushdown(Re p,Re L,Re R){",
            "\t\tif(tr[p].CL)updata_(pl),updata_(pr),tr[p].CL=0;",
            "\t\tif(tr[p].add)updata(pl,L,Mid,tr[p].add),updata(pr,Mid+1,R,tr[p].add),tr[p].add=0;",
            "\t}",
            "\tinline void change(Re p,Re L,Re R,Re l,Re r,Re v){",
            "\t\tif(l<=L&&R<=r){updata(p,L,R,v);return;}",
            "\t\tpushdown(p,L,R);",
            "\t\tif(l<=Mid)change(pl,L,Mid,l,r,v);",
            "\t\tif(r>Mid)change(pr,Mid+1,R,l,r,v);",
            "\t\tpushup(p);",
            "\t}",
            "\tinline LL ask(Re p,Re L,Re R,Re l,Re r){",
            "\t\tif(l<=L&&R<=r)return tr[p].S;",
            "\t\tLL ans=0;pushdown(p,L,R);",
            "\t\tif(l<=Mid)ans+=ask(pl,L,Mid,l,r);",
            "\t\tif(r>Mid)ans+=ask(pr,Mid+1,R,l,r);",
            "\t\treturn ans;",
            "\t}",
            "}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];",
            "\t\t\telse Q2[++m2]=Q[i],T1.change(1,1,n,Q[i].l,Q[i].r,1);",
            "\t\t}",
            "\t\telse{",
            "\t\t\tLL tmp=T1.ask(1,1,n,Q[i].l,Q[i].r);",
            "\t\t\tif(Q[i].k<=tmp)Q2[++m2]=Q[i];",
            "\t\t\telse Q[i].k-=tmp,Q1[++m1]=Q[i];",
            "\t\t}",
            "\tT1.updata_(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<=T;++i){",
            "\t\tin(Q[i].op),in(Q[i].l),in(Q[i].r),in(x),Q[i].k=x;",
            "\t\tif(Q[i].op>1)Q[i].id=++Ans[0];",
            "\t}",
            "\tsakura(-n,n,1,T);",
            "\tfor(Re i=1;i<=Ans[0];++i)printf(\"%d\\n\",Ans[i]);",
            "}",
            ""
        ],
        "description": "插入 整体二分"
    }
}