//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.6mid",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=2e5+3;",
            "int n,m,x,y,p,T,op,A[N],pt[N];LL K;",
            "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 Segment_Tree{",
            "\t#define pl tr[p].lp",
            "\t#define pr tr[p].rp",
            "\t#define mid ((L+R)>>1)",
            "\tint O;",
            "\tstruct QAQ{int lp,rp;LL S;}tr[N*20];",
            "\tinline void pushup(Re p){tr[p].S=tr[pl].S+tr[pr].S;}",
            "\tinline void build(Re &p,Re L,Re R){",
            "\t\tp=++O;",
            "\t\tif(L==R){tr[p].S=A[L];return;}",
            "\t\tbuild(pl,L,mid),build(pr,mid+1,R);",
            "\t\tpushup(p);",
            "\t}",
            "\tinline void change(Re &p,Re L,Re R,Re x,Re v){",
            "\t\tif(!p)p=++O;",
            "\t\tif(L==R){tr[p].S+=v;return;}",
            "\t\tif(x<=mid)change(pl,L,mid,x,v);",
            "\t\telse change(pr,mid+1,R,x,v);",
            "\t\tpushup(p);",
            "\t}",
            "\tinline LL ask(Re p,Re L,Re R,Re l,Re r){",
            "\t\tif(!p)return 0;",
            "\t\tif(l<=L&&R<=r)return tr[p].S;",
            "\t\tLL ans=0;",
            "\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}",
            "\tinline int ask(Re p,Re L,Re R,LL k){",
            "\t\tif(!p||k>tr[p].S)return -1;",
            "\t\tif(L==R)return L;",
            "\t\tLL tmp=tr[pl].S;",
            "\t\tif(tmp>=k)return ask(pl,L,mid,k);",
            "\t\telse return ask(pr,mid+1,R,k-tmp);",
            "\t}",
            "\tinline int merge(Re p,Re q,Re L,Re R){",
            "\t\tif(!p||!q)return p+q;",
            "\t\tif(L==R){tr[p].S+=tr[q].S;return p;}",
            "\t\tpl=merge(pl,tr[q].lp,L,mid);",
            "\t\tpr=merge(pr,tr[q].rp,mid+1,R);",
            "\t\tpushup(p);return p;",
            "\t}",
            "\tinline void split(Re &p,Re &q,Re L,Re R,Re l,Re r){",
            "\t\tif(!q)return;",
            "\t\tif(l<=L&&R<=r){p=q,q=0;return;}",
            "\t\tp=++O;",
            "\t\tif(l<=mid)split(pl,tr[q].lp,L,mid,l,r);",
            "\t\tif(r>mid)split(pr,tr[q].rp,mid+1,R,l,r);",
            "\t\tpushup(p),pushup(q);",
            "\t}",
            "}T1;",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(m),in(T);",
            "\tfor(Re i=1;i<=m;++i)in(A[i]);",
            "\tT1.build(pt[n=1],1,m);",
            "\twhile(T--){",
            "\t\tin(op),in(p);",
            "\t\tif(op<1)in(x),in(y),T1.split(pt[++n],pt[p],1,m,x,y);",
            "\t\telse if(op<2)in(x),pt[p]=T1.merge(pt[p],pt[x],1,m),pt[x]=0;",
            "\t\telse if(op<3)in(x),in(y),T1.change(pt[p],1,m,y,x);",
            "\t\telse if(op<4)in(x),in(y),printf(\"%lld\\n\",T1.ask(pt[p],1,m,x,y));",
            "\t\telse scanf(\"%lld\",&K),printf(\"%d\\n\",T1.ask(pt[p],1,m,K));",
            "\t}",
            "}",
            ""
        ],
        "description": "插入 线段树分裂-分裂指定区间"
    }
}