#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, P;
cin >> n >> P;
vector<vector<int>> adj(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int> h(n);
for (int u = 0; u < n; u++) {
cin >> h[u];
}
vector<int> par(n, -1);
vector<vector<int>> lazy(n, vector<int>(41, 1));
auto dfs = [&](auto self, int u, int p) -> void {
par[u] = p;
for (int v : adj[u]) {
if (v == p) continue;
self(self, v, u);
}
};
dfs(dfs, 0, -1);
auto update = [&](int u, int d, int w) -> void {
while (u != 0 && d >= 0) {
lazy[u][d] = 1LL * lazy[u][d] * w % P;
if (d > 0) lazy[u][d - 1] = 1LL * lazy[u][d - 1] * w % P;
d--;
u = par[u];
}
if (d >= 0) {
assert(u == 0);
for (int x = 0; x <= d; x++) {
lazy[u][x] = 1LL * lazy[u][x] * w % P;
}
}
};
auto get = [&](int u) -> int {
int res = h[u];
for (int d = 0; u != -1 && d <= 40; d++, u = par[u]) {
res = 1LL * res * lazy[u][d] % P;
}
return res;
};
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int type;
cin >> type;
if (type == 1) {
int u, d, w;
cin >> u >> d >> w;
u--;
update(u, d, w);
}
if (type == 2) {
int u;
cin >> u;
u--;
cout << get(u) << '\n';
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
756 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 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 |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
408 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 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 |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
533 ms |
63988 KB |
Output is correct |
3 |
Correct |
313 ms |
64336 KB |
Output is correct |
4 |
Correct |
572 ms |
67136 KB |
Output is correct |
5 |
Correct |
494 ms |
64104 KB |
Output is correct |
6 |
Correct |
311 ms |
63780 KB |
Output is correct |
7 |
Correct |
293 ms |
64340 KB |
Output is correct |
8 |
Correct |
270 ms |
65004 KB |
Output is correct |
9 |
Correct |
705 ms |
68692 KB |
Output is correct |
10 |
Correct |
352 ms |
68436 KB |
Output is correct |
11 |
Correct |
542 ms |
63880 KB |
Output is correct |
12 |
Correct |
327 ms |
64444 KB |
Output is correct |
13 |
Correct |
206 ms |
64548 KB |
Output is correct |
14 |
Correct |
205 ms |
65156 KB |
Output is correct |
15 |
Correct |
198 ms |
64400 KB |
Output is correct |
16 |
Correct |
208 ms |
65032 KB |
Output is correct |
17 |
Correct |
227 ms |
65620 KB |
Output is correct |
18 |
Correct |
1 ms |
344 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
533 ms |
63988 KB |
Output is correct |
3 |
Correct |
313 ms |
64336 KB |
Output is correct |
4 |
Correct |
572 ms |
67136 KB |
Output is correct |
5 |
Correct |
494 ms |
64104 KB |
Output is correct |
6 |
Correct |
311 ms |
63780 KB |
Output is correct |
7 |
Correct |
293 ms |
64340 KB |
Output is correct |
8 |
Correct |
270 ms |
65004 KB |
Output is correct |
9 |
Correct |
705 ms |
68692 KB |
Output is correct |
10 |
Correct |
352 ms |
68436 KB |
Output is correct |
11 |
Correct |
542 ms |
63880 KB |
Output is correct |
12 |
Correct |
327 ms |
64444 KB |
Output is correct |
13 |
Correct |
206 ms |
64548 KB |
Output is correct |
14 |
Correct |
205 ms |
65156 KB |
Output is correct |
15 |
Correct |
198 ms |
64400 KB |
Output is correct |
16 |
Correct |
208 ms |
65032 KB |
Output is correct |
17 |
Correct |
227 ms |
65620 KB |
Output is correct |
18 |
Correct |
1 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
486 ms |
64276 KB |
Output is correct |
25 |
Correct |
358 ms |
64344 KB |
Output is correct |
26 |
Correct |
529 ms |
68860 KB |
Output is correct |
27 |
Correct |
414 ms |
63936 KB |
Output is correct |
28 |
Correct |
296 ms |
64172 KB |
Output is correct |
29 |
Correct |
283 ms |
64060 KB |
Output is correct |
30 |
Correct |
248 ms |
64964 KB |
Output is correct |
31 |
Correct |
621 ms |
67096 KB |
Output is correct |
32 |
Correct |
345 ms |
68436 KB |
Output is correct |
33 |
Correct |
494 ms |
63904 KB |
Output is correct |
34 |
Correct |
318 ms |
64336 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
464 KB |
Output is correct |
40 |
Correct |
1 ms |
460 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
460 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
756 ms |
65564 KB |
Output is correct |
3 |
Correct |
890 ms |
65352 KB |
Output is correct |
4 |
Correct |
578 ms |
64836 KB |
Output is correct |
5 |
Correct |
501 ms |
61132 KB |
Output is correct |
6 |
Correct |
323 ms |
61268 KB |
Output is correct |
7 |
Correct |
298 ms |
61324 KB |
Output is correct |
8 |
Correct |
242 ms |
61892 KB |
Output is correct |
9 |
Correct |
761 ms |
64244 KB |
Output is correct |
10 |
Correct |
880 ms |
65768 KB |
Output is correct |
11 |
Correct |
524 ms |
60780 KB |
Output is correct |
12 |
Correct |
575 ms |
61600 KB |
Output is correct |
13 |
Correct |
387 ms |
62616 KB |
Output is correct |
14 |
Correct |
440 ms |
63144 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 |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
704 ms |
66588 KB |
Output is correct |
3 |
Correct |
923 ms |
64376 KB |
Output is correct |
4 |
Correct |
605 ms |
65472 KB |
Output is correct |
5 |
Correct |
494 ms |
62820 KB |
Output is correct |
6 |
Correct |
319 ms |
62924 KB |
Output is correct |
7 |
Correct |
300 ms |
62544 KB |
Output is correct |
8 |
Correct |
249 ms |
63052 KB |
Output is correct |
9 |
Correct |
683 ms |
67892 KB |
Output is correct |
10 |
Correct |
866 ms |
66356 KB |
Output is correct |
11 |
Correct |
547 ms |
63392 KB |
Output is correct |
12 |
Correct |
574 ms |
62164 KB |
Output is correct |
13 |
Correct |
370 ms |
62820 KB |
Output is correct |
14 |
Correct |
439 ms |
63368 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
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 |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
756 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 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 |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
408 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 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 |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
533 ms |
63988 KB |
Output is correct |
31 |
Correct |
313 ms |
64336 KB |
Output is correct |
32 |
Correct |
572 ms |
67136 KB |
Output is correct |
33 |
Correct |
494 ms |
64104 KB |
Output is correct |
34 |
Correct |
311 ms |
63780 KB |
Output is correct |
35 |
Correct |
293 ms |
64340 KB |
Output is correct |
36 |
Correct |
270 ms |
65004 KB |
Output is correct |
37 |
Correct |
705 ms |
68692 KB |
Output is correct |
38 |
Correct |
352 ms |
68436 KB |
Output is correct |
39 |
Correct |
542 ms |
63880 KB |
Output is correct |
40 |
Correct |
327 ms |
64444 KB |
Output is correct |
41 |
Correct |
206 ms |
64548 KB |
Output is correct |
42 |
Correct |
205 ms |
65156 KB |
Output is correct |
43 |
Correct |
198 ms |
64400 KB |
Output is correct |
44 |
Correct |
208 ms |
65032 KB |
Output is correct |
45 |
Correct |
227 ms |
65620 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 |
50 |
Correct |
1 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
486 ms |
64276 KB |
Output is correct |
53 |
Correct |
358 ms |
64344 KB |
Output is correct |
54 |
Correct |
529 ms |
68860 KB |
Output is correct |
55 |
Correct |
414 ms |
63936 KB |
Output is correct |
56 |
Correct |
296 ms |
64172 KB |
Output is correct |
57 |
Correct |
283 ms |
64060 KB |
Output is correct |
58 |
Correct |
248 ms |
64964 KB |
Output is correct |
59 |
Correct |
621 ms |
67096 KB |
Output is correct |
60 |
Correct |
345 ms |
68436 KB |
Output is correct |
61 |
Correct |
494 ms |
63904 KB |
Output is correct |
62 |
Correct |
318 ms |
64336 KB |
Output is correct |
63 |
Correct |
1 ms |
344 KB |
Output is correct |
64 |
Correct |
1 ms |
344 KB |
Output is correct |
65 |
Correct |
1 ms |
348 KB |
Output is correct |
66 |
Correct |
1 ms |
348 KB |
Output is correct |
67 |
Correct |
1 ms |
464 KB |
Output is correct |
68 |
Correct |
1 ms |
460 KB |
Output is correct |
69 |
Correct |
1 ms |
348 KB |
Output is correct |
70 |
Correct |
1 ms |
460 KB |
Output is correct |
71 |
Correct |
1 ms |
348 KB |
Output is correct |
72 |
Correct |
1 ms |
348 KB |
Output is correct |
73 |
Correct |
1 ms |
348 KB |
Output is correct |
74 |
Correct |
1 ms |
348 KB |
Output is correct |
75 |
Correct |
1 ms |
344 KB |
Output is correct |
76 |
Correct |
1 ms |
348 KB |
Output is correct |
77 |
Correct |
1 ms |
348 KB |
Output is correct |
78 |
Correct |
1 ms |
348 KB |
Output is correct |
79 |
Correct |
756 ms |
65564 KB |
Output is correct |
80 |
Correct |
890 ms |
65352 KB |
Output is correct |
81 |
Correct |
578 ms |
64836 KB |
Output is correct |
82 |
Correct |
501 ms |
61132 KB |
Output is correct |
83 |
Correct |
323 ms |
61268 KB |
Output is correct |
84 |
Correct |
298 ms |
61324 KB |
Output is correct |
85 |
Correct |
242 ms |
61892 KB |
Output is correct |
86 |
Correct |
761 ms |
64244 KB |
Output is correct |
87 |
Correct |
880 ms |
65768 KB |
Output is correct |
88 |
Correct |
524 ms |
60780 KB |
Output is correct |
89 |
Correct |
575 ms |
61600 KB |
Output is correct |
90 |
Correct |
387 ms |
62616 KB |
Output is correct |
91 |
Correct |
440 ms |
63144 KB |
Output is correct |
92 |
Correct |
1 ms |
348 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 |
1 ms |
344 KB |
Output is correct |
97 |
Correct |
1 ms |
348 KB |
Output is correct |
98 |
Correct |
704 ms |
66588 KB |
Output is correct |
99 |
Correct |
923 ms |
64376 KB |
Output is correct |
100 |
Correct |
605 ms |
65472 KB |
Output is correct |
101 |
Correct |
494 ms |
62820 KB |
Output is correct |
102 |
Correct |
319 ms |
62924 KB |
Output is correct |
103 |
Correct |
300 ms |
62544 KB |
Output is correct |
104 |
Correct |
249 ms |
63052 KB |
Output is correct |
105 |
Correct |
683 ms |
67892 KB |
Output is correct |
106 |
Correct |
866 ms |
66356 KB |
Output is correct |
107 |
Correct |
547 ms |
63392 KB |
Output is correct |
108 |
Correct |
574 ms |
62164 KB |
Output is correct |
109 |
Correct |
370 ms |
62820 KB |
Output is correct |
110 |
Correct |
439 ms |
63368 KB |
Output is correct |
111 |
Correct |
1 ms |
348 KB |
Output is correct |
112 |
Correct |
1 ms |
348 KB |
Output is correct |
113 |
Correct |
0 ms |
348 KB |
Output is correct |
114 |
Correct |
1 ms |
348 KB |
Output is correct |
115 |
Correct |
1 ms |
348 KB |
Output is correct |
116 |
Correct |
491 ms |
61880 KB |
Output is correct |
117 |
Correct |
611 ms |
64980 KB |
Output is correct |
118 |
Correct |
688 ms |
68476 KB |
Output is correct |
119 |
Correct |
470 ms |
64212 KB |
Output is correct |
120 |
Correct |
339 ms |
63748 KB |
Output is correct |
121 |
Correct |
372 ms |
64572 KB |
Output is correct |
122 |
Correct |
268 ms |
64896 KB |
Output is correct |
123 |
Correct |
610 ms |
67756 KB |
Output is correct |
124 |
Correct |
855 ms |
67156 KB |
Output is correct |
125 |
Correct |
497 ms |
63140 KB |
Output is correct |
126 |
Correct |
599 ms |
64328 KB |
Output is correct |
127 |
Correct |
602 ms |
64736 KB |
Output is correct |
128 |
Correct |
483 ms |
65560 KB |
Output is correct |
129 |
Correct |
500 ms |
65920 KB |
Output is correct |