For seniors chain sectional separate title, the result could not help but write a segment tree merger
First, not difficult to find: $ b $ is either $ a $ ancestry or a descendant of $ a $.
$ B $ is $ a $ ancestors get well, because the number is always equal to $ c $ $ size_a-1 $; $ b $ for either case future generations $ a $, then that is seeking $ a $ subtree and its depth does not exceed $ k $ points of $ size-1 $ and, according to later be inserted into the depth of the segment tree merge click.
#include<cstdio> #include<vector> #define For(i,A,B) for(i=(A);i<=(B);++i) using namespace std; typedef long long ll; const int N=300050; const int BUF=1<<21; char rB[BUF],*rS,*rT,wB[BUF+50]; int wp=-1; inline char gc(){return rS==rT&&(rT=(rS=rB)+fread(rB,1,BUF,stdin),rS==rT)?EOF:*rS++;} inline void flush(){fwrite(wB,1,wp+1,stdout);wp=-1;} inline int rd(){ char c=gc(); while(c<48||c>57)c=gc(); int x=c&15; for(c=gc();c>=48&&c<=57;c=gc())x=(x<<3)+(x<<1)+(c&15); return x; } short buf[25]; inline void wt(ll x){ if(wp>BUF)flush(); short l=-1; while(x>9){ buf[++l]=x%10; x/=10; } wB[++wp]=x|48; while(l>=0)wB[++wp]=buf[l--]|48; wB[++wp]='\n'; } int G[N],to[N<<1],nxt[N<<1],sz,dep[N],siz[N],rt[N],lc[N*20],rc[N*20],tot,n; ll sum[N*20],s[N]; vector<int> k[N],id[N]; inline int Min(int a,int b){return a<b?a:b;} inline void adde(int u,int v){ is [ ++ s] = v; nxt [s] = G [i], G [u] = s; is [ ++ s] = u; nxt [s] = G [V], G [v] = s; } void add(int &o,int L,int R,int x,int k){ if(!o)o=++tot; sum[o]+=k; if(L<R){ int M=L+R>>1; if(x<=M)add(lc[o],L,M,x,k); else add(rc[o],M+1,R,x,k); } } int merg(int u,int v){ if(!u||!v)return u|v; sum [u] + = sum [v]; I c [u] = go (lc [U] CL [V]) rc [u] = go (rc [u], rc [v]); return u; } ll query(int o,int L,int R,int x,int y){ if(!o)return 0ll; if(x<=L&&y>=R)return sum[o]; int M=L+R>>1; ll ans=0ll; if(x<=M)ans=query(lc[o],L,M,x,y); if(y>M)ans+=query(rc[o],M+1,R,x,y); return ans; } void dfs(int u,int fa){ int i,v; dep [u] = dep [fa] + 1 ; siz[u]=1; for(i=G[u];i;i=nxt[i])if((v=to[i])!=fa){ dfs (v, u); rt[u]=merg(rt[u],rt[v]); [u] + = [c]; } For(i,0,(int)k[u].size()-1)s[id[u][i]]=query(rt[u],1,n,dep[u]+1,Min(n,dep[u]+k[u][i]))+(ll)Min(dep[u]-1,k[u][i])*(siz[u]-1); add (rt [it], 1 , n, DEP [it], you [it] - 1 ); } int main(){ int q,i,u,v; n = rd (); q = rd (); For(i,2,n){ u = rd (); v = rd (); adde (u, v); } For(i,1,q){ u = rd (); v = rd (); k[u].push_back(v);id[u].push_back(i); } dfs(1,0); For(i,1,q)wt(s[i]); flush(); return 0; }