#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(a) a.begin(), a.end()
int mod;
void prod(int &a, int b){
a = (a * b) % mod;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n; cin >> n >> mod;
vector< vector<int> > g(n+1);
for(int i = 1;i < n; i++){
int a, b; cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
vector< vector<int> > ans(n+1, vector<int>(42, 1));
vector<int> up(n+1), h(n+1);
for(int i = 1;i <= n; i++){
cin >> h[i];
}
function<void(int, int)> dfs=[&](int v, int par){
up[v] = par;
for(int to : g[v]){
if(to == par) continue;
dfs(to, v);
}
};
dfs(1, -1);
int q; cin >> q;
while(q--){
int t; cin >> t;
if(t == 1){
int v, d, w; cin >> v >> d >> w;
int cur = v;
while(up[cur] != -1 && d > 0){
prod(ans[cur][d], w);
prod(ans[cur][d-1], w);
cur = up[cur];
d--;
}
while(d >= 0){
prod(ans[cur][d], w);
d--;
}
}else{
int v; cin >> v;
int product = h[v];
for(int i = 0;i <= 40 && v != -1; i++, v = up[v]){
prod(product, ans[v][i]);
}
cout << product << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
352 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
356 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
566 ms |
100868 KB |
Output is correct |
3 |
Correct |
323 ms |
101456 KB |
Output is correct |
4 |
Correct |
510 ms |
112144 KB |
Output is correct |
5 |
Correct |
437 ms |
101200 KB |
Output is correct |
6 |
Correct |
313 ms |
100720 KB |
Output is correct |
7 |
Correct |
291 ms |
101400 KB |
Output is correct |
8 |
Correct |
242 ms |
101584 KB |
Output is correct |
9 |
Correct |
636 ms |
118100 KB |
Output is correct |
10 |
Correct |
356 ms |
116820 KB |
Output is correct |
11 |
Correct |
583 ms |
101248 KB |
Output is correct |
12 |
Correct |
333 ms |
101460 KB |
Output is correct |
13 |
Correct |
203 ms |
101320 KB |
Output is correct |
14 |
Correct |
213 ms |
101884 KB |
Output is correct |
15 |
Correct |
206 ms |
101048 KB |
Output is correct |
16 |
Correct |
215 ms |
101716 KB |
Output is correct |
17 |
Correct |
219 ms |
102228 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
566 ms |
100868 KB |
Output is correct |
3 |
Correct |
323 ms |
101456 KB |
Output is correct |
4 |
Correct |
510 ms |
112144 KB |
Output is correct |
5 |
Correct |
437 ms |
101200 KB |
Output is correct |
6 |
Correct |
313 ms |
100720 KB |
Output is correct |
7 |
Correct |
291 ms |
101400 KB |
Output is correct |
8 |
Correct |
242 ms |
101584 KB |
Output is correct |
9 |
Correct |
636 ms |
118100 KB |
Output is correct |
10 |
Correct |
356 ms |
116820 KB |
Output is correct |
11 |
Correct |
583 ms |
101248 KB |
Output is correct |
12 |
Correct |
333 ms |
101460 KB |
Output is correct |
13 |
Correct |
203 ms |
101320 KB |
Output is correct |
14 |
Correct |
213 ms |
101884 KB |
Output is correct |
15 |
Correct |
206 ms |
101048 KB |
Output is correct |
16 |
Correct |
215 ms |
101716 KB |
Output is correct |
17 |
Correct |
219 ms |
102228 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
554 ms |
101428 KB |
Output is correct |
25 |
Correct |
343 ms |
101460 KB |
Output is correct |
26 |
Correct |
513 ms |
117332 KB |
Output is correct |
27 |
Correct |
463 ms |
101116 KB |
Output is correct |
28 |
Correct |
305 ms |
101200 KB |
Output is correct |
29 |
Correct |
297 ms |
101204 KB |
Output is correct |
30 |
Correct |
252 ms |
101716 KB |
Output is correct |
31 |
Correct |
621 ms |
112552 KB |
Output is correct |
32 |
Correct |
358 ms |
116928 KB |
Output is correct |
33 |
Correct |
572 ms |
101044 KB |
Output is correct |
34 |
Correct |
354 ms |
101636 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
464 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
464 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
460 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
661 ms |
114768 KB |
Output is correct |
3 |
Correct |
856 ms |
111088 KB |
Output is correct |
4 |
Correct |
599 ms |
111440 KB |
Output is correct |
5 |
Correct |
527 ms |
98392 KB |
Output is correct |
6 |
Correct |
351 ms |
98252 KB |
Output is correct |
7 |
Correct |
313 ms |
98356 KB |
Output is correct |
8 |
Correct |
251 ms |
98644 KB |
Output is correct |
9 |
Correct |
720 ms |
109712 KB |
Output is correct |
10 |
Correct |
829 ms |
114060 KB |
Output is correct |
11 |
Correct |
585 ms |
97844 KB |
Output is correct |
12 |
Correct |
622 ms |
97648 KB |
Output is correct |
13 |
Correct |
405 ms |
99668 KB |
Output is correct |
14 |
Correct |
458 ms |
100476 KB |
Output is correct |
15 |
Correct |
1 ms |
348 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 |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
675 ms |
111520 KB |
Output is correct |
3 |
Correct |
872 ms |
107412 KB |
Output is correct |
4 |
Correct |
621 ms |
109820 KB |
Output is correct |
5 |
Correct |
516 ms |
99888 KB |
Output is correct |
6 |
Correct |
340 ms |
99800 KB |
Output is correct |
7 |
Correct |
319 ms |
99668 KB |
Output is correct |
8 |
Correct |
252 ms |
99776 KB |
Output is correct |
9 |
Correct |
688 ms |
116328 KB |
Output is correct |
10 |
Correct |
881 ms |
114852 KB |
Output is correct |
11 |
Correct |
543 ms |
100728 KB |
Output is correct |
12 |
Correct |
631 ms |
99156 KB |
Output is correct |
13 |
Correct |
390 ms |
99784 KB |
Output is correct |
14 |
Correct |
420 ms |
100844 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
352 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
356 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
356 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
566 ms |
100868 KB |
Output is correct |
31 |
Correct |
323 ms |
101456 KB |
Output is correct |
32 |
Correct |
510 ms |
112144 KB |
Output is correct |
33 |
Correct |
437 ms |
101200 KB |
Output is correct |
34 |
Correct |
313 ms |
100720 KB |
Output is correct |
35 |
Correct |
291 ms |
101400 KB |
Output is correct |
36 |
Correct |
242 ms |
101584 KB |
Output is correct |
37 |
Correct |
636 ms |
118100 KB |
Output is correct |
38 |
Correct |
356 ms |
116820 KB |
Output is correct |
39 |
Correct |
583 ms |
101248 KB |
Output is correct |
40 |
Correct |
333 ms |
101460 KB |
Output is correct |
41 |
Correct |
203 ms |
101320 KB |
Output is correct |
42 |
Correct |
213 ms |
101884 KB |
Output is correct |
43 |
Correct |
206 ms |
101048 KB |
Output is correct |
44 |
Correct |
215 ms |
101716 KB |
Output is correct |
45 |
Correct |
219 ms |
102228 KB |
Output is correct |
46 |
Correct |
1 ms |
344 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
344 KB |
Output is correct |
52 |
Correct |
554 ms |
101428 KB |
Output is correct |
53 |
Correct |
343 ms |
101460 KB |
Output is correct |
54 |
Correct |
513 ms |
117332 KB |
Output is correct |
55 |
Correct |
463 ms |
101116 KB |
Output is correct |
56 |
Correct |
305 ms |
101200 KB |
Output is correct |
57 |
Correct |
297 ms |
101204 KB |
Output is correct |
58 |
Correct |
252 ms |
101716 KB |
Output is correct |
59 |
Correct |
621 ms |
112552 KB |
Output is correct |
60 |
Correct |
358 ms |
116928 KB |
Output is correct |
61 |
Correct |
572 ms |
101044 KB |
Output is correct |
62 |
Correct |
354 ms |
101636 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
64 |
Correct |
1 ms |
468 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
1 ms |
348 KB |
Output is correct |
68 |
Correct |
1 ms |
348 KB |
Output is correct |
69 |
Correct |
0 ms |
464 KB |
Output is correct |
70 |
Correct |
0 ms |
348 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
464 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
460 KB |
Output is correct |
77 |
Correct |
1 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
661 ms |
114768 KB |
Output is correct |
80 |
Correct |
856 ms |
111088 KB |
Output is correct |
81 |
Correct |
599 ms |
111440 KB |
Output is correct |
82 |
Correct |
527 ms |
98392 KB |
Output is correct |
83 |
Correct |
351 ms |
98252 KB |
Output is correct |
84 |
Correct |
313 ms |
98356 KB |
Output is correct |
85 |
Correct |
251 ms |
98644 KB |
Output is correct |
86 |
Correct |
720 ms |
109712 KB |
Output is correct |
87 |
Correct |
829 ms |
114060 KB |
Output is correct |
88 |
Correct |
585 ms |
97844 KB |
Output is correct |
89 |
Correct |
622 ms |
97648 KB |
Output is correct |
90 |
Correct |
405 ms |
99668 KB |
Output is correct |
91 |
Correct |
458 ms |
100476 KB |
Output is correct |
92 |
Correct |
1 ms |
348 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 |
600 KB |
Output is correct |
96 |
Correct |
1 ms |
348 KB |
Output is correct |
97 |
Correct |
0 ms |
348 KB |
Output is correct |
98 |
Correct |
675 ms |
111520 KB |
Output is correct |
99 |
Correct |
872 ms |
107412 KB |
Output is correct |
100 |
Correct |
621 ms |
109820 KB |
Output is correct |
101 |
Correct |
516 ms |
99888 KB |
Output is correct |
102 |
Correct |
340 ms |
99800 KB |
Output is correct |
103 |
Correct |
319 ms |
99668 KB |
Output is correct |
104 |
Correct |
252 ms |
99776 KB |
Output is correct |
105 |
Correct |
688 ms |
116328 KB |
Output is correct |
106 |
Correct |
881 ms |
114852 KB |
Output is correct |
107 |
Correct |
543 ms |
100728 KB |
Output is correct |
108 |
Correct |
631 ms |
99156 KB |
Output is correct |
109 |
Correct |
390 ms |
99784 KB |
Output is correct |
110 |
Correct |
420 ms |
100844 KB |
Output is correct |
111 |
Correct |
0 ms |
348 KB |
Output is correct |
112 |
Correct |
1 ms |
348 KB |
Output is correct |
113 |
Correct |
1 ms |
348 KB |
Output is correct |
114 |
Correct |
0 ms |
348 KB |
Output is correct |
115 |
Correct |
1 ms |
348 KB |
Output is correct |
116 |
Correct |
540 ms |
99144 KB |
Output is correct |
117 |
Correct |
590 ms |
101964 KB |
Output is correct |
118 |
Correct |
655 ms |
117340 KB |
Output is correct |
119 |
Correct |
478 ms |
101112 KB |
Output is correct |
120 |
Correct |
370 ms |
100928 KB |
Output is correct |
121 |
Correct |
351 ms |
101640 KB |
Output is correct |
122 |
Correct |
265 ms |
101572 KB |
Output is correct |
123 |
Correct |
608 ms |
114344 KB |
Output is correct |
124 |
Correct |
822 ms |
111188 KB |
Output is correct |
125 |
Correct |
525 ms |
100012 KB |
Output is correct |
126 |
Correct |
607 ms |
101524 KB |
Output is correct |
127 |
Correct |
636 ms |
102148 KB |
Output is correct |
128 |
Correct |
483 ms |
102480 KB |
Output is correct |
129 |
Correct |
482 ms |
103532 KB |
Output is correct |