#include <iostream>
#include <vector>
#include <queue>
#include <array>
#define ll long long
using namespace std;
vector <ll> adj[200000];
bool up;
ll n, M, q, t, a, b, c, p, f, H[200000], P[200000], par[200000][41];
void dfs(ll u, ll p) {
for (auto v : adj[u]) {
if (p != v) {
P[v] = u;
dfs(v, u);
}
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
cin >> n >> M;
for (int i=0; i<n-1; ++i) {
cin >> a >> b;
--a, --b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i=0; i<n; ++i) {
cin >> H[i];
for (int j=0; j<=40; ++j) par[i][j] = 1;
}
dfs(0, -1);
P[0] = -1;
cin >> q;
while (q--) {
cin >> t;
if (t == 1) {
cin >> a >> b >> c;
--a;
up = 0;
for (int i=b; i>=0; --i) {
par[a][i] *= c;
par[a][i] %= M;
up ^= 1;
if (!up) {
if (P[a] != -1) {
a = P[a];
++i;
up = 0;
}
}
}
}
else {
cin >> a;
--a;
f = H[a];
for (int i=0; i<=40; ++i) {
f *= par[a][i];
f %= M;
a = P[a];
if (a == -1) break;
}
cout << f << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8536 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
8540 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8536 KB |
Output is correct |
17 |
Correct |
1 ms |
8540 KB |
Output is correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
19 |
Correct |
1 ms |
8540 KB |
Output is correct |
20 |
Correct |
1 ms |
8540 KB |
Output is correct |
21 |
Correct |
1 ms |
8540 KB |
Output is correct |
22 |
Correct |
1 ms |
8696 KB |
Output is correct |
23 |
Correct |
1 ms |
8540 KB |
Output is correct |
24 |
Correct |
1 ms |
8540 KB |
Output is correct |
25 |
Correct |
2 ms |
8540 KB |
Output is correct |
26 |
Correct |
2 ms |
8540 KB |
Output is correct |
27 |
Correct |
2 ms |
8536 KB |
Output is correct |
28 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
382 ms |
91628 KB |
Output is correct |
3 |
Correct |
225 ms |
91984 KB |
Output is correct |
4 |
Correct |
350 ms |
94112 KB |
Output is correct |
5 |
Correct |
308 ms |
91748 KB |
Output is correct |
6 |
Correct |
209 ms |
91192 KB |
Output is correct |
7 |
Correct |
201 ms |
91984 KB |
Output is correct |
8 |
Correct |
205 ms |
92264 KB |
Output is correct |
9 |
Correct |
440 ms |
95568 KB |
Output is correct |
10 |
Correct |
238 ms |
95828 KB |
Output is correct |
11 |
Correct |
354 ms |
91476 KB |
Output is correct |
12 |
Correct |
211 ms |
91800 KB |
Output is correct |
13 |
Correct |
152 ms |
92052 KB |
Output is correct |
14 |
Correct |
150 ms |
92600 KB |
Output is correct |
15 |
Correct |
148 ms |
91660 KB |
Output is correct |
16 |
Correct |
164 ms |
92248 KB |
Output is correct |
17 |
Correct |
161 ms |
92892 KB |
Output is correct |
18 |
Correct |
2 ms |
8540 KB |
Output is correct |
19 |
Correct |
1 ms |
8640 KB |
Output is correct |
20 |
Correct |
1 ms |
8540 KB |
Output is correct |
21 |
Correct |
2 ms |
8540 KB |
Output is correct |
22 |
Correct |
2 ms |
8692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
382 ms |
91628 KB |
Output is correct |
3 |
Correct |
225 ms |
91984 KB |
Output is correct |
4 |
Correct |
350 ms |
94112 KB |
Output is correct |
5 |
Correct |
308 ms |
91748 KB |
Output is correct |
6 |
Correct |
209 ms |
91192 KB |
Output is correct |
7 |
Correct |
201 ms |
91984 KB |
Output is correct |
8 |
Correct |
205 ms |
92264 KB |
Output is correct |
9 |
Correct |
440 ms |
95568 KB |
Output is correct |
10 |
Correct |
238 ms |
95828 KB |
Output is correct |
11 |
Correct |
354 ms |
91476 KB |
Output is correct |
12 |
Correct |
211 ms |
91800 KB |
Output is correct |
13 |
Correct |
152 ms |
92052 KB |
Output is correct |
14 |
Correct |
150 ms |
92600 KB |
Output is correct |
15 |
Correct |
148 ms |
91660 KB |
Output is correct |
16 |
Correct |
164 ms |
92248 KB |
Output is correct |
17 |
Correct |
161 ms |
92892 KB |
Output is correct |
18 |
Correct |
2 ms |
8540 KB |
Output is correct |
19 |
Correct |
1 ms |
8640 KB |
Output is correct |
20 |
Correct |
1 ms |
8540 KB |
Output is correct |
21 |
Correct |
2 ms |
8540 KB |
Output is correct |
22 |
Correct |
2 ms |
8692 KB |
Output is correct |
23 |
Correct |
1 ms |
8536 KB |
Output is correct |
24 |
Correct |
374 ms |
91520 KB |
Output is correct |
25 |
Correct |
225 ms |
91932 KB |
Output is correct |
26 |
Correct |
329 ms |
95824 KB |
Output is correct |
27 |
Correct |
295 ms |
91600 KB |
Output is correct |
28 |
Correct |
238 ms |
91708 KB |
Output is correct |
29 |
Correct |
202 ms |
91732 KB |
Output is correct |
30 |
Correct |
182 ms |
92348 KB |
Output is correct |
31 |
Correct |
443 ms |
94032 KB |
Output is correct |
32 |
Correct |
253 ms |
95828 KB |
Output is correct |
33 |
Correct |
368 ms |
91400 KB |
Output is correct |
34 |
Correct |
239 ms |
92076 KB |
Output is correct |
35 |
Correct |
1 ms |
8536 KB |
Output is correct |
36 |
Correct |
1 ms |
8540 KB |
Output is correct |
37 |
Correct |
2 ms |
8540 KB |
Output is correct |
38 |
Correct |
1 ms |
8540 KB |
Output is correct |
39 |
Correct |
1 ms |
8540 KB |
Output is correct |
40 |
Correct |
1 ms |
8540 KB |
Output is correct |
41 |
Correct |
2 ms |
8540 KB |
Output is correct |
42 |
Correct |
2 ms |
8540 KB |
Output is correct |
43 |
Correct |
2 ms |
8540 KB |
Output is correct |
44 |
Correct |
1 ms |
8540 KB |
Output is correct |
45 |
Correct |
1 ms |
8540 KB |
Output is correct |
46 |
Correct |
2 ms |
8540 KB |
Output is correct |
47 |
Correct |
2 ms |
8536 KB |
Output is correct |
48 |
Correct |
1 ms |
8540 KB |
Output is correct |
49 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
481 ms |
92796 KB |
Output is correct |
3 |
Correct |
507 ms |
92240 KB |
Output is correct |
4 |
Correct |
439 ms |
91984 KB |
Output is correct |
5 |
Correct |
348 ms |
88660 KB |
Output is correct |
6 |
Correct |
225 ms |
88800 KB |
Output is correct |
7 |
Correct |
218 ms |
89032 KB |
Output is correct |
8 |
Correct |
192 ms |
89332 KB |
Output is correct |
9 |
Correct |
488 ms |
91392 KB |
Output is correct |
10 |
Correct |
499 ms |
92756 KB |
Output is correct |
11 |
Correct |
376 ms |
88656 KB |
Output is correct |
12 |
Correct |
400 ms |
89304 KB |
Output is correct |
13 |
Correct |
259 ms |
90064 KB |
Output is correct |
14 |
Correct |
266 ms |
90956 KB |
Output is correct |
15 |
Correct |
2 ms |
8536 KB |
Output is correct |
16 |
Correct |
1 ms |
8536 KB |
Output is correct |
17 |
Correct |
1 ms |
8540 KB |
Output is correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
19 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
473 ms |
93748 KB |
Output is correct |
3 |
Correct |
493 ms |
91356 KB |
Output is correct |
4 |
Correct |
411 ms |
92284 KB |
Output is correct |
5 |
Correct |
343 ms |
90196 KB |
Output is correct |
6 |
Correct |
239 ms |
90704 KB |
Output is correct |
7 |
Correct |
215 ms |
90192 KB |
Output is correct |
8 |
Correct |
202 ms |
90300 KB |
Output is correct |
9 |
Correct |
468 ms |
95052 KB |
Output is correct |
10 |
Correct |
543 ms |
93612 KB |
Output is correct |
11 |
Correct |
381 ms |
90960 KB |
Output is correct |
12 |
Correct |
379 ms |
89428 KB |
Output is correct |
13 |
Correct |
249 ms |
90156 KB |
Output is correct |
14 |
Correct |
252 ms |
91216 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
1 ms |
8540 KB |
Output is correct |
18 |
Correct |
2 ms |
8536 KB |
Output is correct |
19 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8536 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
8540 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8536 KB |
Output is correct |
17 |
Correct |
1 ms |
8540 KB |
Output is correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
19 |
Correct |
1 ms |
8540 KB |
Output is correct |
20 |
Correct |
1 ms |
8540 KB |
Output is correct |
21 |
Correct |
1 ms |
8540 KB |
Output is correct |
22 |
Correct |
1 ms |
8696 KB |
Output is correct |
23 |
Correct |
1 ms |
8540 KB |
Output is correct |
24 |
Correct |
1 ms |
8540 KB |
Output is correct |
25 |
Correct |
2 ms |
8540 KB |
Output is correct |
26 |
Correct |
2 ms |
8540 KB |
Output is correct |
27 |
Correct |
2 ms |
8536 KB |
Output is correct |
28 |
Correct |
2 ms |
8540 KB |
Output is correct |
29 |
Correct |
2 ms |
8540 KB |
Output is correct |
30 |
Correct |
382 ms |
91628 KB |
Output is correct |
31 |
Correct |
225 ms |
91984 KB |
Output is correct |
32 |
Correct |
350 ms |
94112 KB |
Output is correct |
33 |
Correct |
308 ms |
91748 KB |
Output is correct |
34 |
Correct |
209 ms |
91192 KB |
Output is correct |
35 |
Correct |
201 ms |
91984 KB |
Output is correct |
36 |
Correct |
205 ms |
92264 KB |
Output is correct |
37 |
Correct |
440 ms |
95568 KB |
Output is correct |
38 |
Correct |
238 ms |
95828 KB |
Output is correct |
39 |
Correct |
354 ms |
91476 KB |
Output is correct |
40 |
Correct |
211 ms |
91800 KB |
Output is correct |
41 |
Correct |
152 ms |
92052 KB |
Output is correct |
42 |
Correct |
150 ms |
92600 KB |
Output is correct |
43 |
Correct |
148 ms |
91660 KB |
Output is correct |
44 |
Correct |
164 ms |
92248 KB |
Output is correct |
45 |
Correct |
161 ms |
92892 KB |
Output is correct |
46 |
Correct |
2 ms |
8540 KB |
Output is correct |
47 |
Correct |
1 ms |
8640 KB |
Output is correct |
48 |
Correct |
1 ms |
8540 KB |
Output is correct |
49 |
Correct |
2 ms |
8540 KB |
Output is correct |
50 |
Correct |
2 ms |
8692 KB |
Output is correct |
51 |
Correct |
1 ms |
8536 KB |
Output is correct |
52 |
Correct |
374 ms |
91520 KB |
Output is correct |
53 |
Correct |
225 ms |
91932 KB |
Output is correct |
54 |
Correct |
329 ms |
95824 KB |
Output is correct |
55 |
Correct |
295 ms |
91600 KB |
Output is correct |
56 |
Correct |
238 ms |
91708 KB |
Output is correct |
57 |
Correct |
202 ms |
91732 KB |
Output is correct |
58 |
Correct |
182 ms |
92348 KB |
Output is correct |
59 |
Correct |
443 ms |
94032 KB |
Output is correct |
60 |
Correct |
253 ms |
95828 KB |
Output is correct |
61 |
Correct |
368 ms |
91400 KB |
Output is correct |
62 |
Correct |
239 ms |
92076 KB |
Output is correct |
63 |
Correct |
1 ms |
8536 KB |
Output is correct |
64 |
Correct |
1 ms |
8540 KB |
Output is correct |
65 |
Correct |
2 ms |
8540 KB |
Output is correct |
66 |
Correct |
1 ms |
8540 KB |
Output is correct |
67 |
Correct |
1 ms |
8540 KB |
Output is correct |
68 |
Correct |
1 ms |
8540 KB |
Output is correct |
69 |
Correct |
2 ms |
8540 KB |
Output is correct |
70 |
Correct |
2 ms |
8540 KB |
Output is correct |
71 |
Correct |
2 ms |
8540 KB |
Output is correct |
72 |
Correct |
1 ms |
8540 KB |
Output is correct |
73 |
Correct |
1 ms |
8540 KB |
Output is correct |
74 |
Correct |
2 ms |
8540 KB |
Output is correct |
75 |
Correct |
2 ms |
8536 KB |
Output is correct |
76 |
Correct |
1 ms |
8540 KB |
Output is correct |
77 |
Correct |
2 ms |
8540 KB |
Output is correct |
78 |
Correct |
2 ms |
8540 KB |
Output is correct |
79 |
Correct |
481 ms |
92796 KB |
Output is correct |
80 |
Correct |
507 ms |
92240 KB |
Output is correct |
81 |
Correct |
439 ms |
91984 KB |
Output is correct |
82 |
Correct |
348 ms |
88660 KB |
Output is correct |
83 |
Correct |
225 ms |
88800 KB |
Output is correct |
84 |
Correct |
218 ms |
89032 KB |
Output is correct |
85 |
Correct |
192 ms |
89332 KB |
Output is correct |
86 |
Correct |
488 ms |
91392 KB |
Output is correct |
87 |
Correct |
499 ms |
92756 KB |
Output is correct |
88 |
Correct |
376 ms |
88656 KB |
Output is correct |
89 |
Correct |
400 ms |
89304 KB |
Output is correct |
90 |
Correct |
259 ms |
90064 KB |
Output is correct |
91 |
Correct |
266 ms |
90956 KB |
Output is correct |
92 |
Correct |
2 ms |
8536 KB |
Output is correct |
93 |
Correct |
1 ms |
8536 KB |
Output is correct |
94 |
Correct |
1 ms |
8540 KB |
Output is correct |
95 |
Correct |
1 ms |
8540 KB |
Output is correct |
96 |
Correct |
1 ms |
8540 KB |
Output is correct |
97 |
Correct |
1 ms |
8540 KB |
Output is correct |
98 |
Correct |
473 ms |
93748 KB |
Output is correct |
99 |
Correct |
493 ms |
91356 KB |
Output is correct |
100 |
Correct |
411 ms |
92284 KB |
Output is correct |
101 |
Correct |
343 ms |
90196 KB |
Output is correct |
102 |
Correct |
239 ms |
90704 KB |
Output is correct |
103 |
Correct |
215 ms |
90192 KB |
Output is correct |
104 |
Correct |
202 ms |
90300 KB |
Output is correct |
105 |
Correct |
468 ms |
95052 KB |
Output is correct |
106 |
Correct |
543 ms |
93612 KB |
Output is correct |
107 |
Correct |
381 ms |
90960 KB |
Output is correct |
108 |
Correct |
379 ms |
89428 KB |
Output is correct |
109 |
Correct |
249 ms |
90156 KB |
Output is correct |
110 |
Correct |
252 ms |
91216 KB |
Output is correct |
111 |
Correct |
2 ms |
8540 KB |
Output is correct |
112 |
Correct |
1 ms |
8540 KB |
Output is correct |
113 |
Correct |
1 ms |
8540 KB |
Output is correct |
114 |
Correct |
2 ms |
8536 KB |
Output is correct |
115 |
Correct |
1 ms |
8540 KB |
Output is correct |
116 |
Correct |
377 ms |
89444 KB |
Output is correct |
117 |
Correct |
402 ms |
91988 KB |
Output is correct |
118 |
Correct |
449 ms |
95740 KB |
Output is correct |
119 |
Correct |
362 ms |
91728 KB |
Output is correct |
120 |
Correct |
236 ms |
91476 KB |
Output is correct |
121 |
Correct |
242 ms |
92300 KB |
Output is correct |
122 |
Correct |
190 ms |
92352 KB |
Output is correct |
123 |
Correct |
496 ms |
94804 KB |
Output is correct |
124 |
Correct |
563 ms |
94032 KB |
Output is correct |
125 |
Correct |
395 ms |
90708 KB |
Output is correct |
126 |
Correct |
453 ms |
91980 KB |
Output is correct |
127 |
Correct |
429 ms |
92496 KB |
Output is correct |
128 |
Correct |
323 ms |
93148 KB |
Output is correct |
129 |
Correct |
305 ms |
93984 KB |
Output is correct |