Submission #894066

#TimeUsernameProblemLanguageResultExecution timeMemory
894066juliany2Sprinkler (JOI22_sprinkler)C++17
100 / 100
871 ms101912 KiB
#include<bits/stdc++.h> using namespace std; using ll = long long; #define all(x) (x).begin(), (x).end() const int N = 2e5 + 7, D = 45; int n, mod; vector<int> adj[N]; int p[N]; ll h[N], lz[N][D]; void dfs(int v = 1) { for (int u : adj[v]) { if (u != p[v]) { p[u] = v; dfs(u); } } } int main() { cin.tie(0)->sync_with_stdio(false); cin >> n >> mod; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } for (int i = 1; i <= n; i++) cin >> h[i]; dfs(); for (int i = 1; i <= n; i++) fill(lz[i], lz[i] + D, 1); int q; cin >> q; while (q--) { int t; cin >> t; if (t == 1) { int x, d, w; cin >> x >> d >> w; for (; d >= 0 && x > 0; d--, x = p[x]) (lz[x][d] *= w) %= mod; } else { int x; cin >> x; ll ans = h[x]; vector<int> a; int d = 0; for (; d < D - 1 && x > 0; d++, x = p[x]) a.push_back(x); reverse(all(a)); for (int v : a) { d--; if (v == a.front()) { for (int i = d; i < D; i++) (ans *= lz[v][i]) %= mod; } else (ans *= lz[v][d] * lz[v][d + 1] % mod) %= mod; } cout << ans << '\n'; } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...