#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;
ll Mod = 1e9 + 7;
const int szBL = 916;
const ll INF = 1e9;
const int BASE = 137;
int n, Q;
ll a[N];
ll val[N][45], 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);
}
ll get (int x, int de) {
if (de == 41) return 1;
if (x == 1) {
return val[1][de];
}
return 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 |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
2 ms |
8792 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
1 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8796 KB |
Output is correct |
15 |
Correct |
1 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
2 ms |
8792 KB |
Output is correct |
19 |
Correct |
2 ms |
8792 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
21 |
Correct |
2 ms |
8796 KB |
Output is correct |
22 |
Correct |
2 ms |
8848 KB |
Output is correct |
23 |
Correct |
2 ms |
8796 KB |
Output is correct |
24 |
Correct |
2 ms |
8796 KB |
Output is correct |
25 |
Correct |
1 ms |
8796 KB |
Output is correct |
26 |
Correct |
1 ms |
8796 KB |
Output is correct |
27 |
Correct |
2 ms |
8792 KB |
Output is correct |
28 |
Correct |
1 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
435 ms |
96860 KB |
Output is correct |
3 |
Correct |
246 ms |
97296 KB |
Output is correct |
4 |
Correct |
370 ms |
100688 KB |
Output is correct |
5 |
Correct |
353 ms |
97104 KB |
Output is correct |
6 |
Correct |
223 ms |
96572 KB |
Output is correct |
7 |
Correct |
213 ms |
97240 KB |
Output is correct |
8 |
Correct |
161 ms |
97732 KB |
Output is correct |
9 |
Correct |
504 ms |
102224 KB |
Output is correct |
10 |
Correct |
237 ms |
102252 KB |
Output is correct |
11 |
Correct |
420 ms |
96596 KB |
Output is correct |
12 |
Correct |
215 ms |
97104 KB |
Output is correct |
13 |
Correct |
147 ms |
97504 KB |
Output is correct |
14 |
Correct |
141 ms |
97984 KB |
Output is correct |
15 |
Correct |
148 ms |
97360 KB |
Output is correct |
16 |
Correct |
156 ms |
97896 KB |
Output is correct |
17 |
Correct |
149 ms |
98492 KB |
Output is correct |
18 |
Correct |
1 ms |
8792 KB |
Output is correct |
19 |
Correct |
1 ms |
8792 KB |
Output is correct |
20 |
Correct |
1 ms |
8796 KB |
Output is correct |
21 |
Correct |
2 ms |
8796 KB |
Output is correct |
22 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
435 ms |
96860 KB |
Output is correct |
3 |
Correct |
246 ms |
97296 KB |
Output is correct |
4 |
Correct |
370 ms |
100688 KB |
Output is correct |
5 |
Correct |
353 ms |
97104 KB |
Output is correct |
6 |
Correct |
223 ms |
96572 KB |
Output is correct |
7 |
Correct |
213 ms |
97240 KB |
Output is correct |
8 |
Correct |
161 ms |
97732 KB |
Output is correct |
9 |
Correct |
504 ms |
102224 KB |
Output is correct |
10 |
Correct |
237 ms |
102252 KB |
Output is correct |
11 |
Correct |
420 ms |
96596 KB |
Output is correct |
12 |
Correct |
215 ms |
97104 KB |
Output is correct |
13 |
Correct |
147 ms |
97504 KB |
Output is correct |
14 |
Correct |
141 ms |
97984 KB |
Output is correct |
15 |
Correct |
148 ms |
97360 KB |
Output is correct |
16 |
Correct |
156 ms |
97896 KB |
Output is correct |
17 |
Correct |
149 ms |
98492 KB |
Output is correct |
18 |
Correct |
1 ms |
8792 KB |
Output is correct |
19 |
Correct |
1 ms |
8792 KB |
Output is correct |
20 |
Correct |
1 ms |
8796 KB |
Output is correct |
21 |
Correct |
2 ms |
8796 KB |
Output is correct |
22 |
Correct |
2 ms |
8796 KB |
Output is correct |
23 |
Correct |
1 ms |
8848 KB |
Output is correct |
24 |
Correct |
428 ms |
96668 KB |
Output is correct |
25 |
Correct |
222 ms |
97360 KB |
Output is correct |
26 |
Correct |
393 ms |
102484 KB |
Output is correct |
27 |
Correct |
332 ms |
96800 KB |
Output is correct |
28 |
Correct |
208 ms |
97204 KB |
Output is correct |
29 |
Correct |
200 ms |
87124 KB |
Output is correct |
30 |
Correct |
176 ms |
95912 KB |
Output is correct |
31 |
Correct |
488 ms |
99744 KB |
Output is correct |
32 |
Correct |
243 ms |
100288 KB |
Output is correct |
33 |
Correct |
420 ms |
91472 KB |
Output is correct |
34 |
Correct |
207 ms |
85764 KB |
Output is correct |
35 |
Correct |
1 ms |
8796 KB |
Output is correct |
36 |
Correct |
1 ms |
8796 KB |
Output is correct |
37 |
Correct |
1 ms |
8856 KB |
Output is correct |
38 |
Correct |
1 ms |
8796 KB |
Output is correct |
39 |
Correct |
2 ms |
8796 KB |
Output is correct |
40 |
Correct |
1 ms |
8796 KB |
Output is correct |
41 |
Correct |
1 ms |
8796 KB |
Output is correct |
42 |
Correct |
2 ms |
8796 KB |
Output is correct |
43 |
Correct |
1 ms |
8796 KB |
Output is correct |
44 |
Correct |
2 ms |
8796 KB |
Output is correct |
45 |
Correct |
2 ms |
8844 KB |
Output is correct |
46 |
Correct |
2 ms |
8796 KB |
Output is correct |
47 |
Correct |
2 ms |
8796 KB |
Output is correct |
48 |
Correct |
1 ms |
8796 KB |
Output is correct |
49 |
Correct |
1 ms |
8792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
527 ms |
91396 KB |
Output is correct |
3 |
Correct |
441 ms |
98644 KB |
Output is correct |
4 |
Correct |
420 ms |
98460 KB |
Output is correct |
5 |
Correct |
381 ms |
94124 KB |
Output is correct |
6 |
Correct |
222 ms |
94036 KB |
Output is correct |
7 |
Correct |
205 ms |
94292 KB |
Output is correct |
8 |
Correct |
160 ms |
94916 KB |
Output is correct |
9 |
Correct |
526 ms |
97684 KB |
Output is correct |
10 |
Correct |
500 ms |
99404 KB |
Output is correct |
11 |
Correct |
443 ms |
93684 KB |
Output is correct |
12 |
Correct |
382 ms |
94548 KB |
Output is correct |
13 |
Correct |
217 ms |
95316 KB |
Output is correct |
14 |
Correct |
250 ms |
95800 KB |
Output is correct |
15 |
Correct |
2 ms |
8792 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
1 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
604 ms |
96300 KB |
Output is correct |
3 |
Correct |
453 ms |
95572 KB |
Output is correct |
4 |
Correct |
468 ms |
98900 KB |
Output is correct |
5 |
Correct |
361 ms |
95608 KB |
Output is correct |
6 |
Correct |
247 ms |
95820 KB |
Output is correct |
7 |
Correct |
206 ms |
95568 KB |
Output is correct |
8 |
Correct |
164 ms |
95936 KB |
Output is correct |
9 |
Correct |
540 ms |
101712 KB |
Output is correct |
10 |
Correct |
450 ms |
100072 KB |
Output is correct |
11 |
Correct |
449 ms |
96340 KB |
Output is correct |
12 |
Correct |
429 ms |
94768 KB |
Output is correct |
13 |
Correct |
271 ms |
95568 KB |
Output is correct |
14 |
Correct |
248 ms |
96080 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
1 ms |
8792 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
2 ms |
8792 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
1 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8796 KB |
Output is correct |
15 |
Correct |
1 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
2 ms |
8792 KB |
Output is correct |
19 |
Correct |
2 ms |
8792 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
21 |
Correct |
2 ms |
8796 KB |
Output is correct |
22 |
Correct |
2 ms |
8848 KB |
Output is correct |
23 |
Correct |
2 ms |
8796 KB |
Output is correct |
24 |
Correct |
2 ms |
8796 KB |
Output is correct |
25 |
Correct |
1 ms |
8796 KB |
Output is correct |
26 |
Correct |
1 ms |
8796 KB |
Output is correct |
27 |
Correct |
2 ms |
8792 KB |
Output is correct |
28 |
Correct |
1 ms |
8796 KB |
Output is correct |
29 |
Correct |
1 ms |
8792 KB |
Output is correct |
30 |
Correct |
435 ms |
96860 KB |
Output is correct |
31 |
Correct |
246 ms |
97296 KB |
Output is correct |
32 |
Correct |
370 ms |
100688 KB |
Output is correct |
33 |
Correct |
353 ms |
97104 KB |
Output is correct |
34 |
Correct |
223 ms |
96572 KB |
Output is correct |
35 |
Correct |
213 ms |
97240 KB |
Output is correct |
36 |
Correct |
161 ms |
97732 KB |
Output is correct |
37 |
Correct |
504 ms |
102224 KB |
Output is correct |
38 |
Correct |
237 ms |
102252 KB |
Output is correct |
39 |
Correct |
420 ms |
96596 KB |
Output is correct |
40 |
Correct |
215 ms |
97104 KB |
Output is correct |
41 |
Correct |
147 ms |
97504 KB |
Output is correct |
42 |
Correct |
141 ms |
97984 KB |
Output is correct |
43 |
Correct |
148 ms |
97360 KB |
Output is correct |
44 |
Correct |
156 ms |
97896 KB |
Output is correct |
45 |
Correct |
149 ms |
98492 KB |
Output is correct |
46 |
Correct |
1 ms |
8792 KB |
Output is correct |
47 |
Correct |
1 ms |
8792 KB |
Output is correct |
48 |
Correct |
1 ms |
8796 KB |
Output is correct |
49 |
Correct |
2 ms |
8796 KB |
Output is correct |
50 |
Correct |
2 ms |
8796 KB |
Output is correct |
51 |
Correct |
1 ms |
8848 KB |
Output is correct |
52 |
Correct |
428 ms |
96668 KB |
Output is correct |
53 |
Correct |
222 ms |
97360 KB |
Output is correct |
54 |
Correct |
393 ms |
102484 KB |
Output is correct |
55 |
Correct |
332 ms |
96800 KB |
Output is correct |
56 |
Correct |
208 ms |
97204 KB |
Output is correct |
57 |
Correct |
200 ms |
87124 KB |
Output is correct |
58 |
Correct |
176 ms |
95912 KB |
Output is correct |
59 |
Correct |
488 ms |
99744 KB |
Output is correct |
60 |
Correct |
243 ms |
100288 KB |
Output is correct |
61 |
Correct |
420 ms |
91472 KB |
Output is correct |
62 |
Correct |
207 ms |
85764 KB |
Output is correct |
63 |
Correct |
1 ms |
8796 KB |
Output is correct |
64 |
Correct |
1 ms |
8796 KB |
Output is correct |
65 |
Correct |
1 ms |
8856 KB |
Output is correct |
66 |
Correct |
1 ms |
8796 KB |
Output is correct |
67 |
Correct |
2 ms |
8796 KB |
Output is correct |
68 |
Correct |
1 ms |
8796 KB |
Output is correct |
69 |
Correct |
1 ms |
8796 KB |
Output is correct |
70 |
Correct |
2 ms |
8796 KB |
Output is correct |
71 |
Correct |
1 ms |
8796 KB |
Output is correct |
72 |
Correct |
2 ms |
8796 KB |
Output is correct |
73 |
Correct |
2 ms |
8844 KB |
Output is correct |
74 |
Correct |
2 ms |
8796 KB |
Output is correct |
75 |
Correct |
2 ms |
8796 KB |
Output is correct |
76 |
Correct |
1 ms |
8796 KB |
Output is correct |
77 |
Correct |
1 ms |
8792 KB |
Output is correct |
78 |
Correct |
1 ms |
8796 KB |
Output is correct |
79 |
Correct |
527 ms |
91396 KB |
Output is correct |
80 |
Correct |
441 ms |
98644 KB |
Output is correct |
81 |
Correct |
420 ms |
98460 KB |
Output is correct |
82 |
Correct |
381 ms |
94124 KB |
Output is correct |
83 |
Correct |
222 ms |
94036 KB |
Output is correct |
84 |
Correct |
205 ms |
94292 KB |
Output is correct |
85 |
Correct |
160 ms |
94916 KB |
Output is correct |
86 |
Correct |
526 ms |
97684 KB |
Output is correct |
87 |
Correct |
500 ms |
99404 KB |
Output is correct |
88 |
Correct |
443 ms |
93684 KB |
Output is correct |
89 |
Correct |
382 ms |
94548 KB |
Output is correct |
90 |
Correct |
217 ms |
95316 KB |
Output is correct |
91 |
Correct |
250 ms |
95800 KB |
Output is correct |
92 |
Correct |
2 ms |
8792 KB |
Output is correct |
93 |
Correct |
1 ms |
8796 KB |
Output is correct |
94 |
Correct |
2 ms |
8796 KB |
Output is correct |
95 |
Correct |
1 ms |
8796 KB |
Output is correct |
96 |
Correct |
1 ms |
8796 KB |
Output is correct |
97 |
Correct |
1 ms |
8796 KB |
Output is correct |
98 |
Correct |
604 ms |
96300 KB |
Output is correct |
99 |
Correct |
453 ms |
95572 KB |
Output is correct |
100 |
Correct |
468 ms |
98900 KB |
Output is correct |
101 |
Correct |
361 ms |
95608 KB |
Output is correct |
102 |
Correct |
247 ms |
95820 KB |
Output is correct |
103 |
Correct |
206 ms |
95568 KB |
Output is correct |
104 |
Correct |
164 ms |
95936 KB |
Output is correct |
105 |
Correct |
540 ms |
101712 KB |
Output is correct |
106 |
Correct |
450 ms |
100072 KB |
Output is correct |
107 |
Correct |
449 ms |
96340 KB |
Output is correct |
108 |
Correct |
429 ms |
94768 KB |
Output is correct |
109 |
Correct |
271 ms |
95568 KB |
Output is correct |
110 |
Correct |
248 ms |
96080 KB |
Output is correct |
111 |
Correct |
2 ms |
8796 KB |
Output is correct |
112 |
Correct |
2 ms |
8796 KB |
Output is correct |
113 |
Correct |
1 ms |
8792 KB |
Output is correct |
114 |
Correct |
1 ms |
8796 KB |
Output is correct |
115 |
Correct |
2 ms |
8796 KB |
Output is correct |
116 |
Correct |
495 ms |
94844 KB |
Output is correct |
117 |
Correct |
431 ms |
97364 KB |
Output is correct |
118 |
Correct |
484 ms |
102224 KB |
Output is correct |
119 |
Correct |
425 ms |
97092 KB |
Output is correct |
120 |
Correct |
257 ms |
96592 KB |
Output is correct |
121 |
Correct |
291 ms |
97480 KB |
Output is correct |
122 |
Correct |
226 ms |
97736 KB |
Output is correct |
123 |
Correct |
625 ms |
101196 KB |
Output is correct |
124 |
Correct |
569 ms |
100440 KB |
Output is correct |
125 |
Correct |
484 ms |
95840 KB |
Output is correct |
126 |
Correct |
419 ms |
97108 KB |
Output is correct |
127 |
Correct |
418 ms |
97620 KB |
Output is correct |
128 |
Correct |
278 ms |
98372 KB |
Output is correct |
129 |
Correct |
285 ms |
98892 KB |
Output is correct |