#include<bits/stdc++.h>
using namespace std;
void local() {
#define taskname ""
if (fopen(taskname".inp", "r")) {
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
}
#define ll long long
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
template<class X, class Y> bool mini(X &a, const Y &b) {return (a > b) ? a = b, true : false;}
template<class X, class Y> bool maxi(X &a, const Y &b) {return (a < b) ? a = b, true : false;}
const int N = 1e6 + 5;
int n, l, q, h[N];
vector<int> adj[N];
tuple<int, int, int, int> que[N];
namespace sub1 {
bool check() {
return max(n, q) <= 1e3;
}
void dfs(int u, int p, int d, int bound, int w) {
h[u] = 1ll * h[u] * w % l;
if(d == bound) return;
for(int& v : adj[u]) {
if(v == p) continue;
dfs(v, u, d + 1, bound, w);
}
}
void solve() {
for(int _ = 1; _ <= q; _++) {
int t, x, d, w; tie(t, x, d, w) = que[_];
if(t == 1) {
dfs(x, x, 0, d, w);
}
else {
cout << h[x] << '\n';
}
}
}
}
namespace sub4 {
bool check() {
for(int i = 1; i <= q; i++) if(get<3>(que[i]) > 0) return false;
return true;
}
struct FenwickTree {
vector<int> bit; int n;
void init(int n_) {
n = n_;
bit.assign(n + 5, 1);
}
void update(int i, int v) {
for(; i <= n; i += i & -i) bit[i] = 1ll * bit[i] * v % l;
}
int get(int i) {
int res = 1;
for(; i; i ^= i & -i) res = 1ll * res * bit[i] % l;
return res;
}
} fen[N];
int par[N];
void dfs(int u, int p) {
for(int& v : adj[u]) {
if(v == p) continue;
par[v] = u;
dfs(v, u);
}
}
void solve() {
dfs(1, 1);
for(int i = 1; i <= n; i++) fen[i].init(41);
for(int _ = 1; _ <= q; _++) {
int t, x, d, w; tie(t, x, d, w) = que[_];
if(t == 1) {
fen[x].update(42 - (d + 1), w);
}
else {
int res = h[x];
for(int u = x, d = 1; d <= 41, u > 0; d++, u = par[u]) {
res = 1ll * res * fen[u].get(42 - d);
}
cout << res << '\n';
}
}
}
}
int main() {
fastio; local();
cin >> n >> l;
for(int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
adj[u].emplace_back(v); adj[v].emplace_back(u);
}
for(int i = 1; i <= n; i++) cin >> h[i];
cin >> q;
for(int i = 1; i <= q; i++) {
int t, x, d, w; cin >> t >> x; d = 0; w = 0;
if(t == 1) cin >> d >> w;
que[i] = make_tuple(t, x, d, w);
}
if(sub1::check()) sub1::solve();
else if(sub4::check()) sub4::solve();
}
Compilation message (stderr)
sprinkler.cpp: In function 'void local()':
sprinkler.cpp:7:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
7 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
sprinkler.cpp:8:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
8 | freopen(taskname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |