#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, P;
cin >> n >> P;
vector<vector<int>> adj(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int> h(n);
for (int u = 0; u < n; u++) {
cin >> h[u];
}
vector<int> par(n, -1);
vector<vector<int>> lazy(n, vector<int>(41, 1));
auto dfs = [&](auto self, int u, int p) -> void {
par[u] = p;
for (int v : adj[u]) {
if (v == p) continue;
self(self, v, u);
}
};
dfs(dfs, 0, -1);
auto update = [&](int u, int d, int w) -> void {
while (u != 0 && d >= 0) {
lazy[u][d] = 1LL * lazy[u][d] * w % P;
if (d > 0) lazy[u][d - 1] = 1LL * lazy[u][d - 1] * w % P;
d--;
u = par[u];
}
if (d >= 0) {
assert(u == 0);
for (int x = 0; x <= d; x++) {
lazy[u][x] = 1LL * lazy[u][x] * w % P;
}
}
};
auto get = [&](int u) -> int {
int res = h[u];
for (int d = 0; u != -1 && d <= 40; d++, u = par[u]) {
res = 1LL * res * lazy[u][d] % P;
}
return res;
};
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int type;
cin >> type;
if (type == 1) {
int u, d, w;
cin >> u >> d >> w;
u--;
update(u, d, w);
}
if (type == 2) {
int u;
cin >> u;
u--;
cout << get(u) << '\n';
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |