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>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>
using namespace std;
// using namespace __gnu_cxx;
// using namespace __gnu_pbds;
void Hollwo_Pelw();
signed main(){
#ifndef hollwo_pelw_local
if (fopen(".inp", "r"))
assert(freopen(".inp", "r", stdin)), assert(freopen(".out", "w", stdout));
#else
using namespace chrono;
auto start = steady_clock::now();
#endif
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
int testcases = 1;
// cin >> testcases;
for (int test = 1; test <= testcases; test++){
// cout << "Case #" << test << ": ";
Hollwo_Pelw();
}
#ifdef hollwo_pelw_local
auto end = steady_clock::now();
cout << "\nExecution time : " << duration_cast<milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
const int N = 2e5 + 5;
int n, q, mod, par[N];
long long h[N], keep[N][45];
vector<int> adj[N];
void pre_dfs(int u, int p) {
par[u] = p;
for (int v : adj[u]) if (v != p) {
pre_dfs(v, u);
}
}
void Hollwo_Pelw(){
cin >> n >> mod;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
cin >> h[i];
}
pre_dfs(1, 0);
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= 40; j++)
keep[i][j] = 1;
}
cin >> q;
for (int t; q --; ) {
cin >> t;
if (t == 1) {
// update
int x, d, w; cin >> x >> d >> w;
for (int p = x; p && d >= 0; p = par[p], d --) {
(keep[p][d] *= w) %= mod;
if (d) (keep[p][d - 1] *= w) %= mod;
}
for (d --; d >= 0; d --)
(keep[1][d] *= w) %= mod;
} else {
// query
int x; cin >> x;
long long res = h[x];
for (int p = x, d = 0; p && d <= 40; p = par[p], d ++)
(res *= keep[p][d]) %= mod;
cout << res << '\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... |