#include <bits/stdc++.h>
using namespace std;
int main() {
int N, L; cin >> N >> L;
vector<vector<int>> adj(N);
for(int i = 0; i < N - 1; i++) {
int a, b; cin >> a >> b;
a--, b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<int> p(N);
function<void(int, int)> dfs = [&] (int s, int e) {
p[s] = e;
for(int u : adj[s]) {
if(u != e) {
dfs(u, s);
}
}
};
dfs(0, -1);
vector<int> h(N);
for(int i = 0; i < N; i++) cin >> h[i];
vector<vector<int>> mult(N, vector<int>(41, 1));
int Q; cin >> Q;
for(int i = 0; i < Q; i++) {
int t; cin >> t;
if(t == 1) {
int x, d, w; cin >> x >> d >> w; x--;
for(int i = 0; i <= d; i++) {
if(d - i > 0) mult[x][d - i - 1] = ((long long)mult[x][d - i - 1] * w) % L;
mult[x][d - i] = ((long long)mult[x][d - i] * w) % L;
if(p[x] == -1) {
for(int j = 0; j < d - i - 1; j++) mult[x][j] = ((long long)mult[x][j] * w) % L;
break;
}
x = p[x];
}
} else {
int x; cin >> x; x--;
int ans = h[x];
for(int i = 0; i <= 40; i++) {
ans = ((long long)ans * mult[x][i]) % L;
x = p[x];
if(x == -1) break;
}
cout << ans << endl;
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
600 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
2 ms |
600 KB |
Output is correct |
8 |
Correct |
3 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
452 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1119 ms |
64120 KB |
Output is correct |
3 |
Correct |
619 ms |
64340 KB |
Output is correct |
4 |
Correct |
1049 ms |
74036 KB |
Output is correct |
5 |
Correct |
963 ms |
64484 KB |
Output is correct |
6 |
Correct |
733 ms |
63892 KB |
Output is correct |
7 |
Correct |
744 ms |
64592 KB |
Output is correct |
8 |
Correct |
727 ms |
65012 KB |
Output is correct |
9 |
Correct |
1333 ms |
79148 KB |
Output is correct |
10 |
Correct |
723 ms |
78232 KB |
Output is correct |
11 |
Correct |
1072 ms |
64052 KB |
Output is correct |
12 |
Correct |
675 ms |
64444 KB |
Output is correct |
13 |
Correct |
609 ms |
64704 KB |
Output is correct |
14 |
Correct |
524 ms |
65416 KB |
Output is correct |
15 |
Correct |
508 ms |
64416 KB |
Output is correct |
16 |
Correct |
551 ms |
64920 KB |
Output is correct |
17 |
Correct |
566 ms |
65536 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1119 ms |
64120 KB |
Output is correct |
3 |
Correct |
619 ms |
64340 KB |
Output is correct |
4 |
Correct |
1049 ms |
74036 KB |
Output is correct |
5 |
Correct |
963 ms |
64484 KB |
Output is correct |
6 |
Correct |
733 ms |
63892 KB |
Output is correct |
7 |
Correct |
744 ms |
64592 KB |
Output is correct |
8 |
Correct |
727 ms |
65012 KB |
Output is correct |
9 |
Correct |
1333 ms |
79148 KB |
Output is correct |
10 |
Correct |
723 ms |
78232 KB |
Output is correct |
11 |
Correct |
1072 ms |
64052 KB |
Output is correct |
12 |
Correct |
675 ms |
64444 KB |
Output is correct |
13 |
Correct |
609 ms |
64704 KB |
Output is correct |
14 |
Correct |
524 ms |
65416 KB |
Output is correct |
15 |
Correct |
508 ms |
64416 KB |
Output is correct |
16 |
Correct |
551 ms |
64920 KB |
Output is correct |
17 |
Correct |
566 ms |
65536 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1161 ms |
64124 KB |
Output is correct |
25 |
Correct |
725 ms |
64852 KB |
Output is correct |
26 |
Correct |
1046 ms |
78664 KB |
Output is correct |
27 |
Correct |
970 ms |
64284 KB |
Output is correct |
28 |
Correct |
838 ms |
64096 KB |
Output is correct |
29 |
Correct |
811 ms |
64044 KB |
Output is correct |
30 |
Correct |
797 ms |
64876 KB |
Output is correct |
31 |
Correct |
1409 ms |
74792 KB |
Output is correct |
32 |
Correct |
733 ms |
78084 KB |
Output is correct |
33 |
Correct |
1091 ms |
64232 KB |
Output is correct |
34 |
Correct |
678 ms |
64412 KB |
Output is correct |
35 |
Correct |
2 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
2 ms |
444 KB |
Output is correct |
39 |
Correct |
1 ms |
344 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
2 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
2 ms |
344 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
440 KB |
Output is correct |
46 |
Correct |
1 ms |
344 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1452 ms |
75812 KB |
Output is correct |
3 |
Correct |
1707 ms |
72932 KB |
Output is correct |
4 |
Correct |
1392 ms |
73184 KB |
Output is correct |
5 |
Correct |
1000 ms |
61328 KB |
Output is correct |
6 |
Correct |
810 ms |
61428 KB |
Output is correct |
7 |
Correct |
810 ms |
61524 KB |
Output is correct |
8 |
Correct |
763 ms |
61896 KB |
Output is correct |
9 |
Correct |
1417 ms |
71920 KB |
Output is correct |
10 |
Correct |
1512 ms |
75348 KB |
Output is correct |
11 |
Correct |
1222 ms |
60812 KB |
Output is correct |
12 |
Correct |
1022 ms |
61808 KB |
Output is correct |
13 |
Correct |
675 ms |
62548 KB |
Output is correct |
14 |
Correct |
745 ms |
63448 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1490 ms |
73732 KB |
Output is correct |
3 |
Correct |
1735 ms |
70156 KB |
Output is correct |
4 |
Correct |
1264 ms |
71728 KB |
Output is correct |
5 |
Correct |
955 ms |
62772 KB |
Output is correct |
6 |
Correct |
839 ms |
62792 KB |
Output is correct |
7 |
Correct |
818 ms |
62696 KB |
Output is correct |
8 |
Correct |
730 ms |
63052 KB |
Output is correct |
9 |
Correct |
1544 ms |
77600 KB |
Output is correct |
10 |
Correct |
1748 ms |
76136 KB |
Output is correct |
11 |
Correct |
1204 ms |
63500 KB |
Output is correct |
12 |
Correct |
1022 ms |
62048 KB |
Output is correct |
13 |
Correct |
717 ms |
62720 KB |
Output is correct |
14 |
Correct |
775 ms |
63316 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
600 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
2 ms |
600 KB |
Output is correct |
8 |
Correct |
3 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
452 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1119 ms |
64120 KB |
Output is correct |
31 |
Correct |
619 ms |
64340 KB |
Output is correct |
32 |
Correct |
1049 ms |
74036 KB |
Output is correct |
33 |
Correct |
963 ms |
64484 KB |
Output is correct |
34 |
Correct |
733 ms |
63892 KB |
Output is correct |
35 |
Correct |
744 ms |
64592 KB |
Output is correct |
36 |
Correct |
727 ms |
65012 KB |
Output is correct |
37 |
Correct |
1333 ms |
79148 KB |
Output is correct |
38 |
Correct |
723 ms |
78232 KB |
Output is correct |
39 |
Correct |
1072 ms |
64052 KB |
Output is correct |
40 |
Correct |
675 ms |
64444 KB |
Output is correct |
41 |
Correct |
609 ms |
64704 KB |
Output is correct |
42 |
Correct |
524 ms |
65416 KB |
Output is correct |
43 |
Correct |
508 ms |
64416 KB |
Output is correct |
44 |
Correct |
551 ms |
64920 KB |
Output is correct |
45 |
Correct |
566 ms |
65536 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
2 ms |
344 KB |
Output is correct |
50 |
Correct |
1 ms |
344 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
1161 ms |
64124 KB |
Output is correct |
53 |
Correct |
725 ms |
64852 KB |
Output is correct |
54 |
Correct |
1046 ms |
78664 KB |
Output is correct |
55 |
Correct |
970 ms |
64284 KB |
Output is correct |
56 |
Correct |
838 ms |
64096 KB |
Output is correct |
57 |
Correct |
811 ms |
64044 KB |
Output is correct |
58 |
Correct |
797 ms |
64876 KB |
Output is correct |
59 |
Correct |
1409 ms |
74792 KB |
Output is correct |
60 |
Correct |
733 ms |
78084 KB |
Output is correct |
61 |
Correct |
1091 ms |
64232 KB |
Output is correct |
62 |
Correct |
678 ms |
64412 KB |
Output is correct |
63 |
Correct |
2 ms |
344 KB |
Output is correct |
64 |
Correct |
1 ms |
348 KB |
Output is correct |
65 |
Correct |
1 ms |
348 KB |
Output is correct |
66 |
Correct |
2 ms |
444 KB |
Output is correct |
67 |
Correct |
1 ms |
344 KB |
Output is correct |
68 |
Correct |
1 ms |
348 KB |
Output is correct |
69 |
Correct |
2 ms |
348 KB |
Output is correct |
70 |
Correct |
1 ms |
348 KB |
Output is correct |
71 |
Correct |
2 ms |
344 KB |
Output is correct |
72 |
Correct |
1 ms |
348 KB |
Output is correct |
73 |
Correct |
1 ms |
440 KB |
Output is correct |
74 |
Correct |
1 ms |
344 KB |
Output is correct |
75 |
Correct |
1 ms |
348 KB |
Output is correct |
76 |
Correct |
1 ms |
348 KB |
Output is correct |
77 |
Correct |
1 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
1452 ms |
75812 KB |
Output is correct |
80 |
Correct |
1707 ms |
72932 KB |
Output is correct |
81 |
Correct |
1392 ms |
73184 KB |
Output is correct |
82 |
Correct |
1000 ms |
61328 KB |
Output is correct |
83 |
Correct |
810 ms |
61428 KB |
Output is correct |
84 |
Correct |
810 ms |
61524 KB |
Output is correct |
85 |
Correct |
763 ms |
61896 KB |
Output is correct |
86 |
Correct |
1417 ms |
71920 KB |
Output is correct |
87 |
Correct |
1512 ms |
75348 KB |
Output is correct |
88 |
Correct |
1222 ms |
60812 KB |
Output is correct |
89 |
Correct |
1022 ms |
61808 KB |
Output is correct |
90 |
Correct |
675 ms |
62548 KB |
Output is correct |
91 |
Correct |
745 ms |
63448 KB |
Output is correct |
92 |
Correct |
1 ms |
344 KB |
Output is correct |
93 |
Correct |
1 ms |
348 KB |
Output is correct |
94 |
Correct |
1 ms |
348 KB |
Output is correct |
95 |
Correct |
1 ms |
348 KB |
Output is correct |
96 |
Correct |
2 ms |
348 KB |
Output is correct |
97 |
Correct |
1 ms |
348 KB |
Output is correct |
98 |
Correct |
1490 ms |
73732 KB |
Output is correct |
99 |
Correct |
1735 ms |
70156 KB |
Output is correct |
100 |
Correct |
1264 ms |
71728 KB |
Output is correct |
101 |
Correct |
955 ms |
62772 KB |
Output is correct |
102 |
Correct |
839 ms |
62792 KB |
Output is correct |
103 |
Correct |
818 ms |
62696 KB |
Output is correct |
104 |
Correct |
730 ms |
63052 KB |
Output is correct |
105 |
Correct |
1544 ms |
77600 KB |
Output is correct |
106 |
Correct |
1748 ms |
76136 KB |
Output is correct |
107 |
Correct |
1204 ms |
63500 KB |
Output is correct |
108 |
Correct |
1022 ms |
62048 KB |
Output is correct |
109 |
Correct |
717 ms |
62720 KB |
Output is correct |
110 |
Correct |
775 ms |
63316 KB |
Output is correct |
111 |
Correct |
1 ms |
344 KB |
Output is correct |
112 |
Correct |
1 ms |
348 KB |
Output is correct |
113 |
Correct |
1 ms |
348 KB |
Output is correct |
114 |
Correct |
1 ms |
348 KB |
Output is correct |
115 |
Correct |
2 ms |
344 KB |
Output is correct |
116 |
Correct |
1218 ms |
61924 KB |
Output is correct |
117 |
Correct |
1137 ms |
64620 KB |
Output is correct |
118 |
Correct |
1410 ms |
78456 KB |
Output is correct |
119 |
Correct |
1042 ms |
64316 KB |
Output is correct |
120 |
Correct |
843 ms |
64156 KB |
Output is correct |
121 |
Correct |
862 ms |
64592 KB |
Output is correct |
122 |
Correct |
782 ms |
64844 KB |
Output is correct |
123 |
Correct |
1493 ms |
75716 KB |
Output is correct |
124 |
Correct |
1687 ms |
73412 KB |
Output is correct |
125 |
Correct |
1177 ms |
63404 KB |
Output is correct |
126 |
Correct |
1000 ms |
64772 KB |
Output is correct |
127 |
Correct |
1071 ms |
65024 KB |
Output is correct |
128 |
Correct |
808 ms |
65484 KB |
Output is correct |
129 |
Correct |
836 ms |
66388 KB |
Output is correct |