#pragma GCC optimize ("Ofast") #include using namespace std; template struct cLtraits_identity{ using type = T; } ; template using cLtraits_try_make_signed = typename conditional< is_integral::value, make_signed, cLtraits_identity >::type; template struct cLtraits_common_type{ using tS = typename cLtraits_try_make_signed::type; using tT = typename cLtraits_try_make_signed::type; using type = typename common_type::type; } ; void*wmem; char memarr[96000000]; template inline auto min_L(S a, T b) -> typename cLtraits_common_type::type{ return (typename cLtraits_common_type::type) a <= (typename cLtraits_common_type::type) b ? a : b; } template inline void walloc1d(T **arr, int x, void **mem = &wmem){ static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] ); (*arr)=(T*)(*mem); (*mem)=((*arr)+x); } template inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){ walloc1d(arr, x2-x1, mem); (*arr) -= x1; } inline int my_getchar(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = my_getchar(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } inline void rd(long long &x){ int k; int m=0; x=0; for(;;){ k = my_getchar(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar(a); } inline void wt_L(int x){ int s=0; int m=0; char f[10]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar('-'); } while(s--){ my_putchar(f[s]+'0'); } } inline void wt_L(unsigned x){ int s=0; char f[10]; while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } while(s--){ my_putchar(f[s]+'0'); } } inline void wt_L(long long x){ int s=0; int m=0; char f[20]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar('-'); } while(s--){ my_putchar(f[s]+'0'); } } inline void wt_L(unsigned long long x){ int s=0; char f[21]; while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } while(s--){ my_putchar(f[s]+'0'); } } int WRITER_DOUBLE_DIGIT = 15; inline int writerDigit_double(){ return WRITER_DOUBLE_DIGIT; } inline void writerDigit_double(int d){ WRITER_DOUBLE_DIGIT = d; } inline void wt_L(double x){ const int d = WRITER_DOUBLE_DIGIT; int k; int r; double v; if(x!=x || (x==x+1 && x==2*x)){ my_putchar('E'); my_putchar('r'); my_putchar('r'); return; } if(x < 0){ my_putchar('-'); x = -x; } x += 0.5 * pow(0.1, d); r = 0; v = 1; while(x >= 10*v){ v *= 10; r++; } while(r >= 0){ r--; k = floor(x / v); if(k >= 10){ k = 9; } if(k <= -1){ k = 0; } x -= k * v; v *= 0.1; my_putchar(k + '0'); } if(d > 0){ my_putchar('.'); v = 1; for(r=(0);r<(d);r++){ v *= 0.1; k = floor(x / v); if(k >= 10){ k = 9; } if(k <= -1){ k = 0; } x -= k * v; my_putchar(k + '0'); } } } inline void wt_L(const char c[]){ int i=0; for(i=0;c[i]!='\0';i++){ my_putchar(c[i]); } } inline void wt_L(string &x){ int i=0; for(i=0;x[i]!='\0';i++){ my_putchar(x[i]); } } template inline void arrInsert(const int k, int &sz, S a[], const S aval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } a[k] = aval; } template inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } for(i=sz-1;i>k;i--){ b[i] = b[i-1]; } a[k] = aval; b[k] = bval; } template inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } for(i=sz-1;i>k;i--){ b[i] = b[i-1]; } for(i=sz-1;i>k;i--){ c[i] = c[i-1]; } a[k] = aval; b[k] = bval; c[k] = cval; } template inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval, V d[], const V dval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } for(i=sz-1;i>k;i--){ b[i] = b[i-1]; } for(i=sz-1;i>k;i--){ c[i] = c[i-1]; } for(i=sz-1;i>k;i--){ d[i] = d[i-1]; } a[k] = aval; b[k] = bval; c[k] = cval; d[k] = dval; } template struct segtree{ int N; int logN; T*sum; T*mn; int*mnind; T*fixval; char*fixed; T*addval; void malloc(int maxN, int once = 0){ int i; for(i=1;i 1){ fixed[a*2] = fixed[a*2+1] = 1; fixval[a*2] = fixval[a*2+1] = fixval[a]; sum[a*2] = sum[a*2+1] = sz * fixval[a]; mn[a*2] = mn[a*2+1] = fixval[a]; mnind[a*2] = st; mnind[a*2+1] = st + sz; } else{ sum[a*2] = sum[a*2+1] = sz * fixval[a]; mn[a*2] = mn[a*2+1] = fixval[a]; mnind[a*2] = st; mnind[a*2+1] = st + sz; } fixed[a] = 0; addval[a] = 0; return; } if(addval[a] != 0){ if(sz > 1){ if(fixed[a*2]){ fixval[a*2] += addval[a]; } else{ addval[a*2] += addval[a]; } if(fixed[a*2+1]){ fixval[a*2+1] += addval[a]; } else{ addval[a*2+1] += addval[a]; } sum[a*2] += sz * addval[a]; sum[a*2+1] += sz * addval[a]; mn[a*2] += addval[a]; mn[a*2+1] += addval[a]; } else{ sum[a*2] += sz * addval[a]; sum[a*2+1] += sz * addval[a]; mn[a*2] += addval[a]; mn[a*2+1] += addval[a]; } addval[a] = 0; return; } } inline void push(int a){ int i; int aa = a - N; int nd; int sz; int st; for(i=logN;i;i--){ nd = a>>i; sz = 1<<(i-1); st = 2 * sz * (aa>>i); push_one(nd, sz, st); } } inline void build(int a){ int sz = 1; int st = a - N; while(a > 1){ if(a%2){ st += sz; } a /= 2; sz *= 2; if(fixed[a]){ sum[a] = sz * fixval[a]; mn[a] = fixval[a]; } else{ sum[a] = sum[a*2] + sum[a*2+1]; if(mn[a*2] <= mn[a*2+1]){ mn[a] = mn[a*2]; mnind[a] = mnind[a*2]; } else{ mn[a] = mn[a*2+1]; mnind[a] = mnind[a*2+1]; } if(addval[a] != 0){ mn[a] += addval[a]; sum[a] += sz * addval[a]; } } } } inline void change(int a, int b, T val){ int sz = 1; int aa; int bb; int st_a = a; int st_b = b; if(a >= b){ return; } aa = (a += N); bb = (b += N); push(a); push(b-1); if(a%2){ sum[a] = mn[a] = val; a++; st_a += sz; } if(b%2){ b--; st_b -= sz; sum[b] = mn[b] = val; } a /= 2; b /= 2; while(a < b){ sz *= 2; if(a%2){ fixed[a]=1; fixval[a]=val; sum[a] = sz * val; mn[a] = val; mnind[a] = st_a; a++; st_a += sz; } if(b%2){ b--; st_b -= sz; fixed[b]=1; fixval[b]=val; sum[b] = sz * val; mn[b] = val; mnind[b] = st_b; } a /= 2; b /= 2; } build(aa); build(bb-1); } inline void add(int a, int b, T val){ int sz = 1; int aa; int bb; if(a >= b){ return; } aa = (a += N); bb = (b += N); push(a); push(b-1); if(a%2){ sum[a] += val; mn[a] += val; a++; } if(b%2){ b--; sum[b] += val; mn[b] += val; } a /= 2; b /= 2; while(a < b){ sz *= 2; if(a%2){ if(fixed[a]){ fixval[a] += val; } else{ addval[a] += val; } sum[a] += sz * val; mn[a] += val; a++; } if(b%2){ b--; if(fixed[b]){ fixval[b] += val; } else{ addval[b] += val; } sum[b] += sz * val; mn[b] += val; } a /= 2; b /= 2; } build(aa); build(bb-1); } inline pair getMin(int a, int b){ pair res; pair tmp; int fga = 0; int fgb = 0; a += N; b += N; push(a); push(b-1); while(a < b){ if(a%2){ if(fga){ res =min_L(res, make_pair(mn[a], mnind[a])); } else{ res = make_pair(mn[a], mnind[a]); fga = 1; } a++; } if(b%2){ b--; if(fgb){ tmp =min_L(make_pair(mn[b], mnind[b]), tmp); } else{ tmp = make_pair(mn[b], mnind[b]); fgb = 1; } } a /= 2; b /= 2; } if(fga==1 && fgb==0){ return res; } if(fga==0 && fgb==1){ return tmp; } if(fga==1 && fgb==1){ res =min_L(res, tmp); return res; } return res; } inline T getMinVal(int a, int b){ T res; T tmp; int fga = 0; int fgb = 0; a += N; b += N; push(a); push(b-1); while(a < b){ if(a%2){ if(fga){ res =min_L(res, mn[a]); } else{ res = mn[a]; fga = 1; } a++; } if(b%2){ b--; if(fgb){ tmp =min_L(mn[b], tmp); } else{ tmp = mn[b]; fgb = 1; } } a /= 2; b /= 2; } if(fga==1 && fgb==0){ return res; } if(fga==0 && fgb==1){ return tmp; } if(fga==1 && fgb==1){ res =min_L(res, tmp); return res; } return res; } inline int getMinInd(int a, int b){ return getMin(a,b).second; } inline T getSum(int a, int b){ T res; T tmp; int fga = 0; int fgb = 0; a += N; b += N; push(a); push(b-1); while(a < b){ if(a%2){ if(fga){ res = res + sum[a]; } else{ res = sum[a]; fga = 1; } a++; } if(b%2){ b--; if(fgb){ tmp = sum[b] + tmp; } else{ tmp = sum[b]; fgb = 1; } } a /= 2; b /= 2; } if(fga==1 && fgb==0){ return res; } if(fga==0 && fgb==1){ return tmp; } if(fga==1 && fgb==1){ res = res + tmp; return res; } return res; } } ; struct graph{ int N; int*es; int**edge; void setEdgeRootedTree(int N__, int M, int A[], int B[], int root=0, int reorder=0, int cnv[] = NULL, void **mem = &wmem){ int i; int j; int k; int*dist; int*q; int qs; int qe; int*ind; void*tmem; N = N__; tmem = ((char*)(*mem)) + (sizeof(int) * N + 15) + (sizeof(int*) * N + 15) + (sizeof(int) * M + 15 * N); walloc1d(&es, N, mem); walloc1d(&edge, N, mem); for(i=(0);i<(N);i++){ es[i] = 0; } for(i=(0);i<(M);i++){ es[A[i]]++; es[B[i]]++; } for(i=(0);i<(N);i++){ walloc1d(&edge[i], es[i], &tmem); } for(i=(0);i<(N);i++){ es[i] = 0; } for(i=(0);i<(M);i++){ edge[A[i]][es[A[i]]++] = B[i]; edge[B[i]][es[B[i]]++] = A[i]; } walloc1d(&dist, N, &tmem); walloc1d(&q, N, &tmem); walloc1d(&ind, N, &tmem); if(cnv==NULL){ walloc1d(&cnv, N, &tmem); } for(i=(0);i<(N);i++){ dist[i] = -1; } dist[root] = 0; qs = qe = 0; q[qe++] = root; while(qs < qe){ i = q[qs++]; for(j=(0);j<(es[i]);j++){ k = edge[i][j]; if(dist[k]==-1){ dist[k] = dist[i] + 1; q[qe++] = k; } } } if(reorder == 0){ for(i=(0);i<(N);i++){ cnv[i] = i; } for(i=(0);i<(N);i++){ ind[i] = i; } } else{ for(i=(0);i<(N);i++){ cnv[i] = q[i]; } for(i=(0);i<(N);i++){ ind[cnv[i]] = i; } } for(i=(0);i<(N);i++){ es[i] = 0; } for(i=(0);i<(M);i++){ j = A[i]; k = B[i]; if(dist[j] > dist[k]){ swap(j, k); } es[ind[j]]++; } for(i=(0);i<(N);i++){ walloc1d(&edge[i], es[i], mem); } for(i=(0);i<(N);i++){ es[i] = 0; } for(i=(0);i<(M);i++){ j = A[i]; k = B[i]; if(dist[j] > dist[k]){ swap(j, k); } j = ind[j]; k = ind[k]; edge[j][es[j]++] = k; } } void SubTreeSize(int root, int res[], void *mem = wmem){ int i; int j; int k; int m; int*q; int qs = 0; int qe = 1; walloc1d(&q,N,&mem); for(i=(0);i<(N);i++){ res[i] = -1; } res[root] = 0; q[0] = root; while(qs < qe){ i = q[qs++]; for(j=(0);j<(es[i]);j++){ k = edge[i][j]; if(res[k]==0){ continue; } res[k] = 0; q[qe++] = k; } } for(m=(N)-1;m>=(0);m--){ i = q[m]; res[i] = 1; for(j=(0);j<(es[i]);j++){ k = edge[i][j]; res[i] += res[k]; } } } } ; graph g; int A[100000+10]; int B[100000+10]; int esz; int inorder[100000+10]; int exorder[100000+10]; int tsz[100000+10]; void add_edge(int x, int y){ arrInsert(esz, esz, A, x, B, y); } int N; int Q; int ts; void dfs(int x){ int PiIOrLma; inorder[x] = ts++; for(PiIOrLma=(0);PiIOrLma<(g.es[x]);PiIOrLma++){ auto&i = g.edge[x][PiIOrLma]; dfs(i); } exorder[x] = ts; } segtree t; int main(){ int i; wmem = memarr; t.walloc(300000+10); t.setN(300000+10); rd(N); rd(Q); { int GIHf_YD2; for(GIHf_YD2=(0);GIHf_YD2<(N-1);GIHf_YD2++){ rd(A[GIHf_YD2]);A[GIHf_YD2] += (-1); rd(B[GIHf_YD2]);B[GIHf_YD2] += (-1); } } g.setEdgeRootedTree(N, N-1, A, B); g.SubTreeSize(0, tsz); ts = 1; dfs(0); for(i=(0);i<(Q);i++){ int p; long long x; rd(p); rd(x); --p; t.add(inorder[p], exorder[p], x); wt_L(t.getSum(inorder[0],exorder[0])); wt_L('\n'); } return 0; } // cLay version 20210405-1 // --- original code --- // //no-unlocked // graph g; // int A[1d5+10], B[], esz; // int inorder[], exorder[], tsz[]; // // void add_edge(int x, int y){ // arrInsert(esz, esz, A, x, B, y); // } // int N, Q, ts; // // void dfs(int x){ // inorder[x] = ts++; // rep[g.edge[x]](i, g.es[x]) dfs(i); // exorder[x] = ts; // } // // segtree t; // // { // t.walloc(3d5+10); // t.setN(3d5+10); // rd(N, Q, (A--,B--)(N-1)); // g.setEdgeRootedTree(N, N-1, A, B); // g.SubTreeSize(0, tsz); // ts = 1; // dfs(0); // rep(i, Q){ // int p; ll x; // rd(p, x); // --p; // t.add(inorder[p], exorder[p], x); // wt(t.getSum(inorder[0],exorder[0])); // } // }