#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,q,p[200001];
ll h[200001],mul[200001][42],mod;
vector<int> adj[200001];
void dfs(int x, int par) {
p[x]=par;
for (auto s : adj[x]) if (s!=par) dfs(s,x);
}
void add(int node, int dis, ll val) {
while (node!=0 && dis!=-1) {
mul[node][dis]=(mul[node][dis]*val)%mod;
node=p[node];
--dis;
}
}
void find(int node) {
int dis=0;
ll ans=h[node];
while (dis<=40 && node!=0) {
ans=(ans*mul[node][dis])%mod;
ans=(ans*mul[node][dis+1])%mod;
++dis; node=p[node];
}
for (int i=dis+1; i<=40; ++i) { //distance can higher than root
ans=(ans*mul[1][i])%mod;
}
cout<<ans<<"\n";
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>mod;
for (int i=1; i<=n; ++i) {
for (int j=0; j<=41; ++j) mul[i][j]=1;
}
for (int i=0; i<n-1; ++i) {
int a,b; cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i=1; i<=n; ++i) cin>>h[i];
dfs(1,0);
cin>>q;
while (q--) {
int mode; cin>>mode;
if (mode==1) {
int node,dis;
ll w;
cin>>node>>dis>>w;
add(node,dis,w);
} else {
int node; cin>>node;
find(node);
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5072 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
5332 KB |
Output is correct |
5 |
Correct |
4 ms |
5420 KB |
Output is correct |
6 |
Correct |
4 ms |
5332 KB |
Output is correct |
7 |
Correct |
5 ms |
5332 KB |
Output is correct |
8 |
Correct |
4 ms |
5424 KB |
Output is correct |
9 |
Correct |
4 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5032 KB |
Output is correct |
14 |
Correct |
3 ms |
5032 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5036 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5028 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
4 ms |
5032 KB |
Output is correct |
23 |
Correct |
3 ms |
4948 KB |
Output is correct |
24 |
Correct |
3 ms |
5032 KB |
Output is correct |
25 |
Correct |
3 ms |
4948 KB |
Output is correct |
26 |
Correct |
3 ms |
5032 KB |
Output is correct |
27 |
Correct |
3 ms |
4948 KB |
Output is correct |
28 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
568 ms |
82960 KB |
Output is correct |
3 |
Correct |
327 ms |
79820 KB |
Output is correct |
4 |
Correct |
482 ms |
84164 KB |
Output is correct |
5 |
Correct |
477 ms |
81456 KB |
Output is correct |
6 |
Correct |
341 ms |
81280 KB |
Output is correct |
7 |
Correct |
358 ms |
81632 KB |
Output is correct |
8 |
Correct |
294 ms |
82028 KB |
Output is correct |
9 |
Correct |
604 ms |
87168 KB |
Output is correct |
10 |
Correct |
369 ms |
83660 KB |
Output is correct |
11 |
Correct |
564 ms |
83020 KB |
Output is correct |
12 |
Correct |
383 ms |
91724 KB |
Output is correct |
13 |
Correct |
248 ms |
92036 KB |
Output is correct |
14 |
Correct |
257 ms |
92500 KB |
Output is correct |
15 |
Correct |
256 ms |
91784 KB |
Output is correct |
16 |
Correct |
264 ms |
92312 KB |
Output is correct |
17 |
Correct |
256 ms |
92884 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5036 KB |
Output is correct |
20 |
Correct |
3 ms |
5032 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
4 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
568 ms |
82960 KB |
Output is correct |
3 |
Correct |
327 ms |
79820 KB |
Output is correct |
4 |
Correct |
482 ms |
84164 KB |
Output is correct |
5 |
Correct |
477 ms |
81456 KB |
Output is correct |
6 |
Correct |
341 ms |
81280 KB |
Output is correct |
7 |
Correct |
358 ms |
81632 KB |
Output is correct |
8 |
Correct |
294 ms |
82028 KB |
Output is correct |
9 |
Correct |
604 ms |
87168 KB |
Output is correct |
10 |
Correct |
369 ms |
83660 KB |
Output is correct |
11 |
Correct |
564 ms |
83020 KB |
Output is correct |
12 |
Correct |
383 ms |
91724 KB |
Output is correct |
13 |
Correct |
248 ms |
92036 KB |
Output is correct |
14 |
Correct |
257 ms |
92500 KB |
Output is correct |
15 |
Correct |
256 ms |
91784 KB |
Output is correct |
16 |
Correct |
264 ms |
92312 KB |
Output is correct |
17 |
Correct |
256 ms |
92884 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5036 KB |
Output is correct |
20 |
Correct |
3 ms |
5032 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
4 ms |
4948 KB |
Output is correct |
23 |
Correct |
4 ms |
4948 KB |
Output is correct |
24 |
Correct |
547 ms |
91332 KB |
Output is correct |
25 |
Correct |
385 ms |
91716 KB |
Output is correct |
26 |
Correct |
509 ms |
95564 KB |
Output is correct |
27 |
Correct |
458 ms |
91424 KB |
Output is correct |
28 |
Correct |
425 ms |
91572 KB |
Output is correct |
29 |
Correct |
384 ms |
91308 KB |
Output is correct |
30 |
Correct |
317 ms |
92412 KB |
Output is correct |
31 |
Correct |
656 ms |
94304 KB |
Output is correct |
32 |
Correct |
421 ms |
95596 KB |
Output is correct |
33 |
Correct |
654 ms |
91132 KB |
Output is correct |
34 |
Correct |
431 ms |
91736 KB |
Output is correct |
35 |
Correct |
4 ms |
4948 KB |
Output is correct |
36 |
Correct |
5 ms |
4948 KB |
Output is correct |
37 |
Correct |
4 ms |
4948 KB |
Output is correct |
38 |
Correct |
4 ms |
5036 KB |
Output is correct |
39 |
Correct |
4 ms |
4948 KB |
Output is correct |
40 |
Correct |
5 ms |
4948 KB |
Output is correct |
41 |
Correct |
4 ms |
5032 KB |
Output is correct |
42 |
Correct |
3 ms |
5032 KB |
Output is correct |
43 |
Correct |
4 ms |
5036 KB |
Output is correct |
44 |
Correct |
5 ms |
5032 KB |
Output is correct |
45 |
Correct |
3 ms |
4948 KB |
Output is correct |
46 |
Correct |
3 ms |
4948 KB |
Output is correct |
47 |
Correct |
4 ms |
4988 KB |
Output is correct |
48 |
Correct |
4 ms |
5024 KB |
Output is correct |
49 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
780 ms |
84660 KB |
Output is correct |
3 |
Correct |
658 ms |
82892 KB |
Output is correct |
4 |
Correct |
641 ms |
83544 KB |
Output is correct |
5 |
Correct |
537 ms |
80064 KB |
Output is correct |
6 |
Correct |
469 ms |
80180 KB |
Output is correct |
7 |
Correct |
380 ms |
80424 KB |
Output is correct |
8 |
Correct |
291 ms |
80836 KB |
Output is correct |
9 |
Correct |
707 ms |
83580 KB |
Output is correct |
10 |
Correct |
653 ms |
83712 KB |
Output is correct |
11 |
Correct |
684 ms |
80448 KB |
Output is correct |
12 |
Correct |
562 ms |
79908 KB |
Output is correct |
13 |
Correct |
399 ms |
80672 KB |
Output is correct |
14 |
Correct |
388 ms |
81072 KB |
Output is correct |
15 |
Correct |
4 ms |
4948 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
5028 KB |
Output is correct |
19 |
Correct |
4 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
672 ms |
86100 KB |
Output is correct |
3 |
Correct |
609 ms |
91552 KB |
Output is correct |
4 |
Correct |
645 ms |
92472 KB |
Output is correct |
5 |
Correct |
514 ms |
90104 KB |
Output is correct |
6 |
Correct |
404 ms |
90260 KB |
Output is correct |
7 |
Correct |
399 ms |
89964 KB |
Output is correct |
8 |
Correct |
314 ms |
90404 KB |
Output is correct |
9 |
Correct |
651 ms |
94884 KB |
Output is correct |
10 |
Correct |
610 ms |
93252 KB |
Output is correct |
11 |
Correct |
551 ms |
90796 KB |
Output is correct |
12 |
Correct |
565 ms |
89392 KB |
Output is correct |
13 |
Correct |
366 ms |
90060 KB |
Output is correct |
14 |
Correct |
394 ms |
90556 KB |
Output is correct |
15 |
Correct |
3 ms |
5032 KB |
Output is correct |
16 |
Correct |
3 ms |
5032 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
5028 KB |
Output is correct |
19 |
Correct |
3 ms |
5032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5072 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
5332 KB |
Output is correct |
5 |
Correct |
4 ms |
5420 KB |
Output is correct |
6 |
Correct |
4 ms |
5332 KB |
Output is correct |
7 |
Correct |
5 ms |
5332 KB |
Output is correct |
8 |
Correct |
4 ms |
5424 KB |
Output is correct |
9 |
Correct |
4 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5032 KB |
Output is correct |
14 |
Correct |
3 ms |
5032 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5036 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5028 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
4 ms |
5032 KB |
Output is correct |
23 |
Correct |
3 ms |
4948 KB |
Output is correct |
24 |
Correct |
3 ms |
5032 KB |
Output is correct |
25 |
Correct |
3 ms |
4948 KB |
Output is correct |
26 |
Correct |
3 ms |
5032 KB |
Output is correct |
27 |
Correct |
3 ms |
4948 KB |
Output is correct |
28 |
Correct |
3 ms |
4948 KB |
Output is correct |
29 |
Correct |
3 ms |
4948 KB |
Output is correct |
30 |
Correct |
568 ms |
82960 KB |
Output is correct |
31 |
Correct |
327 ms |
79820 KB |
Output is correct |
32 |
Correct |
482 ms |
84164 KB |
Output is correct |
33 |
Correct |
477 ms |
81456 KB |
Output is correct |
34 |
Correct |
341 ms |
81280 KB |
Output is correct |
35 |
Correct |
358 ms |
81632 KB |
Output is correct |
36 |
Correct |
294 ms |
82028 KB |
Output is correct |
37 |
Correct |
604 ms |
87168 KB |
Output is correct |
38 |
Correct |
369 ms |
83660 KB |
Output is correct |
39 |
Correct |
564 ms |
83020 KB |
Output is correct |
40 |
Correct |
383 ms |
91724 KB |
Output is correct |
41 |
Correct |
248 ms |
92036 KB |
Output is correct |
42 |
Correct |
257 ms |
92500 KB |
Output is correct |
43 |
Correct |
256 ms |
91784 KB |
Output is correct |
44 |
Correct |
264 ms |
92312 KB |
Output is correct |
45 |
Correct |
256 ms |
92884 KB |
Output is correct |
46 |
Correct |
3 ms |
4948 KB |
Output is correct |
47 |
Correct |
3 ms |
5036 KB |
Output is correct |
48 |
Correct |
3 ms |
5032 KB |
Output is correct |
49 |
Correct |
3 ms |
4948 KB |
Output is correct |
50 |
Correct |
4 ms |
4948 KB |
Output is correct |
51 |
Correct |
4 ms |
4948 KB |
Output is correct |
52 |
Correct |
547 ms |
91332 KB |
Output is correct |
53 |
Correct |
385 ms |
91716 KB |
Output is correct |
54 |
Correct |
509 ms |
95564 KB |
Output is correct |
55 |
Correct |
458 ms |
91424 KB |
Output is correct |
56 |
Correct |
425 ms |
91572 KB |
Output is correct |
57 |
Correct |
384 ms |
91308 KB |
Output is correct |
58 |
Correct |
317 ms |
92412 KB |
Output is correct |
59 |
Correct |
656 ms |
94304 KB |
Output is correct |
60 |
Correct |
421 ms |
95596 KB |
Output is correct |
61 |
Correct |
654 ms |
91132 KB |
Output is correct |
62 |
Correct |
431 ms |
91736 KB |
Output is correct |
63 |
Correct |
4 ms |
4948 KB |
Output is correct |
64 |
Correct |
5 ms |
4948 KB |
Output is correct |
65 |
Correct |
4 ms |
4948 KB |
Output is correct |
66 |
Correct |
4 ms |
5036 KB |
Output is correct |
67 |
Correct |
4 ms |
4948 KB |
Output is correct |
68 |
Correct |
5 ms |
4948 KB |
Output is correct |
69 |
Correct |
4 ms |
5032 KB |
Output is correct |
70 |
Correct |
3 ms |
5032 KB |
Output is correct |
71 |
Correct |
4 ms |
5036 KB |
Output is correct |
72 |
Correct |
5 ms |
5032 KB |
Output is correct |
73 |
Correct |
3 ms |
4948 KB |
Output is correct |
74 |
Correct |
3 ms |
4948 KB |
Output is correct |
75 |
Correct |
4 ms |
4988 KB |
Output is correct |
76 |
Correct |
4 ms |
5024 KB |
Output is correct |
77 |
Correct |
3 ms |
4948 KB |
Output is correct |
78 |
Correct |
3 ms |
4948 KB |
Output is correct |
79 |
Correct |
780 ms |
84660 KB |
Output is correct |
80 |
Correct |
658 ms |
82892 KB |
Output is correct |
81 |
Correct |
641 ms |
83544 KB |
Output is correct |
82 |
Correct |
537 ms |
80064 KB |
Output is correct |
83 |
Correct |
469 ms |
80180 KB |
Output is correct |
84 |
Correct |
380 ms |
80424 KB |
Output is correct |
85 |
Correct |
291 ms |
80836 KB |
Output is correct |
86 |
Correct |
707 ms |
83580 KB |
Output is correct |
87 |
Correct |
653 ms |
83712 KB |
Output is correct |
88 |
Correct |
684 ms |
80448 KB |
Output is correct |
89 |
Correct |
562 ms |
79908 KB |
Output is correct |
90 |
Correct |
399 ms |
80672 KB |
Output is correct |
91 |
Correct |
388 ms |
81072 KB |
Output is correct |
92 |
Correct |
4 ms |
4948 KB |
Output is correct |
93 |
Correct |
4 ms |
4948 KB |
Output is correct |
94 |
Correct |
3 ms |
4948 KB |
Output is correct |
95 |
Correct |
4 ms |
5028 KB |
Output is correct |
96 |
Correct |
4 ms |
4948 KB |
Output is correct |
97 |
Correct |
3 ms |
4948 KB |
Output is correct |
98 |
Correct |
672 ms |
86100 KB |
Output is correct |
99 |
Correct |
609 ms |
91552 KB |
Output is correct |
100 |
Correct |
645 ms |
92472 KB |
Output is correct |
101 |
Correct |
514 ms |
90104 KB |
Output is correct |
102 |
Correct |
404 ms |
90260 KB |
Output is correct |
103 |
Correct |
399 ms |
89964 KB |
Output is correct |
104 |
Correct |
314 ms |
90404 KB |
Output is correct |
105 |
Correct |
651 ms |
94884 KB |
Output is correct |
106 |
Correct |
610 ms |
93252 KB |
Output is correct |
107 |
Correct |
551 ms |
90796 KB |
Output is correct |
108 |
Correct |
565 ms |
89392 KB |
Output is correct |
109 |
Correct |
366 ms |
90060 KB |
Output is correct |
110 |
Correct |
394 ms |
90556 KB |
Output is correct |
111 |
Correct |
3 ms |
5032 KB |
Output is correct |
112 |
Correct |
3 ms |
5032 KB |
Output is correct |
113 |
Correct |
3 ms |
4948 KB |
Output is correct |
114 |
Correct |
4 ms |
5028 KB |
Output is correct |
115 |
Correct |
3 ms |
5032 KB |
Output is correct |
116 |
Correct |
588 ms |
89316 KB |
Output is correct |
117 |
Correct |
557 ms |
91800 KB |
Output is correct |
118 |
Correct |
568 ms |
95444 KB |
Output is correct |
119 |
Correct |
525 ms |
91520 KB |
Output is correct |
120 |
Correct |
426 ms |
91256 KB |
Output is correct |
121 |
Correct |
410 ms |
91884 KB |
Output is correct |
122 |
Correct |
324 ms |
92232 KB |
Output is correct |
123 |
Correct |
657 ms |
94536 KB |
Output is correct |
124 |
Correct |
611 ms |
94160 KB |
Output is correct |
125 |
Correct |
574 ms |
90596 KB |
Output is correct |
126 |
Correct |
609 ms |
91672 KB |
Output is correct |
127 |
Correct |
552 ms |
92224 KB |
Output is correct |
128 |
Correct |
426 ms |
92844 KB |
Output is correct |
129 |
Correct |
427 ms |
93444 KB |
Output is correct |