//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.6Intersection of Permutations",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#include<queue>",
            "#define Re register int",
            "using namespace std;",
            "const int N=2e5+3;char opt[N];",
            "int x,y,z,w,n,T,op,A[N],rk[N];",
            "inline void in(int &x){",
            "    Re 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;",
            "}",
            "int pt[N<<1];",
            "struct Segment_Tree{",
            "\t#define pl tr[p].lp",
            "\t#define pr tr[p].rp",
            "\t#define mid ((L+R)>>1)",
            "\tint O;queue<int>Q;",
            "\tstruct QAQ{int S,lp,rp;}tr[N*200];",
            "\tinline int New(){// 新建节点",
            "\t\tif(Q.empty())return ++O;",
            "\t\tRe tmp=Q.front();Q.pop();",
            "\t\treturn tmp;",
            "\t}",
            "\tinline void del(Re &p){pl=pr=0,Q.push(p),p=0;}// 删除节点(注意取地址，初始化为1)",
            "\tinline void change(Re &p,Re L,Re R,Re w,Re v){",
            "\t    if(!p)p=New();tr[p].S+=v;",
            "\t    if(L==R){if(!tr[p].S)del(p);return;}",
            "\t    if(w<=mid)change(pl,L,mid,w,v);",
            "\t    else change(pr,mid+1,R,w,v);",
            "\t    if(!tr[p].S)del(p);",
            "\t}",
            "\tinline int 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\tRe 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}",
            "}TR;",
            "inline int ask(Re L,Re R,Re l,Re r){",
            "    Re ans=0;",
            "    for(Re i=R;i;i-=i&-i)ans+=TR.ask(pt[i],1,n,l,r);",
            "    for(Re i=L-1;i;i-=i&-i)ans-=TR.ask(pt[i],1,n,l,r);",
            "    return ans;",
            "}",
            "inline void add(Re x,Re w,Re v){",
            "    for(Re i=x;i<=n;i+=i&-i)TR.change(pt[i],1,n,w,v);",
            "}",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "    in(n),in(T);",
            "\tfor(Re i=1;i<=n;++i)in(x),rk[x]=i;",
            "\tfor(Re i=1;i<=n;++i)in(A[i]),A[i]=rk[A[i]],add(i,A[i],1);",
            "    while(T--){",
            "    \tin(op),in(x),in(y);",
            "    \tif(op>1)add(x,A[x],-1),add(y,A[y],-1),swap(A[x],A[y]),add(x,A[x],1),add(y,A[y],1);",
            "    \telse in(z),in(w),printf(\"%d\\n\",ask(z,w,x,y));",
            "\t}",
            "}",
            ""
        ],
        "description": "插入 树套树-单修权值，区间权值和+垃圾回收"
    }
}