#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
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)
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9820 KB |
Output is correct |
2 |
Correct |
2 ms |
9820 KB |
Output is correct |
3 |
Correct |
2 ms |
9820 KB |
Output is correct |
4 |
Correct |
3 ms |
10076 KB |
Output is correct |
5 |
Correct |
3 ms |
10120 KB |
Output is correct |
6 |
Correct |
3 ms |
10076 KB |
Output is correct |
7 |
Correct |
3 ms |
10076 KB |
Output is correct |
8 |
Correct |
2 ms |
10076 KB |
Output is correct |
9 |
Correct |
2 ms |
10076 KB |
Output is correct |
10 |
Correct |
2 ms |
10076 KB |
Output is correct |
11 |
Correct |
2 ms |
10076 KB |
Output is correct |
12 |
Correct |
2 ms |
10076 KB |
Output is correct |
13 |
Correct |
2 ms |
10072 KB |
Output is correct |
14 |
Correct |
3 ms |
10228 KB |
Output is correct |
15 |
Correct |
2 ms |
10072 KB |
Output is correct |
16 |
Correct |
2 ms |
10060 KB |
Output is correct |
17 |
Correct |
2 ms |
10076 KB |
Output is correct |
18 |
Correct |
2 ms |
10076 KB |
Output is correct |
19 |
Correct |
2 ms |
9820 KB |
Output is correct |
20 |
Correct |
2 ms |
9820 KB |
Output is correct |
21 |
Correct |
2 ms |
10056 KB |
Output is correct |
22 |
Correct |
2 ms |
9820 KB |
Output is correct |
23 |
Correct |
2 ms |
9820 KB |
Output is correct |
24 |
Correct |
2 ms |
9820 KB |
Output is correct |
25 |
Correct |
3 ms |
9904 KB |
Output is correct |
26 |
Correct |
2 ms |
10076 KB |
Output is correct |
27 |
Correct |
2 ms |
10076 KB |
Output is correct |
28 |
Correct |
2 ms |
10072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9820 KB |
Output is correct |
2 |
Correct |
394 ms |
94416 KB |
Output is correct |
3 |
Correct |
253 ms |
93772 KB |
Output is correct |
4 |
Correct |
397 ms |
99664 KB |
Output is correct |
5 |
Correct |
324 ms |
94624 KB |
Output is correct |
6 |
Correct |
232 ms |
94032 KB |
Output is correct |
7 |
Correct |
222 ms |
94352 KB |
Output is correct |
8 |
Correct |
210 ms |
94728 KB |
Output is correct |
9 |
Correct |
494 ms |
103188 KB |
Output is correct |
10 |
Correct |
277 ms |
101968 KB |
Output is correct |
11 |
Correct |
380 ms |
94492 KB |
Output is correct |
12 |
Correct |
250 ms |
90960 KB |
Output is correct |
13 |
Correct |
171 ms |
91336 KB |
Output is correct |
14 |
Correct |
185 ms |
91636 KB |
Output is correct |
15 |
Correct |
166 ms |
90960 KB |
Output is correct |
16 |
Correct |
176 ms |
91220 KB |
Output is correct |
17 |
Correct |
180 ms |
91952 KB |
Output is correct |
18 |
Correct |
2 ms |
10076 KB |
Output is correct |
19 |
Correct |
2 ms |
10076 KB |
Output is correct |
20 |
Correct |
3 ms |
10076 KB |
Output is correct |
21 |
Correct |
2 ms |
9916 KB |
Output is correct |
22 |
Correct |
2 ms |
10076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9820 KB |
Output is correct |
2 |
Correct |
394 ms |
94416 KB |
Output is correct |
3 |
Correct |
253 ms |
93772 KB |
Output is correct |
4 |
Correct |
397 ms |
99664 KB |
Output is correct |
5 |
Correct |
324 ms |
94624 KB |
Output is correct |
6 |
Correct |
232 ms |
94032 KB |
Output is correct |
7 |
Correct |
222 ms |
94352 KB |
Output is correct |
8 |
Correct |
210 ms |
94728 KB |
Output is correct |
9 |
Correct |
494 ms |
103188 KB |
Output is correct |
10 |
Correct |
277 ms |
101968 KB |
Output is correct |
11 |
Correct |
380 ms |
94492 KB |
Output is correct |
12 |
Correct |
250 ms |
90960 KB |
Output is correct |
13 |
Correct |
171 ms |
91336 KB |
Output is correct |
14 |
Correct |
185 ms |
91636 KB |
Output is correct |
15 |
Correct |
166 ms |
90960 KB |
Output is correct |
16 |
Correct |
176 ms |
91220 KB |
Output is correct |
17 |
Correct |
180 ms |
91952 KB |
Output is correct |
18 |
Correct |
2 ms |
10076 KB |
Output is correct |
19 |
Correct |
2 ms |
10076 KB |
Output is correct |
20 |
Correct |
3 ms |
10076 KB |
Output is correct |
21 |
Correct |
2 ms |
9916 KB |
Output is correct |
22 |
Correct |
2 ms |
10076 KB |
Output is correct |
23 |
Correct |
2 ms |
9820 KB |
Output is correct |
24 |
Correct |
400 ms |
92376 KB |
Output is correct |
25 |
Correct |
255 ms |
96468 KB |
Output is correct |
26 |
Correct |
390 ms |
105044 KB |
Output is correct |
27 |
Correct |
321 ms |
96120 KB |
Output is correct |
28 |
Correct |
254 ms |
96376 KB |
Output is correct |
29 |
Correct |
226 ms |
96008 KB |
Output is correct |
30 |
Correct |
210 ms |
96948 KB |
Output is correct |
31 |
Correct |
497 ms |
102224 KB |
Output is correct |
32 |
Correct |
290 ms |
104904 KB |
Output is correct |
33 |
Correct |
374 ms |
95828 KB |
Output is correct |
34 |
Correct |
267 ms |
96596 KB |
Output is correct |
35 |
Correct |
2 ms |
10072 KB |
Output is correct |
36 |
Correct |
3 ms |
10076 KB |
Output is correct |
37 |
Correct |
2 ms |
10076 KB |
Output is correct |
38 |
Correct |
2 ms |
9852 KB |
Output is correct |
39 |
Correct |
3 ms |
10076 KB |
Output is correct |
40 |
Correct |
2 ms |
9820 KB |
Output is correct |
41 |
Correct |
2 ms |
9820 KB |
Output is correct |
42 |
Correct |
2 ms |
9820 KB |
Output is correct |
43 |
Correct |
2 ms |
10076 KB |
Output is correct |
44 |
Correct |
2 ms |
9820 KB |
Output is correct |
45 |
Correct |
2 ms |
10072 KB |
Output is correct |
46 |
Correct |
2 ms |
9820 KB |
Output is correct |
47 |
Correct |
2 ms |
10076 KB |
Output is correct |
48 |
Correct |
3 ms |
9820 KB |
Output is correct |
49 |
Correct |
2 ms |
10076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9820 KB |
Output is correct |
2 |
Correct |
515 ms |
98532 KB |
Output is correct |
3 |
Correct |
602 ms |
96084 KB |
Output is correct |
4 |
Correct |
491 ms |
96396 KB |
Output is correct |
5 |
Correct |
360 ms |
89172 KB |
Output is correct |
6 |
Correct |
254 ms |
89168 KB |
Output is correct |
7 |
Correct |
243 ms |
89368 KB |
Output is correct |
8 |
Correct |
192 ms |
90052 KB |
Output is correct |
9 |
Correct |
530 ms |
95980 KB |
Output is correct |
10 |
Correct |
584 ms |
99920 KB |
Output is correct |
11 |
Correct |
403 ms |
91336 KB |
Output is correct |
12 |
Correct |
406 ms |
92036 KB |
Output is correct |
13 |
Correct |
313 ms |
92880 KB |
Output is correct |
14 |
Correct |
308 ms |
93212 KB |
Output is correct |
15 |
Correct |
2 ms |
9820 KB |
Output is correct |
16 |
Correct |
2 ms |
9820 KB |
Output is correct |
17 |
Correct |
2 ms |
9820 KB |
Output is correct |
18 |
Correct |
2 ms |
10052 KB |
Output is correct |
19 |
Correct |
3 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9820 KB |
Output is correct |
2 |
Correct |
523 ms |
101464 KB |
Output is correct |
3 |
Correct |
637 ms |
98776 KB |
Output is correct |
4 |
Correct |
464 ms |
100176 KB |
Output is correct |
5 |
Correct |
361 ms |
94800 KB |
Output is correct |
6 |
Correct |
281 ms |
95064 KB |
Output is correct |
7 |
Correct |
267 ms |
94696 KB |
Output is correct |
8 |
Correct |
197 ms |
95156 KB |
Output is correct |
9 |
Correct |
525 ms |
104148 KB |
Output is correct |
10 |
Correct |
587 ms |
102448 KB |
Output is correct |
11 |
Correct |
403 ms |
95716 KB |
Output is correct |
12 |
Correct |
388 ms |
94032 KB |
Output is correct |
13 |
Correct |
312 ms |
94848 KB |
Output is correct |
14 |
Correct |
315 ms |
95292 KB |
Output is correct |
15 |
Correct |
2 ms |
10076 KB |
Output is correct |
16 |
Correct |
2 ms |
9820 KB |
Output is correct |
17 |
Correct |
2 ms |
9912 KB |
Output is correct |
18 |
Correct |
2 ms |
10072 KB |
Output is correct |
19 |
Correct |
3 ms |
10076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9820 KB |
Output is correct |
2 |
Correct |
2 ms |
9820 KB |
Output is correct |
3 |
Correct |
2 ms |
9820 KB |
Output is correct |
4 |
Correct |
3 ms |
10076 KB |
Output is correct |
5 |
Correct |
3 ms |
10120 KB |
Output is correct |
6 |
Correct |
3 ms |
10076 KB |
Output is correct |
7 |
Correct |
3 ms |
10076 KB |
Output is correct |
8 |
Correct |
2 ms |
10076 KB |
Output is correct |
9 |
Correct |
2 ms |
10076 KB |
Output is correct |
10 |
Correct |
2 ms |
10076 KB |
Output is correct |
11 |
Correct |
2 ms |
10076 KB |
Output is correct |
12 |
Correct |
2 ms |
10076 KB |
Output is correct |
13 |
Correct |
2 ms |
10072 KB |
Output is correct |
14 |
Correct |
3 ms |
10228 KB |
Output is correct |
15 |
Correct |
2 ms |
10072 KB |
Output is correct |
16 |
Correct |
2 ms |
10060 KB |
Output is correct |
17 |
Correct |
2 ms |
10076 KB |
Output is correct |
18 |
Correct |
2 ms |
10076 KB |
Output is correct |
19 |
Correct |
2 ms |
9820 KB |
Output is correct |
20 |
Correct |
2 ms |
9820 KB |
Output is correct |
21 |
Correct |
2 ms |
10056 KB |
Output is correct |
22 |
Correct |
2 ms |
9820 KB |
Output is correct |
23 |
Correct |
2 ms |
9820 KB |
Output is correct |
24 |
Correct |
2 ms |
9820 KB |
Output is correct |
25 |
Correct |
3 ms |
9904 KB |
Output is correct |
26 |
Correct |
2 ms |
10076 KB |
Output is correct |
27 |
Correct |
2 ms |
10076 KB |
Output is correct |
28 |
Correct |
2 ms |
10072 KB |
Output is correct |
29 |
Correct |
2 ms |
9820 KB |
Output is correct |
30 |
Correct |
394 ms |
94416 KB |
Output is correct |
31 |
Correct |
253 ms |
93772 KB |
Output is correct |
32 |
Correct |
397 ms |
99664 KB |
Output is correct |
33 |
Correct |
324 ms |
94624 KB |
Output is correct |
34 |
Correct |
232 ms |
94032 KB |
Output is correct |
35 |
Correct |
222 ms |
94352 KB |
Output is correct |
36 |
Correct |
210 ms |
94728 KB |
Output is correct |
37 |
Correct |
494 ms |
103188 KB |
Output is correct |
38 |
Correct |
277 ms |
101968 KB |
Output is correct |
39 |
Correct |
380 ms |
94492 KB |
Output is correct |
40 |
Correct |
250 ms |
90960 KB |
Output is correct |
41 |
Correct |
171 ms |
91336 KB |
Output is correct |
42 |
Correct |
185 ms |
91636 KB |
Output is correct |
43 |
Correct |
166 ms |
90960 KB |
Output is correct |
44 |
Correct |
176 ms |
91220 KB |
Output is correct |
45 |
Correct |
180 ms |
91952 KB |
Output is correct |
46 |
Correct |
2 ms |
10076 KB |
Output is correct |
47 |
Correct |
2 ms |
10076 KB |
Output is correct |
48 |
Correct |
3 ms |
10076 KB |
Output is correct |
49 |
Correct |
2 ms |
9916 KB |
Output is correct |
50 |
Correct |
2 ms |
10076 KB |
Output is correct |
51 |
Correct |
2 ms |
9820 KB |
Output is correct |
52 |
Correct |
400 ms |
92376 KB |
Output is correct |
53 |
Correct |
255 ms |
96468 KB |
Output is correct |
54 |
Correct |
390 ms |
105044 KB |
Output is correct |
55 |
Correct |
321 ms |
96120 KB |
Output is correct |
56 |
Correct |
254 ms |
96376 KB |
Output is correct |
57 |
Correct |
226 ms |
96008 KB |
Output is correct |
58 |
Correct |
210 ms |
96948 KB |
Output is correct |
59 |
Correct |
497 ms |
102224 KB |
Output is correct |
60 |
Correct |
290 ms |
104904 KB |
Output is correct |
61 |
Correct |
374 ms |
95828 KB |
Output is correct |
62 |
Correct |
267 ms |
96596 KB |
Output is correct |
63 |
Correct |
2 ms |
10072 KB |
Output is correct |
64 |
Correct |
3 ms |
10076 KB |
Output is correct |
65 |
Correct |
2 ms |
10076 KB |
Output is correct |
66 |
Correct |
2 ms |
9852 KB |
Output is correct |
67 |
Correct |
3 ms |
10076 KB |
Output is correct |
68 |
Correct |
2 ms |
9820 KB |
Output is correct |
69 |
Correct |
2 ms |
9820 KB |
Output is correct |
70 |
Correct |
2 ms |
9820 KB |
Output is correct |
71 |
Correct |
2 ms |
10076 KB |
Output is correct |
72 |
Correct |
2 ms |
9820 KB |
Output is correct |
73 |
Correct |
2 ms |
10072 KB |
Output is correct |
74 |
Correct |
2 ms |
9820 KB |
Output is correct |
75 |
Correct |
2 ms |
10076 KB |
Output is correct |
76 |
Correct |
3 ms |
9820 KB |
Output is correct |
77 |
Correct |
2 ms |
10076 KB |
Output is correct |
78 |
Correct |
2 ms |
9820 KB |
Output is correct |
79 |
Correct |
515 ms |
98532 KB |
Output is correct |
80 |
Correct |
602 ms |
96084 KB |
Output is correct |
81 |
Correct |
491 ms |
96396 KB |
Output is correct |
82 |
Correct |
360 ms |
89172 KB |
Output is correct |
83 |
Correct |
254 ms |
89168 KB |
Output is correct |
84 |
Correct |
243 ms |
89368 KB |
Output is correct |
85 |
Correct |
192 ms |
90052 KB |
Output is correct |
86 |
Correct |
530 ms |
95980 KB |
Output is correct |
87 |
Correct |
584 ms |
99920 KB |
Output is correct |
88 |
Correct |
403 ms |
91336 KB |
Output is correct |
89 |
Correct |
406 ms |
92036 KB |
Output is correct |
90 |
Correct |
313 ms |
92880 KB |
Output is correct |
91 |
Correct |
308 ms |
93212 KB |
Output is correct |
92 |
Correct |
2 ms |
9820 KB |
Output is correct |
93 |
Correct |
2 ms |
9820 KB |
Output is correct |
94 |
Correct |
2 ms |
9820 KB |
Output is correct |
95 |
Correct |
2 ms |
10052 KB |
Output is correct |
96 |
Correct |
3 ms |
9812 KB |
Output is correct |
97 |
Correct |
2 ms |
9820 KB |
Output is correct |
98 |
Correct |
523 ms |
101464 KB |
Output is correct |
99 |
Correct |
637 ms |
98776 KB |
Output is correct |
100 |
Correct |
464 ms |
100176 KB |
Output is correct |
101 |
Correct |
361 ms |
94800 KB |
Output is correct |
102 |
Correct |
281 ms |
95064 KB |
Output is correct |
103 |
Correct |
267 ms |
94696 KB |
Output is correct |
104 |
Correct |
197 ms |
95156 KB |
Output is correct |
105 |
Correct |
525 ms |
104148 KB |
Output is correct |
106 |
Correct |
587 ms |
102448 KB |
Output is correct |
107 |
Correct |
403 ms |
95716 KB |
Output is correct |
108 |
Correct |
388 ms |
94032 KB |
Output is correct |
109 |
Correct |
312 ms |
94848 KB |
Output is correct |
110 |
Correct |
315 ms |
95292 KB |
Output is correct |
111 |
Correct |
2 ms |
10076 KB |
Output is correct |
112 |
Correct |
2 ms |
9820 KB |
Output is correct |
113 |
Correct |
2 ms |
9912 KB |
Output is correct |
114 |
Correct |
2 ms |
10072 KB |
Output is correct |
115 |
Correct |
3 ms |
10076 KB |
Output is correct |
116 |
Correct |
414 ms |
94036 KB |
Output is correct |
117 |
Correct |
449 ms |
96772 KB |
Output is correct |
118 |
Correct |
515 ms |
104832 KB |
Output is correct |
119 |
Correct |
376 ms |
96364 KB |
Output is correct |
120 |
Correct |
292 ms |
95892 KB |
Output is correct |
121 |
Correct |
283 ms |
96592 KB |
Output is correct |
122 |
Correct |
216 ms |
96692 KB |
Output is correct |
123 |
Correct |
513 ms |
103196 KB |
Output is correct |
124 |
Correct |
663 ms |
101728 KB |
Output is correct |
125 |
Correct |
400 ms |
95024 KB |
Output is correct |
126 |
Correct |
461 ms |
96588 KB |
Output is correct |
127 |
Correct |
486 ms |
96888 KB |
Output is correct |
128 |
Correct |
425 ms |
97884 KB |
Output is correct |
129 |
Correct |
435 ms |
98148 KB |
Output is correct |