#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, MOD;
cin >> n >> MOD;
vector<vector<int>> adj(n);
for (int i = 1; i < n; ++i) {
int a, b;
cin >> a >> b;
a -= 1, b -= 1;
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<int> parent(n, -1), depth(n);
auto dfs = [&](auto self, int v) -> void {
for (int to : adj[v]) {
if (to != parent[v]) {
depth[to] = depth[v] + 1;
parent[to] = v;
self(self, to);
}
}
};
dfs(dfs, 0);
vector<int> h(n);
for (int &x : h) {
cin >> x;
}
int q;
cin >> q;
constexpr int maxD = 40;
vector<array<int, maxD + 1>> kek(n);
for (int i = 0; i < n; ++i) {
fill(kek[i].begin(), kek[i].end(), 1);
}
for (int i = 0; i < q; ++i) {
int t, v;
cin >> t >> v;
v -= 1;
if (t == 1) {
int dist;
ll weight;
cin >> dist >> weight;
while (v != 0 && dist > 0) {
kek[v][dist] = weight * kek[v][dist] % MOD;
kek[v][dist - 1] = weight * kek[v][dist - 1] % MOD;
v = parent[v];
dist -= 1;
}
for (int j = 0; j <= dist; ++j) {
kek[v][j] = kek[v][j] * weight % MOD;
}
} else {
int dist = 0, ans = h[v];
while (v != -1 && dist <= maxD) {
ans = 1LL * ans * kek[v][dist] % MOD;
v = parent[v];
dist += 1;
}
cout << ans << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
508 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
3 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
256 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
567 ms |
49352 KB |
Output is correct |
3 |
Correct |
349 ms |
46132 KB |
Output is correct |
4 |
Correct |
513 ms |
52936 KB |
Output is correct |
5 |
Correct |
454 ms |
47848 KB |
Output is correct |
6 |
Correct |
340 ms |
47620 KB |
Output is correct |
7 |
Correct |
384 ms |
47956 KB |
Output is correct |
8 |
Correct |
289 ms |
48372 KB |
Output is correct |
9 |
Correct |
647 ms |
57332 KB |
Output is correct |
10 |
Correct |
404 ms |
53544 KB |
Output is correct |
11 |
Correct |
557 ms |
49280 KB |
Output is correct |
12 |
Correct |
371 ms |
46156 KB |
Output is correct |
13 |
Correct |
220 ms |
46820 KB |
Output is correct |
14 |
Correct |
241 ms |
46792 KB |
Output is correct |
15 |
Correct |
271 ms |
46716 KB |
Output is correct |
16 |
Correct |
261 ms |
46712 KB |
Output is correct |
17 |
Correct |
264 ms |
47272 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
567 ms |
49352 KB |
Output is correct |
3 |
Correct |
349 ms |
46132 KB |
Output is correct |
4 |
Correct |
513 ms |
52936 KB |
Output is correct |
5 |
Correct |
454 ms |
47848 KB |
Output is correct |
6 |
Correct |
340 ms |
47620 KB |
Output is correct |
7 |
Correct |
384 ms |
47956 KB |
Output is correct |
8 |
Correct |
289 ms |
48372 KB |
Output is correct |
9 |
Correct |
647 ms |
57332 KB |
Output is correct |
10 |
Correct |
404 ms |
53544 KB |
Output is correct |
11 |
Correct |
557 ms |
49280 KB |
Output is correct |
12 |
Correct |
371 ms |
46156 KB |
Output is correct |
13 |
Correct |
220 ms |
46820 KB |
Output is correct |
14 |
Correct |
241 ms |
46792 KB |
Output is correct |
15 |
Correct |
271 ms |
46716 KB |
Output is correct |
16 |
Correct |
261 ms |
46712 KB |
Output is correct |
17 |
Correct |
264 ms |
47272 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
611 ms |
49376 KB |
Output is correct |
25 |
Correct |
458 ms |
46200 KB |
Output is correct |
26 |
Correct |
570 ms |
55408 KB |
Output is correct |
27 |
Correct |
436 ms |
47696 KB |
Output is correct |
28 |
Correct |
348 ms |
47892 KB |
Output is correct |
29 |
Correct |
355 ms |
47808 KB |
Output is correct |
30 |
Correct |
276 ms |
48608 KB |
Output is correct |
31 |
Correct |
645 ms |
55124 KB |
Output is correct |
32 |
Correct |
357 ms |
53760 KB |
Output is correct |
33 |
Correct |
598 ms |
49660 KB |
Output is correct |
34 |
Correct |
365 ms |
46452 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
328 KB |
Output is correct |
40 |
Correct |
1 ms |
320 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
2 ms |
212 KB |
Output is correct |
46 |
Correct |
2 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
790 ms |
54696 KB |
Output is correct |
3 |
Correct |
808 ms |
52180 KB |
Output is correct |
4 |
Correct |
586 ms |
52936 KB |
Output is correct |
5 |
Correct |
484 ms |
46504 KB |
Output is correct |
6 |
Correct |
395 ms |
46672 KB |
Output is correct |
7 |
Correct |
312 ms |
46944 KB |
Output is correct |
8 |
Correct |
244 ms |
47264 KB |
Output is correct |
9 |
Correct |
618 ms |
52696 KB |
Output is correct |
10 |
Correct |
687 ms |
53564 KB |
Output is correct |
11 |
Correct |
556 ms |
46876 KB |
Output is correct |
12 |
Correct |
596 ms |
46392 KB |
Output is correct |
13 |
Correct |
420 ms |
47168 KB |
Output is correct |
14 |
Correct |
435 ms |
47484 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
324 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
659 ms |
54828 KB |
Output is correct |
3 |
Correct |
748 ms |
50704 KB |
Output is correct |
4 |
Correct |
552 ms |
52984 KB |
Output is correct |
5 |
Correct |
461 ms |
48088 KB |
Output is correct |
6 |
Correct |
347 ms |
48236 KB |
Output is correct |
7 |
Correct |
345 ms |
48140 KB |
Output is correct |
8 |
Correct |
275 ms |
48668 KB |
Output is correct |
9 |
Correct |
626 ms |
57084 KB |
Output is correct |
10 |
Correct |
673 ms |
54040 KB |
Output is correct |
11 |
Correct |
521 ms |
49708 KB |
Output is correct |
12 |
Correct |
602 ms |
46704 KB |
Output is correct |
13 |
Correct |
410 ms |
47308 KB |
Output is correct |
14 |
Correct |
430 ms |
47696 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
508 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
3 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
256 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
567 ms |
49352 KB |
Output is correct |
31 |
Correct |
349 ms |
46132 KB |
Output is correct |
32 |
Correct |
513 ms |
52936 KB |
Output is correct |
33 |
Correct |
454 ms |
47848 KB |
Output is correct |
34 |
Correct |
340 ms |
47620 KB |
Output is correct |
35 |
Correct |
384 ms |
47956 KB |
Output is correct |
36 |
Correct |
289 ms |
48372 KB |
Output is correct |
37 |
Correct |
647 ms |
57332 KB |
Output is correct |
38 |
Correct |
404 ms |
53544 KB |
Output is correct |
39 |
Correct |
557 ms |
49280 KB |
Output is correct |
40 |
Correct |
371 ms |
46156 KB |
Output is correct |
41 |
Correct |
220 ms |
46820 KB |
Output is correct |
42 |
Correct |
241 ms |
46792 KB |
Output is correct |
43 |
Correct |
271 ms |
46716 KB |
Output is correct |
44 |
Correct |
261 ms |
46712 KB |
Output is correct |
45 |
Correct |
264 ms |
47272 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
611 ms |
49376 KB |
Output is correct |
53 |
Correct |
458 ms |
46200 KB |
Output is correct |
54 |
Correct |
570 ms |
55408 KB |
Output is correct |
55 |
Correct |
436 ms |
47696 KB |
Output is correct |
56 |
Correct |
348 ms |
47892 KB |
Output is correct |
57 |
Correct |
355 ms |
47808 KB |
Output is correct |
58 |
Correct |
276 ms |
48608 KB |
Output is correct |
59 |
Correct |
645 ms |
55124 KB |
Output is correct |
60 |
Correct |
357 ms |
53760 KB |
Output is correct |
61 |
Correct |
598 ms |
49660 KB |
Output is correct |
62 |
Correct |
365 ms |
46452 KB |
Output is correct |
63 |
Correct |
1 ms |
344 KB |
Output is correct |
64 |
Correct |
1 ms |
332 KB |
Output is correct |
65 |
Correct |
1 ms |
340 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
1 ms |
328 KB |
Output is correct |
68 |
Correct |
1 ms |
320 KB |
Output is correct |
69 |
Correct |
1 ms |
212 KB |
Output is correct |
70 |
Correct |
1 ms |
212 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
212 KB |
Output is correct |
73 |
Correct |
2 ms |
212 KB |
Output is correct |
74 |
Correct |
2 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
340 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
790 ms |
54696 KB |
Output is correct |
80 |
Correct |
808 ms |
52180 KB |
Output is correct |
81 |
Correct |
586 ms |
52936 KB |
Output is correct |
82 |
Correct |
484 ms |
46504 KB |
Output is correct |
83 |
Correct |
395 ms |
46672 KB |
Output is correct |
84 |
Correct |
312 ms |
46944 KB |
Output is correct |
85 |
Correct |
244 ms |
47264 KB |
Output is correct |
86 |
Correct |
618 ms |
52696 KB |
Output is correct |
87 |
Correct |
687 ms |
53564 KB |
Output is correct |
88 |
Correct |
556 ms |
46876 KB |
Output is correct |
89 |
Correct |
596 ms |
46392 KB |
Output is correct |
90 |
Correct |
420 ms |
47168 KB |
Output is correct |
91 |
Correct |
435 ms |
47484 KB |
Output is correct |
92 |
Correct |
1 ms |
212 KB |
Output is correct |
93 |
Correct |
1 ms |
212 KB |
Output is correct |
94 |
Correct |
1 ms |
324 KB |
Output is correct |
95 |
Correct |
1 ms |
212 KB |
Output is correct |
96 |
Correct |
1 ms |
212 KB |
Output is correct |
97 |
Correct |
1 ms |
212 KB |
Output is correct |
98 |
Correct |
659 ms |
54828 KB |
Output is correct |
99 |
Correct |
748 ms |
50704 KB |
Output is correct |
100 |
Correct |
552 ms |
52984 KB |
Output is correct |
101 |
Correct |
461 ms |
48088 KB |
Output is correct |
102 |
Correct |
347 ms |
48236 KB |
Output is correct |
103 |
Correct |
345 ms |
48140 KB |
Output is correct |
104 |
Correct |
275 ms |
48668 KB |
Output is correct |
105 |
Correct |
626 ms |
57084 KB |
Output is correct |
106 |
Correct |
673 ms |
54040 KB |
Output is correct |
107 |
Correct |
521 ms |
49708 KB |
Output is correct |
108 |
Correct |
602 ms |
46704 KB |
Output is correct |
109 |
Correct |
410 ms |
47308 KB |
Output is correct |
110 |
Correct |
430 ms |
47696 KB |
Output is correct |
111 |
Correct |
1 ms |
212 KB |
Output is correct |
112 |
Correct |
1 ms |
212 KB |
Output is correct |
113 |
Correct |
1 ms |
212 KB |
Output is correct |
114 |
Correct |
1 ms |
212 KB |
Output is correct |
115 |
Correct |
1 ms |
340 KB |
Output is correct |
116 |
Correct |
508 ms |
47764 KB |
Output is correct |
117 |
Correct |
586 ms |
46284 KB |
Output is correct |
118 |
Correct |
561 ms |
55716 KB |
Output is correct |
119 |
Correct |
505 ms |
48080 KB |
Output is correct |
120 |
Correct |
371 ms |
48064 KB |
Output is correct |
121 |
Correct |
365 ms |
48412 KB |
Output is correct |
122 |
Correct |
267 ms |
48784 KB |
Output is correct |
123 |
Correct |
621 ms |
55728 KB |
Output is correct |
124 |
Correct |
693 ms |
51144 KB |
Output is correct |
125 |
Correct |
545 ms |
49132 KB |
Output is correct |
126 |
Correct |
595 ms |
46232 KB |
Output is correct |
127 |
Correct |
594 ms |
46068 KB |
Output is correct |
128 |
Correct |
492 ms |
46824 KB |
Output is correct |
129 |
Correct |
498 ms |
47320 KB |
Output is correct |