// Author: caption_mingle
#include "bits/stdc++.h"
using namespace std;
#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define int long long
// const int mod = 1e9 + 7;
const int inf = 2e9;
const int N = 2e5 + 7;
int n, mod, a[N], par[N], q;
int ext[N][50];
vector<int> g[N];
int mul(int x,int y){return (x%mod*y%mod)%mod;}
void dfs(int x,int pp){
par[x]=pp;
for(auto v:g[x]){
if(v==pp) continue;
dfs(v,x);
}
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
#define task ""
if(fopen(task ".INP", "r")) {
freopen(task ".INP", "r", stdin);
freopen(task ".OUT", "w", stdout);
}
cin >> n >> mod;
for(int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
for(int i = 1; i <= n; i++) {
cin >> a[i];
for(int j = 0; j <= 42; j++) {
ext[i][j] = 1;
}
}
dfs(1, -1);
cin >> q;
for(int i = 1; i <= q; i++) {
int t; cin >> t;
if(t == 1) {
int x, d, w; cin >> x >> d >> w;
// for(int j = d, u = x; j >= 0 and u != 0; j--, u = par[u]) {
// ext[u][j] = mul(ext[u][j], w);
// }
while(x!=0 && d>=0){
ext[x][d]=mul(ext[x][d],w);
d--;
x=par[x];
}
} else {
int x; cin >> x;
int ans = a[x];
for(int i = 0; i <= 40; i++){
if(x==1){
for(int j = i; j <= 40; j++) ans=mul(ans,ext[x][j]);
break;
} else {
ans=mul(ans,ext[x][i]);
ans=mul(ans,ext[x][i+1]);
x=par[x];
}
}
// for(int j = 0, u = x; j <= 40 and u != 0; j++, u = par[u]) {
// if(u == 1) {
// for(int t = j; t <= 40; t++) {
// cur = mul(cur, ext[u][t]);
// }
// } else cur = mul(cur, mul(ext[u][j], ext[u][j + 1]));
// }
cout << ans << ln;
}
}
cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
Compilation message (stderr)
sprinkler.cpp: In function 'int main()':
sprinkler.cpp:36:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen(task ".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
sprinkler.cpp:37:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen(task ".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... |