1 /**************************************************************
2 Problem: 3653
3 User: Tunix
4 Language: C++
5 Result: Accepted
6 Time:8964 ms
7 Memory:158752 kb
8 ****************************************************************/
9
10 //Huce #1 D
11 #include<vector>
12 #include<cstdio>
13 #include<cstring>
14 #include<cstdlib>
15 #include<iostream>
16 #include<algorithm>
17 #define rep(i,n) for(int i=0;i<n;++i)
18 #define F(i,j,n) for(int i=j;i<=n;++i)
19 #define D(i,j,n) for(int i=j;i>=n;--i)
20 #define pb push_back
21 using namespace std;
22 inline int getint(){
23 int v=0,sign=1; char ch=getchar();
24 while(ch<‘0‘||ch>‘9‘){ if (ch==‘-‘) sign=-1; ch=getchar();}
25 while(ch>=‘0‘&&ch<=‘9‘){ v=v*10+ch-‘0‘; ch=getchar();}
26 return v*sign;
27 }
28 const int N=3e5+10,INF=~0u>>2;
29 typedef long long LL;
30 /******************tamplate*********************/
31 int head[N],to[N<<1],next[N<<1],cnt;
32 void add(int x,int y){
33 to[++cnt]=y; next[cnt]=head[x]; head[x]=cnt;
34 to[++cnt]=x; next[cnt]=head[y]; head[y]=cnt;
35 }
36 int n,m,a[N],dep[N],sz[N],st[N],ed[N],root[N],tot,ct;
37 struct Tree{
38 int l,r; LL sum;
39 }t[N*30];
40 #define mid (l+r>>1)
41 void update(int &o,int l,int r,int pos,int w){
42 t[++ct]=t[o], o=ct, t[o].sum+=w;
43 if (l==r) return;
44 if (pos<=mid) update(t[o].l,l,mid,pos,w);
45 else update(t[o].r,mid+1,r,pos,w);
46 }
47 LL query(int i,int j,int l,int r,int ql,int qr){
48 if (ql<=l && qr>=r){
49 return t[j].sum-t[i].sum;
50 }else{
51 LL ans=0;
52 if (ql<=mid) ans+=query(t[i].l,t[j].l,l,mid,ql,qr);
53 if (qr> mid) ans+=query(t[i].r,t[j].r,mid+1,r,ql,qr);
54 return ans;
55 }
56 }
57 #undef mid
58 void dfs(int x){
59 a[st[x]=++tot]=x;
60 sz[x]=1;
61 for(int i=head[x];i;i=next[i])
62 if (st[to[i]]==0){
63 dep[to[i]]=dep[x]+1;
64 dfs(to[i]);
65 sz[x]+=sz[to[i]];
66 }
67 ed[x]=tot;
68 }
69
70 int main(){
71 #ifndef ONLINE_JUDGE
72 freopen("D.in","r",stdin);
73 freopen("D.out","w",stdout);
74 #endif
75 n=getint(); m=getint();
76 int x,y;
77 F(i,2,n){
78 x=getint(); y=getint();
79 add(x,y);
80 }
81 dfs(1);
82 F(i,1,n) update(root[i]=root[i-1],0,n,dep[a[i]],sz[a[i]]-1);
83 F(i,1,m){
84 x=getint(); y=getint();
85 LL ans=(LL)(sz[x]-1)*min(dep[x],y);
86 ans+=query(root[st[x]-1],root[ed[x]],0,n,
87 dep[x]+1,min(dep[x]+y,n));
88 printf("%lld\n",ans);
89 }
90 return 0;
91 }
92