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;
#define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define isz(x) ((int)x.size())
#define sumof(x) accumulate(all(x), 0ll)
int mod;
const int N=2e5+10, MAX_DIST=40;
int n, q, par[N], h[N];
int mult[N][42];
vector<int> g[N];
void dfs(int u, int p){
par[u]=p;
if (p) g[u].erase(find(all(g[u]), p));
for (int v:g[u]) dfs(v, u);
for (int i=0; i<=MAX_DIST+1; ++i) mult[u][i]=1;
}
void update(int u, int d, int w){
int lca=u;
while (par[lca] && d){
mult[lca][d]=mult[lca][d]*w%mod;
mult[lca][d-1]=mult[lca][d-1]*w%mod;
lca=par[lca];
--d;
}
for (int i=d; i>=0; --i) mult[lca][i]=mult[lca][i]*w%mod;
}
int query(int u){
int val=h[u];
int lca=u, dist=0;
while (lca && dist<=MAX_DIST){
val=1ll*val*mult[lca][dist]%mod;
lca=par[lca]; ++dist;
}
return val;
}
void solve(){
cin >> n >> mod;
for (int i=0; i<n-1; ++i){
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 0);
for (int i=1; i<=n; ++i) cin >> h[i];
cin >> q;
while (q--){
int o; cin >> o;
if (o==1){
int u, d, w; cin >> u >> d >> w;
update(u, d, w);
}else{
int u; cin >> u;
cout << query(u) << '\n';
}
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int ntests=1;
// cin >> ntests;
for (int i=1; i<=ntests; ++i) solve();
return 0;
}
# | 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... |