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>
using namespace std;
const int maxn = 200005;
const int maxDepth = 40;
int n, q;
long long mod;
vector<int> g[maxn];
long long height[maxn];
long long lazy[maxn][maxDepth + 5];
int parent[maxn];
void dfs(int node, int par)
{
parent[node] = par;
for (int i = 0; i < g[node].size(); ++i)
{
if (g[node][i] != par)
{
dfs(g[node][i], node);
}
}
}
void update(int node, int dist, long long mult)
{
while (node != -1 && dist >= 0)
{
if (node == 1) // if you do this cycle for every node (like in my last submit) you will count one lazy more than one time, which is not good
{
for (int i = dist; i >= 0; --i)
{
lazy[node][i] *= mult;
lazy[node][i] %= mod;
}
break;
}
lazy[node][dist] *= mult;
lazy[node][dist] %= mod;
if (dist >= 1)
{
lazy[node][dist - 1] *= mult;
lazy[node][dist - 1] %= mod;
}
--dist;
node = parent[node];
}
}
long long query(int node)
{
int dist = 0;
long long ans = height[node];
while (node != -1 && dist <= 40)
{
ans = (ans * lazy[node][dist]) % mod;
node = parent[node];
++dist;
}
return ans;
}
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fastIO();
cin >> n >> mod;
for (int i = 1; i <= n - 1; ++i)
{
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for (int i = 1; i <= n; ++i)
{
cin >> height[i];
}
for (int i = 1; i <= n; ++i)
{
for (int j = 0; j <= maxDepth; ++j)
{
lazy[i][j] = 1;
}
}
dfs(1, -1);
cin >> q;
for (int i = 1; i <= q; ++i)
{
int type;
cin >> type;
if (type == 1)
{
int node, dist, mult;
cin >> node >> dist >> mult;
update(node, dist, mult);
}
else
{
int node;
cin >> node;
cout << query(node) << "\n";
}
}
return 0;
}
Compilation message (stderr)
sprinkler.cpp: In function 'void dfs(int, int)':
sprinkler.cpp:23:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for (int i = 0; i < g[node].size(); ++i)
| ~~^~~~~~~~~~~~~~~~
# | 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... |