#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const long long MAXN = 4005;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
long long n, mod;
vector < long long > g[MAXN];
long long h[MAXN];
void read()
{
cin >> n >> mod;
long long xx, yy;
for (long long i = 1; i < n; ++ i)
{
cin >> xx >> yy;
g[xx].push_back(yy);
g[yy].push_back(xx);
}
for (long long i = 1; i <= n; ++ i)
cin >> h[i];
}
///long long tin[MAXN], tout[MAXN];
long long used[MAXN];
long long p[MAXN];
///vector < long long > on_lvls[MAXN];
long long timerr = 0;
void dfs(long long beg, long long parent, long long depth)
{
/// tin[beg] = timerr;
p[beg] = parent;
used[beg] = 1;
///on_lvls[depth].push_back(beg);
long long nb;
/// children = 0;
for (long long i = 0; i < g[beg].size(); ++ i)
{
nb = g[beg][i];
if(!used[nb])
{
/// children ++;
dfs(nb, beg, depth + 1);
}
}
/**if(children)
{
timerr ++;
tout[beg] = timerr;
}*/
}
long long x, d, w;
long long upd[MAXN][88];
void print_upd()
{
for (long long i = 1; i <= n; ++ i)
{
cout << "upd for vertex " << i << " " << endl;
cout << "depth : ";
for (long long j = 0; j <= 3; ++ j)
cout << upd[i][j] << ", ";
cout << endl;
}
}
void update_arrays()
{
/// cout << "***" << endl;
long long vertex = x, depth = d;
while(vertex != 1 && depth >= 0)
{
upd[vertex][depth] *= w;
upd[vertex][depth] %= mod;
if(depth)
{
upd[vertex][depth-1] *= w;
upd[vertex][depth-1] %= mod;
}
///cout << vertex << " " << depth << " " << upd[vertex][depth] << endl;
///cout << vertex << " " << depth-1 << " " << upd[vertex][depth-1] << endl;
vertex = p[vertex];
depth --;
}
if(vertex == 1)
{
for (long long i = 0; i <= depth; ++ i)
{
upd[1][i] *= w;
upd[1][i] %= mod;
}
}
}
void solve_query()
{
long long anc = x, lvl_diff = 0;
long long ans = h[x];
//cout << "solving for v = " << anc << endl;
while(anc && lvl_diff <= 40)
{
//cout << "anc = " << anc << " " << "mult is " << upd[anc][lvl_diff] << endl;
ans = ans * upd[anc][lvl_diff];
ans %= mod;
anc = p[anc];
lvl_diff ++;
}
cout << ans << endl;
}
void queries()
{
long long q;
cin >> q;
long long type;
while(q --)
{
cin >> type;
if(type == 1)
{
cin >> x >> d >> w;
update_arrays();
///prlong long_upd();
}
else
{
cin >> x;
solve_query();
}
}
}
int main()
{
speed();
read();
for (long long i = 1; i <= n; ++ i)
for (long long j = 0; j <= 80; ++ j)
upd[i][j] = 1;
dfs(1, 0, 1);
queries();
return 0;
}
Compilation message
sprinkler.cpp: In function 'void dfs(long long int, long long int, long long int)':
sprinkler.cpp:46:29: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for (long long i = 0; i < g[beg].size(); ++ i)
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
768 KB |
Output is correct |
4 |
Correct |
2 ms |
2916 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2888 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
564 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
568 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
568 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
768 KB |
Output is correct |
4 |
Correct |
2 ms |
2916 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2888 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
564 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Runtime error |
1 ms |
568 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |