#include <cstdio>
#include <vector>
int md;
inline int mul(int a, int b) {
return (long long)a * b % md;
}
const int N = 200000;
const int LG = 19;
const int K = 500;
std::vector<int> g[N], list[N], t[N];
int q, n, dep[N], pr[N], pos[N], h[N], start[N], out[N], amt, st[LG][N * 2];
void dfs(int v) {
static int dt = 0;
++dt;
pos[v] = list[dep[v]].size();
list[dep[v]].push_back(v);
start[v] = amt;
st[0][amt++] = dep[v];
for (int i = 0; i < (int)g[v].size(); ++i) if (g[v][i] != pr[v]) {
st[0][amt++] = dep[v];
pr[g[v][i]] = v;
dep[g[v][i]] = dep[v] + 1;
dfs(g[v][i]);
}
out[v] = dt;
}
int l;
int dlca(int u, int v) {
u = start[u];
v = start[v];
if (u > v) std::swap(u, v);
++v;
l = 31 - __builtin_clz(v - u);
return std::min(st[l][u], st[l][v - (1 << l)]);
}
int f(int u, int v) {
return dep[u] + dep[v] - 2 * dlca(u, v);
}
void update(std::vector<int> &t, int l, int r, int x) {
for (l += t.size() >> 1, r += t.size() >> 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) {
t[l] = mul(t[l], x);
++l;
}
if (r & 1) {
--r;
t[r] = mul(t[r], x);
}
}
}
int get(std::vector<int> &t, int i) {
int res = 1;
for (i += t.size() >> 1; i > 0; i >>= 1) res = mul(res, t[i]);
return res;
}
int main() {
scanf("%d%d", &n, &md);
for (int i = 1; i < n; ++i) {
int a, b;
scanf("%d%d", &a, &b);
g[a - 1].push_back(b - 1);
g[b - 1].push_back(a - 1);
}
dfs(0);
for (int j = 1; j < LG; ++j) for (int i = 0; i + (1 << j) <= 2 * n; ++i) st[j][i] = std::min(st[j - 1][i], st[j - 1][i + (1 << j - 1)]);
for (int i = 0; i < n; ++i) scanf("%d", h + i);
for (int i = 0; i < n; ++i) t[i].resize(list[i].size() << 1, 1);
scanf("%d", &q);
while (q--) {
int type, v;
scanf("%d%d", &type, &v);
--v;
if (type == 1) {
int dist, x;
scanf("%d%d", &dist, &x);
for (int i = dep[v] + dist; i >= dep[v] - dist; --i) {
int l = 0, r = list[i].size();
while (l < r) {
int m = l + r >> 1;
if (out[list[i][m]] >= out[v] || f(v, list[i][m]) <= dist) r = m;
else l = m + 1;
}
int fi = r;
l = 0, r = list[i].size();
while (l < r) {
int m = l + r >> 1;
if (out[list[i][m]] >= out[v] && f(v, list[i][m]) > dist) r = m;
else l = m + 1;
}
update(t[i], fi, r, x);
}
} else printf("%d\n", mul(h[v], get(t[dep[v]], pos[v])));
}
return 0;
}
Compilation message
sprinkler.cpp: In function 'int main()':
sprinkler.cpp:73:131: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
73 | for (int j = 1; j < LG; ++j) for (int i = 0; i + (1 << j) <= 2 * n; ++i) st[j][i] = std::min(st[j - 1][i], st[j - 1][i + (1 << j - 1)]);
| ~~^~~
sprinkler.cpp:87:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
87 | int m = l + r >> 1;
| ~~^~~
sprinkler.cpp:94:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
94 | int m = l + r >> 1;
| ~~^~~
sprinkler.cpp:65:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | scanf("%d%d", &n, &md);
| ~~~~~^~~~~~~~~~~~~~~~~
sprinkler.cpp:68:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
sprinkler.cpp:74:35: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | for (int i = 0; i < n; ++i) scanf("%d", h + i);
| ~~~~~^~~~~~~~~~~~~
sprinkler.cpp:76:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
sprinkler.cpp:79:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | scanf("%d%d", &type, &v);
| ~~~~~^~~~~~~~~~~~~~~~~~~
sprinkler.cpp:83:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | scanf("%d%d", &dist, &x);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14448 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
8 ms |
14360 KB |
Output is correct |
4 |
Correct |
9 ms |
14676 KB |
Output is correct |
5 |
Correct |
10 ms |
14676 KB |
Output is correct |
6 |
Correct |
11 ms |
14676 KB |
Output is correct |
7 |
Correct |
10 ms |
14676 KB |
Output is correct |
8 |
Correct |
9 ms |
14676 KB |
Output is correct |
9 |
Correct |
7 ms |
14420 KB |
Output is correct |
10 |
Correct |
9 ms |
14416 KB |
Output is correct |
11 |
Correct |
9 ms |
14420 KB |
Output is correct |
12 |
Correct |
9 ms |
14468 KB |
Output is correct |
13 |
Correct |
8 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
9 ms |
14404 KB |
Output is correct |
16 |
Correct |
9 ms |
14388 KB |
Output is correct |
17 |
Correct |
11 ms |
14400 KB |
Output is correct |
18 |
Correct |
10 ms |
14404 KB |
Output is correct |
19 |
Correct |
8 ms |
14420 KB |
Output is correct |
20 |
Correct |
7 ms |
14400 KB |
Output is correct |
21 |
Correct |
8 ms |
14396 KB |
Output is correct |
22 |
Correct |
8 ms |
14388 KB |
Output is correct |
23 |
Correct |
8 ms |
14412 KB |
Output is correct |
24 |
Correct |
8 ms |
14392 KB |
Output is correct |
25 |
Correct |
8 ms |
14368 KB |
Output is correct |
26 |
Correct |
9 ms |
14408 KB |
Output is correct |
27 |
Correct |
8 ms |
14464 KB |
Output is correct |
28 |
Correct |
8 ms |
14420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14324 KB |
Output is correct |
2 |
Correct |
484 ms |
59944 KB |
Output is correct |
3 |
Correct |
951 ms |
56524 KB |
Output is correct |
4 |
Correct |
531 ms |
71416 KB |
Output is correct |
5 |
Correct |
650 ms |
58160 KB |
Output is correct |
6 |
Correct |
777 ms |
57792 KB |
Output is correct |
7 |
Correct |
798 ms |
58020 KB |
Output is correct |
8 |
Correct |
659 ms |
58548 KB |
Output is correct |
9 |
Correct |
454 ms |
81208 KB |
Output is correct |
10 |
Correct |
564 ms |
76108 KB |
Output is correct |
11 |
Correct |
486 ms |
59852 KB |
Output is correct |
12 |
Correct |
956 ms |
56672 KB |
Output is correct |
13 |
Correct |
548 ms |
57516 KB |
Output is correct |
14 |
Correct |
633 ms |
56880 KB |
Output is correct |
15 |
Correct |
703 ms |
56860 KB |
Output is correct |
16 |
Correct |
756 ms |
56676 KB |
Output is correct |
17 |
Correct |
839 ms |
57200 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
9 ms |
14420 KB |
Output is correct |
20 |
Correct |
10 ms |
14400 KB |
Output is correct |
21 |
Correct |
9 ms |
14376 KB |
Output is correct |
22 |
Correct |
9 ms |
14396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14324 KB |
Output is correct |
2 |
Correct |
484 ms |
59944 KB |
Output is correct |
3 |
Correct |
951 ms |
56524 KB |
Output is correct |
4 |
Correct |
531 ms |
71416 KB |
Output is correct |
5 |
Correct |
650 ms |
58160 KB |
Output is correct |
6 |
Correct |
777 ms |
57792 KB |
Output is correct |
7 |
Correct |
798 ms |
58020 KB |
Output is correct |
8 |
Correct |
659 ms |
58548 KB |
Output is correct |
9 |
Correct |
454 ms |
81208 KB |
Output is correct |
10 |
Correct |
564 ms |
76108 KB |
Output is correct |
11 |
Correct |
486 ms |
59852 KB |
Output is correct |
12 |
Correct |
956 ms |
56672 KB |
Output is correct |
13 |
Correct |
548 ms |
57516 KB |
Output is correct |
14 |
Correct |
633 ms |
56880 KB |
Output is correct |
15 |
Correct |
703 ms |
56860 KB |
Output is correct |
16 |
Correct |
756 ms |
56676 KB |
Output is correct |
17 |
Correct |
839 ms |
57200 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
9 ms |
14420 KB |
Output is correct |
20 |
Correct |
10 ms |
14400 KB |
Output is correct |
21 |
Correct |
9 ms |
14376 KB |
Output is correct |
22 |
Correct |
9 ms |
14396 KB |
Output is correct |
23 |
Correct |
8 ms |
14420 KB |
Output is correct |
24 |
Correct |
501 ms |
59740 KB |
Output is correct |
25 |
Correct |
1312 ms |
56572 KB |
Output is correct |
26 |
Correct |
620 ms |
78536 KB |
Output is correct |
27 |
Correct |
836 ms |
58220 KB |
Output is correct |
28 |
Correct |
1055 ms |
57980 KB |
Output is correct |
29 |
Correct |
1064 ms |
58156 KB |
Output is correct |
30 |
Correct |
673 ms |
58616 KB |
Output is correct |
31 |
Correct |
515 ms |
73652 KB |
Output is correct |
32 |
Correct |
700 ms |
76204 KB |
Output is correct |
33 |
Correct |
567 ms |
59720 KB |
Output is correct |
34 |
Correct |
1512 ms |
56596 KB |
Output is correct |
35 |
Correct |
9 ms |
14420 KB |
Output is correct |
36 |
Correct |
8 ms |
14396 KB |
Output is correct |
37 |
Correct |
8 ms |
14416 KB |
Output is correct |
38 |
Correct |
8 ms |
14400 KB |
Output is correct |
39 |
Correct |
10 ms |
14420 KB |
Output is correct |
40 |
Correct |
10 ms |
14424 KB |
Output is correct |
41 |
Correct |
9 ms |
14420 KB |
Output is correct |
42 |
Correct |
9 ms |
14420 KB |
Output is correct |
43 |
Correct |
9 ms |
14420 KB |
Output is correct |
44 |
Correct |
10 ms |
14444 KB |
Output is correct |
45 |
Correct |
8 ms |
14396 KB |
Output is correct |
46 |
Correct |
9 ms |
14452 KB |
Output is correct |
47 |
Correct |
8 ms |
14392 KB |
Output is correct |
48 |
Correct |
9 ms |
14420 KB |
Output is correct |
49 |
Correct |
8 ms |
14420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
723 ms |
77996 KB |
Output is correct |
3 |
Correct |
2585 ms |
71936 KB |
Output is correct |
4 |
Correct |
1039 ms |
73072 KB |
Output is correct |
5 |
Execution timed out |
4054 ms |
56504 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14420 KB |
Output is correct |
2 |
Correct |
733 ms |
72744 KB |
Output is correct |
3 |
Correct |
2127 ms |
66524 KB |
Output is correct |
4 |
Correct |
1039 ms |
70160 KB |
Output is correct |
5 |
Correct |
3712 ms |
58776 KB |
Output is correct |
6 |
Correct |
3171 ms |
65884 KB |
Output is correct |
7 |
Correct |
2358 ms |
65568 KB |
Output is correct |
8 |
Correct |
486 ms |
65976 KB |
Output is correct |
9 |
Correct |
589 ms |
86588 KB |
Output is correct |
10 |
Correct |
1684 ms |
84692 KB |
Output is correct |
11 |
Correct |
1817 ms |
66568 KB |
Output is correct |
12 |
Execution timed out |
4038 ms |
61776 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14448 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
8 ms |
14360 KB |
Output is correct |
4 |
Correct |
9 ms |
14676 KB |
Output is correct |
5 |
Correct |
10 ms |
14676 KB |
Output is correct |
6 |
Correct |
11 ms |
14676 KB |
Output is correct |
7 |
Correct |
10 ms |
14676 KB |
Output is correct |
8 |
Correct |
9 ms |
14676 KB |
Output is correct |
9 |
Correct |
7 ms |
14420 KB |
Output is correct |
10 |
Correct |
9 ms |
14416 KB |
Output is correct |
11 |
Correct |
9 ms |
14420 KB |
Output is correct |
12 |
Correct |
9 ms |
14468 KB |
Output is correct |
13 |
Correct |
8 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
9 ms |
14404 KB |
Output is correct |
16 |
Correct |
9 ms |
14388 KB |
Output is correct |
17 |
Correct |
11 ms |
14400 KB |
Output is correct |
18 |
Correct |
10 ms |
14404 KB |
Output is correct |
19 |
Correct |
8 ms |
14420 KB |
Output is correct |
20 |
Correct |
7 ms |
14400 KB |
Output is correct |
21 |
Correct |
8 ms |
14396 KB |
Output is correct |
22 |
Correct |
8 ms |
14388 KB |
Output is correct |
23 |
Correct |
8 ms |
14412 KB |
Output is correct |
24 |
Correct |
8 ms |
14392 KB |
Output is correct |
25 |
Correct |
8 ms |
14368 KB |
Output is correct |
26 |
Correct |
9 ms |
14408 KB |
Output is correct |
27 |
Correct |
8 ms |
14464 KB |
Output is correct |
28 |
Correct |
8 ms |
14420 KB |
Output is correct |
29 |
Correct |
8 ms |
14324 KB |
Output is correct |
30 |
Correct |
484 ms |
59944 KB |
Output is correct |
31 |
Correct |
951 ms |
56524 KB |
Output is correct |
32 |
Correct |
531 ms |
71416 KB |
Output is correct |
33 |
Correct |
650 ms |
58160 KB |
Output is correct |
34 |
Correct |
777 ms |
57792 KB |
Output is correct |
35 |
Correct |
798 ms |
58020 KB |
Output is correct |
36 |
Correct |
659 ms |
58548 KB |
Output is correct |
37 |
Correct |
454 ms |
81208 KB |
Output is correct |
38 |
Correct |
564 ms |
76108 KB |
Output is correct |
39 |
Correct |
486 ms |
59852 KB |
Output is correct |
40 |
Correct |
956 ms |
56672 KB |
Output is correct |
41 |
Correct |
548 ms |
57516 KB |
Output is correct |
42 |
Correct |
633 ms |
56880 KB |
Output is correct |
43 |
Correct |
703 ms |
56860 KB |
Output is correct |
44 |
Correct |
756 ms |
56676 KB |
Output is correct |
45 |
Correct |
839 ms |
57200 KB |
Output is correct |
46 |
Correct |
8 ms |
14420 KB |
Output is correct |
47 |
Correct |
9 ms |
14420 KB |
Output is correct |
48 |
Correct |
10 ms |
14400 KB |
Output is correct |
49 |
Correct |
9 ms |
14376 KB |
Output is correct |
50 |
Correct |
9 ms |
14396 KB |
Output is correct |
51 |
Correct |
8 ms |
14420 KB |
Output is correct |
52 |
Correct |
501 ms |
59740 KB |
Output is correct |
53 |
Correct |
1312 ms |
56572 KB |
Output is correct |
54 |
Correct |
620 ms |
78536 KB |
Output is correct |
55 |
Correct |
836 ms |
58220 KB |
Output is correct |
56 |
Correct |
1055 ms |
57980 KB |
Output is correct |
57 |
Correct |
1064 ms |
58156 KB |
Output is correct |
58 |
Correct |
673 ms |
58616 KB |
Output is correct |
59 |
Correct |
515 ms |
73652 KB |
Output is correct |
60 |
Correct |
700 ms |
76204 KB |
Output is correct |
61 |
Correct |
567 ms |
59720 KB |
Output is correct |
62 |
Correct |
1512 ms |
56596 KB |
Output is correct |
63 |
Correct |
9 ms |
14420 KB |
Output is correct |
64 |
Correct |
8 ms |
14396 KB |
Output is correct |
65 |
Correct |
8 ms |
14416 KB |
Output is correct |
66 |
Correct |
8 ms |
14400 KB |
Output is correct |
67 |
Correct |
10 ms |
14420 KB |
Output is correct |
68 |
Correct |
10 ms |
14424 KB |
Output is correct |
69 |
Correct |
9 ms |
14420 KB |
Output is correct |
70 |
Correct |
9 ms |
14420 KB |
Output is correct |
71 |
Correct |
9 ms |
14420 KB |
Output is correct |
72 |
Correct |
10 ms |
14444 KB |
Output is correct |
73 |
Correct |
8 ms |
14396 KB |
Output is correct |
74 |
Correct |
9 ms |
14452 KB |
Output is correct |
75 |
Correct |
8 ms |
14392 KB |
Output is correct |
76 |
Correct |
9 ms |
14420 KB |
Output is correct |
77 |
Correct |
8 ms |
14420 KB |
Output is correct |
78 |
Correct |
8 ms |
14420 KB |
Output is correct |
79 |
Correct |
723 ms |
77996 KB |
Output is correct |
80 |
Correct |
2585 ms |
71936 KB |
Output is correct |
81 |
Correct |
1039 ms |
73072 KB |
Output is correct |
82 |
Execution timed out |
4054 ms |
56504 KB |
Time limit exceeded |
83 |
Halted |
0 ms |
0 KB |
- |