This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define endl '\n'
#define pb push_back
using pi = array<int, 2>;
const int N = 2e5 + 100;
vector<int> adj[N];
int par[N];
const int D = 42;
int h[N], wch[D][N];
void dfs(int u) {
for (int v : adj[u]) {
if (v == par[u]) continue;
par[v] = u;
dfs(v);
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, l;
cin >> n >> l;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
for (int i = 0; i <= n + D; ++i) {
for (int d = 0; d < D; ++d) wch[d][i] = 1;
}
for (int d = 0; d < D - 1; ++d) {
int here = n + d + 1;
adj[here].pb(here + 1);
adj[here + 1].pb(here);
}
adj[n + D].pb(1);
adj[1].pb(n + D);
dfs(n + 1);
for (int i = 1; i <= n; ++i) cin >> h[i];
int q;
cin >> q;
while (q--) {
int t;
cin >> t;
if (t == 1) {
int u, d, w;
cin >> u >> d >> w;
auto mult = [&](int& var) {
var = var * w % l;
};
int p = u;
for (int i = 0; i <= d; ++i) {
int descend = d - i;
mult(wch[descend][p]);
if (descend > 0) mult(wch[descend - 1][p]);
p = par[p];
}
} else {
int u;
cin >> u;
//cout << u << "!\n";
int p = u;
int ans = h[u];
for (int d = 0; d < D; ++d) {
//cout << p << " | " << d << " | " << wch[d][p] << endl;
ans = ans * wch[d][p] % l;
p = par[p];
}
cout << ans << endl;
}
}
}
/*
4 7
1 2
2 3
3 4
1
1
1
1
5
1 4 8 2
2 1
2 2
2 3
2 4
*/
# | 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... |