#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector <ll>;
using ii = pair <ll, ll>;
using vii = vector <ii>;
const ll MAXN = 2.1E5, MAXD = 42;
vll adj[MAXN];
ll L;
ll ve[MAXN];
ll dp[MAXN][MAXD]; // children of u at depth j should be multed by dp[u][j]
ll par[MAXN], dep[MAXN];
void dfs (ll u, ll par) {
::par[u] = par;
for (ll d = 0; d < 0+MAXD; d++) dp[u][d] = 1;
for (ll v : adj[u]) {
if (v == par) continue;
dep[v] = dep[u]+1;
dfs(v, u);
}
}
int main () {
cin.tie(nullptr) -> sync_with_stdio(false);
ll n;
cin >> n >> L;
for (ll i = 1; i < n; i++) {
ll u, v;
cin >> u >> v;
u += 50; v += 50;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (ll i = 1+50; i <= n+50; i++) cin >> ve[i];
for (ll i = 0; i <= 50; i++) adj[i].push_back(i+1);
dep[0] = 0;
dfs(0, 0);
ll Q;
cin >> Q;
for (ll q = 0; q < Q; q++) {
char type;
cin >> type;
switch (type) {
case '1':
{ll u, maxD, w;
cin >> u >> maxD >> w;
u += 50;
ll uo = u;
for (ll d = 0; d < maxD; d++) {
(dp[u][dep[uo]-dep[u]+maxD-2*d] *= w) %= L;
(dp[u][dep[uo]-dep[u]+maxD-2*d-1] *= w) %= L;
u = par[u];
}
(dp[u][dep[u]-dep[u]] *= w) %= L;
break;}
case '2':
{ll u;
cin >> u;
u += 50;
ll uo = u;
ll ans = ve[u];
for (ll i = 0; i < MAXD; i++, u = par[u]) {
(ans *= dp[u][dep[uo]-dep[u]]) %= L;
}
cout << ans << '\n';
break;}
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
11352 KB |
Output is correct |
2 |
Correct |
15 ms |
11468 KB |
Output is correct |
3 |
Correct |
16 ms |
11356 KB |
Output is correct |
4 |
Correct |
2 ms |
11524 KB |
Output is correct |
5 |
Correct |
2 ms |
11548 KB |
Output is correct |
6 |
Correct |
2 ms |
11528 KB |
Output is correct |
7 |
Correct |
2 ms |
11528 KB |
Output is correct |
8 |
Correct |
3 ms |
11476 KB |
Output is correct |
9 |
Correct |
2 ms |
11356 KB |
Output is correct |
10 |
Correct |
2 ms |
11356 KB |
Output is correct |
11 |
Correct |
2 ms |
11356 KB |
Output is correct |
12 |
Correct |
2 ms |
11468 KB |
Output is correct |
13 |
Correct |
2 ms |
11356 KB |
Output is correct |
14 |
Correct |
2 ms |
11356 KB |
Output is correct |
15 |
Correct |
2 ms |
11468 KB |
Output is correct |
16 |
Correct |
2 ms |
11356 KB |
Output is correct |
17 |
Correct |
2 ms |
11356 KB |
Output is correct |
18 |
Correct |
2 ms |
11356 KB |
Output is correct |
19 |
Correct |
2 ms |
11356 KB |
Output is correct |
20 |
Correct |
2 ms |
11356 KB |
Output is correct |
21 |
Correct |
2 ms |
11356 KB |
Output is correct |
22 |
Correct |
2 ms |
11476 KB |
Output is correct |
23 |
Correct |
2 ms |
11456 KB |
Output is correct |
24 |
Correct |
2 ms |
11356 KB |
Output is correct |
25 |
Correct |
2 ms |
11356 KB |
Output is correct |
26 |
Correct |
2 ms |
11356 KB |
Output is correct |
27 |
Correct |
2 ms |
11356 KB |
Output is correct |
28 |
Correct |
2 ms |
11356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11356 KB |
Output is correct |
2 |
Correct |
508 ms |
91692 KB |
Output is correct |
3 |
Correct |
276 ms |
84820 KB |
Output is correct |
4 |
Correct |
424 ms |
90448 KB |
Output is correct |
5 |
Correct |
433 ms |
85588 KB |
Output is correct |
6 |
Correct |
298 ms |
85588 KB |
Output is correct |
7 |
Correct |
296 ms |
85896 KB |
Output is correct |
8 |
Correct |
248 ms |
86204 KB |
Output is correct |
9 |
Correct |
593 ms |
95128 KB |
Output is correct |
10 |
Correct |
323 ms |
102996 KB |
Output is correct |
11 |
Correct |
532 ms |
95312 KB |
Output is correct |
12 |
Correct |
284 ms |
95844 KB |
Output is correct |
13 |
Correct |
172 ms |
96748 KB |
Output is correct |
14 |
Correct |
182 ms |
96448 KB |
Output is correct |
15 |
Correct |
186 ms |
95812 KB |
Output is correct |
16 |
Correct |
189 ms |
96264 KB |
Output is correct |
17 |
Correct |
193 ms |
96848 KB |
Output is correct |
18 |
Correct |
2 ms |
11356 KB |
Output is correct |
19 |
Correct |
2 ms |
11468 KB |
Output is correct |
20 |
Correct |
2 ms |
11352 KB |
Output is correct |
21 |
Correct |
2 ms |
11356 KB |
Output is correct |
22 |
Correct |
2 ms |
11356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11356 KB |
Output is correct |
2 |
Correct |
508 ms |
91692 KB |
Output is correct |
3 |
Correct |
276 ms |
84820 KB |
Output is correct |
4 |
Correct |
424 ms |
90448 KB |
Output is correct |
5 |
Correct |
433 ms |
85588 KB |
Output is correct |
6 |
Correct |
298 ms |
85588 KB |
Output is correct |
7 |
Correct |
296 ms |
85896 KB |
Output is correct |
8 |
Correct |
248 ms |
86204 KB |
Output is correct |
9 |
Correct |
593 ms |
95128 KB |
Output is correct |
10 |
Correct |
323 ms |
102996 KB |
Output is correct |
11 |
Correct |
532 ms |
95312 KB |
Output is correct |
12 |
Correct |
284 ms |
95844 KB |
Output is correct |
13 |
Correct |
172 ms |
96748 KB |
Output is correct |
14 |
Correct |
182 ms |
96448 KB |
Output is correct |
15 |
Correct |
186 ms |
95812 KB |
Output is correct |
16 |
Correct |
189 ms |
96264 KB |
Output is correct |
17 |
Correct |
193 ms |
96848 KB |
Output is correct |
18 |
Correct |
2 ms |
11356 KB |
Output is correct |
19 |
Correct |
2 ms |
11468 KB |
Output is correct |
20 |
Correct |
2 ms |
11352 KB |
Output is correct |
21 |
Correct |
2 ms |
11356 KB |
Output is correct |
22 |
Correct |
2 ms |
11356 KB |
Output is correct |
23 |
Correct |
2 ms |
11356 KB |
Output is correct |
24 |
Correct |
491 ms |
95352 KB |
Output is correct |
25 |
Correct |
304 ms |
96036 KB |
Output is correct |
26 |
Correct |
461 ms |
103256 KB |
Output is correct |
27 |
Correct |
411 ms |
96556 KB |
Output is correct |
28 |
Correct |
300 ms |
96036 KB |
Output is correct |
29 |
Correct |
292 ms |
96420 KB |
Output is correct |
30 |
Correct |
225 ms |
96188 KB |
Output is correct |
31 |
Correct |
543 ms |
100644 KB |
Output is correct |
32 |
Correct |
296 ms |
103200 KB |
Output is correct |
33 |
Correct |
494 ms |
96300 KB |
Output is correct |
34 |
Correct |
268 ms |
95828 KB |
Output is correct |
35 |
Correct |
2 ms |
11352 KB |
Output is correct |
36 |
Correct |
3 ms |
11356 KB |
Output is correct |
37 |
Correct |
2 ms |
11352 KB |
Output is correct |
38 |
Correct |
2 ms |
11476 KB |
Output is correct |
39 |
Correct |
2 ms |
11356 KB |
Output is correct |
40 |
Correct |
2 ms |
11360 KB |
Output is correct |
41 |
Correct |
2 ms |
11356 KB |
Output is correct |
42 |
Correct |
2 ms |
11356 KB |
Output is correct |
43 |
Correct |
2 ms |
11352 KB |
Output is correct |
44 |
Correct |
2 ms |
11352 KB |
Output is correct |
45 |
Correct |
2 ms |
11356 KB |
Output is correct |
46 |
Correct |
2 ms |
11356 KB |
Output is correct |
47 |
Correct |
2 ms |
11356 KB |
Output is correct |
48 |
Correct |
2 ms |
11356 KB |
Output is correct |
49 |
Correct |
2 ms |
11356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11352 KB |
Output is correct |
2 |
Correct |
615 ms |
92512 KB |
Output is correct |
3 |
Correct |
662 ms |
99924 KB |
Output is correct |
4 |
Correct |
502 ms |
98896 KB |
Output is correct |
5 |
Correct |
434 ms |
93500 KB |
Output is correct |
6 |
Correct |
300 ms |
92756 KB |
Output is correct |
7 |
Correct |
296 ms |
92956 KB |
Output is correct |
8 |
Correct |
245 ms |
93372 KB |
Output is correct |
9 |
Correct |
594 ms |
97872 KB |
Output is correct |
10 |
Correct |
653 ms |
100092 KB |
Output is correct |
11 |
Correct |
538 ms |
92292 KB |
Output is correct |
12 |
Correct |
545 ms |
93232 KB |
Output is correct |
13 |
Correct |
360 ms |
94156 KB |
Output is correct |
14 |
Correct |
372 ms |
94992 KB |
Output is correct |
15 |
Correct |
2 ms |
11356 KB |
Output is correct |
16 |
Correct |
2 ms |
11356 KB |
Output is correct |
17 |
Correct |
2 ms |
11356 KB |
Output is correct |
18 |
Correct |
2 ms |
11356 KB |
Output is correct |
19 |
Correct |
2 ms |
11356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11356 KB |
Output is correct |
2 |
Correct |
617 ms |
92200 KB |
Output is correct |
3 |
Correct |
655 ms |
97104 KB |
Output is correct |
4 |
Correct |
495 ms |
98468 KB |
Output is correct |
5 |
Correct |
445 ms |
95312 KB |
Output is correct |
6 |
Correct |
298 ms |
94424 KB |
Output is correct |
7 |
Correct |
277 ms |
94292 KB |
Output is correct |
8 |
Correct |
235 ms |
94400 KB |
Output is correct |
9 |
Correct |
584 ms |
102368 KB |
Output is correct |
10 |
Correct |
660 ms |
100860 KB |
Output is correct |
11 |
Correct |
561 ms |
94928 KB |
Output is correct |
12 |
Correct |
553 ms |
93524 KB |
Output is correct |
13 |
Correct |
355 ms |
94256 KB |
Output is correct |
14 |
Correct |
363 ms |
95056 KB |
Output is correct |
15 |
Correct |
2 ms |
11356 KB |
Output is correct |
16 |
Correct |
2 ms |
11356 KB |
Output is correct |
17 |
Correct |
2 ms |
11432 KB |
Output is correct |
18 |
Correct |
2 ms |
11356 KB |
Output is correct |
19 |
Correct |
2 ms |
11476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
11352 KB |
Output is correct |
2 |
Correct |
15 ms |
11468 KB |
Output is correct |
3 |
Correct |
16 ms |
11356 KB |
Output is correct |
4 |
Correct |
2 ms |
11524 KB |
Output is correct |
5 |
Correct |
2 ms |
11548 KB |
Output is correct |
6 |
Correct |
2 ms |
11528 KB |
Output is correct |
7 |
Correct |
2 ms |
11528 KB |
Output is correct |
8 |
Correct |
3 ms |
11476 KB |
Output is correct |
9 |
Correct |
2 ms |
11356 KB |
Output is correct |
10 |
Correct |
2 ms |
11356 KB |
Output is correct |
11 |
Correct |
2 ms |
11356 KB |
Output is correct |
12 |
Correct |
2 ms |
11468 KB |
Output is correct |
13 |
Correct |
2 ms |
11356 KB |
Output is correct |
14 |
Correct |
2 ms |
11356 KB |
Output is correct |
15 |
Correct |
2 ms |
11468 KB |
Output is correct |
16 |
Correct |
2 ms |
11356 KB |
Output is correct |
17 |
Correct |
2 ms |
11356 KB |
Output is correct |
18 |
Correct |
2 ms |
11356 KB |
Output is correct |
19 |
Correct |
2 ms |
11356 KB |
Output is correct |
20 |
Correct |
2 ms |
11356 KB |
Output is correct |
21 |
Correct |
2 ms |
11356 KB |
Output is correct |
22 |
Correct |
2 ms |
11476 KB |
Output is correct |
23 |
Correct |
2 ms |
11456 KB |
Output is correct |
24 |
Correct |
2 ms |
11356 KB |
Output is correct |
25 |
Correct |
2 ms |
11356 KB |
Output is correct |
26 |
Correct |
2 ms |
11356 KB |
Output is correct |
27 |
Correct |
2 ms |
11356 KB |
Output is correct |
28 |
Correct |
2 ms |
11356 KB |
Output is correct |
29 |
Correct |
2 ms |
11356 KB |
Output is correct |
30 |
Correct |
508 ms |
91692 KB |
Output is correct |
31 |
Correct |
276 ms |
84820 KB |
Output is correct |
32 |
Correct |
424 ms |
90448 KB |
Output is correct |
33 |
Correct |
433 ms |
85588 KB |
Output is correct |
34 |
Correct |
298 ms |
85588 KB |
Output is correct |
35 |
Correct |
296 ms |
85896 KB |
Output is correct |
36 |
Correct |
248 ms |
86204 KB |
Output is correct |
37 |
Correct |
593 ms |
95128 KB |
Output is correct |
38 |
Correct |
323 ms |
102996 KB |
Output is correct |
39 |
Correct |
532 ms |
95312 KB |
Output is correct |
40 |
Correct |
284 ms |
95844 KB |
Output is correct |
41 |
Correct |
172 ms |
96748 KB |
Output is correct |
42 |
Correct |
182 ms |
96448 KB |
Output is correct |
43 |
Correct |
186 ms |
95812 KB |
Output is correct |
44 |
Correct |
189 ms |
96264 KB |
Output is correct |
45 |
Correct |
193 ms |
96848 KB |
Output is correct |
46 |
Correct |
2 ms |
11356 KB |
Output is correct |
47 |
Correct |
2 ms |
11468 KB |
Output is correct |
48 |
Correct |
2 ms |
11352 KB |
Output is correct |
49 |
Correct |
2 ms |
11356 KB |
Output is correct |
50 |
Correct |
2 ms |
11356 KB |
Output is correct |
51 |
Correct |
2 ms |
11356 KB |
Output is correct |
52 |
Correct |
491 ms |
95352 KB |
Output is correct |
53 |
Correct |
304 ms |
96036 KB |
Output is correct |
54 |
Correct |
461 ms |
103256 KB |
Output is correct |
55 |
Correct |
411 ms |
96556 KB |
Output is correct |
56 |
Correct |
300 ms |
96036 KB |
Output is correct |
57 |
Correct |
292 ms |
96420 KB |
Output is correct |
58 |
Correct |
225 ms |
96188 KB |
Output is correct |
59 |
Correct |
543 ms |
100644 KB |
Output is correct |
60 |
Correct |
296 ms |
103200 KB |
Output is correct |
61 |
Correct |
494 ms |
96300 KB |
Output is correct |
62 |
Correct |
268 ms |
95828 KB |
Output is correct |
63 |
Correct |
2 ms |
11352 KB |
Output is correct |
64 |
Correct |
3 ms |
11356 KB |
Output is correct |
65 |
Correct |
2 ms |
11352 KB |
Output is correct |
66 |
Correct |
2 ms |
11476 KB |
Output is correct |
67 |
Correct |
2 ms |
11356 KB |
Output is correct |
68 |
Correct |
2 ms |
11360 KB |
Output is correct |
69 |
Correct |
2 ms |
11356 KB |
Output is correct |
70 |
Correct |
2 ms |
11356 KB |
Output is correct |
71 |
Correct |
2 ms |
11352 KB |
Output is correct |
72 |
Correct |
2 ms |
11352 KB |
Output is correct |
73 |
Correct |
2 ms |
11356 KB |
Output is correct |
74 |
Correct |
2 ms |
11356 KB |
Output is correct |
75 |
Correct |
2 ms |
11356 KB |
Output is correct |
76 |
Correct |
2 ms |
11356 KB |
Output is correct |
77 |
Correct |
2 ms |
11356 KB |
Output is correct |
78 |
Correct |
2 ms |
11352 KB |
Output is correct |
79 |
Correct |
615 ms |
92512 KB |
Output is correct |
80 |
Correct |
662 ms |
99924 KB |
Output is correct |
81 |
Correct |
502 ms |
98896 KB |
Output is correct |
82 |
Correct |
434 ms |
93500 KB |
Output is correct |
83 |
Correct |
300 ms |
92756 KB |
Output is correct |
84 |
Correct |
296 ms |
92956 KB |
Output is correct |
85 |
Correct |
245 ms |
93372 KB |
Output is correct |
86 |
Correct |
594 ms |
97872 KB |
Output is correct |
87 |
Correct |
653 ms |
100092 KB |
Output is correct |
88 |
Correct |
538 ms |
92292 KB |
Output is correct |
89 |
Correct |
545 ms |
93232 KB |
Output is correct |
90 |
Correct |
360 ms |
94156 KB |
Output is correct |
91 |
Correct |
372 ms |
94992 KB |
Output is correct |
92 |
Correct |
2 ms |
11356 KB |
Output is correct |
93 |
Correct |
2 ms |
11356 KB |
Output is correct |
94 |
Correct |
2 ms |
11356 KB |
Output is correct |
95 |
Correct |
2 ms |
11356 KB |
Output is correct |
96 |
Correct |
2 ms |
11356 KB |
Output is correct |
97 |
Correct |
2 ms |
11356 KB |
Output is correct |
98 |
Correct |
617 ms |
92200 KB |
Output is correct |
99 |
Correct |
655 ms |
97104 KB |
Output is correct |
100 |
Correct |
495 ms |
98468 KB |
Output is correct |
101 |
Correct |
445 ms |
95312 KB |
Output is correct |
102 |
Correct |
298 ms |
94424 KB |
Output is correct |
103 |
Correct |
277 ms |
94292 KB |
Output is correct |
104 |
Correct |
235 ms |
94400 KB |
Output is correct |
105 |
Correct |
584 ms |
102368 KB |
Output is correct |
106 |
Correct |
660 ms |
100860 KB |
Output is correct |
107 |
Correct |
561 ms |
94928 KB |
Output is correct |
108 |
Correct |
553 ms |
93524 KB |
Output is correct |
109 |
Correct |
355 ms |
94256 KB |
Output is correct |
110 |
Correct |
363 ms |
95056 KB |
Output is correct |
111 |
Correct |
2 ms |
11356 KB |
Output is correct |
112 |
Correct |
2 ms |
11356 KB |
Output is correct |
113 |
Correct |
2 ms |
11432 KB |
Output is correct |
114 |
Correct |
2 ms |
11356 KB |
Output is correct |
115 |
Correct |
2 ms |
11476 KB |
Output is correct |
116 |
Correct |
548 ms |
93472 KB |
Output is correct |
117 |
Correct |
577 ms |
96080 KB |
Output is correct |
118 |
Correct |
576 ms |
103248 KB |
Output is correct |
119 |
Correct |
492 ms |
95824 KB |
Output is correct |
120 |
Correct |
339 ms |
95316 KB |
Output is correct |
121 |
Correct |
331 ms |
97104 KB |
Output is correct |
122 |
Correct |
267 ms |
96248 KB |
Output is correct |
123 |
Correct |
602 ms |
101640 KB |
Output is correct |
124 |
Correct |
610 ms |
100080 KB |
Output is correct |
125 |
Correct |
527 ms |
94544 KB |
Output is correct |
126 |
Correct |
537 ms |
96008 KB |
Output is correct |
127 |
Correct |
592 ms |
96336 KB |
Output is correct |
128 |
Correct |
356 ms |
97048 KB |
Output is correct |
129 |
Correct |
411 ms |
97936 KB |
Output is correct |