#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) {
x[v] = cur;
for (int i : graph[v]) x[v] = (x[v] * sub_size[i]) % mod;
for (int i : graph[v]) {
long long cur0 = cur;
for (int j : graph[v]) if (i != j) cur0 = (cur0 * sub_size[j]) % mod;
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;
| ~~~~^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
9 ms |
5072 KB |
Output is correct |
4 |
Correct |
8 ms |
5116 KB |
Output is correct |
5 |
Correct |
7 ms |
5072 KB |
Output is correct |
6 |
Correct |
8 ms |
5072 KB |
Output is correct |
7 |
Correct |
9 ms |
5052 KB |
Output is correct |
8 |
Correct |
7 ms |
5056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
5072 KB |
Output is correct |
3 |
Correct |
3 ms |
5160 KB |
Output is correct |
4 |
Correct |
4 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
4 ms |
5072 KB |
Output is correct |
7 |
Correct |
5 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
4 ms |
5048 KB |
Output is correct |
10 |
Correct |
4 ms |
5072 KB |
Output is correct |
11 |
Correct |
3 ms |
5072 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
9 ms |
5072 KB |
Output is correct |
4 |
Correct |
8 ms |
5116 KB |
Output is correct |
5 |
Correct |
7 ms |
5072 KB |
Output is correct |
6 |
Correct |
8 ms |
5072 KB |
Output is correct |
7 |
Correct |
9 ms |
5052 KB |
Output is correct |
8 |
Correct |
7 ms |
5056 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
5072 KB |
Output is correct |
11 |
Correct |
3 ms |
5160 KB |
Output is correct |
12 |
Correct |
4 ms |
5072 KB |
Output is correct |
13 |
Correct |
3 ms |
5072 KB |
Output is correct |
14 |
Correct |
4 ms |
5072 KB |
Output is correct |
15 |
Correct |
5 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5072 KB |
Output is correct |
17 |
Correct |
4 ms |
5048 KB |
Output is correct |
18 |
Correct |
4 ms |
5072 KB |
Output is correct |
19 |
Correct |
3 ms |
5072 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
4 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 |
3 ms |
5072 KB |
Output is correct |
26 |
Correct |
5 ms |
5072 KB |
Output is correct |
27 |
Correct |
5 ms |
5136 KB |
Output is correct |
28 |
Correct |
3 ms |
5072 KB |
Output is correct |
29 |
Correct |
7 ms |
5072 KB |
Output is correct |
30 |
Correct |
7 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5072 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 KB |
Output is correct |
33 |
Correct |
3 ms |
5072 KB |
Output is correct |
34 |
Correct |
3 ms |
5072 KB |
Output is correct |
35 |
Correct |
4 ms |
5072 KB |
Output is correct |
36 |
Correct |
3 ms |
5072 KB |
Output is correct |
37 |
Correct |
7 ms |
5200 KB |
Output is correct |
38 |
Correct |
7 ms |
5200 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
5 ms |
5072 KB |
Output is correct |
42 |
Correct |
4 ms |
5072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
687 ms |
9168 KB |
Output is correct |
2 |
Correct |
966 ms |
13196 KB |
Output is correct |
3 |
Correct |
976 ms |
13188 KB |
Output is correct |
4 |
Correct |
897 ms |
12540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
687 ms |
9168 KB |
Output is correct |
2 |
Correct |
966 ms |
13196 KB |
Output is correct |
3 |
Correct |
976 ms |
13188 KB |
Output is correct |
4 |
Correct |
897 ms |
12540 KB |
Output is correct |
5 |
Correct |
793 ms |
9112 KB |
Output is correct |
6 |
Correct |
1070 ms |
13200 KB |
Output is correct |
7 |
Correct |
843 ms |
13196 KB |
Output is correct |
8 |
Correct |
995 ms |
13256 KB |
Output is correct |
9 |
Correct |
429 ms |
5240 KB |
Output is correct |
10 |
Correct |
892 ms |
5456 KB |
Output is correct |
11 |
Correct |
679 ms |
5456 KB |
Output is correct |
12 |
Correct |
790 ms |
5456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
5072 KB |
Output is correct |
3 |
Correct |
3 ms |
5160 KB |
Output is correct |
4 |
Correct |
4 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
4 ms |
5072 KB |
Output is correct |
7 |
Correct |
5 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
4 ms |
5048 KB |
Output is correct |
10 |
Correct |
4 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 |
687 ms |
9168 KB |
Output is correct |
14 |
Correct |
966 ms |
13196 KB |
Output is correct |
15 |
Correct |
976 ms |
13188 KB |
Output is correct |
16 |
Correct |
897 ms |
12540 KB |
Output is correct |
17 |
Correct |
793 ms |
9112 KB |
Output is correct |
18 |
Correct |
1070 ms |
13200 KB |
Output is correct |
19 |
Correct |
843 ms |
13196 KB |
Output is correct |
20 |
Correct |
995 ms |
13256 KB |
Output is correct |
21 |
Correct |
429 ms |
5240 KB |
Output is correct |
22 |
Correct |
892 ms |
5456 KB |
Output is correct |
23 |
Correct |
679 ms |
5456 KB |
Output is correct |
24 |
Correct |
790 ms |
5456 KB |
Output is correct |
25 |
Correct |
1093 ms |
18432 KB |
Output is correct |
26 |
Correct |
1092 ms |
18664 KB |
Output is correct |
27 |
Correct |
888 ms |
18628 KB |
Output is correct |
28 |
Correct |
829 ms |
18728 KB |
Output is correct |
29 |
Correct |
961 ms |
22428 KB |
Output is correct |
30 |
Correct |
956 ms |
22260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
9 ms |
5072 KB |
Output is correct |
4 |
Correct |
8 ms |
5116 KB |
Output is correct |
5 |
Correct |
7 ms |
5072 KB |
Output is correct |
6 |
Correct |
8 ms |
5072 KB |
Output is correct |
7 |
Correct |
9 ms |
5052 KB |
Output is correct |
8 |
Correct |
7 ms |
5056 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
5072 KB |
Output is correct |
11 |
Correct |
3 ms |
5160 KB |
Output is correct |
12 |
Correct |
4 ms |
5072 KB |
Output is correct |
13 |
Correct |
3 ms |
5072 KB |
Output is correct |
14 |
Correct |
4 ms |
5072 KB |
Output is correct |
15 |
Correct |
5 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5072 KB |
Output is correct |
17 |
Correct |
4 ms |
5048 KB |
Output is correct |
18 |
Correct |
4 ms |
5072 KB |
Output is correct |
19 |
Correct |
3 ms |
5072 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
4 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 |
3 ms |
5072 KB |
Output is correct |
26 |
Correct |
5 ms |
5072 KB |
Output is correct |
27 |
Correct |
5 ms |
5136 KB |
Output is correct |
28 |
Correct |
3 ms |
5072 KB |
Output is correct |
29 |
Correct |
7 ms |
5072 KB |
Output is correct |
30 |
Correct |
7 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5072 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 KB |
Output is correct |
33 |
Correct |
3 ms |
5072 KB |
Output is correct |
34 |
Correct |
3 ms |
5072 KB |
Output is correct |
35 |
Correct |
4 ms |
5072 KB |
Output is correct |
36 |
Correct |
3 ms |
5072 KB |
Output is correct |
37 |
Correct |
7 ms |
5200 KB |
Output is correct |
38 |
Correct |
7 ms |
5200 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
5 ms |
5072 KB |
Output is correct |
42 |
Correct |
4 ms |
5072 KB |
Output is correct |
43 |
Correct |
588 ms |
5352 KB |
Output is correct |
44 |
Correct |
721 ms |
5456 KB |
Output is correct |
45 |
Correct |
981 ms |
5456 KB |
Output is correct |
46 |
Correct |
849 ms |
5712 KB |
Output is correct |
47 |
Correct |
747 ms |
5780 KB |
Output is correct |
48 |
Correct |
1002 ms |
5752 KB |
Output is correct |
49 |
Correct |
808 ms |
5712 KB |
Output is correct |
50 |
Correct |
911 ms |
5712 KB |
Output is correct |
51 |
Correct |
1012 ms |
5576 KB |
Output is correct |
52 |
Correct |
1053 ms |
5564 KB |
Output is correct |
53 |
Correct |
843 ms |
5456 KB |
Output is correct |
54 |
Correct |
858 ms |
5712 KB |
Output is correct |
55 |
Correct |
1035 ms |
5584 KB |
Output is correct |
56 |
Correct |
832 ms |
5584 KB |
Output is correct |
57 |
Correct |
1086 ms |
5456 KB |
Output is correct |
58 |
Correct |
864 ms |
5956 KB |
Output is correct |
59 |
Correct |
1057 ms |
5996 KB |
Output is correct |
60 |
Correct |
954 ms |
5996 KB |
Output is correct |
61 |
Correct |
989 ms |
5456 KB |
Output is correct |
62 |
Correct |
1006 ms |
5344 KB |
Output is correct |
63 |
Correct |
799 ms |
5328 KB |
Output is correct |
64 |
Correct |
706 ms |
5424 KB |
Output is correct |
65 |
Correct |
473 ms |
5200 KB |
Output is correct |
66 |
Correct |
877 ms |
5512 KB |
Output is correct |
67 |
Correct |
861 ms |
5456 KB |
Output is correct |
68 |
Correct |
911 ms |
5456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
9 ms |
5072 KB |
Output is correct |
4 |
Correct |
8 ms |
5116 KB |
Output is correct |
5 |
Correct |
7 ms |
5072 KB |
Output is correct |
6 |
Correct |
8 ms |
5072 KB |
Output is correct |
7 |
Correct |
9 ms |
5052 KB |
Output is correct |
8 |
Correct |
7 ms |
5056 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
5072 KB |
Output is correct |
11 |
Correct |
3 ms |
5160 KB |
Output is correct |
12 |
Correct |
4 ms |
5072 KB |
Output is correct |
13 |
Correct |
3 ms |
5072 KB |
Output is correct |
14 |
Correct |
4 ms |
5072 KB |
Output is correct |
15 |
Correct |
5 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5072 KB |
Output is correct |
17 |
Correct |
4 ms |
5048 KB |
Output is correct |
18 |
Correct |
4 ms |
5072 KB |
Output is correct |
19 |
Correct |
3 ms |
5072 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
4 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 |
3 ms |
5072 KB |
Output is correct |
26 |
Correct |
5 ms |
5072 KB |
Output is correct |
27 |
Correct |
5 ms |
5136 KB |
Output is correct |
28 |
Correct |
3 ms |
5072 KB |
Output is correct |
29 |
Correct |
7 ms |
5072 KB |
Output is correct |
30 |
Correct |
7 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5072 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 KB |
Output is correct |
33 |
Correct |
3 ms |
5072 KB |
Output is correct |
34 |
Correct |
3 ms |
5072 KB |
Output is correct |
35 |
Correct |
4 ms |
5072 KB |
Output is correct |
36 |
Correct |
3 ms |
5072 KB |
Output is correct |
37 |
Correct |
7 ms |
5200 KB |
Output is correct |
38 |
Correct |
7 ms |
5200 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
5 ms |
5072 KB |
Output is correct |
42 |
Correct |
4 ms |
5072 KB |
Output is correct |
43 |
Correct |
687 ms |
9168 KB |
Output is correct |
44 |
Correct |
966 ms |
13196 KB |
Output is correct |
45 |
Correct |
976 ms |
13188 KB |
Output is correct |
46 |
Correct |
897 ms |
12540 KB |
Output is correct |
47 |
Correct |
793 ms |
9112 KB |
Output is correct |
48 |
Correct |
1070 ms |
13200 KB |
Output is correct |
49 |
Correct |
843 ms |
13196 KB |
Output is correct |
50 |
Correct |
995 ms |
13256 KB |
Output is correct |
51 |
Correct |
429 ms |
5240 KB |
Output is correct |
52 |
Correct |
892 ms |
5456 KB |
Output is correct |
53 |
Correct |
679 ms |
5456 KB |
Output is correct |
54 |
Correct |
790 ms |
5456 KB |
Output is correct |
55 |
Correct |
1093 ms |
18432 KB |
Output is correct |
56 |
Correct |
1092 ms |
18664 KB |
Output is correct |
57 |
Correct |
888 ms |
18628 KB |
Output is correct |
58 |
Correct |
829 ms |
18728 KB |
Output is correct |
59 |
Correct |
961 ms |
22428 KB |
Output is correct |
60 |
Correct |
956 ms |
22260 KB |
Output is correct |
61 |
Correct |
588 ms |
5352 KB |
Output is correct |
62 |
Correct |
721 ms |
5456 KB |
Output is correct |
63 |
Correct |
981 ms |
5456 KB |
Output is correct |
64 |
Correct |
849 ms |
5712 KB |
Output is correct |
65 |
Correct |
747 ms |
5780 KB |
Output is correct |
66 |
Correct |
1002 ms |
5752 KB |
Output is correct |
67 |
Correct |
808 ms |
5712 KB |
Output is correct |
68 |
Correct |
911 ms |
5712 KB |
Output is correct |
69 |
Correct |
1012 ms |
5576 KB |
Output is correct |
70 |
Correct |
1053 ms |
5564 KB |
Output is correct |
71 |
Correct |
843 ms |
5456 KB |
Output is correct |
72 |
Correct |
858 ms |
5712 KB |
Output is correct |
73 |
Correct |
1035 ms |
5584 KB |
Output is correct |
74 |
Correct |
832 ms |
5584 KB |
Output is correct |
75 |
Correct |
1086 ms |
5456 KB |
Output is correct |
76 |
Correct |
864 ms |
5956 KB |
Output is correct |
77 |
Correct |
1057 ms |
5996 KB |
Output is correct |
78 |
Correct |
954 ms |
5996 KB |
Output is correct |
79 |
Correct |
989 ms |
5456 KB |
Output is correct |
80 |
Correct |
1006 ms |
5344 KB |
Output is correct |
81 |
Correct |
799 ms |
5328 KB |
Output is correct |
82 |
Correct |
706 ms |
5424 KB |
Output is correct |
83 |
Correct |
473 ms |
5200 KB |
Output is correct |
84 |
Correct |
877 ms |
5512 KB |
Output is correct |
85 |
Correct |
861 ms |
5456 KB |
Output is correct |
86 |
Correct |
911 ms |
5456 KB |
Output is correct |
87 |
Correct |
3 ms |
4944 KB |
Output is correct |
88 |
Correct |
671 ms |
17736 KB |
Output is correct |
89 |
Correct |
1010 ms |
13316 KB |
Output is correct |
90 |
Correct |
1086 ms |
13052 KB |
Output is correct |
91 |
Correct |
974 ms |
18724 KB |
Output is correct |
92 |
Correct |
1152 ms |
18800 KB |
Output is correct |
93 |
Correct |
991 ms |
17724 KB |
Output is correct |
94 |
Correct |
1130 ms |
18712 KB |
Output is correct |
95 |
Correct |
1006 ms |
18736 KB |
Output is correct |
96 |
Execution timed out |
3042 ms |
8040 KB |
Time limit exceeded |
97 |
Halted |
0 ms |
0 KB |
- |