#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define endl '\n'
#define pb push_back
using pi = array<int, 2>;
const int N = 2e5 + 100;
vector<int> adj[N];
int par[N];
const int D = 42;
int h[N], wch[D][N];
void dfs(int u) {
for (int v : adj[u]) {
if (v == par[u]) continue;
par[v] = u;
dfs(v);
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, l;
cin >> n >> l;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
for (int i = 0; i <= n + D; ++i) {
for (int d = 0; d < D; ++d) wch[d][i] = 1;
}
for (int d = 0; d < D - 1; ++d) {
int here = n + d + 1;
adj[here].pb(here + 1);
adj[here + 1].pb(here);
}
adj[n + D].pb(1);
adj[1].pb(n + D);
dfs(n + 1);
for (int i = 1; i <= n; ++i) cin >> h[i];
int q;
cin >> q;
while (q--) {
int t;
cin >> t;
if (t == 1) {
int u, d, w;
cin >> u >> d >> w;
auto mult = [&](int& var) {
var = var * w % l;
};
int p = u;
for (int i = 0; i <= d; ++i) {
int descend = d - i;
mult(wch[descend][p]);
if (descend > 0) mult(wch[descend - 1][p]);
p = par[p];
}
} else {
int u;
cin >> u;
//cout << u << "!\n";
int p = u;
int ans = h[u];
for (int d = 0; d < D; ++d) {
//cout << p << " | " << d << " | " << wch[d][p] << endl;
ans = ans * wch[d][p] % l;
p = par[p];
}
cout << ans << endl;
}
}
}
/*
4 7
1 2
2 3
3 4
1
1
1
1
5
1 4 8 2
2 1
2 2
2 3
2 4
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
20572 KB |
Output is correct |
2 |
Correct |
6 ms |
22828 KB |
Output is correct |
3 |
Correct |
6 ms |
22620 KB |
Output is correct |
4 |
Correct |
5 ms |
23188 KB |
Output is correct |
5 |
Correct |
5 ms |
23132 KB |
Output is correct |
6 |
Correct |
5 ms |
21088 KB |
Output is correct |
7 |
Correct |
5 ms |
19036 KB |
Output is correct |
8 |
Correct |
5 ms |
19040 KB |
Output is correct |
9 |
Correct |
4 ms |
16728 KB |
Output is correct |
10 |
Correct |
5 ms |
18820 KB |
Output is correct |
11 |
Correct |
5 ms |
18564 KB |
Output is correct |
12 |
Correct |
4 ms |
16684 KB |
Output is correct |
13 |
Correct |
3 ms |
14684 KB |
Output is correct |
14 |
Correct |
4 ms |
14684 KB |
Output is correct |
15 |
Correct |
3 ms |
14684 KB |
Output is correct |
16 |
Correct |
3 ms |
12636 KB |
Output is correct |
17 |
Correct |
3 ms |
14684 KB |
Output is correct |
18 |
Correct |
4 ms |
14836 KB |
Output is correct |
19 |
Correct |
4 ms |
14684 KB |
Output is correct |
20 |
Correct |
4 ms |
14888 KB |
Output is correct |
21 |
Correct |
3 ms |
14680 KB |
Output is correct |
22 |
Correct |
3 ms |
14684 KB |
Output is correct |
23 |
Correct |
3 ms |
14680 KB |
Output is correct |
24 |
Correct |
3 ms |
14684 KB |
Output is correct |
25 |
Correct |
3 ms |
14684 KB |
Output is correct |
26 |
Correct |
3 ms |
14684 KB |
Output is correct |
27 |
Correct |
3 ms |
14680 KB |
Output is correct |
28 |
Correct |
3 ms |
14684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14424 KB |
Output is correct |
2 |
Correct |
556 ms |
92944 KB |
Output is correct |
3 |
Correct |
306 ms |
93612 KB |
Output is correct |
4 |
Correct |
430 ms |
95720 KB |
Output is correct |
5 |
Correct |
398 ms |
93248 KB |
Output is correct |
6 |
Correct |
281 ms |
92756 KB |
Output is correct |
7 |
Correct |
267 ms |
93640 KB |
Output is correct |
8 |
Correct |
225 ms |
93948 KB |
Output is correct |
9 |
Correct |
515 ms |
97504 KB |
Output is correct |
10 |
Correct |
272 ms |
97444 KB |
Output is correct |
11 |
Correct |
546 ms |
92908 KB |
Output is correct |
12 |
Correct |
286 ms |
93588 KB |
Output is correct |
13 |
Correct |
207 ms |
93704 KB |
Output is correct |
14 |
Correct |
193 ms |
94168 KB |
Output is correct |
15 |
Correct |
230 ms |
93332 KB |
Output is correct |
16 |
Correct |
191 ms |
93776 KB |
Output is correct |
17 |
Correct |
195 ms |
94288 KB |
Output is correct |
18 |
Correct |
3 ms |
14644 KB |
Output is correct |
19 |
Correct |
3 ms |
14684 KB |
Output is correct |
20 |
Correct |
3 ms |
14684 KB |
Output is correct |
21 |
Correct |
4 ms |
14684 KB |
Output is correct |
22 |
Correct |
3 ms |
14680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14424 KB |
Output is correct |
2 |
Correct |
556 ms |
92944 KB |
Output is correct |
3 |
Correct |
306 ms |
93612 KB |
Output is correct |
4 |
Correct |
430 ms |
95720 KB |
Output is correct |
5 |
Correct |
398 ms |
93248 KB |
Output is correct |
6 |
Correct |
281 ms |
92756 KB |
Output is correct |
7 |
Correct |
267 ms |
93640 KB |
Output is correct |
8 |
Correct |
225 ms |
93948 KB |
Output is correct |
9 |
Correct |
515 ms |
97504 KB |
Output is correct |
10 |
Correct |
272 ms |
97444 KB |
Output is correct |
11 |
Correct |
546 ms |
92908 KB |
Output is correct |
12 |
Correct |
286 ms |
93588 KB |
Output is correct |
13 |
Correct |
207 ms |
93704 KB |
Output is correct |
14 |
Correct |
193 ms |
94168 KB |
Output is correct |
15 |
Correct |
230 ms |
93332 KB |
Output is correct |
16 |
Correct |
191 ms |
93776 KB |
Output is correct |
17 |
Correct |
195 ms |
94288 KB |
Output is correct |
18 |
Correct |
3 ms |
14644 KB |
Output is correct |
19 |
Correct |
3 ms |
14684 KB |
Output is correct |
20 |
Correct |
3 ms |
14684 KB |
Output is correct |
21 |
Correct |
4 ms |
14684 KB |
Output is correct |
22 |
Correct |
3 ms |
14680 KB |
Output is correct |
23 |
Correct |
3 ms |
14424 KB |
Output is correct |
24 |
Correct |
524 ms |
93016 KB |
Output is correct |
25 |
Correct |
320 ms |
93524 KB |
Output is correct |
26 |
Correct |
484 ms |
97280 KB |
Output is correct |
27 |
Correct |
486 ms |
93164 KB |
Output is correct |
28 |
Correct |
310 ms |
93624 KB |
Output is correct |
29 |
Correct |
296 ms |
93216 KB |
Output is correct |
30 |
Correct |
212 ms |
93884 KB |
Output is correct |
31 |
Correct |
544 ms |
95716 KB |
Output is correct |
32 |
Correct |
341 ms |
97484 KB |
Output is correct |
33 |
Correct |
473 ms |
93036 KB |
Output is correct |
34 |
Correct |
267 ms |
93520 KB |
Output is correct |
35 |
Correct |
4 ms |
14684 KB |
Output is correct |
36 |
Correct |
3 ms |
14684 KB |
Output is correct |
37 |
Correct |
3 ms |
14640 KB |
Output is correct |
38 |
Correct |
4 ms |
14684 KB |
Output is correct |
39 |
Correct |
3 ms |
14684 KB |
Output is correct |
40 |
Correct |
3 ms |
14680 KB |
Output is correct |
41 |
Correct |
3 ms |
14636 KB |
Output is correct |
42 |
Correct |
3 ms |
14684 KB |
Output is correct |
43 |
Correct |
3 ms |
14684 KB |
Output is correct |
44 |
Correct |
3 ms |
14680 KB |
Output is correct |
45 |
Correct |
3 ms |
14684 KB |
Output is correct |
46 |
Correct |
3 ms |
14692 KB |
Output is correct |
47 |
Correct |
3 ms |
14684 KB |
Output is correct |
48 |
Correct |
3 ms |
14684 KB |
Output is correct |
49 |
Correct |
3 ms |
14684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14428 KB |
Output is correct |
2 |
Correct |
610 ms |
94536 KB |
Output is correct |
3 |
Correct |
743 ms |
93696 KB |
Output is correct |
4 |
Correct |
511 ms |
93440 KB |
Output is correct |
5 |
Correct |
538 ms |
90204 KB |
Output is correct |
6 |
Correct |
335 ms |
90452 KB |
Output is correct |
7 |
Correct |
333 ms |
90708 KB |
Output is correct |
8 |
Correct |
266 ms |
90832 KB |
Output is correct |
9 |
Correct |
641 ms |
92816 KB |
Output is correct |
10 |
Correct |
712 ms |
94560 KB |
Output is correct |
11 |
Correct |
524 ms |
90004 KB |
Output is correct |
12 |
Correct |
703 ms |
90716 KB |
Output is correct |
13 |
Correct |
501 ms |
91784 KB |
Output is correct |
14 |
Correct |
515 ms |
92540 KB |
Output is correct |
15 |
Correct |
3 ms |
14680 KB |
Output is correct |
16 |
Correct |
3 ms |
14680 KB |
Output is correct |
17 |
Correct |
3 ms |
14684 KB |
Output is correct |
18 |
Correct |
3 ms |
14684 KB |
Output is correct |
19 |
Correct |
3 ms |
14688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14428 KB |
Output is correct |
2 |
Correct |
590 ms |
95332 KB |
Output is correct |
3 |
Correct |
729 ms |
92876 KB |
Output is correct |
4 |
Correct |
535 ms |
93940 KB |
Output is correct |
5 |
Correct |
540 ms |
91984 KB |
Output is correct |
6 |
Correct |
364 ms |
92044 KB |
Output is correct |
7 |
Correct |
368 ms |
91968 KB |
Output is correct |
8 |
Correct |
273 ms |
91836 KB |
Output is correct |
9 |
Correct |
676 ms |
96640 KB |
Output is correct |
10 |
Correct |
738 ms |
95032 KB |
Output is correct |
11 |
Correct |
655 ms |
92588 KB |
Output is correct |
12 |
Correct |
730 ms |
91220 KB |
Output is correct |
13 |
Correct |
571 ms |
91908 KB |
Output is correct |
14 |
Correct |
607 ms |
92684 KB |
Output is correct |
15 |
Correct |
3 ms |
14680 KB |
Output is correct |
16 |
Correct |
3 ms |
14636 KB |
Output is correct |
17 |
Correct |
3 ms |
14684 KB |
Output is correct |
18 |
Correct |
3 ms |
14684 KB |
Output is correct |
19 |
Correct |
3 ms |
14684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
20572 KB |
Output is correct |
2 |
Correct |
6 ms |
22828 KB |
Output is correct |
3 |
Correct |
6 ms |
22620 KB |
Output is correct |
4 |
Correct |
5 ms |
23188 KB |
Output is correct |
5 |
Correct |
5 ms |
23132 KB |
Output is correct |
6 |
Correct |
5 ms |
21088 KB |
Output is correct |
7 |
Correct |
5 ms |
19036 KB |
Output is correct |
8 |
Correct |
5 ms |
19040 KB |
Output is correct |
9 |
Correct |
4 ms |
16728 KB |
Output is correct |
10 |
Correct |
5 ms |
18820 KB |
Output is correct |
11 |
Correct |
5 ms |
18564 KB |
Output is correct |
12 |
Correct |
4 ms |
16684 KB |
Output is correct |
13 |
Correct |
3 ms |
14684 KB |
Output is correct |
14 |
Correct |
4 ms |
14684 KB |
Output is correct |
15 |
Correct |
3 ms |
14684 KB |
Output is correct |
16 |
Correct |
3 ms |
12636 KB |
Output is correct |
17 |
Correct |
3 ms |
14684 KB |
Output is correct |
18 |
Correct |
4 ms |
14836 KB |
Output is correct |
19 |
Correct |
4 ms |
14684 KB |
Output is correct |
20 |
Correct |
4 ms |
14888 KB |
Output is correct |
21 |
Correct |
3 ms |
14680 KB |
Output is correct |
22 |
Correct |
3 ms |
14684 KB |
Output is correct |
23 |
Correct |
3 ms |
14680 KB |
Output is correct |
24 |
Correct |
3 ms |
14684 KB |
Output is correct |
25 |
Correct |
3 ms |
14684 KB |
Output is correct |
26 |
Correct |
3 ms |
14684 KB |
Output is correct |
27 |
Correct |
3 ms |
14680 KB |
Output is correct |
28 |
Correct |
3 ms |
14684 KB |
Output is correct |
29 |
Correct |
3 ms |
14424 KB |
Output is correct |
30 |
Correct |
556 ms |
92944 KB |
Output is correct |
31 |
Correct |
306 ms |
93612 KB |
Output is correct |
32 |
Correct |
430 ms |
95720 KB |
Output is correct |
33 |
Correct |
398 ms |
93248 KB |
Output is correct |
34 |
Correct |
281 ms |
92756 KB |
Output is correct |
35 |
Correct |
267 ms |
93640 KB |
Output is correct |
36 |
Correct |
225 ms |
93948 KB |
Output is correct |
37 |
Correct |
515 ms |
97504 KB |
Output is correct |
38 |
Correct |
272 ms |
97444 KB |
Output is correct |
39 |
Correct |
546 ms |
92908 KB |
Output is correct |
40 |
Correct |
286 ms |
93588 KB |
Output is correct |
41 |
Correct |
207 ms |
93704 KB |
Output is correct |
42 |
Correct |
193 ms |
94168 KB |
Output is correct |
43 |
Correct |
230 ms |
93332 KB |
Output is correct |
44 |
Correct |
191 ms |
93776 KB |
Output is correct |
45 |
Correct |
195 ms |
94288 KB |
Output is correct |
46 |
Correct |
3 ms |
14644 KB |
Output is correct |
47 |
Correct |
3 ms |
14684 KB |
Output is correct |
48 |
Correct |
3 ms |
14684 KB |
Output is correct |
49 |
Correct |
4 ms |
14684 KB |
Output is correct |
50 |
Correct |
3 ms |
14680 KB |
Output is correct |
51 |
Correct |
3 ms |
14424 KB |
Output is correct |
52 |
Correct |
524 ms |
93016 KB |
Output is correct |
53 |
Correct |
320 ms |
93524 KB |
Output is correct |
54 |
Correct |
484 ms |
97280 KB |
Output is correct |
55 |
Correct |
486 ms |
93164 KB |
Output is correct |
56 |
Correct |
310 ms |
93624 KB |
Output is correct |
57 |
Correct |
296 ms |
93216 KB |
Output is correct |
58 |
Correct |
212 ms |
93884 KB |
Output is correct |
59 |
Correct |
544 ms |
95716 KB |
Output is correct |
60 |
Correct |
341 ms |
97484 KB |
Output is correct |
61 |
Correct |
473 ms |
93036 KB |
Output is correct |
62 |
Correct |
267 ms |
93520 KB |
Output is correct |
63 |
Correct |
4 ms |
14684 KB |
Output is correct |
64 |
Correct |
3 ms |
14684 KB |
Output is correct |
65 |
Correct |
3 ms |
14640 KB |
Output is correct |
66 |
Correct |
4 ms |
14684 KB |
Output is correct |
67 |
Correct |
3 ms |
14684 KB |
Output is correct |
68 |
Correct |
3 ms |
14680 KB |
Output is correct |
69 |
Correct |
3 ms |
14636 KB |
Output is correct |
70 |
Correct |
3 ms |
14684 KB |
Output is correct |
71 |
Correct |
3 ms |
14684 KB |
Output is correct |
72 |
Correct |
3 ms |
14680 KB |
Output is correct |
73 |
Correct |
3 ms |
14684 KB |
Output is correct |
74 |
Correct |
3 ms |
14692 KB |
Output is correct |
75 |
Correct |
3 ms |
14684 KB |
Output is correct |
76 |
Correct |
3 ms |
14684 KB |
Output is correct |
77 |
Correct |
3 ms |
14684 KB |
Output is correct |
78 |
Correct |
3 ms |
14428 KB |
Output is correct |
79 |
Correct |
610 ms |
94536 KB |
Output is correct |
80 |
Correct |
743 ms |
93696 KB |
Output is correct |
81 |
Correct |
511 ms |
93440 KB |
Output is correct |
82 |
Correct |
538 ms |
90204 KB |
Output is correct |
83 |
Correct |
335 ms |
90452 KB |
Output is correct |
84 |
Correct |
333 ms |
90708 KB |
Output is correct |
85 |
Correct |
266 ms |
90832 KB |
Output is correct |
86 |
Correct |
641 ms |
92816 KB |
Output is correct |
87 |
Correct |
712 ms |
94560 KB |
Output is correct |
88 |
Correct |
524 ms |
90004 KB |
Output is correct |
89 |
Correct |
703 ms |
90716 KB |
Output is correct |
90 |
Correct |
501 ms |
91784 KB |
Output is correct |
91 |
Correct |
515 ms |
92540 KB |
Output is correct |
92 |
Correct |
3 ms |
14680 KB |
Output is correct |
93 |
Correct |
3 ms |
14680 KB |
Output is correct |
94 |
Correct |
3 ms |
14684 KB |
Output is correct |
95 |
Correct |
3 ms |
14684 KB |
Output is correct |
96 |
Correct |
3 ms |
14688 KB |
Output is correct |
97 |
Correct |
3 ms |
14428 KB |
Output is correct |
98 |
Correct |
590 ms |
95332 KB |
Output is correct |
99 |
Correct |
729 ms |
92876 KB |
Output is correct |
100 |
Correct |
535 ms |
93940 KB |
Output is correct |
101 |
Correct |
540 ms |
91984 KB |
Output is correct |
102 |
Correct |
364 ms |
92044 KB |
Output is correct |
103 |
Correct |
368 ms |
91968 KB |
Output is correct |
104 |
Correct |
273 ms |
91836 KB |
Output is correct |
105 |
Correct |
676 ms |
96640 KB |
Output is correct |
106 |
Correct |
738 ms |
95032 KB |
Output is correct |
107 |
Correct |
655 ms |
92588 KB |
Output is correct |
108 |
Correct |
730 ms |
91220 KB |
Output is correct |
109 |
Correct |
571 ms |
91908 KB |
Output is correct |
110 |
Correct |
607 ms |
92684 KB |
Output is correct |
111 |
Correct |
3 ms |
14680 KB |
Output is correct |
112 |
Correct |
3 ms |
14636 KB |
Output is correct |
113 |
Correct |
3 ms |
14684 KB |
Output is correct |
114 |
Correct |
3 ms |
14684 KB |
Output is correct |
115 |
Correct |
3 ms |
14684 KB |
Output is correct |
116 |
Correct |
571 ms |
91008 KB |
Output is correct |
117 |
Correct |
814 ms |
93836 KB |
Output is correct |
118 |
Correct |
524 ms |
97364 KB |
Output is correct |
119 |
Correct |
499 ms |
93244 KB |
Output is correct |
120 |
Correct |
412 ms |
93068 KB |
Output is correct |
121 |
Correct |
401 ms |
93780 KB |
Output is correct |
122 |
Correct |
323 ms |
93976 KB |
Output is correct |
123 |
Correct |
522 ms |
96376 KB |
Output is correct |
124 |
Correct |
850 ms |
95496 KB |
Output is correct |
125 |
Correct |
600 ms |
92344 KB |
Output is correct |
126 |
Correct |
737 ms |
93452 KB |
Output is correct |
127 |
Correct |
797 ms |
94048 KB |
Output is correct |
128 |
Correct |
591 ms |
94680 KB |
Output is correct |
129 |
Correct |
566 ms |
95480 KB |
Output is correct |