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>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const int maxn = 200000;
vector<int> v[maxn];
int p[maxn];
int ml[maxn][41];
inline void dfs(int nd, int ss){
p[nd] = ss;
for(int x: v[nd]) if(x != ss) dfs(x, nd);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, md; cin >> n >> md;
for(int i = 0; i < n; i++) for(int j = 0; j <= 40; j++) ml[i][j] = 1;
for(int i = 0; i < n-1; i++){
int a, b; cin >> a >> b; a--, b--;
v[a].pb(b);
v[b].pb(a);
}
for(int i = 0; i < n; i++) cin >> ml[i][0];
dfs(0, -1);
int q; cin >> q;
while(q--){
int tt; cin >> tt;
if(tt == 1){
int nd, d, w; cin >> nd >> d >> w; nd--;
while(d >= 0){
ml[nd][d]=(ll(ml[nd][d])*w)%md;
if(d != 0) ml[nd][d-1]=(ll(ml[nd][d-1])*w)%md;
if(p[nd] == -1){
for(int i = 0; i < d-1; i++) ml[nd][i]=(ll(ml[nd][i])*w)%md;
break;
}
d--;
nd = p[nd];
}
}
else{
int nd; cin >> nd; nd--;
int d = 0;
int ans = 1;
while(d <= 40){
ans=(ll(ans)*ml[nd][d])%md;
nd = p[nd];
d++;
if(nd == -1) break;
}
cout << ans << "\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... |