#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, D = 41;
int n, mod, q;
int a[N];
vector<int> g[N];
int cnt[N][D];
int par[N];
int mul(int x, int y) { return (x * 1LL * y) % mod; }
void smul(int& x, int y) { x = mul(x, y); }
void dfs(int u, int p) {
par[u] = p;
for(int v : g[u]) {
if(v == p) continue;
dfs(v, u);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> mod;
for(int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 0; i < N; i++) for(int j = 0; j < D; j++) cnt[i][j] = 1;
dfs(1, 0);
cin >> q;
for(int query = 0; query < n + q; query++) {
int t;
if(query >= n) cin >> t;
if(t == 1 || query < n) {
int u, d, w;
if(query >= n) cin >> u >> d >> w;
else {
u = query + 1;
d = 0;
w = a[u];
}
for(int i = d; i >= 0; i--) {
if(u == 1) {
smul(cnt[u][i], w);
continue;
}
smul(cnt[u][i], w);
if(i > 0) smul(cnt[u][i - 1], w);
u = par[u];
}
} else {
int u;
cin >> u;
int res = 1;
for(int i = 0; i < D; i++) {
if(u == 0) break;
smul(res, cnt[u][i]);
u = par[u];
}
cout << res << "\n";
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
37724 KB |
Output is correct |
2 |
Correct |
11 ms |
37724 KB |
Output is correct |
3 |
Correct |
10 ms |
37724 KB |
Output is correct |
4 |
Correct |
11 ms |
37876 KB |
Output is correct |
5 |
Correct |
11 ms |
37920 KB |
Output is correct |
6 |
Correct |
11 ms |
37724 KB |
Output is correct |
7 |
Correct |
11 ms |
37864 KB |
Output is correct |
8 |
Correct |
11 ms |
37724 KB |
Output is correct |
9 |
Correct |
10 ms |
37824 KB |
Output is correct |
10 |
Correct |
10 ms |
37724 KB |
Output is correct |
11 |
Correct |
12 ms |
37804 KB |
Output is correct |
12 |
Correct |
10 ms |
37724 KB |
Output is correct |
13 |
Correct |
11 ms |
37724 KB |
Output is correct |
14 |
Correct |
11 ms |
37724 KB |
Output is correct |
15 |
Correct |
11 ms |
37724 KB |
Output is correct |
16 |
Correct |
10 ms |
37724 KB |
Output is correct |
17 |
Correct |
10 ms |
37724 KB |
Output is correct |
18 |
Correct |
10 ms |
37724 KB |
Output is correct |
19 |
Correct |
10 ms |
37796 KB |
Output is correct |
20 |
Correct |
10 ms |
37724 KB |
Output is correct |
21 |
Correct |
12 ms |
37976 KB |
Output is correct |
22 |
Correct |
11 ms |
37724 KB |
Output is correct |
23 |
Correct |
11 ms |
37796 KB |
Output is correct |
24 |
Correct |
12 ms |
37724 KB |
Output is correct |
25 |
Correct |
11 ms |
37796 KB |
Output is correct |
26 |
Correct |
10 ms |
37828 KB |
Output is correct |
27 |
Correct |
10 ms |
37724 KB |
Output is correct |
28 |
Correct |
11 ms |
37920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
37724 KB |
Output is correct |
2 |
Correct |
350 ms |
56856 KB |
Output is correct |
3 |
Correct |
248 ms |
57420 KB |
Output is correct |
4 |
Correct |
359 ms |
60212 KB |
Output is correct |
5 |
Correct |
295 ms |
57164 KB |
Output is correct |
6 |
Correct |
227 ms |
56712 KB |
Output is correct |
7 |
Correct |
221 ms |
57364 KB |
Output is correct |
8 |
Correct |
175 ms |
57848 KB |
Output is correct |
9 |
Correct |
425 ms |
61328 KB |
Output is correct |
10 |
Correct |
256 ms |
61528 KB |
Output is correct |
11 |
Correct |
348 ms |
56840 KB |
Output is correct |
12 |
Correct |
248 ms |
57172 KB |
Output is correct |
13 |
Correct |
182 ms |
57484 KB |
Output is correct |
14 |
Correct |
174 ms |
58084 KB |
Output is correct |
15 |
Correct |
173 ms |
57604 KB |
Output is correct |
16 |
Correct |
168 ms |
57936 KB |
Output is correct |
17 |
Correct |
182 ms |
58452 KB |
Output is correct |
18 |
Correct |
10 ms |
37724 KB |
Output is correct |
19 |
Correct |
10 ms |
37724 KB |
Output is correct |
20 |
Correct |
10 ms |
37724 KB |
Output is correct |
21 |
Correct |
10 ms |
37724 KB |
Output is correct |
22 |
Correct |
12 ms |
37724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
37724 KB |
Output is correct |
2 |
Correct |
350 ms |
56856 KB |
Output is correct |
3 |
Correct |
248 ms |
57420 KB |
Output is correct |
4 |
Correct |
359 ms |
60212 KB |
Output is correct |
5 |
Correct |
295 ms |
57164 KB |
Output is correct |
6 |
Correct |
227 ms |
56712 KB |
Output is correct |
7 |
Correct |
221 ms |
57364 KB |
Output is correct |
8 |
Correct |
175 ms |
57848 KB |
Output is correct |
9 |
Correct |
425 ms |
61328 KB |
Output is correct |
10 |
Correct |
256 ms |
61528 KB |
Output is correct |
11 |
Correct |
348 ms |
56840 KB |
Output is correct |
12 |
Correct |
248 ms |
57172 KB |
Output is correct |
13 |
Correct |
182 ms |
57484 KB |
Output is correct |
14 |
Correct |
174 ms |
58084 KB |
Output is correct |
15 |
Correct |
173 ms |
57604 KB |
Output is correct |
16 |
Correct |
168 ms |
57936 KB |
Output is correct |
17 |
Correct |
182 ms |
58452 KB |
Output is correct |
18 |
Correct |
10 ms |
37724 KB |
Output is correct |
19 |
Correct |
10 ms |
37724 KB |
Output is correct |
20 |
Correct |
10 ms |
37724 KB |
Output is correct |
21 |
Correct |
10 ms |
37724 KB |
Output is correct |
22 |
Correct |
12 ms |
37724 KB |
Output is correct |
23 |
Correct |
10 ms |
37720 KB |
Output is correct |
24 |
Correct |
341 ms |
56724 KB |
Output is correct |
25 |
Correct |
270 ms |
57692 KB |
Output is correct |
26 |
Correct |
340 ms |
61568 KB |
Output is correct |
27 |
Correct |
293 ms |
57028 KB |
Output is correct |
28 |
Correct |
238 ms |
57244 KB |
Output is correct |
29 |
Correct |
214 ms |
56792 KB |
Output is correct |
30 |
Correct |
187 ms |
57912 KB |
Output is correct |
31 |
Correct |
424 ms |
59972 KB |
Output is correct |
32 |
Correct |
261 ms |
61524 KB |
Output is correct |
33 |
Correct |
348 ms |
56740 KB |
Output is correct |
34 |
Correct |
269 ms |
57172 KB |
Output is correct |
35 |
Correct |
10 ms |
37724 KB |
Output is correct |
36 |
Correct |
11 ms |
37724 KB |
Output is correct |
37 |
Correct |
11 ms |
37852 KB |
Output is correct |
38 |
Correct |
10 ms |
37724 KB |
Output is correct |
39 |
Correct |
10 ms |
37724 KB |
Output is correct |
40 |
Correct |
13 ms |
37844 KB |
Output is correct |
41 |
Correct |
10 ms |
37812 KB |
Output is correct |
42 |
Correct |
11 ms |
37848 KB |
Output is correct |
43 |
Correct |
10 ms |
37832 KB |
Output is correct |
44 |
Correct |
11 ms |
37720 KB |
Output is correct |
45 |
Correct |
10 ms |
37852 KB |
Output is correct |
46 |
Correct |
10 ms |
37852 KB |
Output is correct |
47 |
Correct |
11 ms |
37724 KB |
Output is correct |
48 |
Correct |
10 ms |
37720 KB |
Output is correct |
49 |
Correct |
11 ms |
37720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
37856 KB |
Output is correct |
2 |
Correct |
506 ms |
58608 KB |
Output is correct |
3 |
Correct |
568 ms |
58104 KB |
Output is correct |
4 |
Correct |
448 ms |
57680 KB |
Output is correct |
5 |
Correct |
318 ms |
54348 KB |
Output is correct |
6 |
Correct |
244 ms |
54140 KB |
Output is correct |
7 |
Correct |
241 ms |
54328 KB |
Output is correct |
8 |
Correct |
179 ms |
54976 KB |
Output is correct |
9 |
Correct |
506 ms |
57224 KB |
Output is correct |
10 |
Correct |
579 ms |
58688 KB |
Output is correct |
11 |
Correct |
366 ms |
53584 KB |
Output is correct |
12 |
Correct |
388 ms |
54864 KB |
Output is correct |
13 |
Correct |
290 ms |
55420 KB |
Output is correct |
14 |
Correct |
293 ms |
55892 KB |
Output is correct |
15 |
Correct |
11 ms |
37724 KB |
Output is correct |
16 |
Correct |
10 ms |
37724 KB |
Output is correct |
17 |
Correct |
10 ms |
37832 KB |
Output is correct |
18 |
Correct |
12 ms |
37724 KB |
Output is correct |
19 |
Correct |
10 ms |
37724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
37724 KB |
Output is correct |
2 |
Correct |
485 ms |
51568 KB |
Output is correct |
3 |
Correct |
609 ms |
57168 KB |
Output is correct |
4 |
Correct |
456 ms |
58660 KB |
Output is correct |
5 |
Correct |
329 ms |
55720 KB |
Output is correct |
6 |
Correct |
274 ms |
55912 KB |
Output is correct |
7 |
Correct |
242 ms |
55636 KB |
Output is correct |
8 |
Correct |
198 ms |
56000 KB |
Output is correct |
9 |
Correct |
507 ms |
60796 KB |
Output is correct |
10 |
Correct |
561 ms |
59324 KB |
Output is correct |
11 |
Correct |
377 ms |
56376 KB |
Output is correct |
12 |
Correct |
356 ms |
55028 KB |
Output is correct |
13 |
Correct |
308 ms |
55680 KB |
Output is correct |
14 |
Correct |
311 ms |
56276 KB |
Output is correct |
15 |
Correct |
10 ms |
37724 KB |
Output is correct |
16 |
Correct |
10 ms |
37720 KB |
Output is correct |
17 |
Correct |
10 ms |
37724 KB |
Output is correct |
18 |
Correct |
10 ms |
37792 KB |
Output is correct |
19 |
Correct |
10 ms |
37720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
37724 KB |
Output is correct |
2 |
Correct |
11 ms |
37724 KB |
Output is correct |
3 |
Correct |
10 ms |
37724 KB |
Output is correct |
4 |
Correct |
11 ms |
37876 KB |
Output is correct |
5 |
Correct |
11 ms |
37920 KB |
Output is correct |
6 |
Correct |
11 ms |
37724 KB |
Output is correct |
7 |
Correct |
11 ms |
37864 KB |
Output is correct |
8 |
Correct |
11 ms |
37724 KB |
Output is correct |
9 |
Correct |
10 ms |
37824 KB |
Output is correct |
10 |
Correct |
10 ms |
37724 KB |
Output is correct |
11 |
Correct |
12 ms |
37804 KB |
Output is correct |
12 |
Correct |
10 ms |
37724 KB |
Output is correct |
13 |
Correct |
11 ms |
37724 KB |
Output is correct |
14 |
Correct |
11 ms |
37724 KB |
Output is correct |
15 |
Correct |
11 ms |
37724 KB |
Output is correct |
16 |
Correct |
10 ms |
37724 KB |
Output is correct |
17 |
Correct |
10 ms |
37724 KB |
Output is correct |
18 |
Correct |
10 ms |
37724 KB |
Output is correct |
19 |
Correct |
10 ms |
37796 KB |
Output is correct |
20 |
Correct |
10 ms |
37724 KB |
Output is correct |
21 |
Correct |
12 ms |
37976 KB |
Output is correct |
22 |
Correct |
11 ms |
37724 KB |
Output is correct |
23 |
Correct |
11 ms |
37796 KB |
Output is correct |
24 |
Correct |
12 ms |
37724 KB |
Output is correct |
25 |
Correct |
11 ms |
37796 KB |
Output is correct |
26 |
Correct |
10 ms |
37828 KB |
Output is correct |
27 |
Correct |
10 ms |
37724 KB |
Output is correct |
28 |
Correct |
11 ms |
37920 KB |
Output is correct |
29 |
Correct |
10 ms |
37724 KB |
Output is correct |
30 |
Correct |
350 ms |
56856 KB |
Output is correct |
31 |
Correct |
248 ms |
57420 KB |
Output is correct |
32 |
Correct |
359 ms |
60212 KB |
Output is correct |
33 |
Correct |
295 ms |
57164 KB |
Output is correct |
34 |
Correct |
227 ms |
56712 KB |
Output is correct |
35 |
Correct |
221 ms |
57364 KB |
Output is correct |
36 |
Correct |
175 ms |
57848 KB |
Output is correct |
37 |
Correct |
425 ms |
61328 KB |
Output is correct |
38 |
Correct |
256 ms |
61528 KB |
Output is correct |
39 |
Correct |
348 ms |
56840 KB |
Output is correct |
40 |
Correct |
248 ms |
57172 KB |
Output is correct |
41 |
Correct |
182 ms |
57484 KB |
Output is correct |
42 |
Correct |
174 ms |
58084 KB |
Output is correct |
43 |
Correct |
173 ms |
57604 KB |
Output is correct |
44 |
Correct |
168 ms |
57936 KB |
Output is correct |
45 |
Correct |
182 ms |
58452 KB |
Output is correct |
46 |
Correct |
10 ms |
37724 KB |
Output is correct |
47 |
Correct |
10 ms |
37724 KB |
Output is correct |
48 |
Correct |
10 ms |
37724 KB |
Output is correct |
49 |
Correct |
10 ms |
37724 KB |
Output is correct |
50 |
Correct |
12 ms |
37724 KB |
Output is correct |
51 |
Correct |
10 ms |
37720 KB |
Output is correct |
52 |
Correct |
341 ms |
56724 KB |
Output is correct |
53 |
Correct |
270 ms |
57692 KB |
Output is correct |
54 |
Correct |
340 ms |
61568 KB |
Output is correct |
55 |
Correct |
293 ms |
57028 KB |
Output is correct |
56 |
Correct |
238 ms |
57244 KB |
Output is correct |
57 |
Correct |
214 ms |
56792 KB |
Output is correct |
58 |
Correct |
187 ms |
57912 KB |
Output is correct |
59 |
Correct |
424 ms |
59972 KB |
Output is correct |
60 |
Correct |
261 ms |
61524 KB |
Output is correct |
61 |
Correct |
348 ms |
56740 KB |
Output is correct |
62 |
Correct |
269 ms |
57172 KB |
Output is correct |
63 |
Correct |
10 ms |
37724 KB |
Output is correct |
64 |
Correct |
11 ms |
37724 KB |
Output is correct |
65 |
Correct |
11 ms |
37852 KB |
Output is correct |
66 |
Correct |
10 ms |
37724 KB |
Output is correct |
67 |
Correct |
10 ms |
37724 KB |
Output is correct |
68 |
Correct |
13 ms |
37844 KB |
Output is correct |
69 |
Correct |
10 ms |
37812 KB |
Output is correct |
70 |
Correct |
11 ms |
37848 KB |
Output is correct |
71 |
Correct |
10 ms |
37832 KB |
Output is correct |
72 |
Correct |
11 ms |
37720 KB |
Output is correct |
73 |
Correct |
10 ms |
37852 KB |
Output is correct |
74 |
Correct |
10 ms |
37852 KB |
Output is correct |
75 |
Correct |
11 ms |
37724 KB |
Output is correct |
76 |
Correct |
10 ms |
37720 KB |
Output is correct |
77 |
Correct |
11 ms |
37720 KB |
Output is correct |
78 |
Correct |
10 ms |
37856 KB |
Output is correct |
79 |
Correct |
506 ms |
58608 KB |
Output is correct |
80 |
Correct |
568 ms |
58104 KB |
Output is correct |
81 |
Correct |
448 ms |
57680 KB |
Output is correct |
82 |
Correct |
318 ms |
54348 KB |
Output is correct |
83 |
Correct |
244 ms |
54140 KB |
Output is correct |
84 |
Correct |
241 ms |
54328 KB |
Output is correct |
85 |
Correct |
179 ms |
54976 KB |
Output is correct |
86 |
Correct |
506 ms |
57224 KB |
Output is correct |
87 |
Correct |
579 ms |
58688 KB |
Output is correct |
88 |
Correct |
366 ms |
53584 KB |
Output is correct |
89 |
Correct |
388 ms |
54864 KB |
Output is correct |
90 |
Correct |
290 ms |
55420 KB |
Output is correct |
91 |
Correct |
293 ms |
55892 KB |
Output is correct |
92 |
Correct |
11 ms |
37724 KB |
Output is correct |
93 |
Correct |
10 ms |
37724 KB |
Output is correct |
94 |
Correct |
10 ms |
37832 KB |
Output is correct |
95 |
Correct |
12 ms |
37724 KB |
Output is correct |
96 |
Correct |
10 ms |
37724 KB |
Output is correct |
97 |
Correct |
10 ms |
37724 KB |
Output is correct |
98 |
Correct |
485 ms |
51568 KB |
Output is correct |
99 |
Correct |
609 ms |
57168 KB |
Output is correct |
100 |
Correct |
456 ms |
58660 KB |
Output is correct |
101 |
Correct |
329 ms |
55720 KB |
Output is correct |
102 |
Correct |
274 ms |
55912 KB |
Output is correct |
103 |
Correct |
242 ms |
55636 KB |
Output is correct |
104 |
Correct |
198 ms |
56000 KB |
Output is correct |
105 |
Correct |
507 ms |
60796 KB |
Output is correct |
106 |
Correct |
561 ms |
59324 KB |
Output is correct |
107 |
Correct |
377 ms |
56376 KB |
Output is correct |
108 |
Correct |
356 ms |
55028 KB |
Output is correct |
109 |
Correct |
308 ms |
55680 KB |
Output is correct |
110 |
Correct |
311 ms |
56276 KB |
Output is correct |
111 |
Correct |
10 ms |
37724 KB |
Output is correct |
112 |
Correct |
10 ms |
37720 KB |
Output is correct |
113 |
Correct |
10 ms |
37724 KB |
Output is correct |
114 |
Correct |
10 ms |
37792 KB |
Output is correct |
115 |
Correct |
10 ms |
37720 KB |
Output is correct |
116 |
Correct |
347 ms |
54768 KB |
Output is correct |
117 |
Correct |
436 ms |
57580 KB |
Output is correct |
118 |
Correct |
523 ms |
61380 KB |
Output is correct |
119 |
Correct |
372 ms |
57428 KB |
Output is correct |
120 |
Correct |
301 ms |
56916 KB |
Output is correct |
121 |
Correct |
282 ms |
57460 KB |
Output is correct |
122 |
Correct |
205 ms |
57800 KB |
Output is correct |
123 |
Correct |
503 ms |
60532 KB |
Output is correct |
124 |
Correct |
606 ms |
59936 KB |
Output is correct |
125 |
Correct |
369 ms |
55868 KB |
Output is correct |
126 |
Correct |
450 ms |
57176 KB |
Output is correct |
127 |
Correct |
486 ms |
57924 KB |
Output is correct |
128 |
Correct |
420 ms |
58484 KB |
Output is correct |
129 |
Correct |
446 ms |
59072 KB |
Output is correct |