//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-4.3LCT动态维护MST",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=1003,M=1e5+3;",
            "int n,m,x,y,z,T,op,id,id_O,v[N+M],vis[N+M],Ans[M],ID[N][N];",
            "struct Query{int op,x,y;}Q[M];",
            "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;",
            "}",
            "struct Link_Cut_Tree{",
            "\t#define pl tr[p].ps[0]",
            "\t#define pr tr[p].ps[1]",
            "\t#define pf tr[p].fa",
            "\tint Q[N+M];",
            "\tstruct QAQ{int mx,fa,tag,ps[2];}tr[N+M];",
            "\tinline void pushup(Re p){",
            "\t\ttr[p].mx=p;",
            "\t\tif(pl&&v[tr[pl].mx]>v[tr[p].mx])tr[p].mx=tr[pl].mx;",
            "\t\tif(pr&&v[tr[pr].mx]>v[tr[p].mx])tr[p].mx=tr[pr].mx;",
            "\t}",
            "\tinline void updata(Re p){swap(pl,pr),tr[p].tag^=1;}",
            "\tinline void pushdown(Re p){",
            "\t\tif(tr[p].tag){",
            "\t\t\tif(pl)updata(pl);",
            "\t\t\tif(pr)updata(pr);",
            "\t\t\ttr[p].tag=0;",
            "\t\t}",
            "\t}",
            "\tinline int which(Re p){return tr[pf].ps[1]==p;}",
            "\tinline int nort(Re p){return tr[pf].ps[0]==p||tr[pf].ps[1]==p;}",
            "\tinline void connect(Re p,Re fa,Re o){tr[pf=fa].ps[o]=p;}",
            "\tinline void rotate(Re p){",
            "\t\tRe fa=pf,fas=which(p);",
            "\t\tRe pa=tr[fa].fa,pas=which(fa);",
            "\t\tRe x=tr[p].ps[fas^1];",
            "\t\tif(nort(fa))tr[pa].ps[pas]=p;pf=pa;",
            "\t\tconnect(x,fa,fas),connect(fa,p,fas^1);",
            "\t\tpushup(fa),pushup(p); ",
            "\t}",
            "\tinline void splay(Re p){",
            "\t\tRe x=p,t=0;Q[++t]=x;",
            "\t\twhile(nort(x))Q[++t]=x=tr[x].fa;",
            "\t\twhile(t)pushdown(Q[t--]);",
            "\t\tfor(Re fa;nort(p);rotate(p))",
            "\t\t\tif(nort(fa=pf))rotate(which(p)==which(fa)?fa:p);",
            "\t}",
            "\tinline void access(Re p){",
            "\t\tfor(Re son=0;p;son=p,p=pf)",
            "\t\t\tsplay(p),pr=son,pushup(p);",
            "\t}",
            "\tinline void makeroot(Re x){access(x),splay(x),updata(x);}",
            "\tinline int findroot(Re p){",
            "\t\taccess(p),splay(p),pushdown(p);",
            "\t\twhile(pl)pushdown(p=pl);",
            "\t\treturn p;",
            "\t}",
            "\tinline void split(Re x,Re y){makeroot(x),access(y),splay(y);}",
            "\tinline void link(Re x,Re y){makeroot(x);if(findroot(y)!=x)tr[x].fa=y;}",
            "\tinline void cut(Re x,Re y){",
            "\t\tmakeroot(x);",
            "\t\tif(findroot(y)==x&&tr[x].fa==y&&!tr[x].ps[1])",
            "\t\t\ttr[x].fa=tr[y].ps[0]=0,pushup(y);",
            "\t}",
            "\tinline void MST_link(Re x,Re y,Re id){",
            "\t\tmakeroot(x);",
            "\t\tif(findroot(y)!=x)link(x,id),link(id,y);",
            "\t\telse{",
            "\t\t\tsplit(x,y);Re mx=tr[y].mx;",
            "\t\t\tif(v[id]<v[mx]){",
            "\t\t\t\tsplay(mx);",
            "\t\t\t\ttr[tr[mx].ps[0]].fa=tr[tr[mx].ps[1]].fa=0;",
            "\t\t\t\ttr[mx].ps[0]=tr[mx].ps[1]=tr[mx].fa=0;",
            "\t\t\t\tlink(x,id),link(id,y);",
            "\t\t\t}",
            "\t\t}",
            "\t}",
            "\tinline int MST_path(Re x,Re y){split(x,y);return v[tr[y].mx];}",
            "}LCT;",
            "struct Kruscal{",
            "\tint fa[N];",
            "\tstruct QAQ{int x,y,z;inline bool operator<(const QAQ &O)const{return z<O.z;}}a[M];",
            "\tinline int find(Re x){return x==fa[x]?x:fa[x]=find(fa[x]);}",
            "\tinline void sakura(){",
            "\t\tfor(Re i=1;i<=n;++i)fa[i]=i;",
            "\t\tfor(Re i=1;i<=m;++i)in(x),in(y),in(z),v[ID[x][y]=ID[y][x]=++id_O+n]=z,a[i]=(QAQ){x,y,z};",
            "\t\tfor(Re i=1;i<=T;++i)in(op),in(x),in(y),vis[ID[x][y]]|=op>1,Q[i]=(Query){op,x,y};",
            "\t\tsort(a+1,a+m+1);",
            "\t\tfor(Re i=1,t=0;t<n-1&&i<=m;++i)",
            "\t\t\tif(!vis[ID[x=a[i].x][y=a[i].y]]&&find(x)!=find(y))",
            "\t\t\t\tfa[find(x)]=find(y),++t,LCT.MST_link(x,y,ID[x][y]);",
            "\t}",
            "}T1;",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(m),in(T),T1.sakura();",
            "\tfor(Re i=T;i>=1;--i){",
            "\t\tRe op=Q[i].op,x=Q[i].x,y=Q[i].y;",
            "\t\tif(op&1)Ans[++Ans[0]]=LCT.MST_path(x,y);",
            "\t\telse LCT.MST_link(x,y,ID[x][y]);",
            "\t}",
            "\tfor(Re i=Ans[0];i>=1;--i)printf(\"%d\\n\",Ans[i]);",
            "}",
            ""
        ],
        "description": "插入 水管局长"
    }
}