#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 <= x) && (y <= r)) {
(node[i][id] *= val) %= mod;
return;
}
if ((y < l) || (r < x)) return;
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;
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, l, r, ll, rr;
cin >> type;
if (type == 1) {
cin >> u >> dis >> val;
depth = grp[u].f+dis;
up = 0;
while (depth >= grp[u].f) {
l = 0; r = (int)d[depth].size()-1;
while (l <= r) {
int mid = (l+r)/2;
if (rng[d[depth][mid]].f < rng[u].f) l = mid+1;
else r = mid-1;
}
ll = l;
l = 0; r = (int)d[depth].size()-1;
while (l <= r) {
int mid = (l+r)/2;
if (rng[d[depth][mid]].f > rng[u].s) r = mid-1;
else l = mid+1;
}
rr = r;
if (ll <= rr) update(depth,1,0,d[depth].size()-1,ll,rr,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 |
8 ms |
21084 KB |
Output is correct |
2 |
Correct |
4 ms |
21084 KB |
Output is correct |
3 |
Correct |
5 ms |
21084 KB |
Output is correct |
4 |
Correct |
5 ms |
21084 KB |
Output is correct |
5 |
Correct |
6 ms |
21084 KB |
Output is correct |
6 |
Correct |
6 ms |
21340 KB |
Output is correct |
7 |
Correct |
5 ms |
21084 KB |
Output is correct |
8 |
Correct |
5 ms |
21084 KB |
Output is correct |
9 |
Correct |
5 ms |
21084 KB |
Output is correct |
10 |
Correct |
4 ms |
21084 KB |
Output is correct |
11 |
Correct |
5 ms |
21084 KB |
Output is correct |
12 |
Correct |
5 ms |
21084 KB |
Output is correct |
13 |
Correct |
4 ms |
21084 KB |
Output is correct |
14 |
Correct |
4 ms |
21084 KB |
Output is correct |
15 |
Correct |
4 ms |
20920 KB |
Output is correct |
16 |
Correct |
4 ms |
21084 KB |
Output is correct |
17 |
Correct |
4 ms |
21084 KB |
Output is correct |
18 |
Correct |
4 ms |
21084 KB |
Output is correct |
19 |
Correct |
4 ms |
21084 KB |
Output is correct |
20 |
Correct |
5 ms |
21084 KB |
Output is correct |
21 |
Correct |
5 ms |
21080 KB |
Output is correct |
22 |
Correct |
6 ms |
21084 KB |
Output is correct |
23 |
Correct |
4 ms |
21084 KB |
Output is correct |
24 |
Correct |
5 ms |
21084 KB |
Output is correct |
25 |
Correct |
4 ms |
21084 KB |
Output is correct |
26 |
Correct |
5 ms |
21164 KB |
Output is correct |
27 |
Correct |
4 ms |
21084 KB |
Output is correct |
28 |
Correct |
4 ms |
21084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21080 KB |
Output is correct |
2 |
Correct |
312 ms |
43420 KB |
Output is correct |
3 |
Correct |
655 ms |
40512 KB |
Output is correct |
4 |
Correct |
475 ms |
62528 KB |
Output is correct |
5 |
Correct |
387 ms |
41868 KB |
Output is correct |
6 |
Correct |
507 ms |
41664 KB |
Output is correct |
7 |
Correct |
509 ms |
41364 KB |
Output is correct |
8 |
Correct |
452 ms |
41732 KB |
Output is correct |
9 |
Correct |
361 ms |
76668 KB |
Output is correct |
10 |
Correct |
453 ms |
70136 KB |
Output is correct |
11 |
Correct |
340 ms |
43352 KB |
Output is correct |
12 |
Correct |
621 ms |
40344 KB |
Output is correct |
13 |
Correct |
272 ms |
40892 KB |
Output is correct |
14 |
Correct |
442 ms |
39900 KB |
Output is correct |
15 |
Correct |
525 ms |
40020 KB |
Output is correct |
16 |
Correct |
522 ms |
39992 KB |
Output is correct |
17 |
Correct |
559 ms |
40724 KB |
Output is correct |
18 |
Correct |
5 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 |
4 ms |
21080 KB |
Output is correct |
22 |
Correct |
6 ms |
21164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21080 KB |
Output is correct |
2 |
Correct |
312 ms |
43420 KB |
Output is correct |
3 |
Correct |
655 ms |
40512 KB |
Output is correct |
4 |
Correct |
475 ms |
62528 KB |
Output is correct |
5 |
Correct |
387 ms |
41868 KB |
Output is correct |
6 |
Correct |
507 ms |
41664 KB |
Output is correct |
7 |
Correct |
509 ms |
41364 KB |
Output is correct |
8 |
Correct |
452 ms |
41732 KB |
Output is correct |
9 |
Correct |
361 ms |
76668 KB |
Output is correct |
10 |
Correct |
453 ms |
70136 KB |
Output is correct |
11 |
Correct |
340 ms |
43352 KB |
Output is correct |
12 |
Correct |
621 ms |
40344 KB |
Output is correct |
13 |
Correct |
272 ms |
40892 KB |
Output is correct |
14 |
Correct |
442 ms |
39900 KB |
Output is correct |
15 |
Correct |
525 ms |
40020 KB |
Output is correct |
16 |
Correct |
522 ms |
39992 KB |
Output is correct |
17 |
Correct |
559 ms |
40724 KB |
Output is correct |
18 |
Correct |
5 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 |
4 ms |
21080 KB |
Output is correct |
22 |
Correct |
6 ms |
21164 KB |
Output is correct |
23 |
Correct |
4 ms |
21080 KB |
Output is correct |
24 |
Correct |
368 ms |
43428 KB |
Output is correct |
25 |
Correct |
798 ms |
40500 KB |
Output is correct |
26 |
Correct |
473 ms |
73296 KB |
Output is correct |
27 |
Correct |
497 ms |
41840 KB |
Output is correct |
28 |
Correct |
654 ms |
41336 KB |
Output is correct |
29 |
Correct |
625 ms |
41416 KB |
Output is correct |
30 |
Correct |
408 ms |
41532 KB |
Output is correct |
31 |
Correct |
356 ms |
65372 KB |
Output is correct |
32 |
Correct |
519 ms |
70596 KB |
Output is correct |
33 |
Correct |
336 ms |
43472 KB |
Output is correct |
34 |
Correct |
900 ms |
40228 KB |
Output is correct |
35 |
Correct |
5 ms |
21080 KB |
Output is correct |
36 |
Correct |
5 ms |
21084 KB |
Output is correct |
37 |
Correct |
4 ms |
21084 KB |
Output is correct |
38 |
Correct |
5 ms |
21084 KB |
Output is correct |
39 |
Correct |
5 ms |
21168 KB |
Output is correct |
40 |
Correct |
4 ms |
21148 KB |
Output is correct |
41 |
Correct |
4 ms |
21080 KB |
Output is correct |
42 |
Correct |
4 ms |
21080 KB |
Output is correct |
43 |
Correct |
4 ms |
21080 KB |
Output is correct |
44 |
Correct |
5 ms |
21084 KB |
Output is correct |
45 |
Correct |
5 ms |
21080 KB |
Output is correct |
46 |
Correct |
4 ms |
21080 KB |
Output is correct |
47 |
Correct |
4 ms |
21084 KB |
Output is correct |
48 |
Correct |
4 ms |
21084 KB |
Output is correct |
49 |
Correct |
6 ms |
21084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
21084 KB |
Output is correct |
2 |
Correct |
598 ms |
73348 KB |
Output is correct |
3 |
Correct |
2572 ms |
63904 KB |
Output is correct |
4 |
Correct |
1000 ms |
66056 KB |
Output is correct |
5 |
Correct |
3001 ms |
40400 KB |
Output is correct |
6 |
Correct |
1688 ms |
40332 KB |
Output is correct |
7 |
Correct |
921 ms |
39952 KB |
Output is correct |
8 |
Correct |
245 ms |
40096 KB |
Output is correct |
9 |
Correct |
620 ms |
63096 KB |
Output is correct |
10 |
Correct |
2599 ms |
69456 KB |
Output is correct |
11 |
Correct |
1259 ms |
40728 KB |
Output is correct |
12 |
Execution timed out |
4027 ms |
40516 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21336 KB |
Output is correct |
2 |
Correct |
627 ms |
63864 KB |
Output is correct |
3 |
Correct |
2956 ms |
55880 KB |
Output is correct |
4 |
Correct |
993 ms |
60524 KB |
Output is correct |
5 |
Correct |
2915 ms |
42140 KB |
Output is correct |
6 |
Correct |
1675 ms |
41428 KB |
Output is correct |
7 |
Correct |
1002 ms |
41256 KB |
Output is correct |
8 |
Correct |
273 ms |
41496 KB |
Output is correct |
9 |
Correct |
630 ms |
73780 KB |
Output is correct |
10 |
Correct |
2431 ms |
70972 KB |
Output is correct |
11 |
Correct |
1183 ms |
43204 KB |
Output is correct |
12 |
Execution timed out |
4098 ms |
40048 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
21084 KB |
Output is correct |
2 |
Correct |
4 ms |
21084 KB |
Output is correct |
3 |
Correct |
5 ms |
21084 KB |
Output is correct |
4 |
Correct |
5 ms |
21084 KB |
Output is correct |
5 |
Correct |
6 ms |
21084 KB |
Output is correct |
6 |
Correct |
6 ms |
21340 KB |
Output is correct |
7 |
Correct |
5 ms |
21084 KB |
Output is correct |
8 |
Correct |
5 ms |
21084 KB |
Output is correct |
9 |
Correct |
5 ms |
21084 KB |
Output is correct |
10 |
Correct |
4 ms |
21084 KB |
Output is correct |
11 |
Correct |
5 ms |
21084 KB |
Output is correct |
12 |
Correct |
5 ms |
21084 KB |
Output is correct |
13 |
Correct |
4 ms |
21084 KB |
Output is correct |
14 |
Correct |
4 ms |
21084 KB |
Output is correct |
15 |
Correct |
4 ms |
20920 KB |
Output is correct |
16 |
Correct |
4 ms |
21084 KB |
Output is correct |
17 |
Correct |
4 ms |
21084 KB |
Output is correct |
18 |
Correct |
4 ms |
21084 KB |
Output is correct |
19 |
Correct |
4 ms |
21084 KB |
Output is correct |
20 |
Correct |
5 ms |
21084 KB |
Output is correct |
21 |
Correct |
5 ms |
21080 KB |
Output is correct |
22 |
Correct |
6 ms |
21084 KB |
Output is correct |
23 |
Correct |
4 ms |
21084 KB |
Output is correct |
24 |
Correct |
5 ms |
21084 KB |
Output is correct |
25 |
Correct |
4 ms |
21084 KB |
Output is correct |
26 |
Correct |
5 ms |
21164 KB |
Output is correct |
27 |
Correct |
4 ms |
21084 KB |
Output is correct |
28 |
Correct |
4 ms |
21084 KB |
Output is correct |
29 |
Correct |
4 ms |
21080 KB |
Output is correct |
30 |
Correct |
312 ms |
43420 KB |
Output is correct |
31 |
Correct |
655 ms |
40512 KB |
Output is correct |
32 |
Correct |
475 ms |
62528 KB |
Output is correct |
33 |
Correct |
387 ms |
41868 KB |
Output is correct |
34 |
Correct |
507 ms |
41664 KB |
Output is correct |
35 |
Correct |
509 ms |
41364 KB |
Output is correct |
36 |
Correct |
452 ms |
41732 KB |
Output is correct |
37 |
Correct |
361 ms |
76668 KB |
Output is correct |
38 |
Correct |
453 ms |
70136 KB |
Output is correct |
39 |
Correct |
340 ms |
43352 KB |
Output is correct |
40 |
Correct |
621 ms |
40344 KB |
Output is correct |
41 |
Correct |
272 ms |
40892 KB |
Output is correct |
42 |
Correct |
442 ms |
39900 KB |
Output is correct |
43 |
Correct |
525 ms |
40020 KB |
Output is correct |
44 |
Correct |
522 ms |
39992 KB |
Output is correct |
45 |
Correct |
559 ms |
40724 KB |
Output is correct |
46 |
Correct |
5 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 |
4 ms |
21080 KB |
Output is correct |
50 |
Correct |
6 ms |
21164 KB |
Output is correct |
51 |
Correct |
4 ms |
21080 KB |
Output is correct |
52 |
Correct |
368 ms |
43428 KB |
Output is correct |
53 |
Correct |
798 ms |
40500 KB |
Output is correct |
54 |
Correct |
473 ms |
73296 KB |
Output is correct |
55 |
Correct |
497 ms |
41840 KB |
Output is correct |
56 |
Correct |
654 ms |
41336 KB |
Output is correct |
57 |
Correct |
625 ms |
41416 KB |
Output is correct |
58 |
Correct |
408 ms |
41532 KB |
Output is correct |
59 |
Correct |
356 ms |
65372 KB |
Output is correct |
60 |
Correct |
519 ms |
70596 KB |
Output is correct |
61 |
Correct |
336 ms |
43472 KB |
Output is correct |
62 |
Correct |
900 ms |
40228 KB |
Output is correct |
63 |
Correct |
5 ms |
21080 KB |
Output is correct |
64 |
Correct |
5 ms |
21084 KB |
Output is correct |
65 |
Correct |
4 ms |
21084 KB |
Output is correct |
66 |
Correct |
5 ms |
21084 KB |
Output is correct |
67 |
Correct |
5 ms |
21168 KB |
Output is correct |
68 |
Correct |
4 ms |
21148 KB |
Output is correct |
69 |
Correct |
4 ms |
21080 KB |
Output is correct |
70 |
Correct |
4 ms |
21080 KB |
Output is correct |
71 |
Correct |
4 ms |
21080 KB |
Output is correct |
72 |
Correct |
5 ms |
21084 KB |
Output is correct |
73 |
Correct |
5 ms |
21080 KB |
Output is correct |
74 |
Correct |
4 ms |
21080 KB |
Output is correct |
75 |
Correct |
4 ms |
21084 KB |
Output is correct |
76 |
Correct |
4 ms |
21084 KB |
Output is correct |
77 |
Correct |
6 ms |
21084 KB |
Output is correct |
78 |
Correct |
5 ms |
21084 KB |
Output is correct |
79 |
Correct |
598 ms |
73348 KB |
Output is correct |
80 |
Correct |
2572 ms |
63904 KB |
Output is correct |
81 |
Correct |
1000 ms |
66056 KB |
Output is correct |
82 |
Correct |
3001 ms |
40400 KB |
Output is correct |
83 |
Correct |
1688 ms |
40332 KB |
Output is correct |
84 |
Correct |
921 ms |
39952 KB |
Output is correct |
85 |
Correct |
245 ms |
40096 KB |
Output is correct |
86 |
Correct |
620 ms |
63096 KB |
Output is correct |
87 |
Correct |
2599 ms |
69456 KB |
Output is correct |
88 |
Correct |
1259 ms |
40728 KB |
Output is correct |
89 |
Execution timed out |
4027 ms |
40516 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |