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

{
    "线段树分治 (LCT维护MST) (城市建设)": {
        "prefix": "acm-2.6时间分治",
        "body": [
            "#pragma GCC optimize(3,\"Ofast\",\"inline\")",
            "#pragma GCC optimize(2)",
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=2e4+3,M=5e4+3;",
            "int n,m,x,y,z,T,op,X[N+M],Y[N+M],last[M];LL v[N+M],Ans[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 QWQ{int id,op;QWQ(Re ID=0,Re OP=0){id=ID,op=OP;}};",
            "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 size,Q[N+M];QWQ QQ[M*32];LL MST;",
            "\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),tr[x].fa=y;}",
            "\tinline void cut(Re x,Re y){makeroot(x),access(y),splay(y),tr[x].fa=tr[y].ps[0]=0,pushup(y);}",
            "\tinline void cut_(Re id){",
            "\t\tmakeroot(Y[id]),access(X[id]),splay(X[id]),tr[X[id]].ps[0]=tr[Y[id]].fa=tr[id].ps[0]=tr[id].fa=0,pushup(X[id]);",
            "\t}",
            "\tinline void link_(Re id){",
            "\t\tmakeroot(X[id]),makeroot(Y[id]),tr[X[id]].fa=tr[Y[id]].fa=id;",
            "\t}",
            "\tinline void MST_link(Re id){",
            "\t\tRe x=X[id],y=Y[id];makeroot(x);",
            "\t\tif(findroot(y)!=x)link(x,id),link(id,y),QQ[++size]=QWQ(id,0),MST+=v[id];",
            "\t\telse{",
            "\t\t\tRe mx=tr[y].mx;",
            "\t\t\tif(v[id]<v[mx]){",
            "//\t\t\t\tcut(X[mx],mx),cut(mx,Y[mx]),QQ[++size]=QWQ(mx,1);",
            "\t\t\t\tcut_(mx),QQ[++size]=QWQ(mx,1);",
            "\t\t\t\tlink(x,id),link(id,y),QQ[++size]=QWQ(id,0);",
            "//\t\t\t\tlink_(id),QQ[++size]=QWQ(id,0);",
            "\t\t\t\tMST+=v[id]-v[mx];",
            "\t\t\t}",
            "\t\t}",
            "\t}",
            "\tinline void back(Re to){",
            "\t\twhile(size>to){",
            "\t\t\tQWQ a=QQ[size--];Re id=a.id;",
            "\t\t\tif(a.op)link(id,Y[id]),link(X[id],id),MST+=v[id];",
            "//\t\t\telse cut(id,Y[id]),cut(X[id],id),MST-=v[id];",
            "//\t\t\tif(a.op)link_(id),MST+=v[id];",
            "\t\t\telse cut_(id),MST-=v[id];",
            "\t\t}",
            "\t}",
            "\t#undef pl",
            "\t#undef pr",
            "}LCT;",
            "int o,head[M<<2];",
            "struct QAQ{int id,next;LL v;}a[M*32];",
            "inline void add(Re x,Re id,LL v){a[++o].id=id,a[o].v=v,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 id,LL v){",
            "\t\tif(l>r)return;",
            "\t\tif(l<=L&&R<=r){add(p,id,v);return;}",
            "\t\tif(l<=mid)change(pl,L,mid,l,r,id,v);",
            "\t\tif(r>mid)change(pr,mid+1,R,l,r,id,v);",
            "\t}",
            "\tinline void dfs(Re p,Re L,Re R){",
            "\t\tRe tmp=LCT.size;",
            "\t\tfor(Re i=head[p];i;i=a[i].next)v[a[i].id]=a[i].v,LCT.MST_link(a[i].id);",
            "\t\tif(L==R)Ans[L]=LCT.MST;",
            "\t\telse dfs(pl,L,mid),dfs(pr,mid+1,R);",
            "\t\tLCT.back(tmp);",
            "\t}",
            "}T1;",
            "int main(){",
            "// \tfreopen(\"123.txt\",\"r\",stdin);",
            "// \tfreopen(\"my.txt\",\"w\",stdout);",
            "\tin(n),in(m),in(T);",
            "\tfor(Re i=1;i<=m;++i)in(X[i+n]),in(Y[i+n]),in(x),v[i+n]=x,last[i]=1;",
            "\tfor(Re i=1;i<=T;++i){",
            "\t\tin(x),in(y);",
            "\t\tT1.change(1,1,T,last[x],i-1,x+n,v[x+n]);",
            "\t\tlast[x]=i,v[x+n]=y;",
            "\t}",
            "\tfor(Re i=1;i<=m;++i)T1.change(1,1,T,last[i],T,i+n,v[i+n]);",
            "\tT1.dfs(1,1,T);",
            "\tfor(Re i=1;i<=T;++i)printf(\"%lld\\n\",Ans[i]);",
            "}",
            ""
        ],
        "description": "插入 线段树分治 (LCT维护MST) (城市建设)"
    }
}