#include <bits/stdc++.h>
#define mp make_pair
#define all(a) a.begin(),a.end()
using namespace std;
using pii = pair <long long, long long>;
constexpr int maxn = 2e5 + 10;
constexpr int mod = 1000002022;
constexpr long long inf = 1e17 + 10;
vector <int> graph [maxn];
long long sub_size [maxn], ans [maxn];
long long x [maxn], tree [maxn << 2], S [maxn << 2];
bool R [maxn << 2];
int n, m;
inline void build (int v, int t_l, int t_r) {
if (t_l == t_r) {
S[v] = ans[t_l];
return;
}
int t_m = t_l + t_r >> 1;
build (v << 1, t_l, t_m);
build (v << 1 | 1, t_m + 1, t_r);
S[v] = (S[v << 1] + S[v << 1 | 1]) % mod;
}
inline void push (int v, int t_l, int t_r) {
if (!R[v]) return;
tree[v] = (S[v] - tree[v] + mod) % mod;
if (t_l < t_r) {
R[v << 1] ^= 1;
R[v << 1 | 1] ^= 1;
}
R[v] = false;
}
inline void update (int l, int r, int v, int t_l, int t_r) {
push (v, t_l, t_r);
if (l > t_r || t_l > r) return;
if (l <= t_l && t_r <= r) {
R[v] = true;
push (v, t_l, t_r);
return;
}
int t_m = t_l + t_r >> 1;
update (l, r, v << 1, t_l, t_m);
update (l, r, v << 1 | 1, t_m + 1, t_r);
tree[v] = (tree[v << 1] + tree[v << 1 | 1]) % mod;
}
inline void dfs0 (int v) {
sub_size[v] = max (1LL, (long long) graph[v].size ());
for (int i : graph[v]) {
dfs0 (i);
sub_size[v] = (sub_size[v] * sub_size[i]) % mod;
}
}
inline void dfs1 (int v, long long cur) {
if (graph[v].empty ()) return;
x[v] = cur;
for (int i : graph[v]) x[v] = (x[v] * sub_size[i]) % mod;
vector <long long> w0 ((int) graph[v].size ());
vector <long long> w1 ((int) graph[v].size ());
w0[0] = 1;
for (int i = 1; i < (int) graph[v].size (); ++i)
w0[i] = (w0[i - 1] * sub_size[graph[v][i - 1]]) % mod;
w1[(int) graph[v].size () - 1] = 1;
for (int i = (int) graph[v].size () - 2; i >= 0; --i)
w1[i] = (w1[i + 1] * sub_size[graph[v][i + 1]]) % mod;
int d = 0;
for (int i : graph[v]) {
long long cur0 = cur;
cur0 = (cur0 * w0[d]) % mod;
cur0 = (cur0 * w1[d]) % mod;
++d;
dfs1 (i, cur0);
}
}
void init (int N, int M, vector <int> P, vector <int> A) {
n = N, m = M;
for (int i = 1; i < n + m; ++i) graph[P[i]].push_back (i);
dfs0 (0);
dfs1 (0, 1);
for (int i = n; i < n + m; ++i) ans[i] = x[P[i]];
build (1, n, n + m - 1);
for (int i = 0; i < m; ++i) if (A[i]) update (i + n, i + n, 1, n, n + m - 1);
}
int count_ways (int L, int R) {
update (L, R, 1, n, n + m - 1);
return tree[1];
}
Compilation message
circuit.cpp: In function 'void build(int, int, int)':
circuit.cpp:21:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
21 | int t_m = t_l + t_r >> 1;
| ~~~~^~~~~
circuit.cpp: In function 'void update(int, int, int, int, int)':
circuit.cpp:45:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | int t_m = t_l + t_r >> 1;
| ~~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
4 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
5072 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
5072 KB |
Output is correct |
10 |
Correct |
3 ms |
5328 KB |
Output is correct |
11 |
Correct |
3 ms |
5328 KB |
Output is correct |
12 |
Correct |
4 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
4 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
5072 KB |
Output is correct |
11 |
Correct |
3 ms |
5072 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
3 ms |
5072 KB |
Output is correct |
14 |
Correct |
3 ms |
5072 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5072 KB |
Output is correct |
17 |
Correct |
3 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5328 KB |
Output is correct |
19 |
Correct |
3 ms |
5328 KB |
Output is correct |
20 |
Correct |
4 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
5072 KB |
Output is correct |
24 |
Correct |
3 ms |
5072 KB |
Output is correct |
25 |
Correct |
4 ms |
5072 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
3 ms |
5072 KB |
Output is correct |
28 |
Correct |
3 ms |
5072 KB |
Output is correct |
29 |
Correct |
3 ms |
5072 KB |
Output is correct |
30 |
Correct |
3 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5056 KB |
Output is correct |
33 |
Correct |
3 ms |
5072 KB |
Output is correct |
34 |
Correct |
3 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5072 KB |
Output is correct |
36 |
Correct |
5 ms |
5328 KB |
Output is correct |
37 |
Correct |
3 ms |
5328 KB |
Output is correct |
38 |
Correct |
4 ms |
5328 KB |
Output is correct |
39 |
Correct |
4 ms |
5072 KB |
Output is correct |
40 |
Correct |
4 ms |
5072 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
706 ms |
8864 KB |
Output is correct |
2 |
Correct |
804 ms |
12696 KB |
Output is correct |
3 |
Correct |
969 ms |
12748 KB |
Output is correct |
4 |
Correct |
934 ms |
12032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
706 ms |
8864 KB |
Output is correct |
2 |
Correct |
804 ms |
12696 KB |
Output is correct |
3 |
Correct |
969 ms |
12748 KB |
Output is correct |
4 |
Correct |
934 ms |
12032 KB |
Output is correct |
5 |
Correct |
671 ms |
8912 KB |
Output is correct |
6 |
Correct |
924 ms |
12680 KB |
Output is correct |
7 |
Correct |
906 ms |
12768 KB |
Output is correct |
8 |
Correct |
868 ms |
12684 KB |
Output is correct |
9 |
Correct |
496 ms |
5200 KB |
Output is correct |
10 |
Correct |
909 ms |
5456 KB |
Output is correct |
11 |
Correct |
846 ms |
5584 KB |
Output is correct |
12 |
Correct |
710 ms |
5456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
5072 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
5072 KB |
Output is correct |
10 |
Correct |
3 ms |
5328 KB |
Output is correct |
11 |
Correct |
3 ms |
5328 KB |
Output is correct |
12 |
Correct |
4 ms |
5072 KB |
Output is correct |
13 |
Correct |
706 ms |
8864 KB |
Output is correct |
14 |
Correct |
804 ms |
12696 KB |
Output is correct |
15 |
Correct |
969 ms |
12748 KB |
Output is correct |
16 |
Correct |
934 ms |
12032 KB |
Output is correct |
17 |
Correct |
671 ms |
8912 KB |
Output is correct |
18 |
Correct |
924 ms |
12680 KB |
Output is correct |
19 |
Correct |
906 ms |
12768 KB |
Output is correct |
20 |
Correct |
868 ms |
12684 KB |
Output is correct |
21 |
Correct |
496 ms |
5200 KB |
Output is correct |
22 |
Correct |
909 ms |
5456 KB |
Output is correct |
23 |
Correct |
846 ms |
5584 KB |
Output is correct |
24 |
Correct |
710 ms |
5456 KB |
Output is correct |
25 |
Correct |
1035 ms |
17796 KB |
Output is correct |
26 |
Correct |
857 ms |
17920 KB |
Output is correct |
27 |
Correct |
1114 ms |
17816 KB |
Output is correct |
28 |
Correct |
794 ms |
17832 KB |
Output is correct |
29 |
Correct |
1060 ms |
38260 KB |
Output is correct |
30 |
Correct |
929 ms |
38176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
4 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
5072 KB |
Output is correct |
11 |
Correct |
3 ms |
5072 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
3 ms |
5072 KB |
Output is correct |
14 |
Correct |
3 ms |
5072 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5072 KB |
Output is correct |
17 |
Correct |
3 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5328 KB |
Output is correct |
19 |
Correct |
3 ms |
5328 KB |
Output is correct |
20 |
Correct |
4 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
5072 KB |
Output is correct |
24 |
Correct |
3 ms |
5072 KB |
Output is correct |
25 |
Correct |
4 ms |
5072 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
3 ms |
5072 KB |
Output is correct |
28 |
Correct |
3 ms |
5072 KB |
Output is correct |
29 |
Correct |
3 ms |
5072 KB |
Output is correct |
30 |
Correct |
3 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5056 KB |
Output is correct |
33 |
Correct |
3 ms |
5072 KB |
Output is correct |
34 |
Correct |
3 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5072 KB |
Output is correct |
36 |
Correct |
5 ms |
5328 KB |
Output is correct |
37 |
Correct |
3 ms |
5328 KB |
Output is correct |
38 |
Correct |
4 ms |
5328 KB |
Output is correct |
39 |
Correct |
4 ms |
5072 KB |
Output is correct |
40 |
Correct |
4 ms |
5072 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
3 ms |
5072 KB |
Output is correct |
43 |
Correct |
672 ms |
5328 KB |
Output is correct |
44 |
Correct |
893 ms |
5456 KB |
Output is correct |
45 |
Correct |
843 ms |
5328 KB |
Output is correct |
46 |
Correct |
892 ms |
5712 KB |
Output is correct |
47 |
Correct |
858 ms |
5712 KB |
Output is correct |
48 |
Correct |
759 ms |
5712 KB |
Output is correct |
49 |
Correct |
870 ms |
5712 KB |
Output is correct |
50 |
Correct |
803 ms |
5712 KB |
Output is correct |
51 |
Correct |
837 ms |
5584 KB |
Output is correct |
52 |
Correct |
924 ms |
5584 KB |
Output is correct |
53 |
Correct |
690 ms |
6224 KB |
Output is correct |
54 |
Correct |
829 ms |
5712 KB |
Output is correct |
55 |
Correct |
933 ms |
5584 KB |
Output is correct |
56 |
Correct |
839 ms |
5456 KB |
Output is correct |
57 |
Correct |
695 ms |
5456 KB |
Output is correct |
58 |
Correct |
819 ms |
6736 KB |
Output is correct |
59 |
Correct |
811 ms |
6864 KB |
Output is correct |
60 |
Correct |
865 ms |
6736 KB |
Output is correct |
61 |
Correct |
919 ms |
5584 KB |
Output is correct |
62 |
Correct |
853 ms |
5328 KB |
Output is correct |
63 |
Correct |
865 ms |
5328 KB |
Output is correct |
64 |
Correct |
653 ms |
5456 KB |
Output is correct |
65 |
Correct |
473 ms |
5200 KB |
Output is correct |
66 |
Correct |
721 ms |
5456 KB |
Output is correct |
67 |
Correct |
687 ms |
5456 KB |
Output is correct |
68 |
Correct |
820 ms |
5456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
4 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
5072 KB |
Output is correct |
11 |
Correct |
3 ms |
5072 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
3 ms |
5072 KB |
Output is correct |
14 |
Correct |
3 ms |
5072 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5072 KB |
Output is correct |
17 |
Correct |
3 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5328 KB |
Output is correct |
19 |
Correct |
3 ms |
5328 KB |
Output is correct |
20 |
Correct |
4 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
5072 KB |
Output is correct |
24 |
Correct |
3 ms |
5072 KB |
Output is correct |
25 |
Correct |
4 ms |
5072 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
3 ms |
5072 KB |
Output is correct |
28 |
Correct |
3 ms |
5072 KB |
Output is correct |
29 |
Correct |
3 ms |
5072 KB |
Output is correct |
30 |
Correct |
3 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5056 KB |
Output is correct |
33 |
Correct |
3 ms |
5072 KB |
Output is correct |
34 |
Correct |
3 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5072 KB |
Output is correct |
36 |
Correct |
5 ms |
5328 KB |
Output is correct |
37 |
Correct |
3 ms |
5328 KB |
Output is correct |
38 |
Correct |
4 ms |
5328 KB |
Output is correct |
39 |
Correct |
4 ms |
5072 KB |
Output is correct |
40 |
Correct |
4 ms |
5072 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
3 ms |
5072 KB |
Output is correct |
43 |
Correct |
706 ms |
8864 KB |
Output is correct |
44 |
Correct |
804 ms |
12696 KB |
Output is correct |
45 |
Correct |
969 ms |
12748 KB |
Output is correct |
46 |
Correct |
934 ms |
12032 KB |
Output is correct |
47 |
Correct |
671 ms |
8912 KB |
Output is correct |
48 |
Correct |
924 ms |
12680 KB |
Output is correct |
49 |
Correct |
906 ms |
12768 KB |
Output is correct |
50 |
Correct |
868 ms |
12684 KB |
Output is correct |
51 |
Correct |
496 ms |
5200 KB |
Output is correct |
52 |
Correct |
909 ms |
5456 KB |
Output is correct |
53 |
Correct |
846 ms |
5584 KB |
Output is correct |
54 |
Correct |
710 ms |
5456 KB |
Output is correct |
55 |
Correct |
1035 ms |
17796 KB |
Output is correct |
56 |
Correct |
857 ms |
17920 KB |
Output is correct |
57 |
Correct |
1114 ms |
17816 KB |
Output is correct |
58 |
Correct |
794 ms |
17832 KB |
Output is correct |
59 |
Correct |
1060 ms |
38260 KB |
Output is correct |
60 |
Correct |
929 ms |
38176 KB |
Output is correct |
61 |
Correct |
672 ms |
5328 KB |
Output is correct |
62 |
Correct |
893 ms |
5456 KB |
Output is correct |
63 |
Correct |
843 ms |
5328 KB |
Output is correct |
64 |
Correct |
892 ms |
5712 KB |
Output is correct |
65 |
Correct |
858 ms |
5712 KB |
Output is correct |
66 |
Correct |
759 ms |
5712 KB |
Output is correct |
67 |
Correct |
870 ms |
5712 KB |
Output is correct |
68 |
Correct |
803 ms |
5712 KB |
Output is correct |
69 |
Correct |
837 ms |
5584 KB |
Output is correct |
70 |
Correct |
924 ms |
5584 KB |
Output is correct |
71 |
Correct |
690 ms |
6224 KB |
Output is correct |
72 |
Correct |
829 ms |
5712 KB |
Output is correct |
73 |
Correct |
933 ms |
5584 KB |
Output is correct |
74 |
Correct |
839 ms |
5456 KB |
Output is correct |
75 |
Correct |
695 ms |
5456 KB |
Output is correct |
76 |
Correct |
819 ms |
6736 KB |
Output is correct |
77 |
Correct |
811 ms |
6864 KB |
Output is correct |
78 |
Correct |
865 ms |
6736 KB |
Output is correct |
79 |
Correct |
919 ms |
5584 KB |
Output is correct |
80 |
Correct |
853 ms |
5328 KB |
Output is correct |
81 |
Correct |
865 ms |
5328 KB |
Output is correct |
82 |
Correct |
653 ms |
5456 KB |
Output is correct |
83 |
Correct |
473 ms |
5200 KB |
Output is correct |
84 |
Correct |
721 ms |
5456 KB |
Output is correct |
85 |
Correct |
687 ms |
5456 KB |
Output is correct |
86 |
Correct |
820 ms |
5456 KB |
Output is correct |
87 |
Correct |
3 ms |
4944 KB |
Output is correct |
88 |
Correct |
661 ms |
17008 KB |
Output is correct |
89 |
Correct |
997 ms |
13024 KB |
Output is correct |
90 |
Correct |
911 ms |
12632 KB |
Output is correct |
91 |
Correct |
975 ms |
17956 KB |
Output is correct |
92 |
Correct |
1078 ms |
17940 KB |
Output is correct |
93 |
Correct |
970 ms |
16940 KB |
Output is correct |
94 |
Correct |
1020 ms |
18180 KB |
Output is correct |
95 |
Correct |
1055 ms |
17920 KB |
Output is correct |
96 |
Correct |
879 ms |
12960 KB |
Output is correct |
97 |
Correct |
964 ms |
12920 KB |
Output is correct |
98 |
Correct |
782 ms |
30744 KB |
Output is correct |
99 |
Correct |
889 ms |
17916 KB |
Output is correct |
100 |
Correct |
958 ms |
15088 KB |
Output is correct |
101 |
Correct |
782 ms |
14336 KB |
Output is correct |
102 |
Correct |
932 ms |
12968 KB |
Output is correct |
103 |
Correct |
895 ms |
38168 KB |
Output is correct |
104 |
Correct |
920 ms |
38652 KB |
Output is correct |
105 |
Correct |
827 ms |
38640 KB |
Output is correct |
106 |
Correct |
950 ms |
17136 KB |
Output is correct |
107 |
Correct |
999 ms |
13636 KB |
Output is correct |
108 |
Correct |
1031 ms |
13440 KB |
Output is correct |
109 |
Correct |
1033 ms |
13140 KB |
Output is correct |