#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][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);
}
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 |
1 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 |
8796 KB |
Output is correct |
5 |
Correct |
3 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
3 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6744 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6744 KB |
Output is correct |
19 |
Correct |
2 ms |
7000 KB |
Output is correct |
20 |
Correct |
1 ms |
6748 KB |
Output is correct |
21 |
Correct |
1 ms |
6748 KB |
Output is correct |
22 |
Correct |
1 ms |
6748 KB |
Output is correct |
23 |
Correct |
1 ms |
6748 KB |
Output is correct |
24 |
Correct |
1 ms |
6748 KB |
Output is correct |
25 |
Correct |
2 ms |
6748 KB |
Output is correct |
26 |
Correct |
2 ms |
6748 KB |
Output is correct |
27 |
Correct |
1 ms |
6748 KB |
Output is correct |
28 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
486 ms |
87812 KB |
Output is correct |
3 |
Correct |
263 ms |
96596 KB |
Output is correct |
4 |
Correct |
496 ms |
99720 KB |
Output is correct |
5 |
Correct |
360 ms |
96080 KB |
Output is correct |
6 |
Correct |
235 ms |
95736 KB |
Output is correct |
7 |
Correct |
239 ms |
96596 KB |
Output is correct |
8 |
Correct |
191 ms |
96964 KB |
Output is correct |
9 |
Correct |
601 ms |
101288 KB |
Output is correct |
10 |
Correct |
285 ms |
101428 KB |
Output is correct |
11 |
Correct |
508 ms |
95912 KB |
Output is correct |
12 |
Correct |
298 ms |
96364 KB |
Output is correct |
13 |
Correct |
195 ms |
96704 KB |
Output is correct |
14 |
Correct |
210 ms |
97476 KB |
Output is correct |
15 |
Correct |
174 ms |
96592 KB |
Output is correct |
16 |
Correct |
180 ms |
97172 KB |
Output is correct |
17 |
Correct |
187 ms |
97632 KB |
Output is correct |
18 |
Correct |
2 ms |
6744 KB |
Output is correct |
19 |
Correct |
1 ms |
6748 KB |
Output is correct |
20 |
Correct |
1 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6744 KB |
Output is correct |
22 |
Correct |
1 ms |
6812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
486 ms |
87812 KB |
Output is correct |
3 |
Correct |
263 ms |
96596 KB |
Output is correct |
4 |
Correct |
496 ms |
99720 KB |
Output is correct |
5 |
Correct |
360 ms |
96080 KB |
Output is correct |
6 |
Correct |
235 ms |
95736 KB |
Output is correct |
7 |
Correct |
239 ms |
96596 KB |
Output is correct |
8 |
Correct |
191 ms |
96964 KB |
Output is correct |
9 |
Correct |
601 ms |
101288 KB |
Output is correct |
10 |
Correct |
285 ms |
101428 KB |
Output is correct |
11 |
Correct |
508 ms |
95912 KB |
Output is correct |
12 |
Correct |
298 ms |
96364 KB |
Output is correct |
13 |
Correct |
195 ms |
96704 KB |
Output is correct |
14 |
Correct |
210 ms |
97476 KB |
Output is correct |
15 |
Correct |
174 ms |
96592 KB |
Output is correct |
16 |
Correct |
180 ms |
97172 KB |
Output is correct |
17 |
Correct |
187 ms |
97632 KB |
Output is correct |
18 |
Correct |
2 ms |
6744 KB |
Output is correct |
19 |
Correct |
1 ms |
6748 KB |
Output is correct |
20 |
Correct |
1 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6744 KB |
Output is correct |
22 |
Correct |
1 ms |
6812 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
525 ms |
96116 KB |
Output is correct |
25 |
Correct |
284 ms |
96592 KB |
Output is correct |
26 |
Correct |
474 ms |
101572 KB |
Output is correct |
27 |
Correct |
414 ms |
96132 KB |
Output is correct |
28 |
Correct |
271 ms |
96332 KB |
Output is correct |
29 |
Correct |
235 ms |
86376 KB |
Output is correct |
30 |
Correct |
206 ms |
95116 KB |
Output is correct |
31 |
Correct |
681 ms |
98920 KB |
Output is correct |
32 |
Correct |
313 ms |
99768 KB |
Output is correct |
33 |
Correct |
521 ms |
90852 KB |
Output is correct |
34 |
Correct |
303 ms |
84988 KB |
Output is correct |
35 |
Correct |
2 ms |
6744 KB |
Output is correct |
36 |
Correct |
2 ms |
6748 KB |
Output is correct |
37 |
Correct |
3 ms |
6748 KB |
Output is correct |
38 |
Correct |
2 ms |
6748 KB |
Output is correct |
39 |
Correct |
2 ms |
6748 KB |
Output is correct |
40 |
Correct |
1 ms |
6756 KB |
Output is correct |
41 |
Correct |
2 ms |
6748 KB |
Output is correct |
42 |
Correct |
2 ms |
6748 KB |
Output is correct |
43 |
Correct |
3 ms |
6748 KB |
Output is correct |
44 |
Correct |
3 ms |
6748 KB |
Output is correct |
45 |
Correct |
3 ms |
6744 KB |
Output is correct |
46 |
Correct |
2 ms |
6744 KB |
Output is correct |
47 |
Correct |
2 ms |
6744 KB |
Output is correct |
48 |
Correct |
2 ms |
6748 KB |
Output is correct |
49 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
719 ms |
90516 KB |
Output is correct |
3 |
Correct |
578 ms |
88512 KB |
Output is correct |
4 |
Correct |
623 ms |
89000 KB |
Output is correct |
5 |
Correct |
508 ms |
84668 KB |
Output is correct |
6 |
Correct |
265 ms |
84736 KB |
Output is correct |
7 |
Correct |
265 ms |
84844 KB |
Output is correct |
8 |
Correct |
226 ms |
85392 KB |
Output is correct |
9 |
Correct |
856 ms |
88904 KB |
Output is correct |
10 |
Correct |
604 ms |
89424 KB |
Output is correct |
11 |
Correct |
576 ms |
85076 KB |
Output is correct |
12 |
Correct |
455 ms |
84344 KB |
Output is correct |
13 |
Correct |
370 ms |
85076 KB |
Output is correct |
14 |
Correct |
312 ms |
85588 KB |
Output is correct |
15 |
Correct |
2 ms |
6744 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
5 ms |
4952 KB |
Output is correct |
18 |
Correct |
3 ms |
4952 KB |
Output is correct |
19 |
Correct |
4 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
678 ms |
91492 KB |
Output is correct |
3 |
Correct |
564 ms |
87380 KB |
Output is correct |
4 |
Correct |
572 ms |
89556 KB |
Output is correct |
5 |
Correct |
581 ms |
86096 KB |
Output is correct |
6 |
Correct |
320 ms |
86152 KB |
Output is correct |
7 |
Correct |
293 ms |
86104 KB |
Output is correct |
8 |
Correct |
208 ms |
86632 KB |
Output is correct |
9 |
Correct |
647 ms |
93024 KB |
Output is correct |
10 |
Correct |
562 ms |
89996 KB |
Output is correct |
11 |
Correct |
558 ms |
87756 KB |
Output is correct |
12 |
Correct |
543 ms |
84668 KB |
Output is correct |
13 |
Correct |
247 ms |
85240 KB |
Output is correct |
14 |
Correct |
298 ms |
85736 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
3 ms |
4952 KB |
Output is correct |
18 |
Correct |
3 ms |
4956 KB |
Output is correct |
19 |
Correct |
3 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
8796 KB |
Output is correct |
5 |
Correct |
3 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
3 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6744 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6744 KB |
Output is correct |
19 |
Correct |
2 ms |
7000 KB |
Output is correct |
20 |
Correct |
1 ms |
6748 KB |
Output is correct |
21 |
Correct |
1 ms |
6748 KB |
Output is correct |
22 |
Correct |
1 ms |
6748 KB |
Output is correct |
23 |
Correct |
1 ms |
6748 KB |
Output is correct |
24 |
Correct |
1 ms |
6748 KB |
Output is correct |
25 |
Correct |
2 ms |
6748 KB |
Output is correct |
26 |
Correct |
2 ms |
6748 KB |
Output is correct |
27 |
Correct |
1 ms |
6748 KB |
Output is correct |
28 |
Correct |
2 ms |
6748 KB |
Output is correct |
29 |
Correct |
2 ms |
6748 KB |
Output is correct |
30 |
Correct |
486 ms |
87812 KB |
Output is correct |
31 |
Correct |
263 ms |
96596 KB |
Output is correct |
32 |
Correct |
496 ms |
99720 KB |
Output is correct |
33 |
Correct |
360 ms |
96080 KB |
Output is correct |
34 |
Correct |
235 ms |
95736 KB |
Output is correct |
35 |
Correct |
239 ms |
96596 KB |
Output is correct |
36 |
Correct |
191 ms |
96964 KB |
Output is correct |
37 |
Correct |
601 ms |
101288 KB |
Output is correct |
38 |
Correct |
285 ms |
101428 KB |
Output is correct |
39 |
Correct |
508 ms |
95912 KB |
Output is correct |
40 |
Correct |
298 ms |
96364 KB |
Output is correct |
41 |
Correct |
195 ms |
96704 KB |
Output is correct |
42 |
Correct |
210 ms |
97476 KB |
Output is correct |
43 |
Correct |
174 ms |
96592 KB |
Output is correct |
44 |
Correct |
180 ms |
97172 KB |
Output is correct |
45 |
Correct |
187 ms |
97632 KB |
Output is correct |
46 |
Correct |
2 ms |
6744 KB |
Output is correct |
47 |
Correct |
1 ms |
6748 KB |
Output is correct |
48 |
Correct |
1 ms |
6748 KB |
Output is correct |
49 |
Correct |
2 ms |
6744 KB |
Output is correct |
50 |
Correct |
1 ms |
6812 KB |
Output is correct |
51 |
Correct |
2 ms |
6748 KB |
Output is correct |
52 |
Correct |
525 ms |
96116 KB |
Output is correct |
53 |
Correct |
284 ms |
96592 KB |
Output is correct |
54 |
Correct |
474 ms |
101572 KB |
Output is correct |
55 |
Correct |
414 ms |
96132 KB |
Output is correct |
56 |
Correct |
271 ms |
96332 KB |
Output is correct |
57 |
Correct |
235 ms |
86376 KB |
Output is correct |
58 |
Correct |
206 ms |
95116 KB |
Output is correct |
59 |
Correct |
681 ms |
98920 KB |
Output is correct |
60 |
Correct |
313 ms |
99768 KB |
Output is correct |
61 |
Correct |
521 ms |
90852 KB |
Output is correct |
62 |
Correct |
303 ms |
84988 KB |
Output is correct |
63 |
Correct |
2 ms |
6744 KB |
Output is correct |
64 |
Correct |
2 ms |
6748 KB |
Output is correct |
65 |
Correct |
3 ms |
6748 KB |
Output is correct |
66 |
Correct |
2 ms |
6748 KB |
Output is correct |
67 |
Correct |
2 ms |
6748 KB |
Output is correct |
68 |
Correct |
1 ms |
6756 KB |
Output is correct |
69 |
Correct |
2 ms |
6748 KB |
Output is correct |
70 |
Correct |
2 ms |
6748 KB |
Output is correct |
71 |
Correct |
3 ms |
6748 KB |
Output is correct |
72 |
Correct |
3 ms |
6748 KB |
Output is correct |
73 |
Correct |
3 ms |
6744 KB |
Output is correct |
74 |
Correct |
2 ms |
6744 KB |
Output is correct |
75 |
Correct |
2 ms |
6744 KB |
Output is correct |
76 |
Correct |
2 ms |
6748 KB |
Output is correct |
77 |
Correct |
2 ms |
6748 KB |
Output is correct |
78 |
Correct |
2 ms |
6748 KB |
Output is correct |
79 |
Correct |
719 ms |
90516 KB |
Output is correct |
80 |
Correct |
578 ms |
88512 KB |
Output is correct |
81 |
Correct |
623 ms |
89000 KB |
Output is correct |
82 |
Correct |
508 ms |
84668 KB |
Output is correct |
83 |
Correct |
265 ms |
84736 KB |
Output is correct |
84 |
Correct |
265 ms |
84844 KB |
Output is correct |
85 |
Correct |
226 ms |
85392 KB |
Output is correct |
86 |
Correct |
856 ms |
88904 KB |
Output is correct |
87 |
Correct |
604 ms |
89424 KB |
Output is correct |
88 |
Correct |
576 ms |
85076 KB |
Output is correct |
89 |
Correct |
455 ms |
84344 KB |
Output is correct |
90 |
Correct |
370 ms |
85076 KB |
Output is correct |
91 |
Correct |
312 ms |
85588 KB |
Output is correct |
92 |
Correct |
2 ms |
6744 KB |
Output is correct |
93 |
Correct |
2 ms |
6748 KB |
Output is correct |
94 |
Correct |
5 ms |
4952 KB |
Output is correct |
95 |
Correct |
3 ms |
4952 KB |
Output is correct |
96 |
Correct |
4 ms |
4956 KB |
Output is correct |
97 |
Correct |
2 ms |
6748 KB |
Output is correct |
98 |
Correct |
678 ms |
91492 KB |
Output is correct |
99 |
Correct |
564 ms |
87380 KB |
Output is correct |
100 |
Correct |
572 ms |
89556 KB |
Output is correct |
101 |
Correct |
581 ms |
86096 KB |
Output is correct |
102 |
Correct |
320 ms |
86152 KB |
Output is correct |
103 |
Correct |
293 ms |
86104 KB |
Output is correct |
104 |
Correct |
208 ms |
86632 KB |
Output is correct |
105 |
Correct |
647 ms |
93024 KB |
Output is correct |
106 |
Correct |
562 ms |
89996 KB |
Output is correct |
107 |
Correct |
558 ms |
87756 KB |
Output is correct |
108 |
Correct |
543 ms |
84668 KB |
Output is correct |
109 |
Correct |
247 ms |
85240 KB |
Output is correct |
110 |
Correct |
298 ms |
85736 KB |
Output is correct |
111 |
Correct |
2 ms |
6748 KB |
Output is correct |
112 |
Correct |
1 ms |
6748 KB |
Output is correct |
113 |
Correct |
3 ms |
4952 KB |
Output is correct |
114 |
Correct |
3 ms |
4956 KB |
Output is correct |
115 |
Correct |
3 ms |
4956 KB |
Output is correct |
116 |
Correct |
571 ms |
93948 KB |
Output is correct |
117 |
Correct |
491 ms |
96476 KB |
Output is correct |
118 |
Correct |
583 ms |
101520 KB |
Output is correct |
119 |
Correct |
571 ms |
96336 KB |
Output is correct |
120 |
Correct |
310 ms |
95924 KB |
Output is correct |
121 |
Correct |
316 ms |
96704 KB |
Output is correct |
122 |
Correct |
221 ms |
96976 KB |
Output is correct |
123 |
Correct |
699 ms |
100540 KB |
Output is correct |
124 |
Correct |
600 ms |
99724 KB |
Output is correct |
125 |
Correct |
539 ms |
95196 KB |
Output is correct |
126 |
Correct |
486 ms |
96468 KB |
Output is correct |
127 |
Correct |
446 ms |
96848 KB |
Output is correct |
128 |
Correct |
310 ms |
97716 KB |
Output is correct |
129 |
Correct |
285 ms |
98128 KB |
Output is correct |