#include <bits/stdc++.h>
#define se second
#define fs first
#define pb push_back
#define ll long long
#define ii pair<ll,ll>
#define ld long double
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }
const int N = 2e5 + 7;
int Mod = 1e9 + 7;
const int szBL = 916;
const ll INF = 1e9;
const int BASE = 137;
int n, Q;
int a[N];
ll val[N][42], par[N];
vector<int> ke[N];
void dfs (int u, int p){
par[u] = p;
iter (&v, ke[u]) {
if (v != p) dfs (v, u);
}
}
void update (int u, int d, ll w, int h = 0) {
if (h > d) return;
(val[u][d - h] *= w) %= Mod;
update (par[u], d, w, h + 1);
}
int get (int x, int de) {
if (de == 41) return 1;
if (x == 1) {
return val[1][de];
}
return 1LL * val[x][de] * val[x][de + 1] % Mod * get (par[x], de + 1) % Mod;
}
void solution () {
cin >> n >> Mod;
rep (i, 1, n - 1) {
int u, v;
cin >> u >> v;
ke[u].push_back(v);
ke[v].push_back(u);
}
rep (i, 1, n) rep (j, 1, 41) val[i][j] = 1;
rep (i, 1, n) cin >> a[i], val[i][0] = a[i];
dfs (1, 0);
par[1] = 1;
cin >> Q;
rep (i, 1, Q) {
int typ; cin >> typ;
if (typ == 1) {
ll x, d, w;
cin >>x >> d >> w;
update (x, d, w);
}
else {
ll x;
cin >> x;
cout << get (x, 0) <<"\n";
}
}
}
#define file(name) freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);
int main () {
// file("c");
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll num_Test = 1;
// cin >> num_Test;
while(num_Test--)
solution();
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
4 ms |
5464 KB |
Output is correct |
5 |
Correct |
3 ms |
7104 KB |
Output is correct |
6 |
Correct |
2 ms |
7004 KB |
Output is correct |
7 |
Correct |
2 ms |
7004 KB |
Output is correct |
8 |
Correct |
2 ms |
7004 KB |
Output is correct |
9 |
Correct |
2 ms |
6744 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
1 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
6748 KB |
Output is correct |
14 |
Correct |
3 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
4952 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4952 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
4 ms |
4956 KB |
Output is correct |
21 |
Correct |
1 ms |
6748 KB |
Output is correct |
22 |
Correct |
2 ms |
6748 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
3 ms |
4952 KB |
Output is correct |
25 |
Correct |
3 ms |
4952 KB |
Output is correct |
26 |
Correct |
4 ms |
4956 KB |
Output is correct |
27 |
Correct |
3 ms |
4956 KB |
Output is correct |
28 |
Correct |
3 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
602 ms |
82924 KB |
Output is correct |
3 |
Correct |
345 ms |
79864 KB |
Output is correct |
4 |
Correct |
504 ms |
85168 KB |
Output is correct |
5 |
Correct |
403 ms |
81324 KB |
Output is correct |
6 |
Correct |
317 ms |
81368 KB |
Output is correct |
7 |
Correct |
276 ms |
81660 KB |
Output is correct |
8 |
Correct |
210 ms |
82112 KB |
Output is correct |
9 |
Correct |
697 ms |
88540 KB |
Output is correct |
10 |
Correct |
375 ms |
85072 KB |
Output is correct |
11 |
Correct |
661 ms |
83004 KB |
Output is correct |
12 |
Correct |
340 ms |
79856 KB |
Output is correct |
13 |
Correct |
207 ms |
80376 KB |
Output is correct |
14 |
Correct |
189 ms |
80584 KB |
Output is correct |
15 |
Correct |
182 ms |
80468 KB |
Output is correct |
16 |
Correct |
224 ms |
80376 KB |
Output is correct |
17 |
Correct |
215 ms |
80976 KB |
Output is correct |
18 |
Correct |
3 ms |
4952 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
3 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
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 |
602 ms |
82924 KB |
Output is correct |
3 |
Correct |
345 ms |
79864 KB |
Output is correct |
4 |
Correct |
504 ms |
85168 KB |
Output is correct |
5 |
Correct |
403 ms |
81324 KB |
Output is correct |
6 |
Correct |
317 ms |
81368 KB |
Output is correct |
7 |
Correct |
276 ms |
81660 KB |
Output is correct |
8 |
Correct |
210 ms |
82112 KB |
Output is correct |
9 |
Correct |
697 ms |
88540 KB |
Output is correct |
10 |
Correct |
375 ms |
85072 KB |
Output is correct |
11 |
Correct |
661 ms |
83004 KB |
Output is correct |
12 |
Correct |
340 ms |
79856 KB |
Output is correct |
13 |
Correct |
207 ms |
80376 KB |
Output is correct |
14 |
Correct |
189 ms |
80584 KB |
Output is correct |
15 |
Correct |
182 ms |
80468 KB |
Output is correct |
16 |
Correct |
224 ms |
80376 KB |
Output is correct |
17 |
Correct |
215 ms |
80976 KB |
Output is correct |
18 |
Correct |
3 ms |
4952 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
3 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
Correct |
2 ms |
4956 KB |
Output is correct |
23 |
Correct |
2 ms |
4956 KB |
Output is correct |
24 |
Correct |
554 ms |
83104 KB |
Output is correct |
25 |
Correct |
312 ms |
79856 KB |
Output is correct |
26 |
Correct |
480 ms |
86960 KB |
Output is correct |
27 |
Correct |
392 ms |
81532 KB |
Output is correct |
28 |
Correct |
281 ms |
81572 KB |
Output is correct |
29 |
Correct |
243 ms |
81660 KB |
Output is correct |
30 |
Correct |
246 ms |
82032 KB |
Output is correct |
31 |
Correct |
656 ms |
87160 KB |
Output is correct |
32 |
Correct |
340 ms |
85148 KB |
Output is correct |
33 |
Correct |
604 ms |
83076 KB |
Output is correct |
34 |
Correct |
287 ms |
79880 KB |
Output is correct |
35 |
Correct |
2 ms |
6748 KB |
Output is correct |
36 |
Correct |
4 ms |
4952 KB |
Output is correct |
37 |
Correct |
3 ms |
4956 KB |
Output is correct |
38 |
Correct |
4 ms |
4956 KB |
Output is correct |
39 |
Correct |
2 ms |
4956 KB |
Output is correct |
40 |
Correct |
3 ms |
4956 KB |
Output is correct |
41 |
Correct |
2 ms |
4956 KB |
Output is correct |
42 |
Correct |
4 ms |
5068 KB |
Output is correct |
43 |
Correct |
3 ms |
6748 KB |
Output is correct |
44 |
Correct |
2 ms |
6748 KB |
Output is correct |
45 |
Correct |
2 ms |
6748 KB |
Output is correct |
46 |
Correct |
3 ms |
6748 KB |
Output is correct |
47 |
Correct |
4 ms |
5176 KB |
Output is correct |
48 |
Correct |
4 ms |
4956 KB |
Output is correct |
49 |
Correct |
2 ms |
4980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
790 ms |
85872 KB |
Output is correct |
3 |
Correct |
542 ms |
83976 KB |
Output is correct |
4 |
Correct |
558 ms |
84308 KB |
Output is correct |
5 |
Correct |
465 ms |
79952 KB |
Output is correct |
6 |
Correct |
287 ms |
80116 KB |
Output is correct |
7 |
Correct |
283 ms |
80208 KB |
Output is correct |
8 |
Correct |
247 ms |
80628 KB |
Output is correct |
9 |
Correct |
656 ms |
84308 KB |
Output is correct |
10 |
Correct |
553 ms |
84552 KB |
Output is correct |
11 |
Correct |
577 ms |
80248 KB |
Output is correct |
12 |
Correct |
456 ms |
79444 KB |
Output is correct |
13 |
Correct |
276 ms |
80464 KB |
Output is correct |
14 |
Correct |
296 ms |
80908 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6744 KB |
Output is correct |
19 |
Correct |
2 ms |
6764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
644 ms |
86960 KB |
Output is correct |
3 |
Correct |
513 ms |
82804 KB |
Output is correct |
4 |
Correct |
533 ms |
84820 KB |
Output is correct |
5 |
Correct |
428 ms |
81564 KB |
Output is correct |
6 |
Correct |
270 ms |
81456 KB |
Output is correct |
7 |
Correct |
253 ms |
81432 KB |
Output is correct |
8 |
Correct |
194 ms |
81856 KB |
Output is correct |
9 |
Correct |
647 ms |
88152 KB |
Output is correct |
10 |
Correct |
529 ms |
85076 KB |
Output is correct |
11 |
Correct |
542 ms |
83024 KB |
Output is correct |
12 |
Correct |
432 ms |
79956 KB |
Output is correct |
13 |
Correct |
281 ms |
80724 KB |
Output is correct |
14 |
Correct |
255 ms |
81012 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6744 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
2 ms |
6744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
4 ms |
5464 KB |
Output is correct |
5 |
Correct |
3 ms |
7104 KB |
Output is correct |
6 |
Correct |
2 ms |
7004 KB |
Output is correct |
7 |
Correct |
2 ms |
7004 KB |
Output is correct |
8 |
Correct |
2 ms |
7004 KB |
Output is correct |
9 |
Correct |
2 ms |
6744 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
1 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
6748 KB |
Output is correct |
14 |
Correct |
3 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
4952 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4952 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
4 ms |
4956 KB |
Output is correct |
21 |
Correct |
1 ms |
6748 KB |
Output is correct |
22 |
Correct |
2 ms |
6748 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
3 ms |
4952 KB |
Output is correct |
25 |
Correct |
3 ms |
4952 KB |
Output is correct |
26 |
Correct |
4 ms |
4956 KB |
Output is correct |
27 |
Correct |
3 ms |
4956 KB |
Output is correct |
28 |
Correct |
3 ms |
4956 KB |
Output is correct |
29 |
Correct |
2 ms |
4956 KB |
Output is correct |
30 |
Correct |
602 ms |
82924 KB |
Output is correct |
31 |
Correct |
345 ms |
79864 KB |
Output is correct |
32 |
Correct |
504 ms |
85168 KB |
Output is correct |
33 |
Correct |
403 ms |
81324 KB |
Output is correct |
34 |
Correct |
317 ms |
81368 KB |
Output is correct |
35 |
Correct |
276 ms |
81660 KB |
Output is correct |
36 |
Correct |
210 ms |
82112 KB |
Output is correct |
37 |
Correct |
697 ms |
88540 KB |
Output is correct |
38 |
Correct |
375 ms |
85072 KB |
Output is correct |
39 |
Correct |
661 ms |
83004 KB |
Output is correct |
40 |
Correct |
340 ms |
79856 KB |
Output is correct |
41 |
Correct |
207 ms |
80376 KB |
Output is correct |
42 |
Correct |
189 ms |
80584 KB |
Output is correct |
43 |
Correct |
182 ms |
80468 KB |
Output is correct |
44 |
Correct |
224 ms |
80376 KB |
Output is correct |
45 |
Correct |
215 ms |
80976 KB |
Output is correct |
46 |
Correct |
3 ms |
4952 KB |
Output is correct |
47 |
Correct |
2 ms |
4956 KB |
Output is correct |
48 |
Correct |
3 ms |
4956 KB |
Output is correct |
49 |
Correct |
2 ms |
4956 KB |
Output is correct |
50 |
Correct |
2 ms |
4956 KB |
Output is correct |
51 |
Correct |
2 ms |
4956 KB |
Output is correct |
52 |
Correct |
554 ms |
83104 KB |
Output is correct |
53 |
Correct |
312 ms |
79856 KB |
Output is correct |
54 |
Correct |
480 ms |
86960 KB |
Output is correct |
55 |
Correct |
392 ms |
81532 KB |
Output is correct |
56 |
Correct |
281 ms |
81572 KB |
Output is correct |
57 |
Correct |
243 ms |
81660 KB |
Output is correct |
58 |
Correct |
246 ms |
82032 KB |
Output is correct |
59 |
Correct |
656 ms |
87160 KB |
Output is correct |
60 |
Correct |
340 ms |
85148 KB |
Output is correct |
61 |
Correct |
604 ms |
83076 KB |
Output is correct |
62 |
Correct |
287 ms |
79880 KB |
Output is correct |
63 |
Correct |
2 ms |
6748 KB |
Output is correct |
64 |
Correct |
4 ms |
4952 KB |
Output is correct |
65 |
Correct |
3 ms |
4956 KB |
Output is correct |
66 |
Correct |
4 ms |
4956 KB |
Output is correct |
67 |
Correct |
2 ms |
4956 KB |
Output is correct |
68 |
Correct |
3 ms |
4956 KB |
Output is correct |
69 |
Correct |
2 ms |
4956 KB |
Output is correct |
70 |
Correct |
4 ms |
5068 KB |
Output is correct |
71 |
Correct |
3 ms |
6748 KB |
Output is correct |
72 |
Correct |
2 ms |
6748 KB |
Output is correct |
73 |
Correct |
2 ms |
6748 KB |
Output is correct |
74 |
Correct |
3 ms |
6748 KB |
Output is correct |
75 |
Correct |
4 ms |
5176 KB |
Output is correct |
76 |
Correct |
4 ms |
4956 KB |
Output is correct |
77 |
Correct |
2 ms |
4980 KB |
Output is correct |
78 |
Correct |
3 ms |
4956 KB |
Output is correct |
79 |
Correct |
790 ms |
85872 KB |
Output is correct |
80 |
Correct |
542 ms |
83976 KB |
Output is correct |
81 |
Correct |
558 ms |
84308 KB |
Output is correct |
82 |
Correct |
465 ms |
79952 KB |
Output is correct |
83 |
Correct |
287 ms |
80116 KB |
Output is correct |
84 |
Correct |
283 ms |
80208 KB |
Output is correct |
85 |
Correct |
247 ms |
80628 KB |
Output is correct |
86 |
Correct |
656 ms |
84308 KB |
Output is correct |
87 |
Correct |
553 ms |
84552 KB |
Output is correct |
88 |
Correct |
577 ms |
80248 KB |
Output is correct |
89 |
Correct |
456 ms |
79444 KB |
Output is correct |
90 |
Correct |
276 ms |
80464 KB |
Output is correct |
91 |
Correct |
296 ms |
80908 KB |
Output is correct |
92 |
Correct |
2 ms |
6748 KB |
Output is correct |
93 |
Correct |
1 ms |
6748 KB |
Output is correct |
94 |
Correct |
1 ms |
6748 KB |
Output is correct |
95 |
Correct |
2 ms |
6744 KB |
Output is correct |
96 |
Correct |
2 ms |
6764 KB |
Output is correct |
97 |
Correct |
2 ms |
6744 KB |
Output is correct |
98 |
Correct |
644 ms |
86960 KB |
Output is correct |
99 |
Correct |
513 ms |
82804 KB |
Output is correct |
100 |
Correct |
533 ms |
84820 KB |
Output is correct |
101 |
Correct |
428 ms |
81564 KB |
Output is correct |
102 |
Correct |
270 ms |
81456 KB |
Output is correct |
103 |
Correct |
253 ms |
81432 KB |
Output is correct |
104 |
Correct |
194 ms |
81856 KB |
Output is correct |
105 |
Correct |
647 ms |
88152 KB |
Output is correct |
106 |
Correct |
529 ms |
85076 KB |
Output is correct |
107 |
Correct |
542 ms |
83024 KB |
Output is correct |
108 |
Correct |
432 ms |
79956 KB |
Output is correct |
109 |
Correct |
281 ms |
80724 KB |
Output is correct |
110 |
Correct |
255 ms |
81012 KB |
Output is correct |
111 |
Correct |
1 ms |
6748 KB |
Output is correct |
112 |
Correct |
2 ms |
6748 KB |
Output is correct |
113 |
Correct |
1 ms |
6744 KB |
Output is correct |
114 |
Correct |
2 ms |
6748 KB |
Output is correct |
115 |
Correct |
2 ms |
6744 KB |
Output is correct |
116 |
Correct |
527 ms |
80992 KB |
Output is correct |
117 |
Correct |
439 ms |
79484 KB |
Output is correct |
118 |
Correct |
518 ms |
86744 KB |
Output is correct |
119 |
Correct |
431 ms |
81456 KB |
Output is correct |
120 |
Correct |
282 ms |
81492 KB |
Output is correct |
121 |
Correct |
263 ms |
81744 KB |
Output is correct |
122 |
Correct |
221 ms |
82096 KB |
Output is correct |
123 |
Correct |
675 ms |
87420 KB |
Output is correct |
124 |
Correct |
540 ms |
83536 KB |
Output is correct |
125 |
Correct |
526 ms |
82372 KB |
Output is correct |
126 |
Correct |
457 ms |
79696 KB |
Output is correct |
127 |
Correct |
453 ms |
79884 KB |
Output is correct |
128 |
Correct |
275 ms |
80464 KB |
Output is correct |
129 |
Correct |
293 ms |
80980 KB |
Output is correct |