이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define endl '\n'
#define pb push_back
using pi = array<int, 2>;
const int N = 2e5 + 5;
vector<int> adj[N];
int par[N];
int h[N], wch[2][N];
void dfs(int u) {
for (int v : adj[u]) {
if (v == par[u]) continue;
par[v] = u;
dfs(v);
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, l;
cin >> n >> l;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
for (int i = 0; i <= n; ++i) wch[0][i] = wch[1][i] = 1;
dfs(1);
for (int i = 1; i <= n; ++i) cin >> h[i];
int q;
cin >> q;
while (q--) {
int t;
cin >> t;
if (t == 1) {
int u, d, w;
cin >> u >> d >> w;
assert(d <= 2);
auto mult = [&](int& var) {
var = var * w % l;
};
int p = par[u];
if (d == 0) {
mult(h[u]);
} else if (d == 1) {
mult(h[u]);
mult(h[p]);
mult(wch[0][u]);
} else {
mult(h[p]);
mult(wch[0][p]); // all children of p
mult(h[par[p]]);
mult(wch[0][u]);
mult(wch[1][u]);
}
} else {
int u;
cin >> u;
int ans = h[u];
ans = ans * wch[0][par[u]] % l;
ans = ans * wch[1][par[par[u]]] % l;
cout << ans << endl;
}
}
}
# | 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... |