#yukicoder 399 動的な領主 #Heavy-Light decomposition 分解するだけ class HL_decomposition: def __init__(self, N, G, root = 0): #pos[v] = i, order[i] = v 頂点vのDFS順がi番目 #leader[i]: 深さdepth[i]の代表のDFS順 parent[i]: ひとつ根側のDFS順 self._N = N; self._G = G = [[v for v,*_ in S] for S in G] if N > 1 and not isinstance(G[0][0], int) else G; self.pos = pos = [-1] * N; self.order = order = [-1] * N; self.leader = leader = [-1] * N; self.depth = depth = [-1] * N; self.parent = parent = [-1] * N; size = [1] * N; Q = [(root, -1)] for now,back in Q: for nxt in G[now]: if nxt != back: Q.append((nxt, now)) while Q: now,back = Q.pop(); size[back] += size[now] if back != -1 else 0 Q.append((root, -1, 0, -1)) for i in range(N): now, back, d, t = Q.pop(); pos[now], order[i], parent[i], depth[i] = i, now, pos[back], d; leader[i] = t = t if t != -1 else i if size[now] > 1: s, v = 0, now for nxt in G[now]: if nxt == back: continue if s < size[nxt]: if s > 0: Q.append((v, now, d + 1, -1)) s, v = size[nxt], nxt else: Q.append((nxt, now, d + 1, -1)) Q.append((v, now, d, t)) def LCA(self, u, v): i, j = self.pos[u], self.pos[v]; c, d = self.depth[i], self.depth[j]; s, t = self.leader[i], self.leader[j] for c in range(c - 1, d - 1, -1): i = self.parent[s]; s = self.leader[i] for d in range(d - 1, c - 1, -1): j = self.parent[t]; t = self.leader[j] while s != t: i, j = self.parent[s], self.parent[t]; s, t = self.leader[i], self.leader[j] return self.order[ min(i, j) ] def find(self, index_u, v = None): #対応する列の添字を返す return self.pos[index_u] if v == None else max( self.pos[index_u], self.pos[v] ) def rev(self, Lt, Rt = None): #B = A[::-1] A[i], A[Lt,Rt)の対応添字を返す return self._N - 1 - Lt if Rt == None else (self._N - Rt, self._N - Lt) def fold(self, u, v): #u→vパスの作用値順を(to, go, LCA)の順に返す #to, goともに下から区間作用を行い、最後にf( f(to, A[LCA]), go )を行う #to: LCA ← uの作用区間[Lt,Rt) x ← f( x, fold(Lt, Rt) ) の順 反転列を使う #go: LCA → vの作用区間[Lt,Rt) y ← f( fold(Lt, Rt), y ) の順 i, j = self.pos[u], self.pos[v]; c, d = self.depth[i], self.depth[j]; s, t = self.leader[i], self.leader[j]; to, go = [], [] for c in range(c - 1, d - 1, -1): to.append((s, i + 1)); i = self.parent[s]; s = self.leader[i] for d in range(d - 1, c - 1, -1): go.append((t, j + 1)); j = self.parent[t]; t = self.leader[j] while s != t: to.append((s, i + 1)); i = self.parent[s]; s = self.leader[i]; go.append((t, j + 1)); j = self.parent[t]; t = self.leader[j] if i > j: to.append((j + 1, i + 1)) if i < j: go.append((i + 1, j + 1)) return to, go, min(i, j) #Lazy Segment Tree class LazySegmentTree: def __init__(self,n,e_node,e_lazy,node_f,lazy_f,ref_f): self._n=n; self._size=1; self._height=0 while self._size>h if self._lazy[i]!=self._e_lazy: self._lazy[i<<1|0]=self._lazy_f(self._lazy[i<<1|0],self._lazy[i]); self._lazy[i<<1|1]=self._lazy_f(self._lazy[i<<1|1],self._lazy[i]); self._node[i]=self._ref_lazy(i); self._lazy[i]=self._e_lazy def _update_from_bottom(self,index): index=(index+self._size)>>1 while index>0: self._node[index]=self._node_f(self._ref_lazy(index<<1|0),self._ref_lazy(index<<1|1)); index>>=1 def update(self,L,R,value): #区間更新: [L,R)を遅延valueで作用させる if L==R: return self._propagate_from_top(L); self._propagate_from_top(R-1); L_lazy=L+self._size; R_lazy=R+self._size while L_lazy>=1; R_lazy>>=1 self._update_from_bottom(L); self._update_from_bottom(R-1) def fold(self,L,R): #区間取得: [L,R)の値を得る if L==R: return self._e_node self._propagate_from_top(L); self._propagate_from_top(R-1); L+=self._size;R+=self._size; vL,vR=[self._e_node]*2 while L>=1; R>>=1 return self._node_f(vL,vR) #入力高速化 import sys input = sys.stdin.readline #入力受取 N = int(input()) G = [[] for _ in range(N)] for _ in range(N - 1): u,v = map(int,input().split()) G[u - 1].append(v - 1) G[v - 1].append(u - 1) #HLD HLD = HL_decomposition(N, G) #Lazy SegTreeで解く 交換法則が成立するのでセグ木は1本でOK #node: (現在の総和, 区間幅) def node_f(node1, node2): return ( node1[0] + node2[0], node1[1] + node2[1] ) def ref_f(node, lazy): return ( node[0] + node[1] * lazy, node[1] ) add = lambda x,y: x + y LST = LazySegmentTree(N, (0, 0), 0, node_f, add, ref_f) LST.build([(0, 1) for _ in range(N)]) #HLDでクエリを実行 ans = 0 for _ in range(int(input())): u,v = map(int,input().split()) u,v = u-1, v-1 #パスを取得 to, go, LCA = HLD.fold(u, v) #to, goの区間とLCAの一点に1を加算した後、区間和を加算 for Lt,Rt in to + go: LST.update(Lt, Rt, 1) ans += LST.fold(Lt, Rt)[0] LST.update(LCA, LCA + 1, 1) ans += LST.fold(LCA, LCA + 1)[0] print(ans)