#include using namespace std; #define ALL(x) begin(x),end(x) #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<bool chmax(T &a,const T &b){if(abool chmin(T &a,const T &b){if(b ostream &operator<<(ostream &os,const vector&v){ for(int i=0;i<(int)v.size();i++) os< istream &operator>>(istream &is,vector&v){ for(T &x:v)is>>x; return is; } // graph template // ref : https://ei1333.github.io/library/graph/graph-template.cpp template struct Edge{ int from,to; T w; int idx; Edge()=default; Edge(int from,int to,T w=1,int idx=-1):from(from),to(to),w(w),idx(idx){} operator int() const{return to;} }; template struct Graph{ vector>> g; int V,E; Graph()=default; Graph(int n):g(n),V(n),E(0){} size_t size(){ return g.size(); } void resize(int k){ g.resize(k); } inline const vector> &operator[](int k)const{ return (g.at(k)); } inline vector> &operator[](int k){ return (g.at(k)); } void add_directed_edge(int from,int to,T cost=1){ g[from].emplace_back(from,to,cost,E++); } void add_edge(int from,int to,T cost=1){ g[from].emplace_back(from,to,cost,E); g[to].emplace_back(to,from,cost,E++); } void read(int m,int pad=-1,bool weighted=false,bool directed=false){ for(int i=0;i>u>>v; u+=pad,v+=pad; T w=T(1); if(weighted) cin>>w; if(directed) add_directed_edge(u,v,w); else add_edge(u,v,w); } } }; /* ref : https://ferin-tech.hatenablog.com/entry/2019/11/21/HL%E5%88%86%E8%A7%A3%E3%81%AE%E5%AE%9F%E8%A3%85 https://ei1333.github.io/library/graph/tree/heavy-light-decomposition.cpp HLD 各頂点から頂点を扱いやすいように並び替えた時の番号への写像を考える ! 辺属性で考えたい場合 頂点iから親方向に伸びている辺をpos[i]にSegment木などにセットし,各メソッドをedge=trueで扱う member head[i] : iの先頭 in[i], out[i] : i頂点以下の部分木のin, out method lca(u, v) : least common ancestor dist(u, v) : 距離 get_path(u, v) : 区間のvectorを返すので各区間でクエリを処理し,mergeすればよい get_subtree(u) : 部分木にあたる区間を返す pos(u) : 並び替えをした後,頂点uがどの場所へ移るかの射影 */ template struct HeavyLightDecomposition{ Graph g; vector sz,in,out,head,rev,par; vector dis; HeavyLightDecomposition(Graph g,int root=0): g(g),sz(g.V,0),in(g.V,0),out(g.V,0),head(g.V,0),rev(g.V,0),par(g.V,0),dis(g.V,0){ dfs1(-1,root); int t=0; dfs2(-1,root,t); } void dfs1(int pre,int cur){ par[cur]=pre; sz[cur]=1; if(!g[cur].empty() and g[cur][0]==pre) swap(g[cur][0],g[cur].back()); for(auto &e:g[cur])if(e!=pre){ dfs1(cur,e); sz[cur]+=sz[e]; dis[e]+=dis[cur]+e.w; if(sz[g[cur][0]]=in[u]) return rev[in[v]-k]; k-=in[v]-in[u]+1; v=par[u]; } } int lca(int u,int v){ for(;;v=par[head[v]]){ // 深い方から上げていく if(in[u]>in[v]) swap(u,v); if(head[u]==head[v]) return u; } } T dist(int u,int v){ return dis[u]+dis[v]-dis[lca(u,v)]*2; } // return ranges vector> get_path(int u,int v,bool edge=false){ vector> ret; for(;;v=par[head[v]]){ if(in[u]>in[v]) swap(u,v); if(head[u]==head[v]) break; ret.emplace_back(in[head[v]],in[v]+1); } // 最後の区間の始点がLCA ret.emplace_back(in[u]+edge,in[v]+1); return ret; } // return one range pair get_subtree(int u,bool edge=false){ return {in[u]+edge,out[u]}; } int pos(int u){ return in[u]; } }; template struct LazySegmentTree{ using F=function; using G=function; using H=function; private: int sz,height; vector data; vector lazy; // propagate lazy value -> data (node k) inline void propagate(int k){ if(lazy[k]!=OM0){ if(k0;k--) data[k]=f(data[2*k+0],data[2*k+1]); } void update(int a,int b,const OperatorMonoid &x){ update(a,b,x,1,0,sz); } Monoid query(int a,int b){ return query(a,b,1,0,sz); } Monoid operator[](const int &k){ return query(k,k+1); } }; using M=pair; using OM=ll; const M M1=M(0,0); const OM OM0=0; M segf(M a,M b){ return M(a.first+b.first,a.second+b.second); } M segg(M a,OM b){ return M(a.first+a.second*b,a.second); } ll segh(ll a,ll b){ return a+b; } signed main(){ int n;cin>>n; Graph g(n); rep(i,n-1){ int u,v;cin>>u>>v;u--,v--; g.add_edge(u,v); } HeavyLightDecomposition hld(g); LazySegmentTree seg(n,segf,segg,segh,M1,OM0); rep(i,n) seg.set(i,{0,1}); seg.build(); // rep(i,n){ // cout<>q; ll res=0; while(q--){ int u,v;cin>>u>>v;u--,v--; for(auto [l,r]:hld.get_path(u,v)){ seg.update(l,r,1); res+=seg.query(l,r).first; } } cout<