1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
const int N=1e5+10; namespace MAIN{ int n; vector<int> G[N]; int fa[N],dep[N],sz[N],son[N],pos[N]; int top[N],dfn[N],low[N],idx; LL RET; inline int get_lca(res x,res y){ while(top[x]^top[y]){ if(dep[top[x]]<dep[top[y]])swap(x,y); x=fa[top[x]]; } if(dep[x]>dep[y])swap(x,y); return x; } namespace T{ LL sum[N<<2]; int laz[N<<2],len[N<<2]; inline void change(const res &rt,const res &val){ sum[rt]+=1ll*val*len[rt],laz[rt]+=val; } inline void pushdown(const res &rt){ if(!laz[rt])return; change(rt<<1,laz[rt]),change(rt<<1|1,laz[rt]),laz[rt]=0; } inline void pushup(const res &rt){ sum[rt]=sum[rt<<1]+sum[rt<<1|1]; } void build(res rt,res l,res r){ len[rt]=r-l+1,sum[rt]=0,laz[rt]=0; if(l==r)return; res mid=(l+r)>>1; build(rt<<1,l,mid),build(rt<<1|1,mid+1,r); } void modify(res rt,res l,res r,const res &L,const res &R,const res &val){ if(L<=l&&r<=R){change(rt,val);return;} pushdown(rt); res mid=(l+r)>>1; if(L<=mid)modify(rt<<1,l,mid,L,R,val); if(R>mid)modify(rt<<1|1,mid+1,r,L,R,val); pushup(rt); } LL query(res rt,res l,res r,const res &L,const res &R){ if(L<=l&&r<=R)return sum[rt]; pushdown(rt); res mid=(l+r)>>1; RG LL ret=0; if(L<=mid)ret=query(rt<<1,l,mid,L,R); if(R>mid)ret+=query(rt<<1|1,mid+1,r,L,R); pushup(rt); return ret; } } namespace LCT{ struct node{ int ch[2],fa,pre,suf,mndep,sz; }v[N]; inline bool isroot(const res &x){ return v[v[x].fa].ch[0]!=x&&v[v[x].fa].ch[1]!=x; } inline void push_up(const res &rt){ res ls=v[rt].ch[0],rs=v[rt].ch[1]; v[rt].sz=v[ls].sz+v[rs].sz+1; v[rt].pre=ls?v[ls].pre:rt; v[rt].suf=rs?v[rs].suf:rt; v[rt].mndep=min({dep[rt],v[ls].mndep,v[rs].mndep}); } inline void init(){ for(res i=1;i<=n;i++)v[i].ch[0]=v[i].ch[1]=v[i].fa=v[i].pre=v[i].suf=v[i].mndep=0,v[i].sz=1,push_up(i); v[0].mndep=n+1; } inline void rot(const res &x){ res p=v[x].fa,g=v[p].fa; RG bool d=v[p].ch[1]==x; if(!isroot(p))v[g].ch[v[g].ch[1]==p]=x; v[p].ch[d]=v[x].ch[d^1]; v[v[x].ch[d^1]].fa=p; v[x].ch[d^1]=p; v[x].fa=g,v[p].fa=x; push_up(p),push_up(x); } inline void splay(const res &x){ while(!isroot(x)){ res p=v[x].fa,g=v[p].fa; if(!isroot(p))rot(v[g].ch[0]==p^v[p].ch[0]==x?x:p); rot(x); } } inline void ins(const res &y){ if(!y)return; T::modify(1,1,n,dfn[v[y].pre],low[v[y].pre],-1); } inline void del(const res &y){ if(!y)return; T::modify(1,1,n,dfn[v[y].pre],low[v[y].pre],1); } inline void split(const res &x){ splay(1); if(!v[x].ch[0])return; res y=v[v[x].ch[0]].suf; splay(y); if(v[y].ch[1])RET+=1ll*v[v[y].ch[1]].mndep*v[v[y].ch[1]].sz; v[y].ch[1]=0,push_up(y); T::modify(1,1,n,dfn[x],low[x],-1); } inline int limit_access(res x,const res &Dep){ res y; for(y=0;x;y=x,x=v[x].fa){ splay(x); RET-=1ll*v[x].mndep*v[x].sz; del(y),ins(v[x].ch[1]); if(v[x].ch[1])RET+=1ll*v[v[x].ch[1]].mndep*v[v[x].ch[1]].sz; v[x].ch[1]=y,push_up(x); if(dep[v[x].pre]==Dep)return x; } return y; } inline void expose(res x){ split(1); res rt=limit_access(x,dep[1]); RET+=1ll*v[rt].mndep*v[rt].sz; } inline LL ask(const res &x,const res &y){ res lca=get_lca(x,y); return T::query(1,1,n,dfn[x],dfn[x])+T::query(1,1,n,dfn[y],dfn[y])-2*T::query(1,1,n,dfn[lca],dfn[lca]); } } LL a[N]; void dfs(res x,res fax){ fa[x]=fax,dep[x]=dep[fax]+1,sz[x]=1,son[x]=0,a[x]=0; for(auto tox:G[x]){ if(tox==fax)continue; dfs(tox,x),sz[x]+=sz[tox]; if(sz[tox]>sz[son[x]])son[x]=tox; a[x]+=a[tox]; } a[x]+=dep[x]; LCT::v[x].fa=fax,LCT::v[x].mndep=dep[x]; } void dfs_top(res x,res topx){ top[x]=topx,dfn[x]=++idx; if(son[x])dfs_top(son[x],topx); for(auto tox:G[x]){ if(tox==fa[x]||tox==son[x])continue; dfs_top(tox,tox); } low[x]=idx; } inline void MAIN(){ res T=read(); while(T--){ n=read(); for(res i=1;i<=n;i++)vector<int>().swap(G[i]); res q=read(); for(res i=1;i<n;i++){ res u=read(),v=read(); G[u].pb(v),G[v].pb(u); } LCT::init(),idx=0,dfs(1,0),dfs_top(1,1),T::build(1,1,n); RG LL RR=0; RET=0; for(res i=1;i<=n;i++)RET+=dep[i]; RR=RET; while(q--){ res opt=read(); if(opt==1){ res u=read(); read(); LCT::expose(u); } if(opt==2){ res u=read(),v=read(); printf("%lld\n",dep[u]+dep[v]-2*dep[get_lca(u,v)]-LCT::ask(u,v)); } if(opt==3){ res u=read(); printf("%lld\n",a[u]-1ll*sz[u]*dep[u]-T::query(1,1,n,dfn[u],low[u])+1ll*sz[u]*T::query(1,1,n,dfn[u],dfn[u])); } if(opt==4){ printf("%lld\n",RR-RET); } } } } }
|