// Heiy ... key mn na omid shodam azat ke in beshe dovomin bar?
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) x.begin(), x.end()
#define pb push_back
#define se second
#define fi first
const int maxn5 = 2e5 + 10;
int mod, av[maxn5][43], par[maxn5], a[maxn5];
vector <int> adj[maxn5];
void dfs(int v){
fill(av[v], av[v] + 43, 1);
for(auto u : adj[v]) if(u != par[v]){
par[u] = v;
dfs(u);
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n; cin >> n >> mod;
for(int i = 0; i < n - 1; i++){
int a, b; cin >> a >> b;
a--; b--;
adj[a].pb(b);
adj[b].pb(a);
}
for(int i = 0; i < n; i++)
cin >> a[i];
par[0] = -1;
dfs(0);
int q; cin >> q;
while(q--){
int ty, v; cin >> ty >> v;
v--;
if(ty == 1){
int d;
ll w;
cin >> d >> w;
while(d >= 0){
av[v][d] = av[v][d] * w % mod;
if(d)
av[v][d - 1] = av[v][d - 1] * w % mod;
if(!v){
for(int i = 0; i < d - 1; i++)
av[v][i] = av[v][i] * w % mod;
break;
}
v = par[v];
d--;
}
//cout << "here " << av[0][0] << '\n';
}
else{
ll ans = a[v];
int done = 0;
while(v != -1 && done <= 40){
ans = ans * av[v][done] % mod;
//cout << "aha " << v << ' ' << done << ' ' << av[v][done] << '\n';
v = par[v];
done++;
}
cout << ans << '\n';
}
}
}
/*
4 7
1 2
2 3
3 4
1
1
1
1
2
1 2 1 2
2 1
1 1 0 2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5028 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
5204 KB |
Output is correct |
5 |
Correct |
5 ms |
5164 KB |
Output is correct |
6 |
Correct |
4 ms |
5168 KB |
Output is correct |
7 |
Correct |
4 ms |
5172 KB |
Output is correct |
8 |
Correct |
5 ms |
5204 KB |
Output is correct |
9 |
Correct |
5 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 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 |
4948 KB |
Output is correct |
14 |
Correct |
4 ms |
4948 KB |
Output is correct |
15 |
Correct |
4 ms |
5040 KB |
Output is correct |
16 |
Correct |
4 ms |
5040 KB |
Output is correct |
17 |
Correct |
4 ms |
5028 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
19 |
Correct |
4 ms |
4948 KB |
Output is correct |
20 |
Correct |
4 ms |
5040 KB |
Output is correct |
21 |
Correct |
3 ms |
5032 KB |
Output is correct |
22 |
Correct |
3 ms |
4932 KB |
Output is correct |
23 |
Correct |
4 ms |
4948 KB |
Output is correct |
24 |
Correct |
4 ms |
5036 KB |
Output is correct |
25 |
Correct |
3 ms |
4948 KB |
Output is correct |
26 |
Correct |
3 ms |
4948 KB |
Output is correct |
27 |
Correct |
3 ms |
4948 KB |
Output is correct |
28 |
Correct |
4 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
564 ms |
56704 KB |
Output is correct |
3 |
Correct |
435 ms |
57232 KB |
Output is correct |
4 |
Correct |
516 ms |
62780 KB |
Output is correct |
5 |
Correct |
498 ms |
56912 KB |
Output is correct |
6 |
Correct |
344 ms |
56468 KB |
Output is correct |
7 |
Correct |
410 ms |
57204 KB |
Output is correct |
8 |
Correct |
296 ms |
57664 KB |
Output is correct |
9 |
Correct |
692 ms |
65612 KB |
Output is correct |
10 |
Correct |
450 ms |
65252 KB |
Output is correct |
11 |
Correct |
596 ms |
56624 KB |
Output is correct |
12 |
Correct |
434 ms |
57176 KB |
Output is correct |
13 |
Correct |
431 ms |
57408 KB |
Output is correct |
14 |
Correct |
322 ms |
57896 KB |
Output is correct |
15 |
Correct |
366 ms |
57200 KB |
Output is correct |
16 |
Correct |
328 ms |
57784 KB |
Output is correct |
17 |
Correct |
307 ms |
58428 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
4 ms |
4948 KB |
Output is correct |
20 |
Correct |
4 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
5044 KB |
Output is correct |
22 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
564 ms |
56704 KB |
Output is correct |
3 |
Correct |
435 ms |
57232 KB |
Output is correct |
4 |
Correct |
516 ms |
62780 KB |
Output is correct |
5 |
Correct |
498 ms |
56912 KB |
Output is correct |
6 |
Correct |
344 ms |
56468 KB |
Output is correct |
7 |
Correct |
410 ms |
57204 KB |
Output is correct |
8 |
Correct |
296 ms |
57664 KB |
Output is correct |
9 |
Correct |
692 ms |
65612 KB |
Output is correct |
10 |
Correct |
450 ms |
65252 KB |
Output is correct |
11 |
Correct |
596 ms |
56624 KB |
Output is correct |
12 |
Correct |
434 ms |
57176 KB |
Output is correct |
13 |
Correct |
431 ms |
57408 KB |
Output is correct |
14 |
Correct |
322 ms |
57896 KB |
Output is correct |
15 |
Correct |
366 ms |
57200 KB |
Output is correct |
16 |
Correct |
328 ms |
57784 KB |
Output is correct |
17 |
Correct |
307 ms |
58428 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
4 ms |
4948 KB |
Output is correct |
20 |
Correct |
4 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
5044 KB |
Output is correct |
22 |
Correct |
3 ms |
4948 KB |
Output is correct |
23 |
Correct |
4 ms |
4948 KB |
Output is correct |
24 |
Correct |
599 ms |
56604 KB |
Output is correct |
25 |
Correct |
398 ms |
56980 KB |
Output is correct |
26 |
Correct |
563 ms |
65264 KB |
Output is correct |
27 |
Correct |
520 ms |
56720 KB |
Output is correct |
28 |
Correct |
451 ms |
56696 KB |
Output is correct |
29 |
Correct |
462 ms |
56436 KB |
Output is correct |
30 |
Correct |
315 ms |
57484 KB |
Output is correct |
31 |
Correct |
658 ms |
62668 KB |
Output is correct |
32 |
Correct |
452 ms |
65096 KB |
Output is correct |
33 |
Correct |
616 ms |
56508 KB |
Output is correct |
34 |
Correct |
448 ms |
57016 KB |
Output is correct |
35 |
Correct |
4 ms |
4948 KB |
Output is correct |
36 |
Correct |
5 ms |
4948 KB |
Output is correct |
37 |
Correct |
3 ms |
4948 KB |
Output is correct |
38 |
Correct |
3 ms |
4948 KB |
Output is correct |
39 |
Correct |
5 ms |
5040 KB |
Output is correct |
40 |
Correct |
4 ms |
5036 KB |
Output is correct |
41 |
Correct |
4 ms |
4948 KB |
Output is correct |
42 |
Correct |
3 ms |
4948 KB |
Output is correct |
43 |
Correct |
3 ms |
4948 KB |
Output is correct |
44 |
Correct |
4 ms |
4948 KB |
Output is correct |
45 |
Correct |
3 ms |
4948 KB |
Output is correct |
46 |
Correct |
3 ms |
5040 KB |
Output is correct |
47 |
Correct |
3 ms |
5044 KB |
Output is correct |
48 |
Correct |
3 ms |
5004 KB |
Output is correct |
49 |
Correct |
3 ms |
5032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
789 ms |
61316 KB |
Output is correct |
3 |
Correct |
774 ms |
60748 KB |
Output is correct |
4 |
Correct |
676 ms |
60608 KB |
Output is correct |
5 |
Correct |
609 ms |
53500 KB |
Output is correct |
6 |
Correct |
498 ms |
53712 KB |
Output is correct |
7 |
Correct |
384 ms |
53928 KB |
Output is correct |
8 |
Correct |
295 ms |
54504 KB |
Output is correct |
9 |
Correct |
654 ms |
59460 KB |
Output is correct |
10 |
Correct |
805 ms |
62128 KB |
Output is correct |
11 |
Correct |
553 ms |
53068 KB |
Output is correct |
12 |
Correct |
604 ms |
54104 KB |
Output is correct |
13 |
Correct |
448 ms |
55008 KB |
Output is correct |
14 |
Correct |
479 ms |
55480 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 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 |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5032 KB |
Output is correct |
2 |
Correct |
646 ms |
61860 KB |
Output is correct |
3 |
Correct |
837 ms |
59024 KB |
Output is correct |
4 |
Correct |
596 ms |
60460 KB |
Output is correct |
5 |
Correct |
524 ms |
55124 KB |
Output is correct |
6 |
Correct |
420 ms |
55316 KB |
Output is correct |
7 |
Correct |
419 ms |
55052 KB |
Output is correct |
8 |
Correct |
296 ms |
55608 KB |
Output is correct |
9 |
Correct |
680 ms |
64384 KB |
Output is correct |
10 |
Correct |
844 ms |
63208 KB |
Output is correct |
11 |
Correct |
613 ms |
56060 KB |
Output is correct |
12 |
Correct |
649 ms |
54640 KB |
Output is correct |
13 |
Correct |
451 ms |
55400 KB |
Output is correct |
14 |
Correct |
486 ms |
55912 KB |
Output is correct |
15 |
Correct |
3 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 |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
4 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5028 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
5204 KB |
Output is correct |
5 |
Correct |
5 ms |
5164 KB |
Output is correct |
6 |
Correct |
4 ms |
5168 KB |
Output is correct |
7 |
Correct |
4 ms |
5172 KB |
Output is correct |
8 |
Correct |
5 ms |
5204 KB |
Output is correct |
9 |
Correct |
5 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 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 |
4948 KB |
Output is correct |
14 |
Correct |
4 ms |
4948 KB |
Output is correct |
15 |
Correct |
4 ms |
5040 KB |
Output is correct |
16 |
Correct |
4 ms |
5040 KB |
Output is correct |
17 |
Correct |
4 ms |
5028 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
19 |
Correct |
4 ms |
4948 KB |
Output is correct |
20 |
Correct |
4 ms |
5040 KB |
Output is correct |
21 |
Correct |
3 ms |
5032 KB |
Output is correct |
22 |
Correct |
3 ms |
4932 KB |
Output is correct |
23 |
Correct |
4 ms |
4948 KB |
Output is correct |
24 |
Correct |
4 ms |
5036 KB |
Output is correct |
25 |
Correct |
3 ms |
4948 KB |
Output is correct |
26 |
Correct |
3 ms |
4948 KB |
Output is correct |
27 |
Correct |
3 ms |
4948 KB |
Output is correct |
28 |
Correct |
4 ms |
4948 KB |
Output is correct |
29 |
Correct |
3 ms |
4948 KB |
Output is correct |
30 |
Correct |
564 ms |
56704 KB |
Output is correct |
31 |
Correct |
435 ms |
57232 KB |
Output is correct |
32 |
Correct |
516 ms |
62780 KB |
Output is correct |
33 |
Correct |
498 ms |
56912 KB |
Output is correct |
34 |
Correct |
344 ms |
56468 KB |
Output is correct |
35 |
Correct |
410 ms |
57204 KB |
Output is correct |
36 |
Correct |
296 ms |
57664 KB |
Output is correct |
37 |
Correct |
692 ms |
65612 KB |
Output is correct |
38 |
Correct |
450 ms |
65252 KB |
Output is correct |
39 |
Correct |
596 ms |
56624 KB |
Output is correct |
40 |
Correct |
434 ms |
57176 KB |
Output is correct |
41 |
Correct |
431 ms |
57408 KB |
Output is correct |
42 |
Correct |
322 ms |
57896 KB |
Output is correct |
43 |
Correct |
366 ms |
57200 KB |
Output is correct |
44 |
Correct |
328 ms |
57784 KB |
Output is correct |
45 |
Correct |
307 ms |
58428 KB |
Output is correct |
46 |
Correct |
3 ms |
4948 KB |
Output is correct |
47 |
Correct |
4 ms |
4948 KB |
Output is correct |
48 |
Correct |
4 ms |
4948 KB |
Output is correct |
49 |
Correct |
3 ms |
5044 KB |
Output is correct |
50 |
Correct |
3 ms |
4948 KB |
Output is correct |
51 |
Correct |
4 ms |
4948 KB |
Output is correct |
52 |
Correct |
599 ms |
56604 KB |
Output is correct |
53 |
Correct |
398 ms |
56980 KB |
Output is correct |
54 |
Correct |
563 ms |
65264 KB |
Output is correct |
55 |
Correct |
520 ms |
56720 KB |
Output is correct |
56 |
Correct |
451 ms |
56696 KB |
Output is correct |
57 |
Correct |
462 ms |
56436 KB |
Output is correct |
58 |
Correct |
315 ms |
57484 KB |
Output is correct |
59 |
Correct |
658 ms |
62668 KB |
Output is correct |
60 |
Correct |
452 ms |
65096 KB |
Output is correct |
61 |
Correct |
616 ms |
56508 KB |
Output is correct |
62 |
Correct |
448 ms |
57016 KB |
Output is correct |
63 |
Correct |
4 ms |
4948 KB |
Output is correct |
64 |
Correct |
5 ms |
4948 KB |
Output is correct |
65 |
Correct |
3 ms |
4948 KB |
Output is correct |
66 |
Correct |
3 ms |
4948 KB |
Output is correct |
67 |
Correct |
5 ms |
5040 KB |
Output is correct |
68 |
Correct |
4 ms |
5036 KB |
Output is correct |
69 |
Correct |
4 ms |
4948 KB |
Output is correct |
70 |
Correct |
3 ms |
4948 KB |
Output is correct |
71 |
Correct |
3 ms |
4948 KB |
Output is correct |
72 |
Correct |
4 ms |
4948 KB |
Output is correct |
73 |
Correct |
3 ms |
4948 KB |
Output is correct |
74 |
Correct |
3 ms |
5040 KB |
Output is correct |
75 |
Correct |
3 ms |
5044 KB |
Output is correct |
76 |
Correct |
3 ms |
5004 KB |
Output is correct |
77 |
Correct |
3 ms |
5032 KB |
Output is correct |
78 |
Correct |
3 ms |
4948 KB |
Output is correct |
79 |
Correct |
789 ms |
61316 KB |
Output is correct |
80 |
Correct |
774 ms |
60748 KB |
Output is correct |
81 |
Correct |
676 ms |
60608 KB |
Output is correct |
82 |
Correct |
609 ms |
53500 KB |
Output is correct |
83 |
Correct |
498 ms |
53712 KB |
Output is correct |
84 |
Correct |
384 ms |
53928 KB |
Output is correct |
85 |
Correct |
295 ms |
54504 KB |
Output is correct |
86 |
Correct |
654 ms |
59460 KB |
Output is correct |
87 |
Correct |
805 ms |
62128 KB |
Output is correct |
88 |
Correct |
553 ms |
53068 KB |
Output is correct |
89 |
Correct |
604 ms |
54104 KB |
Output is correct |
90 |
Correct |
448 ms |
55008 KB |
Output is correct |
91 |
Correct |
479 ms |
55480 KB |
Output is correct |
92 |
Correct |
3 ms |
4948 KB |
Output is correct |
93 |
Correct |
3 ms |
4948 KB |
Output is correct |
94 |
Correct |
3 ms |
4948 KB |
Output is correct |
95 |
Correct |
3 ms |
4948 KB |
Output is correct |
96 |
Correct |
3 ms |
4948 KB |
Output is correct |
97 |
Correct |
3 ms |
5032 KB |
Output is correct |
98 |
Correct |
646 ms |
61860 KB |
Output is correct |
99 |
Correct |
837 ms |
59024 KB |
Output is correct |
100 |
Correct |
596 ms |
60460 KB |
Output is correct |
101 |
Correct |
524 ms |
55124 KB |
Output is correct |
102 |
Correct |
420 ms |
55316 KB |
Output is correct |
103 |
Correct |
419 ms |
55052 KB |
Output is correct |
104 |
Correct |
296 ms |
55608 KB |
Output is correct |
105 |
Correct |
680 ms |
64384 KB |
Output is correct |
106 |
Correct |
844 ms |
63208 KB |
Output is correct |
107 |
Correct |
613 ms |
56060 KB |
Output is correct |
108 |
Correct |
649 ms |
54640 KB |
Output is correct |
109 |
Correct |
451 ms |
55400 KB |
Output is correct |
110 |
Correct |
486 ms |
55912 KB |
Output is correct |
111 |
Correct |
3 ms |
4948 KB |
Output is correct |
112 |
Correct |
4 ms |
4948 KB |
Output is correct |
113 |
Correct |
3 ms |
4948 KB |
Output is correct |
114 |
Correct |
3 ms |
4948 KB |
Output is correct |
115 |
Correct |
4 ms |
4948 KB |
Output is correct |
116 |
Correct |
575 ms |
50684 KB |
Output is correct |
117 |
Correct |
605 ms |
49096 KB |
Output is correct |
118 |
Correct |
705 ms |
59596 KB |
Output is correct |
119 |
Correct |
631 ms |
51020 KB |
Output is correct |
120 |
Correct |
477 ms |
51016 KB |
Output is correct |
121 |
Correct |
450 ms |
51276 KB |
Output is correct |
122 |
Correct |
353 ms |
51780 KB |
Output is correct |
123 |
Correct |
651 ms |
59760 KB |
Output is correct |
124 |
Correct |
835 ms |
54904 KB |
Output is correct |
125 |
Correct |
595 ms |
52108 KB |
Output is correct |
126 |
Correct |
645 ms |
49228 KB |
Output is correct |
127 |
Correct |
729 ms |
49164 KB |
Output is correct |
128 |
Correct |
569 ms |
49960 KB |
Output is correct |
129 |
Correct |
595 ms |
50368 KB |
Output is correct |