fork(32) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define MAX 50005
  4. #define mp make_pair
  5. #define pb push_back
  6. #define in(n) insert(n)
  7. #define mod 1000000007
  8. vector <int> vec[MAX];
  9. long long arr[MAX], val[MAX], lazy[MAX];
  10. int par[MAX], vis[MAX];
  11.  
  12. void dfs(int node) {
  13. vis[node] = 1;
  14. if (vec[node].size() == 0) {
  15. arr[node] += lazy[node];
  16. val[node] += lazy[node];
  17. arr[node] %= mod;
  18. val[node] %= mod;
  19. return ;
  20. }
  21. for (int i = 0 ; i < vec[node].size() ; i++) {
  22. if (vis[vec[node][i]] == 0) {
  23. dfs(vec[node][i]);
  24. lazy[node] += lazy[vec[node][i]];
  25. lazy[node] %= mod;
  26. val[node] += val[vec[node][i]];
  27. val[node] %= mod;
  28. }
  29. }
  30. val[node] += lazy[node];
  31. arr[node] += lazy[node];
  32. arr[node] %= mod;
  33. val[node] %= mod;
  34. }
  35.  
  36. int main() {
  37.  
  38. int t, m, n, q, x, y, i, j;
  39. char dec[10];
  40. scanf("%d", &t);
  41. while (t--) {
  42. scanf("%d %d %d", &n, &m, &q);
  43. for (i = 1 ; i <= n ; i++) {
  44. scanf("%d", &par[i]);
  45. vec[par[i]].pb(i);
  46. arr[i] = lazy[i] = vis[i] = val[i] = 0;
  47. }
  48. while (m--) {
  49. scanf(" %s %d %d", dec, &x, &y);
  50. if (strcmp(dec, "ADD") == 0) {
  51. arr[x] += y;
  52. val[x] += y;
  53. }
  54. else {
  55. lazy[x] += y;
  56. }
  57. }
  58. dfs(1);
  59. while (q--) {
  60. scanf(" %s %d", dec, &x);
  61. if (strcmp(dec, "VALTREE") == 0) {
  62. printf("%lld\n", val[x]);
  63. }
  64. else {
  65. printf("%lld\n", arr[x]);
  66. }
  67. }
  68. }
  69. return 0;
  70. }
Runtime error #stdin #stdout 0.02s 5872KB
stdin
Standard input is empty
stdout
Standard output is empty