fork(3) download
  1. #include<bits/stdc++.h>
  2. #define REP(i,n) for(ll i=0;i<n;i++)
  3. #define ll long long
  4. using namespace std;
  5. #define MAX 100011
  6.  
  7. ll upd[MAX],val[MAX];
  8. vector <ll> parent(MAX);
  9. vector <set<ll> > child(MAX);
  10.  
  11.  
  12. ll cost(ll node, ll sofar,ll curr)
  13. {
  14. if(node==0)
  15. return curr;
  16.  
  17. ll ret=val[node]+curr;
  18.  
  19. if(upd[node]>0)
  20. {
  21. ret+=upd[node]*sofar;
  22.  
  23. //Push update value to val and children
  24. val[node]+=upd[node];
  25. for(auto it = child[node].begin();it!=child[node].end();it++)
  26. upd[*it]+=upd[node];
  27. upd[node] = 0;
  28. }
  29.  
  30. return cost(parent[node],sofar+1,ret);
  31. }
  32.  
  33. int main()
  34. {
  35. ios_base::sync_with_stdio(0);
  36. std::cin.tie(nullptr);
  37. ll n,m,a,x,y;
  38. cin>>n>>m;
  39. REP(i,n-1)
  40. {
  41. cin>>x>>y;
  42. child[x].insert(y);
  43. parent[y] = x;
  44. }
  45.  
  46. parent[1]=0;
  47. REP(i,m)
  48. {
  49. // cout<<"Query: "<<i+1<<": \n";
  50. cin>>a;
  51. assert(a==2 || a==1);
  52. if(a==1)
  53. {
  54. cin>>x>>y;
  55. upd[x]+=y;
  56. }
  57. else
  58. {
  59. cin>>y;
  60. cout<<cost(y,1,0)<<endl;
  61. }
  62. }
  63. }
  64.  
Runtime error #stdin #stdout 0s 8104KB
stdin
Standard input is empty
stdout
Standard output is empty