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>
typedef long long ll;
using namespace std;
ll MOD;
vector<vector<int>> adj;
vector<int> par;
ll mul[200001][41];
void root(int x = 0, int p = 0) {
par[x] = p;
for(auto it = adj[x].begin(); it != adj[x].end(); it++) {
if(*it == p) {
adj[x].erase(it);
break;
}
}
for(int ch : adj[x]) {
root(ch, x);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n >> MOD;
adj.resize(n);
par.resize(n);
for(int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
x--; y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
root();
for(int i = 0; i < n; i++) {
cin >> mul[i][0];
for(int j = 1; j <= 40; j++) {
mul[i][j] = 1;
}
}
int q;
cin >> q;
while(q--) {
int t;
cin >> t;
if(t == 1) {
int x, d, w;
cin >> x >> d >> w;
x--;
while(x != par[x]) {
if(d >= 0) {
mul[x][d] *= w;
mul[x][d] %= MOD;
}
if(d >= 1) {
mul[x][d-1] *= w;
mul[x][d-1] %= MOD;
}
x = par[x];
d--;
if(d < 0) break;
}
if(d >= 0) {
mul[x][d] *= w;
mul[x][d] %= MOD;
}
if(d >= 1) {
mul[x][d-1] *= w;
mul[x][d-1] %= MOD;
}
}
if(t == 2) {
int x;
cin >> x;
x--;
ll ans = 1;
for(int d = 0; d <= 40; d++) {
ans *= mul[x][d];
ans %= MOD;
if(x == par[x]) d++;
x = par[x];
}
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... |