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

{
    "线段树分治 (冰茶姬+线性基 查询两点最短异或和路径) (Shortest Path Queries)": {
        "prefix": "acm-2.6时间分治",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#include<stack>",
            "#include<map>",
            "#define LL long long",
            "#define Re register int",
            "#define mp make_pair",
            "using namespace std;",
            "const int N=2e5+3,logN=30;",
            "int x,y,n,m,T,op,X[N],Y[N],OP[N];LL Ans[N];map<pair<int,int>,int>last,z;",
            "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 Union{// 冰茶姬（并查集）",
            "\tstruct BK{int x,y,dx,dy,sx,sy;BK(Re X=0,Re d1=0,Re S1=0,Re Y=0,Re d2=0,Re S2=0){x=X,y=Y,sx=S1,sy=S2,dx=d1,dy=d2;}};",
            "\tint fa[N],dis[N],size[N];stack<BK>Q;",
            "\tinline int find(Re x){return fa[x]==x?x:find(fa[x]);}",
            "\tinline int Dis(Re x){return fa[x]==x?0:dis[x]^Dis(fa[x]);}",
            "\tinline void merge(Re x,Re y,Re w){// 启发式合并",
            "\t\tif(size[x]>size[y])swap(x,y);",
            "\t\tQ.push(BK(x,dis[x],size[x],y,dis[y],size[y]));",
            "\t\tfa[x]=y,dis[x]=w,size[y]+=size[x];",
            "\t}",
            "\tinline void back(Re to){// 撤销操作",
            "\t\twhile(int(Q.size())>to){",
            "\t\t\tBK a=Q.top();Re x=a.x,y=a.y;Q.pop();",
            "\t\t\tfa[x]=x,dis[x]=a.dx,size[x]=a.sx;",
            "\t\t\tfa[y]=y,dis[y]=a.dy,size[y]=a.sy;",
            "\t\t}",
            "\t}",
            "}TF;",
            "struct Line_JI{",
            "\tint p[31];stack<int>Q;",
            "\tinline void insert(Re x){",
            "\t\tfor(Re i=logN;i>=0;--i)",
            "\t\t\tif((x>>i)&1){",
            "\t\t\t\tif(!p[i]){p[i]=x,Q.push(i);break;}",
            "\t\t\t\tx^=p[i];",
            "\t\t\t}",
            "\t}",
            "\tinline int ask(Re x){for(Re i=logN;i>=0;--i)if(p[i])x=min(x,x^p[i]);return x;}",
            "\tinline void back(Re to){",
            "\t\twhile(int(Q.size())>to)p[Q.top()]=0,Q.pop();",
            "\t}",
            "}JI;",
            "int o,head[N<<2];",
            "struct QAQ{int x,y,w,next;}a[N*50];",
            "inline void add(Re x,Re y1,Re y2,Re w){a[++o].w=w,a[o].x=y1,a[o].y=y2,a[o].next=head[x],head[x]=o;}",
            "struct Segment_Tree{",
            "\t#define pl (p<<1)",
            "\t#define pr (p<<1|1)",
            "\t#define mid ((L+R)>>1)",
            "\tinline void change(Re p,Re L,Re R,Re l,Re r,Re x,Re y,Re v){",
            "\t\tif(l>r)return;",
            "\t\tif(l<=L&&R<=r){add(p,x,y,v);return;}",
            "\t\tif(l<=mid)change(pl,L,mid,l,r,x,y,v);",
            "\t\tif(r>mid)change(pr,mid+1,R,l,r,x,y,v);",
            "\t}",
            "\tinline void dfs(Re p,Re L,Re R){",
            "\t\tRe tmp1=TF.Q.size(),tmp2=JI.Q.size();",
            "\t\tfor(Re i=head[p];i;i=a[i].next){",
            "\t\t\tRe x=TF.find(a[i].x),y=TF.find(a[i].y),w=TF.Dis(a[i].x)^TF.Dis(a[i].y)^a[i].w;",
            "\t\t\tif(x!=y)TF.merge(x,y,w);",
            "\t\t\telse JI.insert(w);",
            "\t\t}",
            "\t\tif(L==R){if(OP[L])Ans[L]=JI.ask(TF.Dis(X[L])^TF.Dis(Y[L]));}",
            "\t\telse dfs(pl,L,mid),dfs(pr,mid+1,R);",
            "\t\tTF.back(tmp1),JI.back(tmp2);",
            "\t}",
            "}T1;",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(m);",
            "\tfor(Re i=1;i<=n;++i)TF.fa[i]=i,TF.size[i]=1;",
            "\tfor(Re i=1;i<=m;++i){",
            "\t\tin(x),in(y);if(x>y)swap(x,y);",
            "\t\tin(z[mp(x,y)]),last[mp(x,y)]=1;",
            "\t}",
            "\tin(T);",
            "\tfor(Re i=1;i<=T;++i){",
            "\t\tin(op),in(x),in(y);if(x>y)swap(x,y);",
            "\t\tif(op<2)in(z[mp(x,y)]),last[mp(x,y)]=i;",
            "\t\telse if(op<3)T1.change(1,1,T,last[mp(x,y)],i-1,x,y,z[mp(x,y)]),last.erase(last.find(mp(x,y)));",
            "\t\telse OP[i]=1,X[i]=x,Y[i]=y;",
            "\t}",
            "\tfor(map<pair<int,int>,int>::iterator it=last.begin();it!=last.end();++it)",
            "\t\tT1.change(1,1,T,it->second,T,it->first.first,it->first.second,z[it->first]);",
            "\tT1.dfs(1,1,T);",
            "\tfor(Re i=1;i<=T;++i)if(OP[i])printf(\"%lld\\n\",Ans[i]);",
            "}",
            ""
        ],
        "description": "插入 线段树分治 (冰茶姬+线性基 查询两点最短异或和路径) (Shortest Path Queries)"
    }
}