#include <iostream>
#include <vector>
#define f first
#define s second
#define int long long
using namespace std;
int n, mod, a[200050], cnt, test, p[200050];
pair<int,int> rng[200050], grp[200050];
vector<int> edge[200050], d[200050], node[200050];
bool up;
void dfs(int u, int v, int depth) {
p[u] = v;
rng[u].f = ++cnt;
grp[u] = {depth,d[depth].size()};
d[depth].push_back(u);
for (int i = 0; i < edge[u].size(); i++) if (edge[u][i] != v) dfs(edge[u][i],u,depth+1);
rng[u].s = cnt;
}
void build(int i, int id, int x, int y) {
if (x == y) {
node[i][id] = a[d[i][x]];
return;
}
int mid = (x+y)/2;
build(i,id*2,x,mid);
build(i,id*2+1,mid+1,y);
node[i][id] = 1;
}
void update(int i, int id, int x, int y, int l, int r, int val) {
if ((l <= rng[d[i][x]].f) && (rng[d[i][y]].f <= r)) {
(node[i][id] *= val) %= mod;
return;
}
if ((rng[d[i][y]].f < l) || (r < rng[d[i][x]].f)) return;
int mid = (x+y)/2;
update(i,id*2,x,mid,l,r,val);
update(i,id*2+1,mid+1,y,l,r,val);
}
int query(int i, int id, int x, int y, int pos) {
if (x == y) return node[i][id];
int mid = (x+y)/2;
(node[i][id*2] *= node[i][id]) %= mod;
(node[i][id*2+1] *= node[i][id]) %= mod;
node[i][id] = 1;
if (pos <= mid) return query(i,id*2,x,mid,pos);
else return query(i,id*2+1,mid+1,y,pos);
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> mod;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
edge[u].push_back(v);
edge[v].push_back(u);
}
for (int i = 1; i <= n; i++) cin >> a[i];
dfs(1,0,1);
for (int i = 1; i <= n; i++) {
if (d[i].size()) {
node[i].resize(d[i].size()*4+10);
build(i,1,0,d[i].size()-1);
}
}
cin >> test;
while (test--) {
int type, u, dis, val, depth;
cin >> type;
if (type == 1) {
cin >> u >> dis >> val;
depth = grp[u].f+dis;
up = 0;
while (depth >= grp[u].f) {
if (d[depth].size()) update(depth,1,0,d[depth].size()-1,rng[u].f,rng[u].s,val);
if ((up) && (p[u])) u = p[u];
up = 1-up;
depth--;
}
} else {
cin >> u;
cout << query(grp[u].f,1,0,d[grp[u].f].size()-1,grp[u].s) << "\n";
}
}
}
Compilation message
sprinkler.cpp: In function 'void dfs(long long int, long long int, long long int)':
sprinkler.cpp:18:20: 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]
18 | for (int i = 0; i < edge[u].size(); i++) if (edge[u][i] != v) dfs(edge[u][i],u,depth+1);
| ~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
21084 KB |
Output is correct |
2 |
Correct |
4 ms |
21084 KB |
Output is correct |
3 |
Correct |
4 ms |
21084 KB |
Output is correct |
4 |
Correct |
6 ms |
21080 KB |
Output is correct |
5 |
Correct |
7 ms |
21084 KB |
Output is correct |
6 |
Correct |
6 ms |
21084 KB |
Output is correct |
7 |
Correct |
6 ms |
21084 KB |
Output is correct |
8 |
Correct |
6 ms |
21080 KB |
Output is correct |
9 |
Correct |
4 ms |
21080 KB |
Output is correct |
10 |
Correct |
6 ms |
21084 KB |
Output is correct |
11 |
Correct |
5 ms |
21084 KB |
Output is correct |
12 |
Correct |
4 ms |
21084 KB |
Output is correct |
13 |
Correct |
4 ms |
21084 KB |
Output is correct |
14 |
Correct |
5 ms |
21084 KB |
Output is correct |
15 |
Correct |
5 ms |
21340 KB |
Output is correct |
16 |
Correct |
5 ms |
21084 KB |
Output is correct |
17 |
Correct |
6 ms |
21084 KB |
Output is correct |
18 |
Correct |
5 ms |
21168 KB |
Output is correct |
19 |
Correct |
5 ms |
21084 KB |
Output is correct |
20 |
Correct |
4 ms |
21080 KB |
Output is correct |
21 |
Correct |
4 ms |
21084 KB |
Output is correct |
22 |
Correct |
5 ms |
21084 KB |
Output is correct |
23 |
Correct |
4 ms |
21084 KB |
Output is correct |
24 |
Correct |
4 ms |
21084 KB |
Output is correct |
25 |
Correct |
4 ms |
21112 KB |
Output is correct |
26 |
Correct |
6 ms |
21084 KB |
Output is correct |
27 |
Correct |
4 ms |
21336 KB |
Output is correct |
28 |
Correct |
4 ms |
21084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21084 KB |
Output is correct |
2 |
Correct |
337 ms |
43564 KB |
Output is correct |
3 |
Correct |
605 ms |
40404 KB |
Output is correct |
4 |
Correct |
433 ms |
62544 KB |
Output is correct |
5 |
Correct |
368 ms |
41960 KB |
Output is correct |
6 |
Correct |
476 ms |
41452 KB |
Output is correct |
7 |
Correct |
434 ms |
41580 KB |
Output is correct |
8 |
Correct |
392 ms |
41648 KB |
Output is correct |
9 |
Correct |
384 ms |
76676 KB |
Output is correct |
10 |
Correct |
457 ms |
70128 KB |
Output is correct |
11 |
Correct |
385 ms |
43352 KB |
Output is correct |
12 |
Correct |
601 ms |
40160 KB |
Output is correct |
13 |
Correct |
249 ms |
40992 KB |
Output is correct |
14 |
Correct |
412 ms |
39788 KB |
Output is correct |
15 |
Correct |
414 ms |
40020 KB |
Output is correct |
16 |
Correct |
438 ms |
39832 KB |
Output is correct |
17 |
Correct |
542 ms |
40432 KB |
Output is correct |
18 |
Correct |
6 ms |
21084 KB |
Output is correct |
19 |
Correct |
4 ms |
21084 KB |
Output is correct |
20 |
Correct |
4 ms |
21084 KB |
Output is correct |
21 |
Correct |
5 ms |
21084 KB |
Output is correct |
22 |
Correct |
5 ms |
21080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21084 KB |
Output is correct |
2 |
Correct |
337 ms |
43564 KB |
Output is correct |
3 |
Correct |
605 ms |
40404 KB |
Output is correct |
4 |
Correct |
433 ms |
62544 KB |
Output is correct |
5 |
Correct |
368 ms |
41960 KB |
Output is correct |
6 |
Correct |
476 ms |
41452 KB |
Output is correct |
7 |
Correct |
434 ms |
41580 KB |
Output is correct |
8 |
Correct |
392 ms |
41648 KB |
Output is correct |
9 |
Correct |
384 ms |
76676 KB |
Output is correct |
10 |
Correct |
457 ms |
70128 KB |
Output is correct |
11 |
Correct |
385 ms |
43352 KB |
Output is correct |
12 |
Correct |
601 ms |
40160 KB |
Output is correct |
13 |
Correct |
249 ms |
40992 KB |
Output is correct |
14 |
Correct |
412 ms |
39788 KB |
Output is correct |
15 |
Correct |
414 ms |
40020 KB |
Output is correct |
16 |
Correct |
438 ms |
39832 KB |
Output is correct |
17 |
Correct |
542 ms |
40432 KB |
Output is correct |
18 |
Correct |
6 ms |
21084 KB |
Output is correct |
19 |
Correct |
4 ms |
21084 KB |
Output is correct |
20 |
Correct |
4 ms |
21084 KB |
Output is correct |
21 |
Correct |
5 ms |
21084 KB |
Output is correct |
22 |
Correct |
5 ms |
21080 KB |
Output is correct |
23 |
Correct |
4 ms |
21080 KB |
Output is correct |
24 |
Correct |
349 ms |
43348 KB |
Output is correct |
25 |
Correct |
805 ms |
40284 KB |
Output is correct |
26 |
Correct |
425 ms |
73172 KB |
Output is correct |
27 |
Correct |
443 ms |
42228 KB |
Output is correct |
28 |
Correct |
529 ms |
41292 KB |
Output is correct |
29 |
Correct |
603 ms |
41340 KB |
Output is correct |
30 |
Correct |
325 ms |
41664 KB |
Output is correct |
31 |
Correct |
364 ms |
65160 KB |
Output is correct |
32 |
Correct |
526 ms |
70380 KB |
Output is correct |
33 |
Correct |
362 ms |
43380 KB |
Output is correct |
34 |
Correct |
824 ms |
40256 KB |
Output is correct |
35 |
Correct |
4 ms |
21084 KB |
Output is correct |
36 |
Correct |
4 ms |
21084 KB |
Output is correct |
37 |
Correct |
4 ms |
21080 KB |
Output is correct |
38 |
Correct |
4 ms |
21084 KB |
Output is correct |
39 |
Correct |
5 ms |
21084 KB |
Output is correct |
40 |
Correct |
4 ms |
20920 KB |
Output is correct |
41 |
Correct |
4 ms |
21084 KB |
Output is correct |
42 |
Correct |
4 ms |
21084 KB |
Output is correct |
43 |
Correct |
4 ms |
21084 KB |
Output is correct |
44 |
Correct |
5 ms |
21084 KB |
Output is correct |
45 |
Correct |
5 ms |
21236 KB |
Output is correct |
46 |
Correct |
5 ms |
21080 KB |
Output is correct |
47 |
Correct |
4 ms |
21084 KB |
Output is correct |
48 |
Correct |
6 ms |
21084 KB |
Output is correct |
49 |
Correct |
4 ms |
21084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21084 KB |
Output is correct |
2 |
Correct |
562 ms |
73124 KB |
Output is correct |
3 |
Correct |
2443 ms |
64128 KB |
Output is correct |
4 |
Correct |
981 ms |
65792 KB |
Output is correct |
5 |
Correct |
2704 ms |
40480 KB |
Output is correct |
6 |
Correct |
1594 ms |
39664 KB |
Output is correct |
7 |
Correct |
710 ms |
40124 KB |
Output is correct |
8 |
Correct |
233 ms |
40060 KB |
Output is correct |
9 |
Correct |
668 ms |
62904 KB |
Output is correct |
10 |
Correct |
2144 ms |
69416 KB |
Output is correct |
11 |
Correct |
1146 ms |
40632 KB |
Output is correct |
12 |
Execution timed out |
4065 ms |
40268 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21084 KB |
Output is correct |
2 |
Correct |
610 ms |
63828 KB |
Output is correct |
3 |
Correct |
2541 ms |
55628 KB |
Output is correct |
4 |
Correct |
1035 ms |
60456 KB |
Output is correct |
5 |
Correct |
2417 ms |
41780 KB |
Output is correct |
6 |
Correct |
1453 ms |
41520 KB |
Output is correct |
7 |
Correct |
902 ms |
41132 KB |
Output is correct |
8 |
Correct |
251 ms |
41436 KB |
Output is correct |
9 |
Correct |
568 ms |
73652 KB |
Output is correct |
10 |
Correct |
2143 ms |
71024 KB |
Output is correct |
11 |
Correct |
1136 ms |
43300 KB |
Output is correct |
12 |
Execution timed out |
4062 ms |
40540 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
21084 KB |
Output is correct |
2 |
Correct |
4 ms |
21084 KB |
Output is correct |
3 |
Correct |
4 ms |
21084 KB |
Output is correct |
4 |
Correct |
6 ms |
21080 KB |
Output is correct |
5 |
Correct |
7 ms |
21084 KB |
Output is correct |
6 |
Correct |
6 ms |
21084 KB |
Output is correct |
7 |
Correct |
6 ms |
21084 KB |
Output is correct |
8 |
Correct |
6 ms |
21080 KB |
Output is correct |
9 |
Correct |
4 ms |
21080 KB |
Output is correct |
10 |
Correct |
6 ms |
21084 KB |
Output is correct |
11 |
Correct |
5 ms |
21084 KB |
Output is correct |
12 |
Correct |
4 ms |
21084 KB |
Output is correct |
13 |
Correct |
4 ms |
21084 KB |
Output is correct |
14 |
Correct |
5 ms |
21084 KB |
Output is correct |
15 |
Correct |
5 ms |
21340 KB |
Output is correct |
16 |
Correct |
5 ms |
21084 KB |
Output is correct |
17 |
Correct |
6 ms |
21084 KB |
Output is correct |
18 |
Correct |
5 ms |
21168 KB |
Output is correct |
19 |
Correct |
5 ms |
21084 KB |
Output is correct |
20 |
Correct |
4 ms |
21080 KB |
Output is correct |
21 |
Correct |
4 ms |
21084 KB |
Output is correct |
22 |
Correct |
5 ms |
21084 KB |
Output is correct |
23 |
Correct |
4 ms |
21084 KB |
Output is correct |
24 |
Correct |
4 ms |
21084 KB |
Output is correct |
25 |
Correct |
4 ms |
21112 KB |
Output is correct |
26 |
Correct |
6 ms |
21084 KB |
Output is correct |
27 |
Correct |
4 ms |
21336 KB |
Output is correct |
28 |
Correct |
4 ms |
21084 KB |
Output is correct |
29 |
Correct |
4 ms |
21084 KB |
Output is correct |
30 |
Correct |
337 ms |
43564 KB |
Output is correct |
31 |
Correct |
605 ms |
40404 KB |
Output is correct |
32 |
Correct |
433 ms |
62544 KB |
Output is correct |
33 |
Correct |
368 ms |
41960 KB |
Output is correct |
34 |
Correct |
476 ms |
41452 KB |
Output is correct |
35 |
Correct |
434 ms |
41580 KB |
Output is correct |
36 |
Correct |
392 ms |
41648 KB |
Output is correct |
37 |
Correct |
384 ms |
76676 KB |
Output is correct |
38 |
Correct |
457 ms |
70128 KB |
Output is correct |
39 |
Correct |
385 ms |
43352 KB |
Output is correct |
40 |
Correct |
601 ms |
40160 KB |
Output is correct |
41 |
Correct |
249 ms |
40992 KB |
Output is correct |
42 |
Correct |
412 ms |
39788 KB |
Output is correct |
43 |
Correct |
414 ms |
40020 KB |
Output is correct |
44 |
Correct |
438 ms |
39832 KB |
Output is correct |
45 |
Correct |
542 ms |
40432 KB |
Output is correct |
46 |
Correct |
6 ms |
21084 KB |
Output is correct |
47 |
Correct |
4 ms |
21084 KB |
Output is correct |
48 |
Correct |
4 ms |
21084 KB |
Output is correct |
49 |
Correct |
5 ms |
21084 KB |
Output is correct |
50 |
Correct |
5 ms |
21080 KB |
Output is correct |
51 |
Correct |
4 ms |
21080 KB |
Output is correct |
52 |
Correct |
349 ms |
43348 KB |
Output is correct |
53 |
Correct |
805 ms |
40284 KB |
Output is correct |
54 |
Correct |
425 ms |
73172 KB |
Output is correct |
55 |
Correct |
443 ms |
42228 KB |
Output is correct |
56 |
Correct |
529 ms |
41292 KB |
Output is correct |
57 |
Correct |
603 ms |
41340 KB |
Output is correct |
58 |
Correct |
325 ms |
41664 KB |
Output is correct |
59 |
Correct |
364 ms |
65160 KB |
Output is correct |
60 |
Correct |
526 ms |
70380 KB |
Output is correct |
61 |
Correct |
362 ms |
43380 KB |
Output is correct |
62 |
Correct |
824 ms |
40256 KB |
Output is correct |
63 |
Correct |
4 ms |
21084 KB |
Output is correct |
64 |
Correct |
4 ms |
21084 KB |
Output is correct |
65 |
Correct |
4 ms |
21080 KB |
Output is correct |
66 |
Correct |
4 ms |
21084 KB |
Output is correct |
67 |
Correct |
5 ms |
21084 KB |
Output is correct |
68 |
Correct |
4 ms |
20920 KB |
Output is correct |
69 |
Correct |
4 ms |
21084 KB |
Output is correct |
70 |
Correct |
4 ms |
21084 KB |
Output is correct |
71 |
Correct |
4 ms |
21084 KB |
Output is correct |
72 |
Correct |
5 ms |
21084 KB |
Output is correct |
73 |
Correct |
5 ms |
21236 KB |
Output is correct |
74 |
Correct |
5 ms |
21080 KB |
Output is correct |
75 |
Correct |
4 ms |
21084 KB |
Output is correct |
76 |
Correct |
6 ms |
21084 KB |
Output is correct |
77 |
Correct |
4 ms |
21084 KB |
Output is correct |
78 |
Correct |
4 ms |
21084 KB |
Output is correct |
79 |
Correct |
562 ms |
73124 KB |
Output is correct |
80 |
Correct |
2443 ms |
64128 KB |
Output is correct |
81 |
Correct |
981 ms |
65792 KB |
Output is correct |
82 |
Correct |
2704 ms |
40480 KB |
Output is correct |
83 |
Correct |
1594 ms |
39664 KB |
Output is correct |
84 |
Correct |
710 ms |
40124 KB |
Output is correct |
85 |
Correct |
233 ms |
40060 KB |
Output is correct |
86 |
Correct |
668 ms |
62904 KB |
Output is correct |
87 |
Correct |
2144 ms |
69416 KB |
Output is correct |
88 |
Correct |
1146 ms |
40632 KB |
Output is correct |
89 |
Execution timed out |
4065 ms |
40268 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |