#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() {
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) << endl;
}
}
}
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);
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21080 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 |
21084 KB |
Output is correct |
5 |
Correct |
9 ms |
21084 KB |
Output is correct |
6 |
Correct |
6 ms |
21108 KB |
Output is correct |
7 |
Correct |
6 ms |
21080 KB |
Output is correct |
8 |
Correct |
7 ms |
21080 KB |
Output is correct |
9 |
Correct |
6 ms |
21084 KB |
Output is correct |
10 |
Correct |
5 ms |
21084 KB |
Output is correct |
11 |
Correct |
6 ms |
21084 KB |
Output is correct |
12 |
Correct |
5 ms |
21136 KB |
Output is correct |
13 |
Correct |
6 ms |
21084 KB |
Output is correct |
14 |
Correct |
5 ms |
21084 KB |
Output is correct |
15 |
Correct |
5 ms |
21084 KB |
Output is correct |
16 |
Correct |
7 ms |
21084 KB |
Output is correct |
17 |
Correct |
7 ms |
21084 KB |
Output is correct |
18 |
Correct |
5 ms |
21084 KB |
Output is correct |
19 |
Correct |
5 ms |
21084 KB |
Output is correct |
20 |
Correct |
5 ms |
21084 KB |
Output is correct |
21 |
Correct |
7 ms |
21084 KB |
Output is correct |
22 |
Correct |
7 ms |
21080 KB |
Output is correct |
23 |
Correct |
6 ms |
21084 KB |
Output is correct |
24 |
Correct |
5 ms |
21084 KB |
Output is correct |
25 |
Correct |
7 ms |
21080 KB |
Output is correct |
26 |
Correct |
5 ms |
21084 KB |
Output is correct |
27 |
Correct |
5 ms |
21084 KB |
Output is correct |
28 |
Correct |
7 ms |
20916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21080 KB |
Output is correct |
2 |
Correct |
923 ms |
51796 KB |
Output is correct |
3 |
Correct |
1050 ms |
52320 KB |
Output is correct |
4 |
Correct |
888 ms |
72384 KB |
Output is correct |
5 |
Correct |
901 ms |
51896 KB |
Output is correct |
6 |
Correct |
985 ms |
50884 KB |
Output is correct |
7 |
Correct |
976 ms |
51548 KB |
Output is correct |
8 |
Correct |
869 ms |
52028 KB |
Output is correct |
9 |
Correct |
978 ms |
84560 KB |
Output is correct |
10 |
Correct |
796 ms |
82088 KB |
Output is correct |
11 |
Correct |
951 ms |
51500 KB |
Output is correct |
12 |
Correct |
977 ms |
52244 KB |
Output is correct |
13 |
Correct |
614 ms |
52516 KB |
Output is correct |
14 |
Correct |
796 ms |
51824 KB |
Output is correct |
15 |
Correct |
857 ms |
51484 KB |
Output is correct |
16 |
Correct |
896 ms |
51940 KB |
Output is correct |
17 |
Correct |
1008 ms |
52416 KB |
Output is correct |
18 |
Correct |
5 ms |
21084 KB |
Output is correct |
19 |
Correct |
5 ms |
21080 KB |
Output is correct |
20 |
Correct |
7 ms |
21084 KB |
Output is correct |
21 |
Correct |
5 ms |
21080 KB |
Output is correct |
22 |
Correct |
5 ms |
21084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21080 KB |
Output is correct |
2 |
Correct |
923 ms |
51796 KB |
Output is correct |
3 |
Correct |
1050 ms |
52320 KB |
Output is correct |
4 |
Correct |
888 ms |
72384 KB |
Output is correct |
5 |
Correct |
901 ms |
51896 KB |
Output is correct |
6 |
Correct |
985 ms |
50884 KB |
Output is correct |
7 |
Correct |
976 ms |
51548 KB |
Output is correct |
8 |
Correct |
869 ms |
52028 KB |
Output is correct |
9 |
Correct |
978 ms |
84560 KB |
Output is correct |
10 |
Correct |
796 ms |
82088 KB |
Output is correct |
11 |
Correct |
951 ms |
51500 KB |
Output is correct |
12 |
Correct |
977 ms |
52244 KB |
Output is correct |
13 |
Correct |
614 ms |
52516 KB |
Output is correct |
14 |
Correct |
796 ms |
51824 KB |
Output is correct |
15 |
Correct |
857 ms |
51484 KB |
Output is correct |
16 |
Correct |
896 ms |
51940 KB |
Output is correct |
17 |
Correct |
1008 ms |
52416 KB |
Output is correct |
18 |
Correct |
5 ms |
21084 KB |
Output is correct |
19 |
Correct |
5 ms |
21080 KB |
Output is correct |
20 |
Correct |
7 ms |
21084 KB |
Output is correct |
21 |
Correct |
5 ms |
21080 KB |
Output is correct |
22 |
Correct |
5 ms |
21084 KB |
Output is correct |
23 |
Correct |
4 ms |
21080 KB |
Output is correct |
24 |
Correct |
1032 ms |
51432 KB |
Output is correct |
25 |
Correct |
1216 ms |
52528 KB |
Output is correct |
26 |
Correct |
988 ms |
83536 KB |
Output is correct |
27 |
Correct |
999 ms |
51756 KB |
Output is correct |
28 |
Correct |
1096 ms |
51284 KB |
Output is correct |
29 |
Correct |
1094 ms |
51144 KB |
Output is correct |
30 |
Correct |
891 ms |
52000 KB |
Output is correct |
31 |
Correct |
994 ms |
73384 KB |
Output is correct |
32 |
Correct |
821 ms |
82008 KB |
Output is correct |
33 |
Correct |
989 ms |
51716 KB |
Output is correct |
34 |
Correct |
1243 ms |
52340 KB |
Output is correct |
35 |
Correct |
5 ms |
21080 KB |
Output is correct |
36 |
Correct |
6 ms |
21084 KB |
Output is correct |
37 |
Correct |
5 ms |
21084 KB |
Output is correct |
38 |
Correct |
6 ms |
21136 KB |
Output is correct |
39 |
Correct |
5 ms |
21084 KB |
Output is correct |
40 |
Correct |
6 ms |
21084 KB |
Output is correct |
41 |
Correct |
6 ms |
21084 KB |
Output is correct |
42 |
Correct |
6 ms |
21084 KB |
Output is correct |
43 |
Correct |
5 ms |
21084 KB |
Output is correct |
44 |
Correct |
5 ms |
21084 KB |
Output is correct |
45 |
Correct |
5 ms |
21084 KB |
Output is correct |
46 |
Correct |
5 ms |
21080 KB |
Output is correct |
47 |
Correct |
7 ms |
21144 KB |
Output is correct |
48 |
Correct |
5 ms |
21084 KB |
Output is correct |
49 |
Correct |
5 ms |
20924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
21084 KB |
Output is correct |
2 |
Correct |
1449 ms |
81124 KB |
Output is correct |
3 |
Correct |
2731 ms |
73080 KB |
Output is correct |
4 |
Correct |
1511 ms |
74244 KB |
Output is correct |
5 |
Correct |
3697 ms |
48812 KB |
Output is correct |
6 |
Correct |
2334 ms |
48296 KB |
Output is correct |
7 |
Correct |
1501 ms |
48404 KB |
Output is correct |
8 |
Correct |
814 ms |
48660 KB |
Output is correct |
9 |
Correct |
1269 ms |
70564 KB |
Output is correct |
10 |
Correct |
2910 ms |
78636 KB |
Output is correct |
11 |
Correct |
2023 ms |
48808 KB |
Output is correct |
12 |
Execution timed out |
4070 ms |
46780 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21084 KB |
Output is correct |
2 |
Correct |
1345 ms |
71868 KB |
Output is correct |
3 |
Correct |
3739 ms |
65132 KB |
Output is correct |
4 |
Correct |
1738 ms |
69128 KB |
Output is correct |
5 |
Correct |
3654 ms |
50636 KB |
Output is correct |
6 |
Correct |
2232 ms |
50108 KB |
Output is correct |
7 |
Correct |
1575 ms |
49804 KB |
Output is correct |
8 |
Correct |
710 ms |
49740 KB |
Output is correct |
9 |
Correct |
1282 ms |
81528 KB |
Output is correct |
10 |
Correct |
2815 ms |
80648 KB |
Output is correct |
11 |
Correct |
1902 ms |
51168 KB |
Output is correct |
12 |
Execution timed out |
4030 ms |
46856 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21080 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 |
21084 KB |
Output is correct |
5 |
Correct |
9 ms |
21084 KB |
Output is correct |
6 |
Correct |
6 ms |
21108 KB |
Output is correct |
7 |
Correct |
6 ms |
21080 KB |
Output is correct |
8 |
Correct |
7 ms |
21080 KB |
Output is correct |
9 |
Correct |
6 ms |
21084 KB |
Output is correct |
10 |
Correct |
5 ms |
21084 KB |
Output is correct |
11 |
Correct |
6 ms |
21084 KB |
Output is correct |
12 |
Correct |
5 ms |
21136 KB |
Output is correct |
13 |
Correct |
6 ms |
21084 KB |
Output is correct |
14 |
Correct |
5 ms |
21084 KB |
Output is correct |
15 |
Correct |
5 ms |
21084 KB |
Output is correct |
16 |
Correct |
7 ms |
21084 KB |
Output is correct |
17 |
Correct |
7 ms |
21084 KB |
Output is correct |
18 |
Correct |
5 ms |
21084 KB |
Output is correct |
19 |
Correct |
5 ms |
21084 KB |
Output is correct |
20 |
Correct |
5 ms |
21084 KB |
Output is correct |
21 |
Correct |
7 ms |
21084 KB |
Output is correct |
22 |
Correct |
7 ms |
21080 KB |
Output is correct |
23 |
Correct |
6 ms |
21084 KB |
Output is correct |
24 |
Correct |
5 ms |
21084 KB |
Output is correct |
25 |
Correct |
7 ms |
21080 KB |
Output is correct |
26 |
Correct |
5 ms |
21084 KB |
Output is correct |
27 |
Correct |
5 ms |
21084 KB |
Output is correct |
28 |
Correct |
7 ms |
20916 KB |
Output is correct |
29 |
Correct |
4 ms |
21080 KB |
Output is correct |
30 |
Correct |
923 ms |
51796 KB |
Output is correct |
31 |
Correct |
1050 ms |
52320 KB |
Output is correct |
32 |
Correct |
888 ms |
72384 KB |
Output is correct |
33 |
Correct |
901 ms |
51896 KB |
Output is correct |
34 |
Correct |
985 ms |
50884 KB |
Output is correct |
35 |
Correct |
976 ms |
51548 KB |
Output is correct |
36 |
Correct |
869 ms |
52028 KB |
Output is correct |
37 |
Correct |
978 ms |
84560 KB |
Output is correct |
38 |
Correct |
796 ms |
82088 KB |
Output is correct |
39 |
Correct |
951 ms |
51500 KB |
Output is correct |
40 |
Correct |
977 ms |
52244 KB |
Output is correct |
41 |
Correct |
614 ms |
52516 KB |
Output is correct |
42 |
Correct |
796 ms |
51824 KB |
Output is correct |
43 |
Correct |
857 ms |
51484 KB |
Output is correct |
44 |
Correct |
896 ms |
51940 KB |
Output is correct |
45 |
Correct |
1008 ms |
52416 KB |
Output is correct |
46 |
Correct |
5 ms |
21084 KB |
Output is correct |
47 |
Correct |
5 ms |
21080 KB |
Output is correct |
48 |
Correct |
7 ms |
21084 KB |
Output is correct |
49 |
Correct |
5 ms |
21080 KB |
Output is correct |
50 |
Correct |
5 ms |
21084 KB |
Output is correct |
51 |
Correct |
4 ms |
21080 KB |
Output is correct |
52 |
Correct |
1032 ms |
51432 KB |
Output is correct |
53 |
Correct |
1216 ms |
52528 KB |
Output is correct |
54 |
Correct |
988 ms |
83536 KB |
Output is correct |
55 |
Correct |
999 ms |
51756 KB |
Output is correct |
56 |
Correct |
1096 ms |
51284 KB |
Output is correct |
57 |
Correct |
1094 ms |
51144 KB |
Output is correct |
58 |
Correct |
891 ms |
52000 KB |
Output is correct |
59 |
Correct |
994 ms |
73384 KB |
Output is correct |
60 |
Correct |
821 ms |
82008 KB |
Output is correct |
61 |
Correct |
989 ms |
51716 KB |
Output is correct |
62 |
Correct |
1243 ms |
52340 KB |
Output is correct |
63 |
Correct |
5 ms |
21080 KB |
Output is correct |
64 |
Correct |
6 ms |
21084 KB |
Output is correct |
65 |
Correct |
5 ms |
21084 KB |
Output is correct |
66 |
Correct |
6 ms |
21136 KB |
Output is correct |
67 |
Correct |
5 ms |
21084 KB |
Output is correct |
68 |
Correct |
6 ms |
21084 KB |
Output is correct |
69 |
Correct |
6 ms |
21084 KB |
Output is correct |
70 |
Correct |
6 ms |
21084 KB |
Output is correct |
71 |
Correct |
5 ms |
21084 KB |
Output is correct |
72 |
Correct |
5 ms |
21084 KB |
Output is correct |
73 |
Correct |
5 ms |
21084 KB |
Output is correct |
74 |
Correct |
5 ms |
21080 KB |
Output is correct |
75 |
Correct |
7 ms |
21144 KB |
Output is correct |
76 |
Correct |
5 ms |
21084 KB |
Output is correct |
77 |
Correct |
5 ms |
20924 KB |
Output is correct |
78 |
Correct |
5 ms |
21084 KB |
Output is correct |
79 |
Correct |
1449 ms |
81124 KB |
Output is correct |
80 |
Correct |
2731 ms |
73080 KB |
Output is correct |
81 |
Correct |
1511 ms |
74244 KB |
Output is correct |
82 |
Correct |
3697 ms |
48812 KB |
Output is correct |
83 |
Correct |
2334 ms |
48296 KB |
Output is correct |
84 |
Correct |
1501 ms |
48404 KB |
Output is correct |
85 |
Correct |
814 ms |
48660 KB |
Output is correct |
86 |
Correct |
1269 ms |
70564 KB |
Output is correct |
87 |
Correct |
2910 ms |
78636 KB |
Output is correct |
88 |
Correct |
2023 ms |
48808 KB |
Output is correct |
89 |
Execution timed out |
4070 ms |
46780 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |