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>
#ifdef LOCAL
#include <bits/debug.hpp>
#endif // LOCAL
#define ll long long
#define all(x) x.begin(), x.end()
#define Neco "Sprinkler"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 2e5 + 7
using namespace std;
ll mod = 1e9 + 7; //972663749
const ll base = 911382323;
int n, q;
int par[maxn];
ll f[maxn][41], a[maxn];
vector<int> edges[maxn];
void pre_dfs(int u, int p) {
for(int v : edges[u]) if(v != p) {
par[v] = u;
pre_dfs(v, u);
}
}
void update(int x, int k, ll w) {
int i;
for(i = 0; i <= k && x != 0; ++i) {
int remain = k - i;
f[x][remain] = f[x][remain] * w % mod;
if(remain - 1 >= 0) f[x][remain - 1] = f[x][remain - 1] * w % mod;
x = par[x];
}
if(x == 0) {
fi(t, 1, k - i) f[0][t] = f[0][t] * w % mod;
}
}
ll get(int x) {
ll ans = a[x], d = 0;
fi(_, 0, 40) {
ans = ans * f[x][d] ;
x = par[x], ++d;
if(x == 0) break;
}
if(x == 0) ans = ans * f[0][d] % mod;
return ans;
}
void solve()
{
cin >> n >> mod;
fi(i, 1, n - 1) {
int x, y; cin >> x >> y;
edges[x].push_back(y), edges[y].push_back(x);
}
fi(i, 1, n) cin >> a[i];
fi(i, 0, n) fi(j, 0, 40) f[i][j] = 1;
pre_dfs(1, 0);
cin >> q;
fi(i, 1, q) {
int type, x, k, w; cin >> type >> x;
if(type == 1) cin >> k >> w, update(x, k, w);
else cout << get(x) << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(Neco".inp", "r")) {
freopen(Neco".inp", "r", stdin);
freopen(Neco".out", "w", stdout);
}
int nTest = 1;
// cin >> nTest;
while(nTest--)
{
solve();
}
return 0;
}
Compilation message (stderr)
sprinkler.cpp: In function 'int main()':
sprinkler.cpp:91:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | freopen(Neco".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sprinkler.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | freopen(Neco".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |