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 ll long long
using namespace std;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 3e5 + 50;
vector <int> ed[N];
int p[N];
ll mul[N][41], l;
ll h[N];
void dfs(int u, int v) {
p[u] = v;
for (int i = 0; i < 41; i++)
mul[u][i] = 1;
for (auto x : ed[u]) {
if (x == v)
continue;
dfs(x, u);
}
}
void upd(int x, int d, ll w) {
for (int i = 0; i <= d; i++) {
mul[x][d - i] = (mul[x][d - i] * w) % l;
x = p[x];
}
}
ll get(int x) {
ll pr = h[x];
for (int i = 0; i < 41; i++) {
pr = (pr * mul[x][i]) % l;
if (i + 1 < 41)
pr = (pr * mul[x][i + 1]) % l;
x = p[x];
}
return pr;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n >> l;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
a += 40;
b += 40;
ed[a].pb(b);
ed[b].pb(a);
}
for (int i = 1; i <= 40; i++) {
ed[i].pb(i + 1);
ed[i + 1].pb(i);
}
for (int i = 41; i <= n + 40; i++)
cin >> h[i];
dfs(1, 1);
int q;
cin >> q;
for (int i = 1; i <= q; i++) {
int t;
cin >> t;
if (t == 1) {
ll x, d, w;
cin >> x >> d >> w;
x += 40;
upd(x, d, w);
} else {
int x;
cin >> x;
x += 40;
cout << get(x) << '\n';
}
}
return 0;
}
/*
10 10
10 -8
7 2
7 -8
-3 -6
-2 1
-8 6
8 -1
2 4
6 -6
2 -1
*/
# | 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... |