//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.6时间分治",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#include<stack>",
            "#include<map>",
            "#define LL long long",
            "#define Re register int",
            "#define mp(x,y) make_pair(x,y)",
            "using namespace std;",
            "const int N=1e4+3,M=2e5+3;",
            "int x,y,n,T,Ans[M];char ch[20];",
            "map<pair<int,int>,int>last;",
            "struct Query{int x,y,op;}Q[M];",
            "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,sizex,sizey;BK(Re X=0,Re S1=0,Re Y=0,Re S2=0){x=X,y=Y,sizex=S1,sizey=S2;}};",
            "\tint fa[N],size[N];stack<BK>Q;",
            "\tinline int find(Re x){return fa[x]==x?x:find(fa[x]);}",
            "\tinline void merge(Re x,Re y){// 启发式合并",
            "\t\tx=find(x),y=find(y);",
            "\t\tif(size[x]>size[y])swap(x,y);",
            "\t\tQ.push(BK(x,size[x],y,size[y]));",
            "\t\tfa[x]=y,size[y]+=size[x];",
            "\t}",
            "\tinline void back(Re to){// 撤销操作",
            "\t\twhile(Q.size()>to){",
            "\t\t\tBK a=Q.top();Q.pop();",
            "\t\t\tfa[a.x]=a.x,size[a.x]=a.sizex;",
            "\t\t\tfa[a.y]=a.y,size[a.y]=a.sizey;",
            "\t\t}",
            "\t}",
            "}TF;",
            "int o,head[M<<2];",
            "struct QAQ{int x,y,next;}a[M*20];",
            "inline void add(Re x,Re y1,Re y2){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){",
            "\t\tif(l<=L&&R<=r){add(p,x,y);return;}",
            "\t\tif(l<=mid)change(pl,L,mid,l,r,x,y);",
            "\t\tif(r>mid)change(pr,mid+1,R,l,r,x,y);",
            "\t}",
            "\tinline void dfs(Re p,Re L,Re R){",
            "\t\tRe tmp=TF.Q.size();",
            "\t\tfor(Re i=head[p];i;i=a[i].next)TF.merge(a[i].x,a[i].y);",
            "\t\tif(L==R){if(Q[L].op)Ans[L]=(TF.find(Q[L].x)==TF.find(Q[L].y));}",
            "\t\telse dfs(pl,L,mid),dfs(pr,mid+1,R);",
            "\t\tTF.back(tmp);",
            "\t}",
            "}T1;",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(T);",
            "\tfor(Re i=1;i<=n;++i)TF.fa[i]=i,TF.size[i]=1;",
            "\tfor(Re i=1;i<=T;++i){",
            "\t\tscanf(\"%s\",ch),in(x),in(y);if(x>y)swap(x,y);",
            "\t\tif(ch[0]=='Q')Q[i].op=1,Q[i].x=x,Q[i].y=y;",
            "\t\telse if(ch[0]=='C')last[mp(x,y)]=i;",
            "\t\telse T1.change(1,1,T,last[mp(x,y)],i,x,y),last.erase(mp(x,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);",
            "\tT1.dfs(1,1,T);",
            "\tfor(Re i=1;i<=T;++i)if(Q[i].op)puts(Ans[i]?\"Yes\":\"No\");",
            "}",
            ""
        ],
        "description": "插入 线段树分治 (冰茶姬 查询图的连通性) (洞穴勘测)"
    }
}