#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 2e5 + 5, D = 41;
int n, q, L;
int pr[N], dp[N][D], dep[N];
vector<int> g[N];
void dfs(int u) {
for (int v : g[u]) {
if (v != pr[u]) {
pr[v] = u;
dep[v] = dep[u] + 1;
dfs(v);
}
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> L;
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
pr[1] = 1;
dfs(1);
for (int i = 1; i <= n; ++i) {
cin >> dp[i][0];
fill(dp[i] + 1, dp[i] + D, 1);
}
cin >> q;
while (q--) {
int type; cin >> type;
if (type == 1) {
int u, d, w; cin >> u >> d >> w;
for (int i = 0; i <= d; ++i, u = pr[u]) {
dp[u][d - i] = (long long) dp[u][d - i] * w % L;
if (u != pr[u] && d - i) {
dp[u][d - i - 1] = (long long) dp[u][d - i - 1] * w % L;
}
}
} else {
int u; cin >> u;
int res = 1, d = dep[u] + 1;
for (int i = 0; i < min(D, d); ++i, u = pr[u]) {
res = (long long) res * dp[u][i] % L;
}
cout << res << "\n";
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
1 ms |
6876 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
1 ms |
6880 KB |
Output is correct |
9 |
Correct |
1 ms |
6748 KB |
Output is correct |
10 |
Correct |
1 ms |
6748 KB |
Output is correct |
11 |
Correct |
1 ms |
6808 KB |
Output is correct |
12 |
Correct |
1 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6748 KB |
Output is correct |
19 |
Correct |
1 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
6744 KB |
Output is correct |
21 |
Correct |
1 ms |
6748 KB |
Output is correct |
22 |
Correct |
1 ms |
6748 KB |
Output is correct |
23 |
Correct |
1 ms |
6748 KB |
Output is correct |
24 |
Correct |
1 ms |
6748 KB |
Output is correct |
25 |
Correct |
1 ms |
6748 KB |
Output is correct |
26 |
Correct |
1 ms |
6748 KB |
Output is correct |
27 |
Correct |
1 ms |
6748 KB |
Output is correct |
28 |
Correct |
1 ms |
6804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
365 ms |
56844 KB |
Output is correct |
3 |
Correct |
187 ms |
57428 KB |
Output is correct |
4 |
Correct |
311 ms |
65044 KB |
Output is correct |
5 |
Correct |
278 ms |
57116 KB |
Output is correct |
6 |
Correct |
195 ms |
56616 KB |
Output is correct |
7 |
Correct |
190 ms |
57428 KB |
Output is correct |
8 |
Correct |
149 ms |
57832 KB |
Output is correct |
9 |
Correct |
404 ms |
68692 KB |
Output is correct |
10 |
Correct |
213 ms |
68168 KB |
Output is correct |
11 |
Correct |
350 ms |
56828 KB |
Output is correct |
12 |
Correct |
227 ms |
57428 KB |
Output is correct |
13 |
Correct |
137 ms |
57540 KB |
Output is correct |
14 |
Correct |
140 ms |
58056 KB |
Output is correct |
15 |
Correct |
135 ms |
57428 KB |
Output is correct |
16 |
Correct |
154 ms |
58084 KB |
Output is correct |
17 |
Correct |
152 ms |
58452 KB |
Output is correct |
18 |
Correct |
1 ms |
6748 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
1 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
2 ms |
6744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
365 ms |
56844 KB |
Output is correct |
3 |
Correct |
187 ms |
57428 KB |
Output is correct |
4 |
Correct |
311 ms |
65044 KB |
Output is correct |
5 |
Correct |
278 ms |
57116 KB |
Output is correct |
6 |
Correct |
195 ms |
56616 KB |
Output is correct |
7 |
Correct |
190 ms |
57428 KB |
Output is correct |
8 |
Correct |
149 ms |
57832 KB |
Output is correct |
9 |
Correct |
404 ms |
68692 KB |
Output is correct |
10 |
Correct |
213 ms |
68168 KB |
Output is correct |
11 |
Correct |
350 ms |
56828 KB |
Output is correct |
12 |
Correct |
227 ms |
57428 KB |
Output is correct |
13 |
Correct |
137 ms |
57540 KB |
Output is correct |
14 |
Correct |
140 ms |
58056 KB |
Output is correct |
15 |
Correct |
135 ms |
57428 KB |
Output is correct |
16 |
Correct |
154 ms |
58084 KB |
Output is correct |
17 |
Correct |
152 ms |
58452 KB |
Output is correct |
18 |
Correct |
1 ms |
6748 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
1 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
2 ms |
6744 KB |
Output is correct |
23 |
Correct |
1 ms |
6748 KB |
Output is correct |
24 |
Correct |
360 ms |
56880 KB |
Output is correct |
25 |
Correct |
203 ms |
57428 KB |
Output is correct |
26 |
Correct |
310 ms |
68472 KB |
Output is correct |
27 |
Correct |
277 ms |
57172 KB |
Output is correct |
28 |
Correct |
188 ms |
57120 KB |
Output is correct |
29 |
Correct |
182 ms |
56952 KB |
Output is correct |
30 |
Correct |
144 ms |
57796 KB |
Output is correct |
31 |
Correct |
461 ms |
65104 KB |
Output is correct |
32 |
Correct |
204 ms |
68436 KB |
Output is correct |
33 |
Correct |
358 ms |
56844 KB |
Output is correct |
34 |
Correct |
195 ms |
57312 KB |
Output is correct |
35 |
Correct |
1 ms |
6748 KB |
Output is correct |
36 |
Correct |
2 ms |
6748 KB |
Output is correct |
37 |
Correct |
2 ms |
6748 KB |
Output is correct |
38 |
Correct |
2 ms |
6748 KB |
Output is correct |
39 |
Correct |
1 ms |
6808 KB |
Output is correct |
40 |
Correct |
2 ms |
7020 KB |
Output is correct |
41 |
Correct |
1 ms |
6748 KB |
Output is correct |
42 |
Correct |
1 ms |
6748 KB |
Output is correct |
43 |
Correct |
1 ms |
6748 KB |
Output is correct |
44 |
Correct |
1 ms |
6748 KB |
Output is correct |
45 |
Correct |
1 ms |
6748 KB |
Output is correct |
46 |
Correct |
1 ms |
6744 KB |
Output is correct |
47 |
Correct |
1 ms |
6744 KB |
Output is correct |
48 |
Correct |
1 ms |
6744 KB |
Output is correct |
49 |
Correct |
1 ms |
6812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
419 ms |
65620 KB |
Output is correct |
3 |
Correct |
429 ms |
63572 KB |
Output is correct |
4 |
Correct |
407 ms |
63564 KB |
Output is correct |
5 |
Correct |
322 ms |
54352 KB |
Output is correct |
6 |
Correct |
216 ms |
54356 KB |
Output is correct |
7 |
Correct |
196 ms |
54340 KB |
Output is correct |
8 |
Correct |
161 ms |
54976 KB |
Output is correct |
9 |
Correct |
442 ms |
62396 KB |
Output is correct |
10 |
Correct |
452 ms |
65364 KB |
Output is correct |
11 |
Correct |
384 ms |
53820 KB |
Output is correct |
12 |
Correct |
407 ms |
54612 KB |
Output is correct |
13 |
Correct |
245 ms |
55540 KB |
Output is correct |
14 |
Correct |
239 ms |
55992 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6804 KB |
Output is correct |
19 |
Correct |
1 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
449 ms |
64524 KB |
Output is correct |
3 |
Correct |
435 ms |
61012 KB |
Output is correct |
4 |
Correct |
356 ms |
62804 KB |
Output is correct |
5 |
Correct |
339 ms |
55892 KB |
Output is correct |
6 |
Correct |
211 ms |
55888 KB |
Output is correct |
7 |
Correct |
196 ms |
55632 KB |
Output is correct |
8 |
Correct |
178 ms |
56016 KB |
Output is correct |
9 |
Correct |
423 ms |
67668 KB |
Output is correct |
10 |
Correct |
452 ms |
66132 KB |
Output is correct |
11 |
Correct |
375 ms |
56404 KB |
Output is correct |
12 |
Correct |
370 ms |
55044 KB |
Output is correct |
13 |
Correct |
236 ms |
55692 KB |
Output is correct |
14 |
Correct |
235 ms |
56148 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6748 KB |
Output is correct |
19 |
Correct |
1 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
1 ms |
6876 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
1 ms |
6880 KB |
Output is correct |
9 |
Correct |
1 ms |
6748 KB |
Output is correct |
10 |
Correct |
1 ms |
6748 KB |
Output is correct |
11 |
Correct |
1 ms |
6808 KB |
Output is correct |
12 |
Correct |
1 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6748 KB |
Output is correct |
19 |
Correct |
1 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
6744 KB |
Output is correct |
21 |
Correct |
1 ms |
6748 KB |
Output is correct |
22 |
Correct |
1 ms |
6748 KB |
Output is correct |
23 |
Correct |
1 ms |
6748 KB |
Output is correct |
24 |
Correct |
1 ms |
6748 KB |
Output is correct |
25 |
Correct |
1 ms |
6748 KB |
Output is correct |
26 |
Correct |
1 ms |
6748 KB |
Output is correct |
27 |
Correct |
1 ms |
6748 KB |
Output is correct |
28 |
Correct |
1 ms |
6804 KB |
Output is correct |
29 |
Correct |
1 ms |
6748 KB |
Output is correct |
30 |
Correct |
365 ms |
56844 KB |
Output is correct |
31 |
Correct |
187 ms |
57428 KB |
Output is correct |
32 |
Correct |
311 ms |
65044 KB |
Output is correct |
33 |
Correct |
278 ms |
57116 KB |
Output is correct |
34 |
Correct |
195 ms |
56616 KB |
Output is correct |
35 |
Correct |
190 ms |
57428 KB |
Output is correct |
36 |
Correct |
149 ms |
57832 KB |
Output is correct |
37 |
Correct |
404 ms |
68692 KB |
Output is correct |
38 |
Correct |
213 ms |
68168 KB |
Output is correct |
39 |
Correct |
350 ms |
56828 KB |
Output is correct |
40 |
Correct |
227 ms |
57428 KB |
Output is correct |
41 |
Correct |
137 ms |
57540 KB |
Output is correct |
42 |
Correct |
140 ms |
58056 KB |
Output is correct |
43 |
Correct |
135 ms |
57428 KB |
Output is correct |
44 |
Correct |
154 ms |
58084 KB |
Output is correct |
45 |
Correct |
152 ms |
58452 KB |
Output is correct |
46 |
Correct |
1 ms |
6748 KB |
Output is correct |
47 |
Correct |
2 ms |
6748 KB |
Output is correct |
48 |
Correct |
1 ms |
6748 KB |
Output is correct |
49 |
Correct |
2 ms |
6748 KB |
Output is correct |
50 |
Correct |
2 ms |
6744 KB |
Output is correct |
51 |
Correct |
1 ms |
6748 KB |
Output is correct |
52 |
Correct |
360 ms |
56880 KB |
Output is correct |
53 |
Correct |
203 ms |
57428 KB |
Output is correct |
54 |
Correct |
310 ms |
68472 KB |
Output is correct |
55 |
Correct |
277 ms |
57172 KB |
Output is correct |
56 |
Correct |
188 ms |
57120 KB |
Output is correct |
57 |
Correct |
182 ms |
56952 KB |
Output is correct |
58 |
Correct |
144 ms |
57796 KB |
Output is correct |
59 |
Correct |
461 ms |
65104 KB |
Output is correct |
60 |
Correct |
204 ms |
68436 KB |
Output is correct |
61 |
Correct |
358 ms |
56844 KB |
Output is correct |
62 |
Correct |
195 ms |
57312 KB |
Output is correct |
63 |
Correct |
1 ms |
6748 KB |
Output is correct |
64 |
Correct |
2 ms |
6748 KB |
Output is correct |
65 |
Correct |
2 ms |
6748 KB |
Output is correct |
66 |
Correct |
2 ms |
6748 KB |
Output is correct |
67 |
Correct |
1 ms |
6808 KB |
Output is correct |
68 |
Correct |
2 ms |
7020 KB |
Output is correct |
69 |
Correct |
1 ms |
6748 KB |
Output is correct |
70 |
Correct |
1 ms |
6748 KB |
Output is correct |
71 |
Correct |
1 ms |
6748 KB |
Output is correct |
72 |
Correct |
1 ms |
6748 KB |
Output is correct |
73 |
Correct |
1 ms |
6748 KB |
Output is correct |
74 |
Correct |
1 ms |
6744 KB |
Output is correct |
75 |
Correct |
1 ms |
6744 KB |
Output is correct |
76 |
Correct |
1 ms |
6744 KB |
Output is correct |
77 |
Correct |
1 ms |
6812 KB |
Output is correct |
78 |
Correct |
1 ms |
6748 KB |
Output is correct |
79 |
Correct |
419 ms |
65620 KB |
Output is correct |
80 |
Correct |
429 ms |
63572 KB |
Output is correct |
81 |
Correct |
407 ms |
63564 KB |
Output is correct |
82 |
Correct |
322 ms |
54352 KB |
Output is correct |
83 |
Correct |
216 ms |
54356 KB |
Output is correct |
84 |
Correct |
196 ms |
54340 KB |
Output is correct |
85 |
Correct |
161 ms |
54976 KB |
Output is correct |
86 |
Correct |
442 ms |
62396 KB |
Output is correct |
87 |
Correct |
452 ms |
65364 KB |
Output is correct |
88 |
Correct |
384 ms |
53820 KB |
Output is correct |
89 |
Correct |
407 ms |
54612 KB |
Output is correct |
90 |
Correct |
245 ms |
55540 KB |
Output is correct |
91 |
Correct |
239 ms |
55992 KB |
Output is correct |
92 |
Correct |
1 ms |
6748 KB |
Output is correct |
93 |
Correct |
1 ms |
6748 KB |
Output is correct |
94 |
Correct |
1 ms |
6748 KB |
Output is correct |
95 |
Correct |
1 ms |
6804 KB |
Output is correct |
96 |
Correct |
1 ms |
6748 KB |
Output is correct |
97 |
Correct |
1 ms |
6748 KB |
Output is correct |
98 |
Correct |
449 ms |
64524 KB |
Output is correct |
99 |
Correct |
435 ms |
61012 KB |
Output is correct |
100 |
Correct |
356 ms |
62804 KB |
Output is correct |
101 |
Correct |
339 ms |
55892 KB |
Output is correct |
102 |
Correct |
211 ms |
55888 KB |
Output is correct |
103 |
Correct |
196 ms |
55632 KB |
Output is correct |
104 |
Correct |
178 ms |
56016 KB |
Output is correct |
105 |
Correct |
423 ms |
67668 KB |
Output is correct |
106 |
Correct |
452 ms |
66132 KB |
Output is correct |
107 |
Correct |
375 ms |
56404 KB |
Output is correct |
108 |
Correct |
370 ms |
55044 KB |
Output is correct |
109 |
Correct |
236 ms |
55692 KB |
Output is correct |
110 |
Correct |
235 ms |
56148 KB |
Output is correct |
111 |
Correct |
1 ms |
6748 KB |
Output is correct |
112 |
Correct |
1 ms |
6748 KB |
Output is correct |
113 |
Correct |
1 ms |
6748 KB |
Output is correct |
114 |
Correct |
1 ms |
6748 KB |
Output is correct |
115 |
Correct |
1 ms |
6748 KB |
Output is correct |
116 |
Correct |
379 ms |
54868 KB |
Output is correct |
117 |
Correct |
389 ms |
57424 KB |
Output is correct |
118 |
Correct |
370 ms |
68440 KB |
Output is correct |
119 |
Correct |
338 ms |
57168 KB |
Output is correct |
120 |
Correct |
211 ms |
56780 KB |
Output is correct |
121 |
Correct |
200 ms |
57424 KB |
Output is correct |
122 |
Correct |
161 ms |
57796 KB |
Output is correct |
123 |
Correct |
444 ms |
66344 KB |
Output is correct |
124 |
Correct |
424 ms |
64592 KB |
Output is correct |
125 |
Correct |
369 ms |
56144 KB |
Output is correct |
126 |
Correct |
406 ms |
57380 KB |
Output is correct |
127 |
Correct |
361 ms |
57704 KB |
Output is correct |
128 |
Correct |
250 ms |
58392 KB |
Output is correct |
129 |
Correct |
272 ms |
58960 KB |
Output is correct |