#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int n,m,x,y,op,tot,tOt; int sum[300030],cov[300030]; int deep[300030],size[300030],top[300030],fa[300030],head[300030],dfn[300030]; struct edge{ int v,next; }e[300030]; void add(int x,int y){ e[++tot].v=y; e[tot].next=head[x]; head[x]=tot; } void dfs1(int cur){ if (fa[cur]==0) fa[cur]=cur; deep[cur]=deep[fa[cur]]+1; size[cur]=1; for (int i=head[cur];i;i=e[i].next) if (fa[cur]!=e[i].v){ fa[e[i].v]=cur; dfs1(e[i].v); size[cur]+=size[e[i].v]; } } void dfs2(int cur){ if (top[cur]==0) top[cur]=cur; dfn[cur]=++tOt; int t=0; for (int i=head[cur];i;i=e[i].next) t=(e[i].v!=fa[cur] && size[e[i].v]>size[t])?e[i].v:t; if (t==0) return; top[t]=top[cur],dfs2(t); for (int i=head[cur];i;i=e[i].next) if (e[i].v!=fa[cur] && e[i].v!=t) dfs2(e[i].v); } void update(int cur){ sum[cur]=sum[cur<<1]+sum[cur<<1|1]; } void pushdown(int cur,int l,int r,int mid){ cov[cur<<1]+=cov[cur]; cov[cur<<1|1]+=cov[cur]; sum[cur<<1]+=cov[cur]*(mid-l+1); sum[cur<<1|1]+=cov[cur]*(r-mid); cov[cur]=0; } void s_modify(int L,int R,int l,int r,int cur){ if (l>=L && r<=R){cov[cur]++; sum[cur]+=r-l+1; return;} int mid=(l+r)>>1; pushdown(cur,l,r,mid); if (L<=mid) s_modify(L,R,l,mid,cur<<1); if (R>mid) s_modify(L,R,mid+1,r,cur<<1|1); update(cur); } void modify(int x,int y){ for (;top[x]!=top[y];x=fa[top[x]]){ if (deep[top[x]]<deep[top[y]]) swap(x,y); s_modify(dfn[top[x]],dfn[x],1,n,1); } if (deep[x]>deep[y]) swap(x,y); s_modify(dfn[x],dfn[y],1,n,1); } int s_query(int L,int R,int l,int r,int cur){ if (l>=L && r<=R) return sum[cur]; int mid=(l+r)>>1,SUM=0; pushdown(cur,l,r,mid); SUM+=(L<=mid)?s_query(L,R,l,mid,cur<<1):0; SUM+=(R>mid)?s_query(L,R,mid+1,r,cur<<1|1):0; update(cur); return SUM; } int query(int x,int y){ int SUM=0; for (;top[x]!=top[y];x=fa[top[x]]){ if (deep[top[x]]<deep[top[y]]) swap(x,y); SUM+=s_query(dfn[top[x]],dfn[x],1,n,1); } if (deep[x]>deep[y]) swap(x,y); SUM+=s_query(dfn[x],dfn[y],1,n,1); return SUM; } int main(){ scanf("%d",&n); for (int i=1;i<n;i++){ scanf("%d%d",&x,&y); add(x,y); add(y,x); } dfs1(1); dfs2(1); scanf("%d",&m); for (int i=1;i<=m;i++){ scanf("%d%d%d",&op,&x,&y); if (op==1) modify(x,y); if (op==2) printf("%d\n",query(x,y)); } return 0; }
|