#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#ifndef LOCAL
#define cerr if(false)cerr
#endif
const ll MAX_N = 2e5 + 10;
const ll MAX_D = 45;
ll cnt[MAX_N][MAX_D];
ll n;
ll L;
vector<ll> g[MAX_N];
ll par[MAX_N];
ll start[MAX_N];
void dfs(ll x, ll p) {
par[x] = p;
for(const auto it : g[x]) {
if(it == p) { continue; }
dfs(it, x);
}
}
void upd(ll x, ll d, ll w) {
if(d == 0) {
cnt[x][d] = (cnt[x][d] * w) % L;
return;
}
if(par[x] == 0) {
upd(x, d - 1, w);
cnt[x][d] = (cnt[x][d] * w) % L;
} else {
upd(par[x], d - 1, w);
cnt[x][d] = (cnt[x][d] * w) % L;
cnt[x][d - 1] = (cnt[x][d - 1] * w) % L;
}
}
ll ans(ll x) {
ll ret = start[x];
ll curr = x, d = 0;
while(curr != 0 && d < MAX_D) {
ret = (ret * cnt[curr][d]) % L;
curr = par[curr];
d ++;
}
return ret;
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n >> L;
for(ll i = 0; i < n - 1; i ++) {
ll a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
for(ll i = 1; i <= n; i ++) {
cin >> start[i];
}
for(ll i = 0; i < MAX_N; i ++) {
for(ll j = 0; j < MAX_D; j ++) {
cnt[i][j] = 1;
}
}
dfs(1, 0);
ll q;
cin >> q;
while(q --) {
ll t;
cin >> t;
if(t == 1) {
ll x, d, w;
cin >> x >> d >> w;
upd(x, d, w);
} else {
ll x;
cin >> x;
cout << ans(x) << "\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
75468 KB |
Output is correct |
2 |
Correct |
34 ms |
75448 KB |
Output is correct |
3 |
Correct |
36 ms |
75468 KB |
Output is correct |
4 |
Correct |
34 ms |
75560 KB |
Output is correct |
5 |
Correct |
35 ms |
75540 KB |
Output is correct |
6 |
Correct |
33 ms |
75468 KB |
Output is correct |
7 |
Correct |
34 ms |
75484 KB |
Output is correct |
8 |
Correct |
34 ms |
75548 KB |
Output is correct |
9 |
Correct |
34 ms |
75480 KB |
Output is correct |
10 |
Correct |
34 ms |
75436 KB |
Output is correct |
11 |
Correct |
34 ms |
75468 KB |
Output is correct |
12 |
Correct |
34 ms |
75412 KB |
Output is correct |
13 |
Correct |
34 ms |
75476 KB |
Output is correct |
14 |
Correct |
36 ms |
75440 KB |
Output is correct |
15 |
Correct |
35 ms |
75472 KB |
Output is correct |
16 |
Correct |
34 ms |
75480 KB |
Output is correct |
17 |
Correct |
35 ms |
75388 KB |
Output is correct |
18 |
Correct |
34 ms |
75464 KB |
Output is correct |
19 |
Correct |
34 ms |
75476 KB |
Output is correct |
20 |
Correct |
34 ms |
75476 KB |
Output is correct |
21 |
Correct |
36 ms |
75376 KB |
Output is correct |
22 |
Correct |
37 ms |
75380 KB |
Output is correct |
23 |
Correct |
35 ms |
75456 KB |
Output is correct |
24 |
Correct |
36 ms |
75468 KB |
Output is correct |
25 |
Correct |
35 ms |
75460 KB |
Output is correct |
26 |
Correct |
34 ms |
75392 KB |
Output is correct |
27 |
Correct |
34 ms |
75396 KB |
Output is correct |
28 |
Correct |
35 ms |
75468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
75420 KB |
Output is correct |
2 |
Correct |
488 ms |
97840 KB |
Output is correct |
3 |
Correct |
290 ms |
98228 KB |
Output is correct |
4 |
Correct |
446 ms |
100340 KB |
Output is correct |
5 |
Correct |
384 ms |
97832 KB |
Output is correct |
6 |
Correct |
266 ms |
97516 KB |
Output is correct |
7 |
Correct |
266 ms |
98304 KB |
Output is correct |
8 |
Correct |
220 ms |
98488 KB |
Output is correct |
9 |
Correct |
554 ms |
101848 KB |
Output is correct |
10 |
Correct |
303 ms |
101976 KB |
Output is correct |
11 |
Correct |
481 ms |
97560 KB |
Output is correct |
12 |
Correct |
291 ms |
98156 KB |
Output is correct |
13 |
Correct |
200 ms |
98140 KB |
Output is correct |
14 |
Correct |
195 ms |
98720 KB |
Output is correct |
15 |
Correct |
203 ms |
97916 KB |
Output is correct |
16 |
Correct |
204 ms |
98516 KB |
Output is correct |
17 |
Correct |
204 ms |
99020 KB |
Output is correct |
18 |
Correct |
31 ms |
75376 KB |
Output is correct |
19 |
Correct |
30 ms |
75448 KB |
Output is correct |
20 |
Correct |
30 ms |
75360 KB |
Output is correct |
21 |
Correct |
30 ms |
75428 KB |
Output is correct |
22 |
Correct |
30 ms |
75412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
75420 KB |
Output is correct |
2 |
Correct |
488 ms |
97840 KB |
Output is correct |
3 |
Correct |
290 ms |
98228 KB |
Output is correct |
4 |
Correct |
446 ms |
100340 KB |
Output is correct |
5 |
Correct |
384 ms |
97832 KB |
Output is correct |
6 |
Correct |
266 ms |
97516 KB |
Output is correct |
7 |
Correct |
266 ms |
98304 KB |
Output is correct |
8 |
Correct |
220 ms |
98488 KB |
Output is correct |
9 |
Correct |
554 ms |
101848 KB |
Output is correct |
10 |
Correct |
303 ms |
101976 KB |
Output is correct |
11 |
Correct |
481 ms |
97560 KB |
Output is correct |
12 |
Correct |
291 ms |
98156 KB |
Output is correct |
13 |
Correct |
200 ms |
98140 KB |
Output is correct |
14 |
Correct |
195 ms |
98720 KB |
Output is correct |
15 |
Correct |
203 ms |
97916 KB |
Output is correct |
16 |
Correct |
204 ms |
98516 KB |
Output is correct |
17 |
Correct |
204 ms |
99020 KB |
Output is correct |
18 |
Correct |
31 ms |
75376 KB |
Output is correct |
19 |
Correct |
30 ms |
75448 KB |
Output is correct |
20 |
Correct |
30 ms |
75360 KB |
Output is correct |
21 |
Correct |
30 ms |
75428 KB |
Output is correct |
22 |
Correct |
30 ms |
75412 KB |
Output is correct |
23 |
Correct |
30 ms |
75452 KB |
Output is correct |
24 |
Correct |
469 ms |
97656 KB |
Output is correct |
25 |
Correct |
310 ms |
98308 KB |
Output is correct |
26 |
Correct |
427 ms |
102028 KB |
Output is correct |
27 |
Correct |
396 ms |
97868 KB |
Output is correct |
28 |
Correct |
272 ms |
97956 KB |
Output is correct |
29 |
Correct |
270 ms |
97832 KB |
Output is correct |
30 |
Correct |
228 ms |
98524 KB |
Output is correct |
31 |
Correct |
544 ms |
100268 KB |
Output is correct |
32 |
Correct |
312 ms |
101956 KB |
Output is correct |
33 |
Correct |
475 ms |
97692 KB |
Output is correct |
34 |
Correct |
301 ms |
98100 KB |
Output is correct |
35 |
Correct |
30 ms |
75412 KB |
Output is correct |
36 |
Correct |
29 ms |
75440 KB |
Output is correct |
37 |
Correct |
29 ms |
75400 KB |
Output is correct |
38 |
Correct |
29 ms |
75404 KB |
Output is correct |
39 |
Correct |
30 ms |
75468 KB |
Output is correct |
40 |
Correct |
30 ms |
75436 KB |
Output is correct |
41 |
Correct |
30 ms |
75348 KB |
Output is correct |
42 |
Correct |
30 ms |
75476 KB |
Output is correct |
43 |
Correct |
29 ms |
75456 KB |
Output is correct |
44 |
Correct |
29 ms |
75372 KB |
Output is correct |
45 |
Correct |
31 ms |
75476 KB |
Output is correct |
46 |
Correct |
30 ms |
75408 KB |
Output is correct |
47 |
Correct |
29 ms |
75456 KB |
Output is correct |
48 |
Correct |
30 ms |
75440 KB |
Output is correct |
49 |
Correct |
30 ms |
75416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
75400 KB |
Output is correct |
2 |
Correct |
611 ms |
98940 KB |
Output is correct |
3 |
Correct |
893 ms |
98376 KB |
Output is correct |
4 |
Correct |
593 ms |
98212 KB |
Output is correct |
5 |
Correct |
470 ms |
94872 KB |
Output is correct |
6 |
Correct |
318 ms |
95052 KB |
Output is correct |
7 |
Correct |
294 ms |
95232 KB |
Output is correct |
8 |
Correct |
237 ms |
95528 KB |
Output is correct |
9 |
Correct |
607 ms |
97460 KB |
Output is correct |
10 |
Correct |
878 ms |
99048 KB |
Output is correct |
11 |
Correct |
514 ms |
94580 KB |
Output is correct |
12 |
Correct |
653 ms |
95452 KB |
Output is correct |
13 |
Correct |
427 ms |
96312 KB |
Output is correct |
14 |
Correct |
466 ms |
97204 KB |
Output is correct |
15 |
Correct |
29 ms |
75468 KB |
Output is correct |
16 |
Correct |
29 ms |
75452 KB |
Output is correct |
17 |
Correct |
28 ms |
75452 KB |
Output is correct |
18 |
Correct |
29 ms |
75460 KB |
Output is correct |
19 |
Correct |
30 ms |
75432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
75452 KB |
Output is correct |
2 |
Correct |
623 ms |
99864 KB |
Output is correct |
3 |
Correct |
891 ms |
97480 KB |
Output is correct |
4 |
Correct |
586 ms |
98568 KB |
Output is correct |
5 |
Correct |
532 ms |
96464 KB |
Output is correct |
6 |
Correct |
333 ms |
96660 KB |
Output is correct |
7 |
Correct |
327 ms |
96628 KB |
Output is correct |
8 |
Correct |
234 ms |
96556 KB |
Output is correct |
9 |
Correct |
634 ms |
101208 KB |
Output is correct |
10 |
Correct |
915 ms |
99872 KB |
Output is correct |
11 |
Correct |
529 ms |
97316 KB |
Output is correct |
12 |
Correct |
665 ms |
95748 KB |
Output is correct |
13 |
Correct |
431 ms |
96460 KB |
Output is correct |
14 |
Correct |
463 ms |
97384 KB |
Output is correct |
15 |
Correct |
32 ms |
75448 KB |
Output is correct |
16 |
Correct |
31 ms |
75404 KB |
Output is correct |
17 |
Correct |
30 ms |
75392 KB |
Output is correct |
18 |
Correct |
31 ms |
75476 KB |
Output is correct |
19 |
Correct |
31 ms |
75392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
75468 KB |
Output is correct |
2 |
Correct |
34 ms |
75448 KB |
Output is correct |
3 |
Correct |
36 ms |
75468 KB |
Output is correct |
4 |
Correct |
34 ms |
75560 KB |
Output is correct |
5 |
Correct |
35 ms |
75540 KB |
Output is correct |
6 |
Correct |
33 ms |
75468 KB |
Output is correct |
7 |
Correct |
34 ms |
75484 KB |
Output is correct |
8 |
Correct |
34 ms |
75548 KB |
Output is correct |
9 |
Correct |
34 ms |
75480 KB |
Output is correct |
10 |
Correct |
34 ms |
75436 KB |
Output is correct |
11 |
Correct |
34 ms |
75468 KB |
Output is correct |
12 |
Correct |
34 ms |
75412 KB |
Output is correct |
13 |
Correct |
34 ms |
75476 KB |
Output is correct |
14 |
Correct |
36 ms |
75440 KB |
Output is correct |
15 |
Correct |
35 ms |
75472 KB |
Output is correct |
16 |
Correct |
34 ms |
75480 KB |
Output is correct |
17 |
Correct |
35 ms |
75388 KB |
Output is correct |
18 |
Correct |
34 ms |
75464 KB |
Output is correct |
19 |
Correct |
34 ms |
75476 KB |
Output is correct |
20 |
Correct |
34 ms |
75476 KB |
Output is correct |
21 |
Correct |
36 ms |
75376 KB |
Output is correct |
22 |
Correct |
37 ms |
75380 KB |
Output is correct |
23 |
Correct |
35 ms |
75456 KB |
Output is correct |
24 |
Correct |
36 ms |
75468 KB |
Output is correct |
25 |
Correct |
35 ms |
75460 KB |
Output is correct |
26 |
Correct |
34 ms |
75392 KB |
Output is correct |
27 |
Correct |
34 ms |
75396 KB |
Output is correct |
28 |
Correct |
35 ms |
75468 KB |
Output is correct |
29 |
Correct |
33 ms |
75420 KB |
Output is correct |
30 |
Correct |
488 ms |
97840 KB |
Output is correct |
31 |
Correct |
290 ms |
98228 KB |
Output is correct |
32 |
Correct |
446 ms |
100340 KB |
Output is correct |
33 |
Correct |
384 ms |
97832 KB |
Output is correct |
34 |
Correct |
266 ms |
97516 KB |
Output is correct |
35 |
Correct |
266 ms |
98304 KB |
Output is correct |
36 |
Correct |
220 ms |
98488 KB |
Output is correct |
37 |
Correct |
554 ms |
101848 KB |
Output is correct |
38 |
Correct |
303 ms |
101976 KB |
Output is correct |
39 |
Correct |
481 ms |
97560 KB |
Output is correct |
40 |
Correct |
291 ms |
98156 KB |
Output is correct |
41 |
Correct |
200 ms |
98140 KB |
Output is correct |
42 |
Correct |
195 ms |
98720 KB |
Output is correct |
43 |
Correct |
203 ms |
97916 KB |
Output is correct |
44 |
Correct |
204 ms |
98516 KB |
Output is correct |
45 |
Correct |
204 ms |
99020 KB |
Output is correct |
46 |
Correct |
31 ms |
75376 KB |
Output is correct |
47 |
Correct |
30 ms |
75448 KB |
Output is correct |
48 |
Correct |
30 ms |
75360 KB |
Output is correct |
49 |
Correct |
30 ms |
75428 KB |
Output is correct |
50 |
Correct |
30 ms |
75412 KB |
Output is correct |
51 |
Correct |
30 ms |
75452 KB |
Output is correct |
52 |
Correct |
469 ms |
97656 KB |
Output is correct |
53 |
Correct |
310 ms |
98308 KB |
Output is correct |
54 |
Correct |
427 ms |
102028 KB |
Output is correct |
55 |
Correct |
396 ms |
97868 KB |
Output is correct |
56 |
Correct |
272 ms |
97956 KB |
Output is correct |
57 |
Correct |
270 ms |
97832 KB |
Output is correct |
58 |
Correct |
228 ms |
98524 KB |
Output is correct |
59 |
Correct |
544 ms |
100268 KB |
Output is correct |
60 |
Correct |
312 ms |
101956 KB |
Output is correct |
61 |
Correct |
475 ms |
97692 KB |
Output is correct |
62 |
Correct |
301 ms |
98100 KB |
Output is correct |
63 |
Correct |
30 ms |
75412 KB |
Output is correct |
64 |
Correct |
29 ms |
75440 KB |
Output is correct |
65 |
Correct |
29 ms |
75400 KB |
Output is correct |
66 |
Correct |
29 ms |
75404 KB |
Output is correct |
67 |
Correct |
30 ms |
75468 KB |
Output is correct |
68 |
Correct |
30 ms |
75436 KB |
Output is correct |
69 |
Correct |
30 ms |
75348 KB |
Output is correct |
70 |
Correct |
30 ms |
75476 KB |
Output is correct |
71 |
Correct |
29 ms |
75456 KB |
Output is correct |
72 |
Correct |
29 ms |
75372 KB |
Output is correct |
73 |
Correct |
31 ms |
75476 KB |
Output is correct |
74 |
Correct |
30 ms |
75408 KB |
Output is correct |
75 |
Correct |
29 ms |
75456 KB |
Output is correct |
76 |
Correct |
30 ms |
75440 KB |
Output is correct |
77 |
Correct |
30 ms |
75416 KB |
Output is correct |
78 |
Correct |
30 ms |
75400 KB |
Output is correct |
79 |
Correct |
611 ms |
98940 KB |
Output is correct |
80 |
Correct |
893 ms |
98376 KB |
Output is correct |
81 |
Correct |
593 ms |
98212 KB |
Output is correct |
82 |
Correct |
470 ms |
94872 KB |
Output is correct |
83 |
Correct |
318 ms |
95052 KB |
Output is correct |
84 |
Correct |
294 ms |
95232 KB |
Output is correct |
85 |
Correct |
237 ms |
95528 KB |
Output is correct |
86 |
Correct |
607 ms |
97460 KB |
Output is correct |
87 |
Correct |
878 ms |
99048 KB |
Output is correct |
88 |
Correct |
514 ms |
94580 KB |
Output is correct |
89 |
Correct |
653 ms |
95452 KB |
Output is correct |
90 |
Correct |
427 ms |
96312 KB |
Output is correct |
91 |
Correct |
466 ms |
97204 KB |
Output is correct |
92 |
Correct |
29 ms |
75468 KB |
Output is correct |
93 |
Correct |
29 ms |
75452 KB |
Output is correct |
94 |
Correct |
28 ms |
75452 KB |
Output is correct |
95 |
Correct |
29 ms |
75460 KB |
Output is correct |
96 |
Correct |
30 ms |
75432 KB |
Output is correct |
97 |
Correct |
28 ms |
75452 KB |
Output is correct |
98 |
Correct |
623 ms |
99864 KB |
Output is correct |
99 |
Correct |
891 ms |
97480 KB |
Output is correct |
100 |
Correct |
586 ms |
98568 KB |
Output is correct |
101 |
Correct |
532 ms |
96464 KB |
Output is correct |
102 |
Correct |
333 ms |
96660 KB |
Output is correct |
103 |
Correct |
327 ms |
96628 KB |
Output is correct |
104 |
Correct |
234 ms |
96556 KB |
Output is correct |
105 |
Correct |
634 ms |
101208 KB |
Output is correct |
106 |
Correct |
915 ms |
99872 KB |
Output is correct |
107 |
Correct |
529 ms |
97316 KB |
Output is correct |
108 |
Correct |
665 ms |
95748 KB |
Output is correct |
109 |
Correct |
431 ms |
96460 KB |
Output is correct |
110 |
Correct |
463 ms |
97384 KB |
Output is correct |
111 |
Correct |
32 ms |
75448 KB |
Output is correct |
112 |
Correct |
31 ms |
75404 KB |
Output is correct |
113 |
Correct |
30 ms |
75392 KB |
Output is correct |
114 |
Correct |
31 ms |
75476 KB |
Output is correct |
115 |
Correct |
31 ms |
75392 KB |
Output is correct |
116 |
Correct |
512 ms |
95692 KB |
Output is correct |
117 |
Correct |
717 ms |
98344 KB |
Output is correct |
118 |
Correct |
606 ms |
101864 KB |
Output is correct |
119 |
Correct |
526 ms |
97996 KB |
Output is correct |
120 |
Correct |
357 ms |
97624 KB |
Output is correct |
121 |
Correct |
361 ms |
98392 KB |
Output is correct |
122 |
Correct |
263 ms |
98436 KB |
Output is correct |
123 |
Correct |
618 ms |
100816 KB |
Output is correct |
124 |
Correct |
930 ms |
100216 KB |
Output is correct |
125 |
Correct |
529 ms |
96808 KB |
Output is correct |
126 |
Correct |
728 ms |
98200 KB |
Output is correct |
127 |
Correct |
741 ms |
98600 KB |
Output is correct |
128 |
Correct |
560 ms |
99312 KB |
Output is correct |
129 |
Correct |
569 ms |
100132 KB |
Output is correct |