-
Notifications
You must be signed in to change notification settings - Fork 0
/
directi22.cpp
97 lines (77 loc) · 1.46 KB
/
directi22.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <bits/stdc++.h>
using namespace std;
int mod = 1000000007;
struct cell
{
int val;
int parent;
int sub_tree_val;
cell() {
val = 0;
parent = 0;
sub_tree_val = 0;
}
};
void changeParentVals(cell arr[], int n, int x, int y) {
if (x == 0) {
return;
}
arr[x-1].sub_tree_val += y;
arr[x-1].sub_tree_val %= mod;
changeParentVals(arr, n, arr[x-1].parent, y);
}
void changeAllVals(cell arr[], int n, int x, int v, int sv) {
if (x == 0) {
return;
}
arr[x-1].val += v;
arr[x-1].val %= mod;
arr[x-1].sub_tree_val += sv;
changeAllVals(arr, n, arr[x-1].parent, v, (sv + v) % mod);
}
int main()
{
int t;
cin >> t;
while(t--) {
int n, m, q;
cin >> n >> m >> q;
cell arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i].parent;
}
for (int i = 0; i < m; i++ ) {
string str;
cin >> str;
int x;
int y;
cin >> x >> y;
if (str == "ADD") {
arr[x-1].val += y;
arr[x-1].val %= mod;
arr[x-1].sub_tree_val += y;
arr[x-1].sub_tree_val %= mod;
changeParentVals(arr, n, arr[x-1].parent, y);
}
if (str == "ADDUP") {
arr[x-1].val += y;
arr[x-1].val %= mod;
arr[x-1].sub_tree_val += y;
arr[x-1].sub_tree_val %= mod;
changeAllVals(arr, n, arr[x-1].parent, y, y);
}
}
for (int i = 0; i < q; ++i) {
string str;
cin >> str;
int x;
cin >> x;
if (str == "VAL") {
cout << arr[x-1].val << endl;
} else {
cout << arr[x-1].sub_tree_val << endl;
}
}
}
return 0;
}