#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define isz(x) ((int)x.size())
#define sumof(x) accumulate(all(x), 0ll)
int mod;
const int N=2e5+10, MAX_DIST=40;
int n, q, par[N], h[N];
int mult[N][42];
vector<int> g[N];
void dfs(int u, int p){
par[u]=p;
if (p) g[u].erase(find(all(g[u]), p));
for (int v:g[u]) dfs(v, u);
for (int i=0; i<=MAX_DIST+1; ++i) mult[u][i]=1;
}
void update(int u, int d, int w){
int lca=u;
while (par[lca] && d){
mult[lca][d]=mult[lca][d]*w%mod;
mult[lca][d-1]=mult[lca][d-1]*w%mod;
lca=par[lca];
--d;
}
for (int i=d; i>=0; --i) mult[lca][i]=mult[lca][i]*w%mod;
}
int query(int u){
int val=h[u];
int lca=u, dist=0;
while (lca && dist<=MAX_DIST){
val=1ll*val*mult[lca][dist]%mod;
lca=par[lca]; ++dist;
}
return val;
}
void solve(){
cin >> n >> mod;
for (int i=0; i<n-1; ++i){
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 0);
for (int i=1; i<=n; ++i) cin >> h[i];
cin >> q;
while (q--){
int o; cin >> o;
if (o==1){
int u, d, w; cin >> u >> d >> w;
update(u, d, w);
}else{
int u; cin >> u;
cout << query(u) << '\n';
}
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int ntests=1;
// cin >> ntests;
for (int i=1; i<=ntests; ++i) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
5464 KB |
Output is correct |
5 |
Correct |
3 ms |
5468 KB |
Output is correct |
6 |
Correct |
3 ms |
5560 KB |
Output is correct |
7 |
Correct |
3 ms |
5468 KB |
Output is correct |
8 |
Correct |
3 ms |
5468 KB |
Output is correct |
9 |
Correct |
3 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5192 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
3 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
3 ms |
4956 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
5180 KB |
Output is correct |
18 |
Correct |
3 ms |
5100 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
3 ms |
4952 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
Correct |
3 ms |
5172 KB |
Output is correct |
23 |
Correct |
2 ms |
4956 KB |
Output is correct |
24 |
Correct |
3 ms |
4956 KB |
Output is correct |
25 |
Correct |
3 ms |
5208 KB |
Output is correct |
26 |
Correct |
2 ms |
4956 KB |
Output is correct |
27 |
Correct |
3 ms |
5052 KB |
Output is correct |
28 |
Correct |
3 ms |
4952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
528 ms |
91400 KB |
Output is correct |
3 |
Correct |
337 ms |
90824 KB |
Output is correct |
4 |
Correct |
493 ms |
97888 KB |
Output is correct |
5 |
Correct |
467 ms |
93176 KB |
Output is correct |
6 |
Correct |
381 ms |
92980 KB |
Output is correct |
7 |
Correct |
329 ms |
93520 KB |
Output is correct |
8 |
Correct |
277 ms |
93672 KB |
Output is correct |
9 |
Correct |
685 ms |
100948 KB |
Output is correct |
10 |
Correct |
370 ms |
100572 KB |
Output is correct |
11 |
Correct |
659 ms |
92888 KB |
Output is correct |
12 |
Correct |
369 ms |
93296 KB |
Output is correct |
13 |
Correct |
238 ms |
93404 KB |
Output is correct |
14 |
Correct |
246 ms |
93876 KB |
Output is correct |
15 |
Correct |
244 ms |
93112 KB |
Output is correct |
16 |
Correct |
261 ms |
93524 KB |
Output is correct |
17 |
Correct |
303 ms |
93968 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
3 ms |
5016 KB |
Output is correct |
20 |
Correct |
3 ms |
4956 KB |
Output is correct |
21 |
Correct |
3 ms |
4956 KB |
Output is correct |
22 |
Correct |
3 ms |
5196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
528 ms |
91400 KB |
Output is correct |
3 |
Correct |
337 ms |
90824 KB |
Output is correct |
4 |
Correct |
493 ms |
97888 KB |
Output is correct |
5 |
Correct |
467 ms |
93176 KB |
Output is correct |
6 |
Correct |
381 ms |
92980 KB |
Output is correct |
7 |
Correct |
329 ms |
93520 KB |
Output is correct |
8 |
Correct |
277 ms |
93672 KB |
Output is correct |
9 |
Correct |
685 ms |
100948 KB |
Output is correct |
10 |
Correct |
370 ms |
100572 KB |
Output is correct |
11 |
Correct |
659 ms |
92888 KB |
Output is correct |
12 |
Correct |
369 ms |
93296 KB |
Output is correct |
13 |
Correct |
238 ms |
93404 KB |
Output is correct |
14 |
Correct |
246 ms |
93876 KB |
Output is correct |
15 |
Correct |
244 ms |
93112 KB |
Output is correct |
16 |
Correct |
261 ms |
93524 KB |
Output is correct |
17 |
Correct |
303 ms |
93968 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
3 ms |
5016 KB |
Output is correct |
20 |
Correct |
3 ms |
4956 KB |
Output is correct |
21 |
Correct |
3 ms |
4956 KB |
Output is correct |
22 |
Correct |
3 ms |
5196 KB |
Output is correct |
23 |
Correct |
3 ms |
4956 KB |
Output is correct |
24 |
Correct |
593 ms |
93280 KB |
Output is correct |
25 |
Correct |
392 ms |
93384 KB |
Output is correct |
26 |
Correct |
597 ms |
100948 KB |
Output is correct |
27 |
Correct |
457 ms |
93176 KB |
Output is correct |
28 |
Correct |
368 ms |
92440 KB |
Output is correct |
29 |
Correct |
340 ms |
92256 KB |
Output is correct |
30 |
Correct |
278 ms |
93172 KB |
Output is correct |
31 |
Correct |
682 ms |
97648 KB |
Output is correct |
32 |
Correct |
410 ms |
99664 KB |
Output is correct |
33 |
Correct |
546 ms |
92500 KB |
Output is correct |
34 |
Correct |
382 ms |
92352 KB |
Output is correct |
35 |
Correct |
2 ms |
4956 KB |
Output is correct |
36 |
Correct |
2 ms |
4956 KB |
Output is correct |
37 |
Correct |
2 ms |
4956 KB |
Output is correct |
38 |
Correct |
3 ms |
5176 KB |
Output is correct |
39 |
Correct |
3 ms |
4956 KB |
Output is correct |
40 |
Correct |
3 ms |
5036 KB |
Output is correct |
41 |
Correct |
3 ms |
4956 KB |
Output is correct |
42 |
Correct |
3 ms |
4956 KB |
Output is correct |
43 |
Correct |
2 ms |
4956 KB |
Output is correct |
44 |
Correct |
2 ms |
4956 KB |
Output is correct |
45 |
Correct |
2 ms |
4952 KB |
Output is correct |
46 |
Correct |
2 ms |
4956 KB |
Output is correct |
47 |
Correct |
3 ms |
5112 KB |
Output is correct |
48 |
Correct |
3 ms |
4956 KB |
Output is correct |
49 |
Correct |
2 ms |
5192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4952 KB |
Output is correct |
2 |
Correct |
676 ms |
97464 KB |
Output is correct |
3 |
Correct |
741 ms |
95944 KB |
Output is correct |
4 |
Correct |
582 ms |
95668 KB |
Output is correct |
5 |
Correct |
539 ms |
89780 KB |
Output is correct |
6 |
Correct |
370 ms |
88660 KB |
Output is correct |
7 |
Correct |
337 ms |
89180 KB |
Output is correct |
8 |
Correct |
260 ms |
89036 KB |
Output is correct |
9 |
Correct |
648 ms |
93704 KB |
Output is correct |
10 |
Correct |
745 ms |
95792 KB |
Output is correct |
11 |
Correct |
565 ms |
88392 KB |
Output is correct |
12 |
Correct |
552 ms |
89128 KB |
Output is correct |
13 |
Correct |
411 ms |
90180 KB |
Output is correct |
14 |
Correct |
428 ms |
92720 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
3 ms |
4952 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
646 ms |
96040 KB |
Output is correct |
3 |
Correct |
741 ms |
92980 KB |
Output is correct |
4 |
Correct |
595 ms |
94572 KB |
Output is correct |
5 |
Correct |
485 ms |
90044 KB |
Output is correct |
6 |
Correct |
370 ms |
90196 KB |
Output is correct |
7 |
Correct |
345 ms |
90292 KB |
Output is correct |
8 |
Correct |
277 ms |
90156 KB |
Output is correct |
9 |
Correct |
619 ms |
98168 KB |
Output is correct |
10 |
Correct |
771 ms |
96912 KB |
Output is correct |
11 |
Correct |
554 ms |
90960 KB |
Output is correct |
12 |
Correct |
546 ms |
89792 KB |
Output is correct |
13 |
Correct |
407 ms |
90432 KB |
Output is correct |
14 |
Correct |
414 ms |
92692 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4952 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
5464 KB |
Output is correct |
5 |
Correct |
3 ms |
5468 KB |
Output is correct |
6 |
Correct |
3 ms |
5560 KB |
Output is correct |
7 |
Correct |
3 ms |
5468 KB |
Output is correct |
8 |
Correct |
3 ms |
5468 KB |
Output is correct |
9 |
Correct |
3 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5192 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
3 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
3 ms |
4956 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
5180 KB |
Output is correct |
18 |
Correct |
3 ms |
5100 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
3 ms |
4952 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
Correct |
3 ms |
5172 KB |
Output is correct |
23 |
Correct |
2 ms |
4956 KB |
Output is correct |
24 |
Correct |
3 ms |
4956 KB |
Output is correct |
25 |
Correct |
3 ms |
5208 KB |
Output is correct |
26 |
Correct |
2 ms |
4956 KB |
Output is correct |
27 |
Correct |
3 ms |
5052 KB |
Output is correct |
28 |
Correct |
3 ms |
4952 KB |
Output is correct |
29 |
Correct |
2 ms |
4956 KB |
Output is correct |
30 |
Correct |
528 ms |
91400 KB |
Output is correct |
31 |
Correct |
337 ms |
90824 KB |
Output is correct |
32 |
Correct |
493 ms |
97888 KB |
Output is correct |
33 |
Correct |
467 ms |
93176 KB |
Output is correct |
34 |
Correct |
381 ms |
92980 KB |
Output is correct |
35 |
Correct |
329 ms |
93520 KB |
Output is correct |
36 |
Correct |
277 ms |
93672 KB |
Output is correct |
37 |
Correct |
685 ms |
100948 KB |
Output is correct |
38 |
Correct |
370 ms |
100572 KB |
Output is correct |
39 |
Correct |
659 ms |
92888 KB |
Output is correct |
40 |
Correct |
369 ms |
93296 KB |
Output is correct |
41 |
Correct |
238 ms |
93404 KB |
Output is correct |
42 |
Correct |
246 ms |
93876 KB |
Output is correct |
43 |
Correct |
244 ms |
93112 KB |
Output is correct |
44 |
Correct |
261 ms |
93524 KB |
Output is correct |
45 |
Correct |
303 ms |
93968 KB |
Output is correct |
46 |
Correct |
2 ms |
4956 KB |
Output is correct |
47 |
Correct |
3 ms |
5016 KB |
Output is correct |
48 |
Correct |
3 ms |
4956 KB |
Output is correct |
49 |
Correct |
3 ms |
4956 KB |
Output is correct |
50 |
Correct |
3 ms |
5196 KB |
Output is correct |
51 |
Correct |
3 ms |
4956 KB |
Output is correct |
52 |
Correct |
593 ms |
93280 KB |
Output is correct |
53 |
Correct |
392 ms |
93384 KB |
Output is correct |
54 |
Correct |
597 ms |
100948 KB |
Output is correct |
55 |
Correct |
457 ms |
93176 KB |
Output is correct |
56 |
Correct |
368 ms |
92440 KB |
Output is correct |
57 |
Correct |
340 ms |
92256 KB |
Output is correct |
58 |
Correct |
278 ms |
93172 KB |
Output is correct |
59 |
Correct |
682 ms |
97648 KB |
Output is correct |
60 |
Correct |
410 ms |
99664 KB |
Output is correct |
61 |
Correct |
546 ms |
92500 KB |
Output is correct |
62 |
Correct |
382 ms |
92352 KB |
Output is correct |
63 |
Correct |
2 ms |
4956 KB |
Output is correct |
64 |
Correct |
2 ms |
4956 KB |
Output is correct |
65 |
Correct |
2 ms |
4956 KB |
Output is correct |
66 |
Correct |
3 ms |
5176 KB |
Output is correct |
67 |
Correct |
3 ms |
4956 KB |
Output is correct |
68 |
Correct |
3 ms |
5036 KB |
Output is correct |
69 |
Correct |
3 ms |
4956 KB |
Output is correct |
70 |
Correct |
3 ms |
4956 KB |
Output is correct |
71 |
Correct |
2 ms |
4956 KB |
Output is correct |
72 |
Correct |
2 ms |
4956 KB |
Output is correct |
73 |
Correct |
2 ms |
4952 KB |
Output is correct |
74 |
Correct |
2 ms |
4956 KB |
Output is correct |
75 |
Correct |
3 ms |
5112 KB |
Output is correct |
76 |
Correct |
3 ms |
4956 KB |
Output is correct |
77 |
Correct |
2 ms |
5192 KB |
Output is correct |
78 |
Correct |
3 ms |
4952 KB |
Output is correct |
79 |
Correct |
676 ms |
97464 KB |
Output is correct |
80 |
Correct |
741 ms |
95944 KB |
Output is correct |
81 |
Correct |
582 ms |
95668 KB |
Output is correct |
82 |
Correct |
539 ms |
89780 KB |
Output is correct |
83 |
Correct |
370 ms |
88660 KB |
Output is correct |
84 |
Correct |
337 ms |
89180 KB |
Output is correct |
85 |
Correct |
260 ms |
89036 KB |
Output is correct |
86 |
Correct |
648 ms |
93704 KB |
Output is correct |
87 |
Correct |
745 ms |
95792 KB |
Output is correct |
88 |
Correct |
565 ms |
88392 KB |
Output is correct |
89 |
Correct |
552 ms |
89128 KB |
Output is correct |
90 |
Correct |
411 ms |
90180 KB |
Output is correct |
91 |
Correct |
428 ms |
92720 KB |
Output is correct |
92 |
Correct |
2 ms |
4956 KB |
Output is correct |
93 |
Correct |
3 ms |
4952 KB |
Output is correct |
94 |
Correct |
2 ms |
4956 KB |
Output is correct |
95 |
Correct |
2 ms |
4956 KB |
Output is correct |
96 |
Correct |
2 ms |
4956 KB |
Output is correct |
97 |
Correct |
2 ms |
4952 KB |
Output is correct |
98 |
Correct |
646 ms |
96040 KB |
Output is correct |
99 |
Correct |
741 ms |
92980 KB |
Output is correct |
100 |
Correct |
595 ms |
94572 KB |
Output is correct |
101 |
Correct |
485 ms |
90044 KB |
Output is correct |
102 |
Correct |
370 ms |
90196 KB |
Output is correct |
103 |
Correct |
345 ms |
90292 KB |
Output is correct |
104 |
Correct |
277 ms |
90156 KB |
Output is correct |
105 |
Correct |
619 ms |
98168 KB |
Output is correct |
106 |
Correct |
771 ms |
96912 KB |
Output is correct |
107 |
Correct |
554 ms |
90960 KB |
Output is correct |
108 |
Correct |
546 ms |
89792 KB |
Output is correct |
109 |
Correct |
407 ms |
90432 KB |
Output is correct |
110 |
Correct |
414 ms |
92692 KB |
Output is correct |
111 |
Correct |
2 ms |
4956 KB |
Output is correct |
112 |
Correct |
2 ms |
4952 KB |
Output is correct |
113 |
Correct |
2 ms |
4956 KB |
Output is correct |
114 |
Correct |
2 ms |
4956 KB |
Output is correct |
115 |
Correct |
2 ms |
4956 KB |
Output is correct |
116 |
Correct |
595 ms |
91148 KB |
Output is correct |
117 |
Correct |
572 ms |
93704 KB |
Output is correct |
118 |
Correct |
605 ms |
100776 KB |
Output is correct |
119 |
Correct |
523 ms |
93308 KB |
Output is correct |
120 |
Correct |
392 ms |
93056 KB |
Output is correct |
121 |
Correct |
380 ms |
93956 KB |
Output is correct |
122 |
Correct |
289 ms |
93896 KB |
Output is correct |
123 |
Correct |
631 ms |
99224 KB |
Output is correct |
124 |
Correct |
808 ms |
97708 KB |
Output is correct |
125 |
Correct |
533 ms |
92248 KB |
Output is correct |
126 |
Correct |
628 ms |
93520 KB |
Output is correct |
127 |
Correct |
609 ms |
94120 KB |
Output is correct |
128 |
Correct |
518 ms |
94604 KB |
Output is correct |
129 |
Correct |
522 ms |
95504 KB |
Output is correct |