#pragma GCC optimize("O3,unroll-loops")
#include <cstdio>
#include <vector>
int md;
inline int mul(int a, int b) {
return (long long)a * b % md;
}
const int N = 200005;
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], clz[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]) {
if (i) 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 = 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) {
if (!x) {
for (l += t.size() >> 1, r += t.size() >> 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) t[l++] = 0;
if (r & 1) t[--r] = 0;
}
return;
}
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);
clz[1] = 0;
for (int i = 2; i <= 2 * n; ++i) {
clz[i] = clz[i - 1];
while (1 << clz[i] + 1 <= i) ++clz[i];
}
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 >= std::max(0, 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 = r, 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:77:22: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
77 | while (1 << clz[i] + 1 <= i) ++clz[i];
| ~~~~~~~^~~
sprinkler.cpp:86:131: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
86 | 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:100:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
100 | int m = l + r >> 1;
| ~~^~~
sprinkler.cpp:107:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
107 | int m = l + r >> 1;
| ~~^~~
sprinkler.cpp:73:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | scanf("%d%d", &n, &md);
| ~~~~~^~~~~~~~~~~~~~~~~
sprinkler.cpp:81:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
sprinkler.cpp:87:35: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | for (int i = 0; i < n; ++i) scanf("%d", h + i);
| ~~~~~^~~~~~~~~~~~~
sprinkler.cpp:89:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
sprinkler.cpp:92:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | scanf("%d%d", &type, &v);
| ~~~~~^~~~~~~~~~~~~~~~~~~
sprinkler.cpp:96:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | scanf("%d%d", &dist, &x);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
9 ms |
14420 KB |
Output is correct |
4 |
Correct |
10 ms |
14676 KB |
Output is correct |
5 |
Correct |
10 ms |
14660 KB |
Output is correct |
6 |
Correct |
10 ms |
14548 KB |
Output is correct |
7 |
Correct |
10 ms |
14540 KB |
Output is correct |
8 |
Correct |
9 ms |
14548 KB |
Output is correct |
9 |
Correct |
8 ms |
14448 KB |
Output is correct |
10 |
Correct |
9 ms |
14420 KB |
Output is correct |
11 |
Correct |
10 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14460 KB |
Output is correct |
13 |
Correct |
8 ms |
14420 KB |
Output is correct |
14 |
Correct |
11 ms |
14368 KB |
Output is correct |
15 |
Correct |
9 ms |
14420 KB |
Output is correct |
16 |
Correct |
9 ms |
14420 KB |
Output is correct |
17 |
Correct |
9 ms |
14468 KB |
Output is correct |
18 |
Correct |
9 ms |
14420 KB |
Output is correct |
19 |
Correct |
9 ms |
14420 KB |
Output is correct |
20 |
Correct |
9 ms |
14420 KB |
Output is correct |
21 |
Correct |
9 ms |
14420 KB |
Output is correct |
22 |
Correct |
9 ms |
14420 KB |
Output is correct |
23 |
Correct |
8 ms |
14420 KB |
Output is correct |
24 |
Correct |
8 ms |
14420 KB |
Output is correct |
25 |
Correct |
8 ms |
14420 KB |
Output is correct |
26 |
Correct |
9 ms |
14368 KB |
Output is correct |
27 |
Correct |
9 ms |
14420 KB |
Output is correct |
28 |
Correct |
9 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
506 ms |
60712 KB |
Output is correct |
3 |
Correct |
892 ms |
57564 KB |
Output is correct |
4 |
Correct |
540 ms |
74292 KB |
Output is correct |
5 |
Correct |
640 ms |
59392 KB |
Output is correct |
6 |
Correct |
855 ms |
58852 KB |
Output is correct |
7 |
Correct |
853 ms |
59132 KB |
Output is correct |
8 |
Correct |
634 ms |
59744 KB |
Output is correct |
9 |
Correct |
438 ms |
85196 KB |
Output is correct |
10 |
Correct |
579 ms |
79764 KB |
Output is correct |
11 |
Correct |
427 ms |
60744 KB |
Output is correct |
12 |
Correct |
865 ms |
57736 KB |
Output is correct |
13 |
Correct |
490 ms |
58888 KB |
Output is correct |
14 |
Correct |
588 ms |
58140 KB |
Output is correct |
15 |
Correct |
654 ms |
58180 KB |
Output is correct |
16 |
Correct |
828 ms |
58180 KB |
Output is correct |
17 |
Correct |
868 ms |
58720 KB |
Output is correct |
18 |
Correct |
9 ms |
14420 KB |
Output is correct |
19 |
Correct |
10 ms |
14420 KB |
Output is correct |
20 |
Correct |
9 ms |
14420 KB |
Output is correct |
21 |
Correct |
9 ms |
14464 KB |
Output is correct |
22 |
Correct |
9 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
506 ms |
60712 KB |
Output is correct |
3 |
Correct |
892 ms |
57564 KB |
Output is correct |
4 |
Correct |
540 ms |
74292 KB |
Output is correct |
5 |
Correct |
640 ms |
59392 KB |
Output is correct |
6 |
Correct |
855 ms |
58852 KB |
Output is correct |
7 |
Correct |
853 ms |
59132 KB |
Output is correct |
8 |
Correct |
634 ms |
59744 KB |
Output is correct |
9 |
Correct |
438 ms |
85196 KB |
Output is correct |
10 |
Correct |
579 ms |
79764 KB |
Output is correct |
11 |
Correct |
427 ms |
60744 KB |
Output is correct |
12 |
Correct |
865 ms |
57736 KB |
Output is correct |
13 |
Correct |
490 ms |
58888 KB |
Output is correct |
14 |
Correct |
588 ms |
58140 KB |
Output is correct |
15 |
Correct |
654 ms |
58180 KB |
Output is correct |
16 |
Correct |
828 ms |
58180 KB |
Output is correct |
17 |
Correct |
868 ms |
58720 KB |
Output is correct |
18 |
Correct |
9 ms |
14420 KB |
Output is correct |
19 |
Correct |
10 ms |
14420 KB |
Output is correct |
20 |
Correct |
9 ms |
14420 KB |
Output is correct |
21 |
Correct |
9 ms |
14464 KB |
Output is correct |
22 |
Correct |
9 ms |
14420 KB |
Output is correct |
23 |
Correct |
8 ms |
14432 KB |
Output is correct |
24 |
Correct |
495 ms |
60756 KB |
Output is correct |
25 |
Correct |
1135 ms |
57780 KB |
Output is correct |
26 |
Correct |
516 ms |
82256 KB |
Output is correct |
27 |
Correct |
673 ms |
59168 KB |
Output is correct |
28 |
Correct |
1071 ms |
59012 KB |
Output is correct |
29 |
Correct |
983 ms |
59124 KB |
Output is correct |
30 |
Correct |
613 ms |
59776 KB |
Output is correct |
31 |
Correct |
438 ms |
76708 KB |
Output is correct |
32 |
Correct |
580 ms |
79760 KB |
Output is correct |
33 |
Correct |
457 ms |
60768 KB |
Output is correct |
34 |
Correct |
1223 ms |
57580 KB |
Output is correct |
35 |
Correct |
9 ms |
14420 KB |
Output is correct |
36 |
Correct |
9 ms |
14456 KB |
Output is correct |
37 |
Correct |
9 ms |
14420 KB |
Output is correct |
38 |
Correct |
8 ms |
14420 KB |
Output is correct |
39 |
Correct |
9 ms |
14440 KB |
Output is correct |
40 |
Correct |
11 ms |
14376 KB |
Output is correct |
41 |
Correct |
10 ms |
14420 KB |
Output is correct |
42 |
Correct |
9 ms |
14420 KB |
Output is correct |
43 |
Correct |
8 ms |
14420 KB |
Output is correct |
44 |
Correct |
9 ms |
14400 KB |
Output is correct |
45 |
Correct |
10 ms |
14444 KB |
Output is correct |
46 |
Correct |
9 ms |
14420 KB |
Output is correct |
47 |
Correct |
8 ms |
14416 KB |
Output is correct |
48 |
Correct |
9 ms |
14432 KB |
Output is correct |
49 |
Correct |
8 ms |
14380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14456 KB |
Output is correct |
2 |
Correct |
635 ms |
81944 KB |
Output is correct |
3 |
Correct |
1741 ms |
74612 KB |
Output is correct |
4 |
Correct |
831 ms |
76364 KB |
Output is correct |
5 |
Execution timed out |
4097 ms |
57536 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
612 ms |
75708 KB |
Output is correct |
3 |
Correct |
1982 ms |
68620 KB |
Output is correct |
4 |
Correct |
864 ms |
72780 KB |
Output is correct |
5 |
Correct |
3707 ms |
59212 KB |
Output is correct |
6 |
Execution timed out |
4097 ms |
59056 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
9 ms |
14420 KB |
Output is correct |
4 |
Correct |
10 ms |
14676 KB |
Output is correct |
5 |
Correct |
10 ms |
14660 KB |
Output is correct |
6 |
Correct |
10 ms |
14548 KB |
Output is correct |
7 |
Correct |
10 ms |
14540 KB |
Output is correct |
8 |
Correct |
9 ms |
14548 KB |
Output is correct |
9 |
Correct |
8 ms |
14448 KB |
Output is correct |
10 |
Correct |
9 ms |
14420 KB |
Output is correct |
11 |
Correct |
10 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14460 KB |
Output is correct |
13 |
Correct |
8 ms |
14420 KB |
Output is correct |
14 |
Correct |
11 ms |
14368 KB |
Output is correct |
15 |
Correct |
9 ms |
14420 KB |
Output is correct |
16 |
Correct |
9 ms |
14420 KB |
Output is correct |
17 |
Correct |
9 ms |
14468 KB |
Output is correct |
18 |
Correct |
9 ms |
14420 KB |
Output is correct |
19 |
Correct |
9 ms |
14420 KB |
Output is correct |
20 |
Correct |
9 ms |
14420 KB |
Output is correct |
21 |
Correct |
9 ms |
14420 KB |
Output is correct |
22 |
Correct |
9 ms |
14420 KB |
Output is correct |
23 |
Correct |
8 ms |
14420 KB |
Output is correct |
24 |
Correct |
8 ms |
14420 KB |
Output is correct |
25 |
Correct |
8 ms |
14420 KB |
Output is correct |
26 |
Correct |
9 ms |
14368 KB |
Output is correct |
27 |
Correct |
9 ms |
14420 KB |
Output is correct |
28 |
Correct |
9 ms |
14420 KB |
Output is correct |
29 |
Correct |
8 ms |
14420 KB |
Output is correct |
30 |
Correct |
506 ms |
60712 KB |
Output is correct |
31 |
Correct |
892 ms |
57564 KB |
Output is correct |
32 |
Correct |
540 ms |
74292 KB |
Output is correct |
33 |
Correct |
640 ms |
59392 KB |
Output is correct |
34 |
Correct |
855 ms |
58852 KB |
Output is correct |
35 |
Correct |
853 ms |
59132 KB |
Output is correct |
36 |
Correct |
634 ms |
59744 KB |
Output is correct |
37 |
Correct |
438 ms |
85196 KB |
Output is correct |
38 |
Correct |
579 ms |
79764 KB |
Output is correct |
39 |
Correct |
427 ms |
60744 KB |
Output is correct |
40 |
Correct |
865 ms |
57736 KB |
Output is correct |
41 |
Correct |
490 ms |
58888 KB |
Output is correct |
42 |
Correct |
588 ms |
58140 KB |
Output is correct |
43 |
Correct |
654 ms |
58180 KB |
Output is correct |
44 |
Correct |
828 ms |
58180 KB |
Output is correct |
45 |
Correct |
868 ms |
58720 KB |
Output is correct |
46 |
Correct |
9 ms |
14420 KB |
Output is correct |
47 |
Correct |
10 ms |
14420 KB |
Output is correct |
48 |
Correct |
9 ms |
14420 KB |
Output is correct |
49 |
Correct |
9 ms |
14464 KB |
Output is correct |
50 |
Correct |
9 ms |
14420 KB |
Output is correct |
51 |
Correct |
8 ms |
14432 KB |
Output is correct |
52 |
Correct |
495 ms |
60756 KB |
Output is correct |
53 |
Correct |
1135 ms |
57780 KB |
Output is correct |
54 |
Correct |
516 ms |
82256 KB |
Output is correct |
55 |
Correct |
673 ms |
59168 KB |
Output is correct |
56 |
Correct |
1071 ms |
59012 KB |
Output is correct |
57 |
Correct |
983 ms |
59124 KB |
Output is correct |
58 |
Correct |
613 ms |
59776 KB |
Output is correct |
59 |
Correct |
438 ms |
76708 KB |
Output is correct |
60 |
Correct |
580 ms |
79760 KB |
Output is correct |
61 |
Correct |
457 ms |
60768 KB |
Output is correct |
62 |
Correct |
1223 ms |
57580 KB |
Output is correct |
63 |
Correct |
9 ms |
14420 KB |
Output is correct |
64 |
Correct |
9 ms |
14456 KB |
Output is correct |
65 |
Correct |
9 ms |
14420 KB |
Output is correct |
66 |
Correct |
8 ms |
14420 KB |
Output is correct |
67 |
Correct |
9 ms |
14440 KB |
Output is correct |
68 |
Correct |
11 ms |
14376 KB |
Output is correct |
69 |
Correct |
10 ms |
14420 KB |
Output is correct |
70 |
Correct |
9 ms |
14420 KB |
Output is correct |
71 |
Correct |
8 ms |
14420 KB |
Output is correct |
72 |
Correct |
9 ms |
14400 KB |
Output is correct |
73 |
Correct |
10 ms |
14444 KB |
Output is correct |
74 |
Correct |
9 ms |
14420 KB |
Output is correct |
75 |
Correct |
8 ms |
14416 KB |
Output is correct |
76 |
Correct |
9 ms |
14432 KB |
Output is correct |
77 |
Correct |
8 ms |
14380 KB |
Output is correct |
78 |
Correct |
9 ms |
14456 KB |
Output is correct |
79 |
Correct |
635 ms |
81944 KB |
Output is correct |
80 |
Correct |
1741 ms |
74612 KB |
Output is correct |
81 |
Correct |
831 ms |
76364 KB |
Output is correct |
82 |
Execution timed out |
4097 ms |
57536 KB |
Time limit exceeded |
83 |
Halted |
0 ms |
0 KB |
- |