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

{
    "Qtree4": {
        "prefix": "acm-4.3点分树+可删堆",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#include<queue>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=1e5+3,logN=18,inf=1e9;",
            "int n,m,x,y,z,o,T,Cnt,A[N],ans[N],head[N];char op;",
            "struct QAQ{int w,to,next;}a[N<<1];",
            "inline void add(Re x,Re y,Re z){a[++o].w=z,a[o].to=y,a[o].next=head[x],head[x]=o;}",
            "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 LCA{",
            "\tint O,lg[N<<1],fir[N],idx[N<<1],idd[N<<1],dis[N],deep[N],f[N<<1][20],ans[N<<1][20];",
            "\tinline void dfs(Re x,Re fa,Re w){",
            "\t\tidx[fir[x]=++O]=x,idd[O]=deep[x]=deep[fa]+1,dis[x]=dis[fa]+w;",
            "\t\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\t\tif((to=a[i].to)!=fa)dfs(to,x,a[i].w),idx[++O]=x,idd[O]=deep[x];",
            "\t}",
            "\tinline void build(Re rt){",
            "\t\tdfs(rt,0,0),lg[0]=-1;",
            "\t\tfor(Re i=1;i<=O;++i)f[i][0]=idd[i],ans[i][0]=idx[i],lg[i]=lg[i>>1]+1;",
            "\t\tfor(Re j=1;j<=logN;++j)",
            "\t\t\tfor(Re i=1;i+(1<<j)-1<=O;++i)",
            "\t\t\t\tif(f[i][j-1]<f[i+(1<<j-1)][j-1])f[i][j]=f[i][j-1],ans[i][j]=ans[i][j-1];",
            "\t\t\t\telse f[i][j]=f[i+(1<<j-1)][j-1],ans[i][j]=ans[i+(1<<j-1)][j-1];",
            "\t}",
            "\tinline int lca(Re x,Re y){",
            "\t\tRe L=fir[x],R=fir[y];",
            "\t\tif(L>R)swap(L,R);",
            "\t\tRe m=lg[R-L+1];",
            "\t\treturn f[L][m]<f[R-(1<<m)+1][m]?ans[L][m]:ans[R-(1<<m)+1][m];",
            "\t}",
            "}T0;",
            "inline int Dis(Re x,Re y){return T0.dis[x]+T0.dis[y]-(T0.dis[T0.lca(x,y)]<<1);}",
            "struct Queue{",
            "\tpriority_queue<int>Q,D;",
            "\tinline int top(){",
            "\t\twhile(!Q.empty()&&!D.empty()&&Q.top()==D.top())Q.pop(),D.pop();",
            "\t\treturn Q.empty()?-inf:Q.top();",
            "\t}",
            "\tinline int sec(){",
            "\t\tRe x=top();if(x==-inf)return x;",
            "\t\tQ.pop();Re ans=top();Q.push(x);",
            "\t\treturn ans;",
            "\t}",
            "\tinline void erase(Re x){if(Q.top()==x)Q.pop();else D.push(x);}",
            "\tinline void insert(Re x){Q.push(x);}",
            "\tinline int empty(){",
            "\t\twhile(!Q.empty()&&!D.empty()&&Q.top()==D.top())Q.pop(),D.pop();",
            "\t\treturn Q.empty();",
            "\t}",
            "}Ans,Q[N],dis[N];",
            "int rt,sum,Fa[N],vis[N],size[N],maxp[N];",
            "inline void getrt(Re x,Re fa){",
            "\tsize[x]=1,maxp[x]=0;",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif(!vis[to=a[i].to]&&to!=fa)",
            "\t\t\tgetrt(to,x),size[x]+=size[to],maxp[x]=max(maxp[x],size[to]);",
            "\tmaxp[x]=max(maxp[x],sum-size[x]);",
            "\tif(maxp[x]<maxp[rt])rt=x;",
            "}",
            "inline void getdis(Re x,Re fa,Re rt){",
            "\tdis[rt].insert(Dis(x,Fa[rt]));",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif(!vis[to=a[i].to]&&to!=fa)getdis(to,x,rt);",
            "}",
            "inline void sakura(Re x){",
            "\tRe now=sum;vis[x]=1,getdis(x,0,x);",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif(!vis[to=a[i].to]){",
            "\t\t\tsum=size[to]>size[x]?now-size[x]:size[to],maxp[rt=0]=inf,getrt(to,0);",
            "\t\t\tFa[to=rt]=x,sakura(rt),Q[x].insert(dis[to].top());",
            "\t\t}",
            "\tQ[x].insert(0),Ans.insert(ans[x]=max(0,Q[x].top()+Q[x].sec()));",
            "}",
            "inline void insert(Re x){",
            "\tAns.erase(ans[x]),Q[x].insert(0),Ans.insert(ans[x]=max(0,Q[x].top()+Q[x].sec()));",
            "\tfor(Re fa=x;Fa[fa];fa=Fa[fa]){",
            "\t\tRe pa=Fa[fa];",
            "\t\tAns.erase(ans[pa]);",
            "\t\tif(!dis[fa].empty())Q[pa].erase(dis[fa].top());",
            "\t\t",
            "\t\tdis[fa].insert(Dis(x,pa));",
            "\t\t",
            "\t\tQ[pa].insert(dis[fa].top());",
            "\t\tAns.insert(ans[pa]=max(0,Q[pa].top()+Q[pa].sec()));",
            "\t}",
            "}",
            "inline void erase(Re x){",
            "\tAns.erase(ans[x]),Q[x].erase(0),Ans.insert(ans[x]=max(0,Q[x].top()+Q[x].sec()));",
            "\tfor(Re fa=x;Fa[fa];fa=Fa[fa]){",
            "\t\tRe pa=Fa[fa];",
            "\t\tAns.erase(ans[pa]);",
            "\t\tQ[pa].erase(dis[fa].top());",
            "\t\t",
            "\t\tdis[fa].erase(Dis(x,pa));",
            "\t\t",
            "\t\tif(!dis[fa].empty())Q[pa].insert(dis[fa].top());",
            "\t\tAns.insert(ans[pa]=max(0,Q[pa].top()+Q[pa].sec()));",
            "\t}",
            "}",
            "inline void change(Re x){",
            "\tif(A[x])insert(x),A[x]=0,++Cnt;",
            "\telse erase(x),A[x]=1,--Cnt;",
            "}",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),m=n-1,Cnt=n;",
            "\twhile(m--)in(x),in(y),in(z),add(x,y,z),add(y,x,z);",
            "\tin(T),T0.build(1),sum=n,maxp[rt=0]=inf,getrt(1,0),sakura(rt);",
            "\twhile(T--){",
            "\t\tscanf(\" %c\",&op);",
            "\t\tif(op=='C')in(x),change(x);",
            "\t\telse if(!Cnt)puts(\"They have disappeared.\");",
            "\t\telse printf(\"%d\\n\",Cnt<2?0:Ans.top());",
            "\t}",
            "}",
            ""
        ],
        "description": "插入 Qtree4"
    }
}