#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll MOD;
vector<vector<int>> adj;
vector<int> par;
ll mul[200001][41];
void root(int x = 0, int p = 0) {
par[x] = p;
for(auto it = adj[x].begin(); it != adj[x].end(); it++) {
if(*it == p) {
adj[x].erase(it);
break;
}
}
for(int ch : adj[x]) {
root(ch, x);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n >> MOD;
adj.resize(n);
par.resize(n);
for(int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
x--; y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
root();
for(int i = 0; i < n; i++) {
cin >> mul[i][0];
for(int j = 1; j <= 40; j++) {
mul[i][j] = 1;
}
}
int q;
cin >> q;
while(q--) {
int t;
cin >> t;
if(t == 1) {
int x, d, w;
cin >> x >> d >> w;
x--;
while(x != par[x]) {
if(d >= 0) {
mul[x][d] *= w;
mul[x][d] %= MOD;
}
if(d >= 1) {
mul[x][d-1] *= w;
mul[x][d-1] %= MOD;
}
x = par[x];
d--;
}
if(d >= 0) {
mul[x][d] *= w;
mul[x][d] %= MOD;
}
if(d >= 1) {
mul[x][d-1] *= w;
mul[x][d-1] %= MOD;
}
}
if(t == 2) {
int x;
cin >> x;
x--;
ll ans = 1;
for(int d = 0; d <= 40; d++) {
ans *= mul[x][d];
ans %= MOD;
if(x == par[x]) d++;
x = par[x];
}
cout << ans << '\n';
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
728 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
2 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
761 ms |
88248 KB |
Output is correct |
3 |
Correct |
1599 ms |
88704 KB |
Output is correct |
4 |
Execution timed out |
4070 ms |
87468 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
761 ms |
88248 KB |
Output is correct |
3 |
Correct |
1599 ms |
88704 KB |
Output is correct |
4 |
Execution timed out |
4070 ms |
87468 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
4065 ms |
90360 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
4051 ms |
88048 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
728 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
2 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
761 ms |
88248 KB |
Output is correct |
31 |
Correct |
1599 ms |
88704 KB |
Output is correct |
32 |
Execution timed out |
4070 ms |
87468 KB |
Time limit exceeded |
33 |
Halted |
0 ms |
0 KB |
- |