#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef unsigned long long ull;
typedef __uint128_t L;
struct FastMod {
ull b, m;
FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
ull reduce(ull a) {
ull q = (ull)((L(m) * a) >> 64);
ull r = a - q * b; // can be proven that 0 <= r < 2*b
return r >= b ? r - b : r;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, MOD;
cin >> n >> MOD;
FastMod F(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] = F.reduce(weight * kek[v][dist]);
kek[v][dist - 1] = F.reduce(weight * kek[v][dist - 1]);
v = parent[v];
dist -= 1;
}
for (int j = 0; j <= dist; ++j) {
kek[v][j] = F.reduce(kek[v][j] * weight);
}
} else {
int dist = 0;
ll ans = h[v];
while (v != -1 && dist <= maxD) {
ans = F.reduce(ans * kek[v][dist]);
v = parent[v];
dist += 1;
}
cout << ans << '\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
312 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
584 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
224 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
328 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 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 |
320 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
535 ms |
49716 KB |
Output is correct |
3 |
Correct |
325 ms |
46540 KB |
Output is correct |
4 |
Correct |
542 ms |
53348 KB |
Output is correct |
5 |
Correct |
424 ms |
48256 KB |
Output is correct |
6 |
Correct |
298 ms |
48076 KB |
Output is correct |
7 |
Correct |
290 ms |
48524 KB |
Output is correct |
8 |
Correct |
231 ms |
48836 KB |
Output is correct |
9 |
Correct |
678 ms |
57664 KB |
Output is correct |
10 |
Correct |
351 ms |
54044 KB |
Output is correct |
11 |
Correct |
515 ms |
49848 KB |
Output is correct |
12 |
Correct |
366 ms |
46600 KB |
Output is correct |
13 |
Correct |
206 ms |
47248 KB |
Output is correct |
14 |
Correct |
215 ms |
47232 KB |
Output is correct |
15 |
Correct |
226 ms |
47116 KB |
Output is correct |
16 |
Correct |
229 ms |
47052 KB |
Output is correct |
17 |
Correct |
231 ms |
47688 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
535 ms |
49716 KB |
Output is correct |
3 |
Correct |
325 ms |
46540 KB |
Output is correct |
4 |
Correct |
542 ms |
53348 KB |
Output is correct |
5 |
Correct |
424 ms |
48256 KB |
Output is correct |
6 |
Correct |
298 ms |
48076 KB |
Output is correct |
7 |
Correct |
290 ms |
48524 KB |
Output is correct |
8 |
Correct |
231 ms |
48836 KB |
Output is correct |
9 |
Correct |
678 ms |
57664 KB |
Output is correct |
10 |
Correct |
351 ms |
54044 KB |
Output is correct |
11 |
Correct |
515 ms |
49848 KB |
Output is correct |
12 |
Correct |
366 ms |
46600 KB |
Output is correct |
13 |
Correct |
206 ms |
47248 KB |
Output is correct |
14 |
Correct |
215 ms |
47232 KB |
Output is correct |
15 |
Correct |
226 ms |
47116 KB |
Output is correct |
16 |
Correct |
229 ms |
47052 KB |
Output is correct |
17 |
Correct |
231 ms |
47688 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
519 ms |
49728 KB |
Output is correct |
25 |
Correct |
360 ms |
46540 KB |
Output is correct |
26 |
Correct |
529 ms |
55884 KB |
Output is correct |
27 |
Correct |
415 ms |
48180 KB |
Output is correct |
28 |
Correct |
323 ms |
48384 KB |
Output is correct |
29 |
Correct |
287 ms |
48336 KB |
Output is correct |
30 |
Correct |
233 ms |
49124 KB |
Output is correct |
31 |
Correct |
665 ms |
55320 KB |
Output is correct |
32 |
Correct |
360 ms |
53732 KB |
Output is correct |
33 |
Correct |
539 ms |
49620 KB |
Output is correct |
34 |
Correct |
333 ms |
46448 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
320 KB |
Output is correct |
43 |
Correct |
1 ms |
324 KB |
Output is correct |
44 |
Correct |
1 ms |
324 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
320 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 |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
751 ms |
54724 KB |
Output is correct |
3 |
Correct |
985 ms |
51824 KB |
Output is correct |
4 |
Correct |
677 ms |
52760 KB |
Output is correct |
5 |
Correct |
529 ms |
46248 KB |
Output is correct |
6 |
Correct |
351 ms |
46268 KB |
Output is correct |
7 |
Correct |
310 ms |
46428 KB |
Output is correct |
8 |
Correct |
234 ms |
46844 KB |
Output is correct |
9 |
Correct |
740 ms |
52300 KB |
Output is correct |
10 |
Correct |
987 ms |
53204 KB |
Output is correct |
11 |
Correct |
562 ms |
46680 KB |
Output is correct |
12 |
Correct |
665 ms |
46156 KB |
Output is correct |
13 |
Correct |
417 ms |
46668 KB |
Output is correct |
14 |
Correct |
455 ms |
47208 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
769 ms |
54648 KB |
Output is correct |
3 |
Correct |
987 ms |
50308 KB |
Output is correct |
4 |
Correct |
690 ms |
52556 KB |
Output is correct |
5 |
Correct |
523 ms |
47724 KB |
Output is correct |
6 |
Correct |
338 ms |
47896 KB |
Output is correct |
7 |
Correct |
328 ms |
47824 KB |
Output is correct |
8 |
Correct |
234 ms |
48336 KB |
Output is correct |
9 |
Correct |
750 ms |
56716 KB |
Output is correct |
10 |
Correct |
973 ms |
53660 KB |
Output is correct |
11 |
Correct |
577 ms |
49248 KB |
Output is correct |
12 |
Correct |
644 ms |
46128 KB |
Output is correct |
13 |
Correct |
413 ms |
46824 KB |
Output is correct |
14 |
Correct |
444 ms |
47332 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 |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
312 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
584 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
224 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
328 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 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 |
320 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
535 ms |
49716 KB |
Output is correct |
31 |
Correct |
325 ms |
46540 KB |
Output is correct |
32 |
Correct |
542 ms |
53348 KB |
Output is correct |
33 |
Correct |
424 ms |
48256 KB |
Output is correct |
34 |
Correct |
298 ms |
48076 KB |
Output is correct |
35 |
Correct |
290 ms |
48524 KB |
Output is correct |
36 |
Correct |
231 ms |
48836 KB |
Output is correct |
37 |
Correct |
678 ms |
57664 KB |
Output is correct |
38 |
Correct |
351 ms |
54044 KB |
Output is correct |
39 |
Correct |
515 ms |
49848 KB |
Output is correct |
40 |
Correct |
366 ms |
46600 KB |
Output is correct |
41 |
Correct |
206 ms |
47248 KB |
Output is correct |
42 |
Correct |
215 ms |
47232 KB |
Output is correct |
43 |
Correct |
226 ms |
47116 KB |
Output is correct |
44 |
Correct |
229 ms |
47052 KB |
Output is correct |
45 |
Correct |
231 ms |
47688 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
328 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
519 ms |
49728 KB |
Output is correct |
53 |
Correct |
360 ms |
46540 KB |
Output is correct |
54 |
Correct |
529 ms |
55884 KB |
Output is correct |
55 |
Correct |
415 ms |
48180 KB |
Output is correct |
56 |
Correct |
323 ms |
48384 KB |
Output is correct |
57 |
Correct |
287 ms |
48336 KB |
Output is correct |
58 |
Correct |
233 ms |
49124 KB |
Output is correct |
59 |
Correct |
665 ms |
55320 KB |
Output is correct |
60 |
Correct |
360 ms |
53732 KB |
Output is correct |
61 |
Correct |
539 ms |
49620 KB |
Output is correct |
62 |
Correct |
333 ms |
46448 KB |
Output is correct |
63 |
Correct |
1 ms |
212 KB |
Output is correct |
64 |
Correct |
1 ms |
212 KB |
Output is correct |
65 |
Correct |
1 ms |
212 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
1 ms |
340 KB |
Output is correct |
68 |
Correct |
1 ms |
212 KB |
Output is correct |
69 |
Correct |
1 ms |
212 KB |
Output is correct |
70 |
Correct |
1 ms |
320 KB |
Output is correct |
71 |
Correct |
1 ms |
324 KB |
Output is correct |
72 |
Correct |
1 ms |
324 KB |
Output is correct |
73 |
Correct |
1 ms |
212 KB |
Output is correct |
74 |
Correct |
1 ms |
320 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 |
340 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
751 ms |
54724 KB |
Output is correct |
80 |
Correct |
985 ms |
51824 KB |
Output is correct |
81 |
Correct |
677 ms |
52760 KB |
Output is correct |
82 |
Correct |
529 ms |
46248 KB |
Output is correct |
83 |
Correct |
351 ms |
46268 KB |
Output is correct |
84 |
Correct |
310 ms |
46428 KB |
Output is correct |
85 |
Correct |
234 ms |
46844 KB |
Output is correct |
86 |
Correct |
740 ms |
52300 KB |
Output is correct |
87 |
Correct |
987 ms |
53204 KB |
Output is correct |
88 |
Correct |
562 ms |
46680 KB |
Output is correct |
89 |
Correct |
665 ms |
46156 KB |
Output is correct |
90 |
Correct |
417 ms |
46668 KB |
Output is correct |
91 |
Correct |
455 ms |
47208 KB |
Output is correct |
92 |
Correct |
1 ms |
212 KB |
Output is correct |
93 |
Correct |
0 ms |
212 KB |
Output is correct |
94 |
Correct |
0 ms |
212 KB |
Output is correct |
95 |
Correct |
1 ms |
212 KB |
Output is correct |
96 |
Correct |
1 ms |
212 KB |
Output is correct |
97 |
Correct |
0 ms |
212 KB |
Output is correct |
98 |
Correct |
769 ms |
54648 KB |
Output is correct |
99 |
Correct |
987 ms |
50308 KB |
Output is correct |
100 |
Correct |
690 ms |
52556 KB |
Output is correct |
101 |
Correct |
523 ms |
47724 KB |
Output is correct |
102 |
Correct |
338 ms |
47896 KB |
Output is correct |
103 |
Correct |
328 ms |
47824 KB |
Output is correct |
104 |
Correct |
234 ms |
48336 KB |
Output is correct |
105 |
Correct |
750 ms |
56716 KB |
Output is correct |
106 |
Correct |
973 ms |
53660 KB |
Output is correct |
107 |
Correct |
577 ms |
49248 KB |
Output is correct |
108 |
Correct |
644 ms |
46128 KB |
Output is correct |
109 |
Correct |
413 ms |
46824 KB |
Output is correct |
110 |
Correct |
444 ms |
47332 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 |
0 ms |
212 KB |
Output is correct |
116 |
Correct |
566 ms |
47304 KB |
Output is correct |
117 |
Correct |
675 ms |
45900 KB |
Output is correct |
118 |
Correct |
730 ms |
55384 KB |
Output is correct |
119 |
Correct |
526 ms |
47672 KB |
Output is correct |
120 |
Correct |
371 ms |
47728 KB |
Output is correct |
121 |
Correct |
335 ms |
48076 KB |
Output is correct |
122 |
Correct |
247 ms |
48476 KB |
Output is correct |
123 |
Correct |
789 ms |
55632 KB |
Output is correct |
124 |
Correct |
1002 ms |
51096 KB |
Output is correct |
125 |
Correct |
586 ms |
48896 KB |
Output is correct |
126 |
Correct |
686 ms |
46096 KB |
Output is correct |
127 |
Correct |
665 ms |
46060 KB |
Output is correct |
128 |
Correct |
455 ms |
46920 KB |
Output is correct |
129 |
Correct |
467 ms |
47532 KB |
Output is correct |