#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
int Mod;
void prod(int &u, const int &v){
u = (u * v) % Mod;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n >> Mod;
vector <vector<int>> G(n);
for ( int i = 1; i < n; i++ ){
int u, v; cin >> u >> v;
--u, --v;
G[u].pb(v), G[v].pb(u);
}
vector <int> fa(n);
auto dfs = [&](auto dfs, int u, int p) -> void{
fa[u] = p;
for ( auto &v: G[u] ){
if ( v != p ){
dfs(dfs, v, u);
}
}
};
dfs(dfs, 0, -1);
vector <int> h(n);
for ( auto &i: h ) cin >> i;
vector <vector<int>> a(n, vector <int> (41, 1));
int q; cin >> q;
while ( q-- ){
int t, u; cin >> t >> u;
--u;
if ( t == 1 ){
int d, w; cin >> d >> w;
while ( d > 0 && fa[u] != -1 ){
prod(a[u][d], w);
prod(a[u][d - 1], w);
--d; u = fa[u];
}
while ( d > 0 ){
prod(a[u][d], w);
--d;
}
prod(a[u][0], w);
} else{
int ans = h[u];
for ( int i = 0; i <= 40 && u != -1; i++ ){
prod(ans, a[u][i]);
u = fa[u];
}
cout << ans << ln;
}
}
cout << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
856 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
456 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
523 ms |
89596 KB |
Output is correct |
3 |
Correct |
303 ms |
86356 KB |
Output is correct |
4 |
Correct |
507 ms |
90452 KB |
Output is correct |
5 |
Correct |
418 ms |
87988 KB |
Output is correct |
6 |
Correct |
284 ms |
87888 KB |
Output is correct |
7 |
Correct |
273 ms |
88400 KB |
Output is correct |
8 |
Correct |
235 ms |
88536 KB |
Output is correct |
9 |
Correct |
599 ms |
93880 KB |
Output is correct |
10 |
Correct |
346 ms |
90124 KB |
Output is correct |
11 |
Correct |
549 ms |
89556 KB |
Output is correct |
12 |
Correct |
335 ms |
86392 KB |
Output is correct |
13 |
Correct |
224 ms |
86552 KB |
Output is correct |
14 |
Correct |
201 ms |
86716 KB |
Output is correct |
15 |
Correct |
221 ms |
86536 KB |
Output is correct |
16 |
Correct |
206 ms |
86708 KB |
Output is correct |
17 |
Correct |
215 ms |
87268 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
523 ms |
89596 KB |
Output is correct |
3 |
Correct |
303 ms |
86356 KB |
Output is correct |
4 |
Correct |
507 ms |
90452 KB |
Output is correct |
5 |
Correct |
418 ms |
87988 KB |
Output is correct |
6 |
Correct |
284 ms |
87888 KB |
Output is correct |
7 |
Correct |
273 ms |
88400 KB |
Output is correct |
8 |
Correct |
235 ms |
88536 KB |
Output is correct |
9 |
Correct |
599 ms |
93880 KB |
Output is correct |
10 |
Correct |
346 ms |
90124 KB |
Output is correct |
11 |
Correct |
549 ms |
89556 KB |
Output is correct |
12 |
Correct |
335 ms |
86392 KB |
Output is correct |
13 |
Correct |
224 ms |
86552 KB |
Output is correct |
14 |
Correct |
201 ms |
86716 KB |
Output is correct |
15 |
Correct |
221 ms |
86536 KB |
Output is correct |
16 |
Correct |
206 ms |
86708 KB |
Output is correct |
17 |
Correct |
215 ms |
87268 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
563 ms |
89616 KB |
Output is correct |
25 |
Correct |
341 ms |
86624 KB |
Output is correct |
26 |
Correct |
506 ms |
92060 KB |
Output is correct |
27 |
Correct |
417 ms |
87968 KB |
Output is correct |
28 |
Correct |
312 ms |
87948 KB |
Output is correct |
29 |
Correct |
308 ms |
88220 KB |
Output is correct |
30 |
Correct |
242 ms |
88356 KB |
Output is correct |
31 |
Correct |
666 ms |
92160 KB |
Output is correct |
32 |
Correct |
353 ms |
90308 KB |
Output is correct |
33 |
Correct |
527 ms |
89556 KB |
Output is correct |
34 |
Correct |
345 ms |
86440 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
344 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
500 KB |
Output is correct |
43 |
Correct |
1 ms |
504 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
344 KB |
Output is correct |
49 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
789 ms |
91100 KB |
Output is correct |
3 |
Correct |
1200 ms |
89020 KB |
Output is correct |
4 |
Correct |
610 ms |
89464 KB |
Output is correct |
5 |
Correct |
525 ms |
86340 KB |
Output is correct |
6 |
Correct |
341 ms |
86556 KB |
Output is correct |
7 |
Correct |
325 ms |
86608 KB |
Output is correct |
8 |
Correct |
243 ms |
86720 KB |
Output is correct |
9 |
Correct |
745 ms |
89600 KB |
Output is correct |
10 |
Correct |
996 ms |
89736 KB |
Output is correct |
11 |
Correct |
591 ms |
86576 KB |
Output is correct |
12 |
Correct |
661 ms |
86072 KB |
Output is correct |
13 |
Correct |
393 ms |
87124 KB |
Output is correct |
14 |
Correct |
472 ms |
87632 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
629 ms |
92124 KB |
Output is correct |
3 |
Correct |
850 ms |
88116 KB |
Output is correct |
4 |
Correct |
603 ms |
90052 KB |
Output is correct |
5 |
Correct |
557 ms |
88228 KB |
Output is correct |
6 |
Correct |
341 ms |
88124 KB |
Output is correct |
7 |
Correct |
336 ms |
88100 KB |
Output is correct |
8 |
Correct |
248 ms |
88260 KB |
Output is correct |
9 |
Correct |
686 ms |
93256 KB |
Output is correct |
10 |
Correct |
986 ms |
90456 KB |
Output is correct |
11 |
Correct |
571 ms |
89716 KB |
Output is correct |
12 |
Correct |
639 ms |
86308 KB |
Output is correct |
13 |
Correct |
396 ms |
87172 KB |
Output is correct |
14 |
Correct |
401 ms |
88136 KB |
Output is correct |
15 |
Correct |
1 ms |
552 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
856 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
456 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
523 ms |
89596 KB |
Output is correct |
31 |
Correct |
303 ms |
86356 KB |
Output is correct |
32 |
Correct |
507 ms |
90452 KB |
Output is correct |
33 |
Correct |
418 ms |
87988 KB |
Output is correct |
34 |
Correct |
284 ms |
87888 KB |
Output is correct |
35 |
Correct |
273 ms |
88400 KB |
Output is correct |
36 |
Correct |
235 ms |
88536 KB |
Output is correct |
37 |
Correct |
599 ms |
93880 KB |
Output is correct |
38 |
Correct |
346 ms |
90124 KB |
Output is correct |
39 |
Correct |
549 ms |
89556 KB |
Output is correct |
40 |
Correct |
335 ms |
86392 KB |
Output is correct |
41 |
Correct |
224 ms |
86552 KB |
Output is correct |
42 |
Correct |
201 ms |
86716 KB |
Output is correct |
43 |
Correct |
221 ms |
86536 KB |
Output is correct |
44 |
Correct |
206 ms |
86708 KB |
Output is correct |
45 |
Correct |
215 ms |
87268 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
2 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
563 ms |
89616 KB |
Output is correct |
53 |
Correct |
341 ms |
86624 KB |
Output is correct |
54 |
Correct |
506 ms |
92060 KB |
Output is correct |
55 |
Correct |
417 ms |
87968 KB |
Output is correct |
56 |
Correct |
312 ms |
87948 KB |
Output is correct |
57 |
Correct |
308 ms |
88220 KB |
Output is correct |
58 |
Correct |
242 ms |
88356 KB |
Output is correct |
59 |
Correct |
666 ms |
92160 KB |
Output is correct |
60 |
Correct |
353 ms |
90308 KB |
Output is correct |
61 |
Correct |
527 ms |
89556 KB |
Output is correct |
62 |
Correct |
345 ms |
86440 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
64 |
Correct |
1 ms |
344 KB |
Output is correct |
65 |
Correct |
1 ms |
344 KB |
Output is correct |
66 |
Correct |
1 ms |
348 KB |
Output is correct |
67 |
Correct |
1 ms |
348 KB |
Output is correct |
68 |
Correct |
0 ms |
348 KB |
Output is correct |
69 |
Correct |
1 ms |
348 KB |
Output is correct |
70 |
Correct |
1 ms |
500 KB |
Output is correct |
71 |
Correct |
1 ms |
504 KB |
Output is correct |
72 |
Correct |
1 ms |
348 KB |
Output is correct |
73 |
Correct |
1 ms |
348 KB |
Output is correct |
74 |
Correct |
1 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
1 ms |
344 KB |
Output is correct |
77 |
Correct |
1 ms |
344 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
789 ms |
91100 KB |
Output is correct |
80 |
Correct |
1200 ms |
89020 KB |
Output is correct |
81 |
Correct |
610 ms |
89464 KB |
Output is correct |
82 |
Correct |
525 ms |
86340 KB |
Output is correct |
83 |
Correct |
341 ms |
86556 KB |
Output is correct |
84 |
Correct |
325 ms |
86608 KB |
Output is correct |
85 |
Correct |
243 ms |
86720 KB |
Output is correct |
86 |
Correct |
745 ms |
89600 KB |
Output is correct |
87 |
Correct |
996 ms |
89736 KB |
Output is correct |
88 |
Correct |
591 ms |
86576 KB |
Output is correct |
89 |
Correct |
661 ms |
86072 KB |
Output is correct |
90 |
Correct |
393 ms |
87124 KB |
Output is correct |
91 |
Correct |
472 ms |
87632 KB |
Output is correct |
92 |
Correct |
1 ms |
344 KB |
Output is correct |
93 |
Correct |
0 ms |
348 KB |
Output is correct |
94 |
Correct |
0 ms |
348 KB |
Output is correct |
95 |
Correct |
0 ms |
348 KB |
Output is correct |
96 |
Correct |
0 ms |
348 KB |
Output is correct |
97 |
Correct |
0 ms |
348 KB |
Output is correct |
98 |
Correct |
629 ms |
92124 KB |
Output is correct |
99 |
Correct |
850 ms |
88116 KB |
Output is correct |
100 |
Correct |
603 ms |
90052 KB |
Output is correct |
101 |
Correct |
557 ms |
88228 KB |
Output is correct |
102 |
Correct |
341 ms |
88124 KB |
Output is correct |
103 |
Correct |
336 ms |
88100 KB |
Output is correct |
104 |
Correct |
248 ms |
88260 KB |
Output is correct |
105 |
Correct |
686 ms |
93256 KB |
Output is correct |
106 |
Correct |
986 ms |
90456 KB |
Output is correct |
107 |
Correct |
571 ms |
89716 KB |
Output is correct |
108 |
Correct |
639 ms |
86308 KB |
Output is correct |
109 |
Correct |
396 ms |
87172 KB |
Output is correct |
110 |
Correct |
401 ms |
88136 KB |
Output is correct |
111 |
Correct |
1 ms |
552 KB |
Output is correct |
112 |
Correct |
1 ms |
348 KB |
Output is correct |
113 |
Correct |
0 ms |
348 KB |
Output is correct |
114 |
Correct |
1 ms |
348 KB |
Output is correct |
115 |
Correct |
1 ms |
348 KB |
Output is correct |
116 |
Correct |
562 ms |
87592 KB |
Output is correct |
117 |
Correct |
739 ms |
86088 KB |
Output is correct |
118 |
Correct |
619 ms |
92240 KB |
Output is correct |
119 |
Correct |
622 ms |
87892 KB |
Output is correct |
120 |
Correct |
375 ms |
87860 KB |
Output is correct |
121 |
Correct |
369 ms |
88372 KB |
Output is correct |
122 |
Correct |
278 ms |
88516 KB |
Output is correct |
123 |
Correct |
674 ms |
92784 KB |
Output is correct |
124 |
Correct |
933 ms |
88660 KB |
Output is correct |
125 |
Correct |
566 ms |
89168 KB |
Output is correct |
126 |
Correct |
732 ms |
86240 KB |
Output is correct |
127 |
Correct |
740 ms |
86120 KB |
Output is correct |
128 |
Correct |
489 ms |
87124 KB |
Output is correct |
129 |
Correct |
559 ms |
87880 KB |
Output is correct |