#include <bits/stdc++.h>
using namespace std;
const int N = 202020;
const int M = 44;
int n, mod, q;
vector<int> g[N];
int sub[N], dep[N], gp[N], a[N];
bool dead[N];
void pre_dfs(int p, int v) {
sub[v] = 1;
for (int x : g[v]) {
if (p == x || dead[x])
continue;
pre_dfs(v, x);
sub[v] += sub[x];
}
}
int find_centroid(int p, int v, int z) {
for (int x : g[v]) {
if (p == x || dead[x])
continue;
if (2 * sub[x] > z)
return find_centroid(v, x, z);
}
return v;
}
void dfs(int p, int v, int z) {
gp[v] = z;
dep[v] = dep[p] + 1;
for (int x : g[v]) {
if (p == x || dead[x])
continue;
dfs(v, x, z);
}
}
struct query {
int t, x, d, w, i;
};
vector<query> qr[N];
int ans[2 * N];
struct fenwick {
int tree[N];
void init(int sz) {
fill(tree, tree + sz + 1, 1);
}
void update(int a, int x) {
for (; a < N; a += a & -a)
tree[a] = 1ll * tree[a] * x % mod;
}
int query(int a) {
int res = 1;
for (; a; a -= a & -a)
res = 1ll * res * tree[a] % mod;
return res;
}
} t1[M], t2[M];
void solve(int v) {
pre_dfs(v, v);
int centroid = find_centroid(v, v, sub[v]);
dep[centroid] = 0;
for (int i = 0; i < g[centroid].size(); i++)
if (!dead[g[centroid][i]])
dfs(centroid, g[centroid][i], i);
if (v != centroid) {
qr[centroid] = qr[v];
vector<query>().swap(qr[v]);
}
int sz = g[centroid].size();
for (int i = 0; i < M; i++) {
t1[i].init(sz);
t2[i].init(sz);
}
vector<int> s(M, 1);
for (auto [t, x, d, w, i] : qr[centroid]) {
if (x != centroid)
qr[g[centroid][gp[x]]].push_back({t, x, d, w, i});
if (t == 1) {
if (dep[x] > d)
continue;
if (x == centroid)
s[d] = 1ll * s[d] * w % mod;
else {
t1[d - dep[x]].update(gp[x] + 1, w);
t2[d - dep[x]].update(sz - gp[x], w);
}
// cout << v << ' ' << centroid << ' ' << x << ' ' << d - dep[x] << ' ' << gp[x] << ' ' << w << '\n';
}
else {
for (int j = dep[x]; j < M; j++) {
if (x == centroid)
ans[i] = 1ll * ans[i] * t1[j].query(sz) % mod;
else {
if (gp[x])
ans[i] = 1ll * ans[i] * t1[j].query(gp[x]) % mod;
if (gp[x] != sz - 1)
ans[i] = 1ll * ans[i] * t2[j].query(sz - gp[x] - 1) % mod;
}
ans[i] = 1ll * ans[i] * s[j] % mod;
}
}
}
dead[centroid] = 1;
for (int i = 0; i < g[centroid].size(); i++)
if (!dead[g[centroid][i]])
solve(g[centroid][i]);
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> n >> mod;
for (int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++)
cin >> a[i];
cin >> q;
int sz = 0;
for (int i = 1; i <= q; i++) {
int t; cin >> t;
if (t == 1) {
int x, d, w; cin >> x >> d >> w;
qr[1].push_back({t, x, d, w, 0});
}
else {
int x; cin >> x;
qr[1].push_back({t, x, 0, 0, ++sz});
ans[sz] = a[x] % mod;
}
}
solve(1);
for (int i = 1; i <= sz; i++)
cout << ans[i] << '\n';
}
Compilation message
sprinkler.cpp: In function 'void solve(int)':
sprinkler.cpp:72:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int i = 0; i < g[centroid].size(); i++)
| ~~^~~~~~~~~~~~~~~~~~~~
sprinkler.cpp:120:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for (int i = 0; i < g[centroid].size(); i++)
| ~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10324 KB |
Output is correct |
2 |
Correct |
5 ms |
10324 KB |
Output is correct |
3 |
Correct |
6 ms |
10368 KB |
Output is correct |
4 |
Correct |
10 ms |
13048 KB |
Output is correct |
5 |
Correct |
11 ms |
13012 KB |
Output is correct |
6 |
Correct |
11 ms |
13036 KB |
Output is correct |
7 |
Correct |
10 ms |
13012 KB |
Output is correct |
8 |
Correct |
9 ms |
13012 KB |
Output is correct |
9 |
Correct |
6 ms |
10436 KB |
Output is correct |
10 |
Correct |
6 ms |
10452 KB |
Output is correct |
11 |
Correct |
6 ms |
10452 KB |
Output is correct |
12 |
Correct |
7 ms |
10452 KB |
Output is correct |
13 |
Correct |
7 ms |
10452 KB |
Output is correct |
14 |
Correct |
6 ms |
10468 KB |
Output is correct |
15 |
Correct |
7 ms |
10520 KB |
Output is correct |
16 |
Correct |
6 ms |
10452 KB |
Output is correct |
17 |
Correct |
7 ms |
10452 KB |
Output is correct |
18 |
Correct |
7 ms |
10452 KB |
Output is correct |
19 |
Correct |
6 ms |
10452 KB |
Output is correct |
20 |
Correct |
6 ms |
10460 KB |
Output is correct |
21 |
Correct |
6 ms |
10452 KB |
Output is correct |
22 |
Correct |
7 ms |
10452 KB |
Output is correct |
23 |
Correct |
7 ms |
10452 KB |
Output is correct |
24 |
Correct |
6 ms |
10452 KB |
Output is correct |
25 |
Correct |
6 ms |
10452 KB |
Output is correct |
26 |
Correct |
7 ms |
10452 KB |
Output is correct |
27 |
Correct |
7 ms |
10452 KB |
Output is correct |
28 |
Correct |
7 ms |
10468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10324 KB |
Output is correct |
2 |
Correct |
2449 ms |
131960 KB |
Output is correct |
3 |
Correct |
1018 ms |
135172 KB |
Output is correct |
4 |
Correct |
1724 ms |
177624 KB |
Output is correct |
5 |
Correct |
1737 ms |
135420 KB |
Output is correct |
6 |
Correct |
2341 ms |
116976 KB |
Output is correct |
7 |
Correct |
2360 ms |
110732 KB |
Output is correct |
8 |
Correct |
2339 ms |
121848 KB |
Output is correct |
9 |
Correct |
2234 ms |
183252 KB |
Output is correct |
10 |
Correct |
1295 ms |
181116 KB |
Output is correct |
11 |
Correct |
2462 ms |
137752 KB |
Output is correct |
12 |
Correct |
1031 ms |
136084 KB |
Output is correct |
13 |
Correct |
698 ms |
104832 KB |
Output is correct |
14 |
Correct |
785 ms |
75832 KB |
Output is correct |
15 |
Correct |
844 ms |
65432 KB |
Output is correct |
16 |
Correct |
840 ms |
65068 KB |
Output is correct |
17 |
Correct |
913 ms |
74212 KB |
Output is correct |
18 |
Correct |
6 ms |
10452 KB |
Output is correct |
19 |
Correct |
6 ms |
10468 KB |
Output is correct |
20 |
Correct |
7 ms |
10452 KB |
Output is correct |
21 |
Correct |
8 ms |
10472 KB |
Output is correct |
22 |
Correct |
6 ms |
10380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10324 KB |
Output is correct |
2 |
Correct |
2449 ms |
131960 KB |
Output is correct |
3 |
Correct |
1018 ms |
135172 KB |
Output is correct |
4 |
Correct |
1724 ms |
177624 KB |
Output is correct |
5 |
Correct |
1737 ms |
135420 KB |
Output is correct |
6 |
Correct |
2341 ms |
116976 KB |
Output is correct |
7 |
Correct |
2360 ms |
110732 KB |
Output is correct |
8 |
Correct |
2339 ms |
121848 KB |
Output is correct |
9 |
Correct |
2234 ms |
183252 KB |
Output is correct |
10 |
Correct |
1295 ms |
181116 KB |
Output is correct |
11 |
Correct |
2462 ms |
137752 KB |
Output is correct |
12 |
Correct |
1031 ms |
136084 KB |
Output is correct |
13 |
Correct |
698 ms |
104832 KB |
Output is correct |
14 |
Correct |
785 ms |
75832 KB |
Output is correct |
15 |
Correct |
844 ms |
65432 KB |
Output is correct |
16 |
Correct |
840 ms |
65068 KB |
Output is correct |
17 |
Correct |
913 ms |
74212 KB |
Output is correct |
18 |
Correct |
6 ms |
10452 KB |
Output is correct |
19 |
Correct |
6 ms |
10468 KB |
Output is correct |
20 |
Correct |
7 ms |
10452 KB |
Output is correct |
21 |
Correct |
8 ms |
10472 KB |
Output is correct |
22 |
Correct |
6 ms |
10380 KB |
Output is correct |
23 |
Correct |
6 ms |
10380 KB |
Output is correct |
24 |
Correct |
2491 ms |
138428 KB |
Output is correct |
25 |
Correct |
1131 ms |
135748 KB |
Output is correct |
26 |
Correct |
1766 ms |
182164 KB |
Output is correct |
27 |
Correct |
1743 ms |
136196 KB |
Output is correct |
28 |
Correct |
2452 ms |
120232 KB |
Output is correct |
29 |
Correct |
2257 ms |
110268 KB |
Output is correct |
30 |
Correct |
2305 ms |
121904 KB |
Output is correct |
31 |
Correct |
2257 ms |
178672 KB |
Output is correct |
32 |
Correct |
1365 ms |
181492 KB |
Output is correct |
33 |
Correct |
2525 ms |
136960 KB |
Output is correct |
34 |
Correct |
1129 ms |
134760 KB |
Output is correct |
35 |
Correct |
6 ms |
10452 KB |
Output is correct |
36 |
Correct |
8 ms |
10580 KB |
Output is correct |
37 |
Correct |
7 ms |
10468 KB |
Output is correct |
38 |
Correct |
7 ms |
10488 KB |
Output is correct |
39 |
Correct |
7 ms |
10452 KB |
Output is correct |
40 |
Correct |
6 ms |
10452 KB |
Output is correct |
41 |
Correct |
6 ms |
10452 KB |
Output is correct |
42 |
Correct |
7 ms |
10452 KB |
Output is correct |
43 |
Correct |
7 ms |
10452 KB |
Output is correct |
44 |
Correct |
7 ms |
10536 KB |
Output is correct |
45 |
Correct |
7 ms |
10452 KB |
Output is correct |
46 |
Correct |
7 ms |
10464 KB |
Output is correct |
47 |
Correct |
6 ms |
10460 KB |
Output is correct |
48 |
Correct |
7 ms |
10452 KB |
Output is correct |
49 |
Correct |
7 ms |
10452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
10332 KB |
Output is correct |
2 |
Correct |
2357 ms |
176112 KB |
Output is correct |
3 |
Correct |
1579 ms |
178408 KB |
Output is correct |
4 |
Correct |
1917 ms |
179248 KB |
Output is correct |
5 |
Correct |
1895 ms |
135212 KB |
Output is correct |
6 |
Correct |
2471 ms |
114860 KB |
Output is correct |
7 |
Correct |
2477 ms |
109464 KB |
Output is correct |
8 |
Correct |
2329 ms |
118996 KB |
Output is correct |
9 |
Correct |
2283 ms |
178172 KB |
Output is correct |
10 |
Correct |
1575 ms |
180644 KB |
Output is correct |
11 |
Correct |
2542 ms |
137340 KB |
Output is correct |
12 |
Correct |
1399 ms |
136608 KB |
Output is correct |
13 |
Correct |
1053 ms |
115064 KB |
Output is correct |
14 |
Correct |
1064 ms |
121848 KB |
Output is correct |
15 |
Correct |
6 ms |
10452 KB |
Output is correct |
16 |
Correct |
8 ms |
10520 KB |
Output is correct |
17 |
Correct |
6 ms |
10460 KB |
Output is correct |
18 |
Correct |
7 ms |
10460 KB |
Output is correct |
19 |
Correct |
7 ms |
10464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10328 KB |
Output is correct |
2 |
Correct |
2262 ms |
174024 KB |
Output is correct |
3 |
Correct |
1583 ms |
175232 KB |
Output is correct |
4 |
Correct |
1876 ms |
177792 KB |
Output is correct |
5 |
Correct |
1886 ms |
137700 KB |
Output is correct |
6 |
Correct |
2742 ms |
120380 KB |
Output is correct |
7 |
Correct |
2562 ms |
113504 KB |
Output is correct |
8 |
Correct |
2319 ms |
119972 KB |
Output is correct |
9 |
Correct |
2282 ms |
184284 KB |
Output is correct |
10 |
Correct |
1591 ms |
181628 KB |
Output is correct |
11 |
Correct |
2533 ms |
138588 KB |
Output is correct |
12 |
Correct |
1330 ms |
133388 KB |
Output is correct |
13 |
Correct |
1075 ms |
115176 KB |
Output is correct |
14 |
Correct |
1054 ms |
122036 KB |
Output is correct |
15 |
Correct |
6 ms |
10452 KB |
Output is correct |
16 |
Correct |
7 ms |
10452 KB |
Output is correct |
17 |
Correct |
8 ms |
10464 KB |
Output is correct |
18 |
Correct |
7 ms |
10468 KB |
Output is correct |
19 |
Correct |
6 ms |
10452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10324 KB |
Output is correct |
2 |
Correct |
5 ms |
10324 KB |
Output is correct |
3 |
Correct |
6 ms |
10368 KB |
Output is correct |
4 |
Correct |
10 ms |
13048 KB |
Output is correct |
5 |
Correct |
11 ms |
13012 KB |
Output is correct |
6 |
Correct |
11 ms |
13036 KB |
Output is correct |
7 |
Correct |
10 ms |
13012 KB |
Output is correct |
8 |
Correct |
9 ms |
13012 KB |
Output is correct |
9 |
Correct |
6 ms |
10436 KB |
Output is correct |
10 |
Correct |
6 ms |
10452 KB |
Output is correct |
11 |
Correct |
6 ms |
10452 KB |
Output is correct |
12 |
Correct |
7 ms |
10452 KB |
Output is correct |
13 |
Correct |
7 ms |
10452 KB |
Output is correct |
14 |
Correct |
6 ms |
10468 KB |
Output is correct |
15 |
Correct |
7 ms |
10520 KB |
Output is correct |
16 |
Correct |
6 ms |
10452 KB |
Output is correct |
17 |
Correct |
7 ms |
10452 KB |
Output is correct |
18 |
Correct |
7 ms |
10452 KB |
Output is correct |
19 |
Correct |
6 ms |
10452 KB |
Output is correct |
20 |
Correct |
6 ms |
10460 KB |
Output is correct |
21 |
Correct |
6 ms |
10452 KB |
Output is correct |
22 |
Correct |
7 ms |
10452 KB |
Output is correct |
23 |
Correct |
7 ms |
10452 KB |
Output is correct |
24 |
Correct |
6 ms |
10452 KB |
Output is correct |
25 |
Correct |
6 ms |
10452 KB |
Output is correct |
26 |
Correct |
7 ms |
10452 KB |
Output is correct |
27 |
Correct |
7 ms |
10452 KB |
Output is correct |
28 |
Correct |
7 ms |
10468 KB |
Output is correct |
29 |
Correct |
5 ms |
10324 KB |
Output is correct |
30 |
Correct |
2449 ms |
131960 KB |
Output is correct |
31 |
Correct |
1018 ms |
135172 KB |
Output is correct |
32 |
Correct |
1724 ms |
177624 KB |
Output is correct |
33 |
Correct |
1737 ms |
135420 KB |
Output is correct |
34 |
Correct |
2341 ms |
116976 KB |
Output is correct |
35 |
Correct |
2360 ms |
110732 KB |
Output is correct |
36 |
Correct |
2339 ms |
121848 KB |
Output is correct |
37 |
Correct |
2234 ms |
183252 KB |
Output is correct |
38 |
Correct |
1295 ms |
181116 KB |
Output is correct |
39 |
Correct |
2462 ms |
137752 KB |
Output is correct |
40 |
Correct |
1031 ms |
136084 KB |
Output is correct |
41 |
Correct |
698 ms |
104832 KB |
Output is correct |
42 |
Correct |
785 ms |
75832 KB |
Output is correct |
43 |
Correct |
844 ms |
65432 KB |
Output is correct |
44 |
Correct |
840 ms |
65068 KB |
Output is correct |
45 |
Correct |
913 ms |
74212 KB |
Output is correct |
46 |
Correct |
6 ms |
10452 KB |
Output is correct |
47 |
Correct |
6 ms |
10468 KB |
Output is correct |
48 |
Correct |
7 ms |
10452 KB |
Output is correct |
49 |
Correct |
8 ms |
10472 KB |
Output is correct |
50 |
Correct |
6 ms |
10380 KB |
Output is correct |
51 |
Correct |
6 ms |
10380 KB |
Output is correct |
52 |
Correct |
2491 ms |
138428 KB |
Output is correct |
53 |
Correct |
1131 ms |
135748 KB |
Output is correct |
54 |
Correct |
1766 ms |
182164 KB |
Output is correct |
55 |
Correct |
1743 ms |
136196 KB |
Output is correct |
56 |
Correct |
2452 ms |
120232 KB |
Output is correct |
57 |
Correct |
2257 ms |
110268 KB |
Output is correct |
58 |
Correct |
2305 ms |
121904 KB |
Output is correct |
59 |
Correct |
2257 ms |
178672 KB |
Output is correct |
60 |
Correct |
1365 ms |
181492 KB |
Output is correct |
61 |
Correct |
2525 ms |
136960 KB |
Output is correct |
62 |
Correct |
1129 ms |
134760 KB |
Output is correct |
63 |
Correct |
6 ms |
10452 KB |
Output is correct |
64 |
Correct |
8 ms |
10580 KB |
Output is correct |
65 |
Correct |
7 ms |
10468 KB |
Output is correct |
66 |
Correct |
7 ms |
10488 KB |
Output is correct |
67 |
Correct |
7 ms |
10452 KB |
Output is correct |
68 |
Correct |
6 ms |
10452 KB |
Output is correct |
69 |
Correct |
6 ms |
10452 KB |
Output is correct |
70 |
Correct |
7 ms |
10452 KB |
Output is correct |
71 |
Correct |
7 ms |
10452 KB |
Output is correct |
72 |
Correct |
7 ms |
10536 KB |
Output is correct |
73 |
Correct |
7 ms |
10452 KB |
Output is correct |
74 |
Correct |
7 ms |
10464 KB |
Output is correct |
75 |
Correct |
6 ms |
10460 KB |
Output is correct |
76 |
Correct |
7 ms |
10452 KB |
Output is correct |
77 |
Correct |
7 ms |
10452 KB |
Output is correct |
78 |
Correct |
6 ms |
10332 KB |
Output is correct |
79 |
Correct |
2357 ms |
176112 KB |
Output is correct |
80 |
Correct |
1579 ms |
178408 KB |
Output is correct |
81 |
Correct |
1917 ms |
179248 KB |
Output is correct |
82 |
Correct |
1895 ms |
135212 KB |
Output is correct |
83 |
Correct |
2471 ms |
114860 KB |
Output is correct |
84 |
Correct |
2477 ms |
109464 KB |
Output is correct |
85 |
Correct |
2329 ms |
118996 KB |
Output is correct |
86 |
Correct |
2283 ms |
178172 KB |
Output is correct |
87 |
Correct |
1575 ms |
180644 KB |
Output is correct |
88 |
Correct |
2542 ms |
137340 KB |
Output is correct |
89 |
Correct |
1399 ms |
136608 KB |
Output is correct |
90 |
Correct |
1053 ms |
115064 KB |
Output is correct |
91 |
Correct |
1064 ms |
121848 KB |
Output is correct |
92 |
Correct |
6 ms |
10452 KB |
Output is correct |
93 |
Correct |
8 ms |
10520 KB |
Output is correct |
94 |
Correct |
6 ms |
10460 KB |
Output is correct |
95 |
Correct |
7 ms |
10460 KB |
Output is correct |
96 |
Correct |
7 ms |
10464 KB |
Output is correct |
97 |
Correct |
5 ms |
10328 KB |
Output is correct |
98 |
Correct |
2262 ms |
174024 KB |
Output is correct |
99 |
Correct |
1583 ms |
175232 KB |
Output is correct |
100 |
Correct |
1876 ms |
177792 KB |
Output is correct |
101 |
Correct |
1886 ms |
137700 KB |
Output is correct |
102 |
Correct |
2742 ms |
120380 KB |
Output is correct |
103 |
Correct |
2562 ms |
113504 KB |
Output is correct |
104 |
Correct |
2319 ms |
119972 KB |
Output is correct |
105 |
Correct |
2282 ms |
184284 KB |
Output is correct |
106 |
Correct |
1591 ms |
181628 KB |
Output is correct |
107 |
Correct |
2533 ms |
138588 KB |
Output is correct |
108 |
Correct |
1330 ms |
133388 KB |
Output is correct |
109 |
Correct |
1075 ms |
115176 KB |
Output is correct |
110 |
Correct |
1054 ms |
122036 KB |
Output is correct |
111 |
Correct |
6 ms |
10452 KB |
Output is correct |
112 |
Correct |
7 ms |
10452 KB |
Output is correct |
113 |
Correct |
8 ms |
10464 KB |
Output is correct |
114 |
Correct |
7 ms |
10468 KB |
Output is correct |
115 |
Correct |
6 ms |
10452 KB |
Output is correct |
116 |
Correct |
2605 ms |
139308 KB |
Output is correct |
117 |
Correct |
1420 ms |
139468 KB |
Output is correct |
118 |
Correct |
1936 ms |
184572 KB |
Output is correct |
119 |
Correct |
2033 ms |
139868 KB |
Output is correct |
120 |
Correct |
2762 ms |
116048 KB |
Output is correct |
121 |
Correct |
2990 ms |
117580 KB |
Output is correct |
122 |
Correct |
2426 ms |
122000 KB |
Output is correct |
123 |
Correct |
2296 ms |
182656 KB |
Output is correct |
124 |
Correct |
1623 ms |
178828 KB |
Output is correct |
125 |
Correct |
2574 ms |
138260 KB |
Output is correct |
126 |
Correct |
1418 ms |
138532 KB |
Output is correct |
127 |
Correct |
1378 ms |
140960 KB |
Output is correct |
128 |
Correct |
1153 ms |
118288 KB |
Output is correct |
129 |
Correct |
1135 ms |
124556 KB |
Output is correct |