#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 10;
const int MK = 45;
vector<int> adj[MX];
int N, L, Q, p[MX], h[MX], dp[MX][MK];
// dp[i][j] is the multiplication towards all of node i's descendants that are of distance d
// Update is updating all dp[anc_i][D - dist(anc_i, i)]
// But observe that this won't handle cases where we go back up and return through the path we came from
// that is why we also add dp[anc_i][D - dist(anc_i, i) - 1]
// Query is querying all dp[anc_i][dist(anc_i, i)]
void dfs(int u, int v){
p[u] = v;
for(int nxt : adj[u]){
if(nxt == v) continue;
dfs(nxt, u);
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N >> L;
for(int i = 1; i < N; i++){
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i = 1; i <= N; i++) cin >> h[i];
dfs(1, 0);
for(int i = 0; i < MX; i++)
for(int j = 0; j < MK; j++) dp[i][j] = 1;
cin >> Q;
for(int i = 1; i <= Q; i++){
int t, x, d, w; cin >> t;
if(t == 1){
cin >> x >> d >> w;
dp[x][d] = 1ll * dp[x][d] * w % L;
if(x != 1 && d > 0) dp[x][d - 1] = 1ll * dp[x][d - 1] * w % L;
int curr = x;
for(int j = 1; j <= d; j++){
if(p[curr] != 0) curr = p[curr];
dp[curr][d - j] = 1ll * dp[curr][d - j] * w % L;
if(curr != 1 && d > j) dp[curr][d - j - 1] = 1ll * dp[curr][d - j - 1] * w % L;
}
}else{
cin >> x;
int ans = h[x], curr = x;
for(int j = 0; j <= 41; j++){
// cout << "At query #" << i << " := anc " << x << " " << curr << " " << j << " " << dp[curr][j] << '\n';
ans = 1ll * ans * dp[curr][j] % L;
if(p[curr] == 0) break;
else curr = p[curr];
}
cout << ans << '\n';
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
40160 KB |
Output is correct |
2 |
Correct |
18 ms |
40220 KB |
Output is correct |
3 |
Correct |
18 ms |
40168 KB |
Output is correct |
4 |
Correct |
19 ms |
40208 KB |
Output is correct |
5 |
Correct |
18 ms |
40276 KB |
Output is correct |
6 |
Correct |
20 ms |
40328 KB |
Output is correct |
7 |
Correct |
19 ms |
40320 KB |
Output is correct |
8 |
Correct |
19 ms |
40240 KB |
Output is correct |
9 |
Correct |
18 ms |
40212 KB |
Output is correct |
10 |
Correct |
18 ms |
40252 KB |
Output is correct |
11 |
Correct |
17 ms |
40276 KB |
Output is correct |
12 |
Correct |
18 ms |
40232 KB |
Output is correct |
13 |
Correct |
18 ms |
40148 KB |
Output is correct |
14 |
Correct |
19 ms |
40160 KB |
Output is correct |
15 |
Correct |
18 ms |
40192 KB |
Output is correct |
16 |
Correct |
22 ms |
40240 KB |
Output is correct |
17 |
Correct |
18 ms |
40264 KB |
Output is correct |
18 |
Correct |
18 ms |
40248 KB |
Output is correct |
19 |
Correct |
18 ms |
40148 KB |
Output is correct |
20 |
Correct |
21 ms |
40452 KB |
Output is correct |
21 |
Correct |
22 ms |
40196 KB |
Output is correct |
22 |
Correct |
20 ms |
40140 KB |
Output is correct |
23 |
Correct |
17 ms |
40148 KB |
Output is correct |
24 |
Correct |
21 ms |
40200 KB |
Output is correct |
25 |
Correct |
18 ms |
40136 KB |
Output is correct |
26 |
Correct |
17 ms |
40160 KB |
Output is correct |
27 |
Correct |
18 ms |
40148 KB |
Output is correct |
28 |
Correct |
18 ms |
40232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
40208 KB |
Output is correct |
2 |
Correct |
415 ms |
59936 KB |
Output is correct |
3 |
Correct |
254 ms |
60428 KB |
Output is correct |
4 |
Correct |
392 ms |
63100 KB |
Output is correct |
5 |
Correct |
358 ms |
60176 KB |
Output is correct |
6 |
Correct |
256 ms |
59628 KB |
Output is correct |
7 |
Correct |
230 ms |
60464 KB |
Output is correct |
8 |
Correct |
194 ms |
60912 KB |
Output is correct |
9 |
Correct |
521 ms |
64320 KB |
Output is correct |
10 |
Correct |
269 ms |
64524 KB |
Output is correct |
11 |
Correct |
396 ms |
59828 KB |
Output is correct |
12 |
Correct |
266 ms |
60396 KB |
Output is correct |
13 |
Correct |
176 ms |
60712 KB |
Output is correct |
14 |
Correct |
177 ms |
61196 KB |
Output is correct |
15 |
Correct |
178 ms |
60476 KB |
Output is correct |
16 |
Correct |
187 ms |
60996 KB |
Output is correct |
17 |
Correct |
201 ms |
61472 KB |
Output is correct |
18 |
Correct |
17 ms |
40276 KB |
Output is correct |
19 |
Correct |
18 ms |
40276 KB |
Output is correct |
20 |
Correct |
17 ms |
40268 KB |
Output is correct |
21 |
Correct |
17 ms |
40224 KB |
Output is correct |
22 |
Correct |
17 ms |
40236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
40208 KB |
Output is correct |
2 |
Correct |
415 ms |
59936 KB |
Output is correct |
3 |
Correct |
254 ms |
60428 KB |
Output is correct |
4 |
Correct |
392 ms |
63100 KB |
Output is correct |
5 |
Correct |
358 ms |
60176 KB |
Output is correct |
6 |
Correct |
256 ms |
59628 KB |
Output is correct |
7 |
Correct |
230 ms |
60464 KB |
Output is correct |
8 |
Correct |
194 ms |
60912 KB |
Output is correct |
9 |
Correct |
521 ms |
64320 KB |
Output is correct |
10 |
Correct |
269 ms |
64524 KB |
Output is correct |
11 |
Correct |
396 ms |
59828 KB |
Output is correct |
12 |
Correct |
266 ms |
60396 KB |
Output is correct |
13 |
Correct |
176 ms |
60712 KB |
Output is correct |
14 |
Correct |
177 ms |
61196 KB |
Output is correct |
15 |
Correct |
178 ms |
60476 KB |
Output is correct |
16 |
Correct |
187 ms |
60996 KB |
Output is correct |
17 |
Correct |
201 ms |
61472 KB |
Output is correct |
18 |
Correct |
17 ms |
40276 KB |
Output is correct |
19 |
Correct |
18 ms |
40276 KB |
Output is correct |
20 |
Correct |
17 ms |
40268 KB |
Output is correct |
21 |
Correct |
17 ms |
40224 KB |
Output is correct |
22 |
Correct |
17 ms |
40236 KB |
Output is correct |
23 |
Correct |
16 ms |
40148 KB |
Output is correct |
24 |
Correct |
394 ms |
59804 KB |
Output is correct |
25 |
Correct |
260 ms |
60452 KB |
Output is correct |
26 |
Correct |
443 ms |
64512 KB |
Output is correct |
27 |
Correct |
338 ms |
60024 KB |
Output is correct |
28 |
Correct |
250 ms |
60244 KB |
Output is correct |
29 |
Correct |
251 ms |
59892 KB |
Output is correct |
30 |
Correct |
225 ms |
60968 KB |
Output is correct |
31 |
Correct |
499 ms |
63080 KB |
Output is correct |
32 |
Correct |
296 ms |
64544 KB |
Output is correct |
33 |
Correct |
389 ms |
59888 KB |
Output is correct |
34 |
Correct |
271 ms |
60448 KB |
Output is correct |
35 |
Correct |
18 ms |
40272 KB |
Output is correct |
36 |
Correct |
17 ms |
40276 KB |
Output is correct |
37 |
Correct |
16 ms |
40148 KB |
Output is correct |
38 |
Correct |
16 ms |
40164 KB |
Output is correct |
39 |
Correct |
17 ms |
40148 KB |
Output is correct |
40 |
Correct |
16 ms |
40212 KB |
Output is correct |
41 |
Correct |
16 ms |
40160 KB |
Output is correct |
42 |
Correct |
21 ms |
40232 KB |
Output is correct |
43 |
Correct |
17 ms |
40148 KB |
Output is correct |
44 |
Correct |
18 ms |
40164 KB |
Output is correct |
45 |
Correct |
18 ms |
40240 KB |
Output is correct |
46 |
Correct |
21 ms |
40168 KB |
Output is correct |
47 |
Correct |
18 ms |
40148 KB |
Output is correct |
48 |
Correct |
18 ms |
40148 KB |
Output is correct |
49 |
Correct |
17 ms |
40236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
40148 KB |
Output is correct |
2 |
Correct |
529 ms |
61480 KB |
Output is correct |
3 |
Correct |
688 ms |
61092 KB |
Output is correct |
4 |
Correct |
480 ms |
60816 KB |
Output is correct |
5 |
Correct |
400 ms |
57196 KB |
Output is correct |
6 |
Correct |
269 ms |
57328 KB |
Output is correct |
7 |
Correct |
268 ms |
57484 KB |
Output is correct |
8 |
Correct |
209 ms |
58012 KB |
Output is correct |
9 |
Correct |
549 ms |
60204 KB |
Output is correct |
10 |
Correct |
773 ms |
61592 KB |
Output is correct |
11 |
Correct |
420 ms |
56784 KB |
Output is correct |
12 |
Correct |
470 ms |
57632 KB |
Output is correct |
13 |
Correct |
332 ms |
58572 KB |
Output is correct |
14 |
Correct |
349 ms |
59036 KB |
Output is correct |
15 |
Correct |
17 ms |
40148 KB |
Output is correct |
16 |
Correct |
17 ms |
40228 KB |
Output is correct |
17 |
Correct |
17 ms |
40148 KB |
Output is correct |
18 |
Correct |
18 ms |
40188 KB |
Output is correct |
19 |
Correct |
17 ms |
40188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
40220 KB |
Output is correct |
2 |
Correct |
581 ms |
62596 KB |
Output is correct |
3 |
Correct |
714 ms |
60432 KB |
Output is correct |
4 |
Correct |
540 ms |
61332 KB |
Output is correct |
5 |
Correct |
378 ms |
58720 KB |
Output is correct |
6 |
Correct |
288 ms |
58988 KB |
Output is correct |
7 |
Correct |
275 ms |
58656 KB |
Output is correct |
8 |
Correct |
213 ms |
58944 KB |
Output is correct |
9 |
Correct |
541 ms |
63764 KB |
Output is correct |
10 |
Correct |
696 ms |
62272 KB |
Output is correct |
11 |
Correct |
429 ms |
59540 KB |
Output is correct |
12 |
Correct |
440 ms |
58020 KB |
Output is correct |
13 |
Correct |
335 ms |
58700 KB |
Output is correct |
14 |
Correct |
329 ms |
59272 KB |
Output is correct |
15 |
Correct |
18 ms |
40140 KB |
Output is correct |
16 |
Correct |
19 ms |
40180 KB |
Output is correct |
17 |
Correct |
16 ms |
40232 KB |
Output is correct |
18 |
Correct |
18 ms |
40268 KB |
Output is correct |
19 |
Correct |
17 ms |
40212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
40160 KB |
Output is correct |
2 |
Correct |
18 ms |
40220 KB |
Output is correct |
3 |
Correct |
18 ms |
40168 KB |
Output is correct |
4 |
Correct |
19 ms |
40208 KB |
Output is correct |
5 |
Correct |
18 ms |
40276 KB |
Output is correct |
6 |
Correct |
20 ms |
40328 KB |
Output is correct |
7 |
Correct |
19 ms |
40320 KB |
Output is correct |
8 |
Correct |
19 ms |
40240 KB |
Output is correct |
9 |
Correct |
18 ms |
40212 KB |
Output is correct |
10 |
Correct |
18 ms |
40252 KB |
Output is correct |
11 |
Correct |
17 ms |
40276 KB |
Output is correct |
12 |
Correct |
18 ms |
40232 KB |
Output is correct |
13 |
Correct |
18 ms |
40148 KB |
Output is correct |
14 |
Correct |
19 ms |
40160 KB |
Output is correct |
15 |
Correct |
18 ms |
40192 KB |
Output is correct |
16 |
Correct |
22 ms |
40240 KB |
Output is correct |
17 |
Correct |
18 ms |
40264 KB |
Output is correct |
18 |
Correct |
18 ms |
40248 KB |
Output is correct |
19 |
Correct |
18 ms |
40148 KB |
Output is correct |
20 |
Correct |
21 ms |
40452 KB |
Output is correct |
21 |
Correct |
22 ms |
40196 KB |
Output is correct |
22 |
Correct |
20 ms |
40140 KB |
Output is correct |
23 |
Correct |
17 ms |
40148 KB |
Output is correct |
24 |
Correct |
21 ms |
40200 KB |
Output is correct |
25 |
Correct |
18 ms |
40136 KB |
Output is correct |
26 |
Correct |
17 ms |
40160 KB |
Output is correct |
27 |
Correct |
18 ms |
40148 KB |
Output is correct |
28 |
Correct |
18 ms |
40232 KB |
Output is correct |
29 |
Correct |
17 ms |
40208 KB |
Output is correct |
30 |
Correct |
415 ms |
59936 KB |
Output is correct |
31 |
Correct |
254 ms |
60428 KB |
Output is correct |
32 |
Correct |
392 ms |
63100 KB |
Output is correct |
33 |
Correct |
358 ms |
60176 KB |
Output is correct |
34 |
Correct |
256 ms |
59628 KB |
Output is correct |
35 |
Correct |
230 ms |
60464 KB |
Output is correct |
36 |
Correct |
194 ms |
60912 KB |
Output is correct |
37 |
Correct |
521 ms |
64320 KB |
Output is correct |
38 |
Correct |
269 ms |
64524 KB |
Output is correct |
39 |
Correct |
396 ms |
59828 KB |
Output is correct |
40 |
Correct |
266 ms |
60396 KB |
Output is correct |
41 |
Correct |
176 ms |
60712 KB |
Output is correct |
42 |
Correct |
177 ms |
61196 KB |
Output is correct |
43 |
Correct |
178 ms |
60476 KB |
Output is correct |
44 |
Correct |
187 ms |
60996 KB |
Output is correct |
45 |
Correct |
201 ms |
61472 KB |
Output is correct |
46 |
Correct |
17 ms |
40276 KB |
Output is correct |
47 |
Correct |
18 ms |
40276 KB |
Output is correct |
48 |
Correct |
17 ms |
40268 KB |
Output is correct |
49 |
Correct |
17 ms |
40224 KB |
Output is correct |
50 |
Correct |
17 ms |
40236 KB |
Output is correct |
51 |
Correct |
16 ms |
40148 KB |
Output is correct |
52 |
Correct |
394 ms |
59804 KB |
Output is correct |
53 |
Correct |
260 ms |
60452 KB |
Output is correct |
54 |
Correct |
443 ms |
64512 KB |
Output is correct |
55 |
Correct |
338 ms |
60024 KB |
Output is correct |
56 |
Correct |
250 ms |
60244 KB |
Output is correct |
57 |
Correct |
251 ms |
59892 KB |
Output is correct |
58 |
Correct |
225 ms |
60968 KB |
Output is correct |
59 |
Correct |
499 ms |
63080 KB |
Output is correct |
60 |
Correct |
296 ms |
64544 KB |
Output is correct |
61 |
Correct |
389 ms |
59888 KB |
Output is correct |
62 |
Correct |
271 ms |
60448 KB |
Output is correct |
63 |
Correct |
18 ms |
40272 KB |
Output is correct |
64 |
Correct |
17 ms |
40276 KB |
Output is correct |
65 |
Correct |
16 ms |
40148 KB |
Output is correct |
66 |
Correct |
16 ms |
40164 KB |
Output is correct |
67 |
Correct |
17 ms |
40148 KB |
Output is correct |
68 |
Correct |
16 ms |
40212 KB |
Output is correct |
69 |
Correct |
16 ms |
40160 KB |
Output is correct |
70 |
Correct |
21 ms |
40232 KB |
Output is correct |
71 |
Correct |
17 ms |
40148 KB |
Output is correct |
72 |
Correct |
18 ms |
40164 KB |
Output is correct |
73 |
Correct |
18 ms |
40240 KB |
Output is correct |
74 |
Correct |
21 ms |
40168 KB |
Output is correct |
75 |
Correct |
18 ms |
40148 KB |
Output is correct |
76 |
Correct |
18 ms |
40148 KB |
Output is correct |
77 |
Correct |
17 ms |
40236 KB |
Output is correct |
78 |
Correct |
17 ms |
40148 KB |
Output is correct |
79 |
Correct |
529 ms |
61480 KB |
Output is correct |
80 |
Correct |
688 ms |
61092 KB |
Output is correct |
81 |
Correct |
480 ms |
60816 KB |
Output is correct |
82 |
Correct |
400 ms |
57196 KB |
Output is correct |
83 |
Correct |
269 ms |
57328 KB |
Output is correct |
84 |
Correct |
268 ms |
57484 KB |
Output is correct |
85 |
Correct |
209 ms |
58012 KB |
Output is correct |
86 |
Correct |
549 ms |
60204 KB |
Output is correct |
87 |
Correct |
773 ms |
61592 KB |
Output is correct |
88 |
Correct |
420 ms |
56784 KB |
Output is correct |
89 |
Correct |
470 ms |
57632 KB |
Output is correct |
90 |
Correct |
332 ms |
58572 KB |
Output is correct |
91 |
Correct |
349 ms |
59036 KB |
Output is correct |
92 |
Correct |
17 ms |
40148 KB |
Output is correct |
93 |
Correct |
17 ms |
40228 KB |
Output is correct |
94 |
Correct |
17 ms |
40148 KB |
Output is correct |
95 |
Correct |
18 ms |
40188 KB |
Output is correct |
96 |
Correct |
17 ms |
40188 KB |
Output is correct |
97 |
Correct |
17 ms |
40220 KB |
Output is correct |
98 |
Correct |
581 ms |
62596 KB |
Output is correct |
99 |
Correct |
714 ms |
60432 KB |
Output is correct |
100 |
Correct |
540 ms |
61332 KB |
Output is correct |
101 |
Correct |
378 ms |
58720 KB |
Output is correct |
102 |
Correct |
288 ms |
58988 KB |
Output is correct |
103 |
Correct |
275 ms |
58656 KB |
Output is correct |
104 |
Correct |
213 ms |
58944 KB |
Output is correct |
105 |
Correct |
541 ms |
63764 KB |
Output is correct |
106 |
Correct |
696 ms |
62272 KB |
Output is correct |
107 |
Correct |
429 ms |
59540 KB |
Output is correct |
108 |
Correct |
440 ms |
58020 KB |
Output is correct |
109 |
Correct |
335 ms |
58700 KB |
Output is correct |
110 |
Correct |
329 ms |
59272 KB |
Output is correct |
111 |
Correct |
18 ms |
40140 KB |
Output is correct |
112 |
Correct |
19 ms |
40180 KB |
Output is correct |
113 |
Correct |
16 ms |
40232 KB |
Output is correct |
114 |
Correct |
18 ms |
40268 KB |
Output is correct |
115 |
Correct |
17 ms |
40212 KB |
Output is correct |
116 |
Correct |
431 ms |
57900 KB |
Output is correct |
117 |
Correct |
542 ms |
60476 KB |
Output is correct |
118 |
Correct |
490 ms |
64516 KB |
Output is correct |
119 |
Correct |
395 ms |
60108 KB |
Output is correct |
120 |
Correct |
310 ms |
59776 KB |
Output is correct |
121 |
Correct |
304 ms |
60532 KB |
Output is correct |
122 |
Correct |
278 ms |
60868 KB |
Output is correct |
123 |
Correct |
543 ms |
63544 KB |
Output is correct |
124 |
Correct |
718 ms |
62916 KB |
Output is correct |
125 |
Correct |
433 ms |
59176 KB |
Output is correct |
126 |
Correct |
498 ms |
60416 KB |
Output is correct |
127 |
Correct |
530 ms |
60796 KB |
Output is correct |
128 |
Correct |
440 ms |
61504 KB |
Output is correct |
129 |
Correct |
459 ms |
61988 KB |
Output is correct |