#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000002022;
struct Vertex {
int s0, s1;
};
vector<int> c, now;
int dim, subtract;
vector<Vertex> tree;
vector<int> lazy;
void build(int v, int tl, int tr) {
lazy[v] = 0;
if (tl == tr) {
tree[v].s0 = now[tl] * c[tl];
tree[v].s1 = (1 - now[tl]) * c[tl];
} else {
int tm = (tl + tr) / 2;
build(2 * v, tl, tm);
build(2 * v + 1, tm + 1, tr);
tree[v].s0 = tree[2 * v].s0 + tree[2 * v + 1].s0;
tree[v].s1 = tree[2 * v].s1 + tree[2 * v + 1].s1;
if (tree[v].s0 >= MOD) tree[v].s0 -= MOD;
if (tree[v].s1 >= MOD) tree[v].s1 -= MOD;
}
}
void push(int v, int tl, int tr) {
if (lazy[v]) {
swap(tree[v].s0, tree[v].s1);
if (tl < tr) lazy[2 * v] ^= 1, lazy[2 * v + 1] ^= 1;
lazy[v] = 0;
}
}
void op(int v, int tl, int tr, int l, int r) {
push(v, tl, tr);
if (tr < l || r < tl) return;
if (l <= tl && tr <= r) {
lazy[v] ^= 1;
push(v, tl, tr);
return;
}
int tm = (tl + tr) / 2;
op(2 * v, tl, tm, l, r);
op(2 * v + 1, tm + 1, tr, l, r);
tree[v].s0 = tree[2 * v].s0 + tree[2 * v + 1].s0;
tree[v].s1 = tree[2 * v].s1 + tree[2 * v + 1].s1;
if (tree[v].s0 >= MOD) tree[v].s0 -= MOD;
if (tree[v].s1 >= MOD) tree[v].s1 -= MOD;
}
void init(int n, int m, std::vector<int> par, std::vector<int> init) {
dim = m;
tree.resize(4 * dim + 7);
lazy.resize(4 * dim + 7);
subtract = n;
c.resize(m, 0);
vector<vector<int>> g(n + m);
vector<int> prod(n + m, 1);
for (int i = 1; i < n + m; i++) {
g[par[i]].push_back(i);
}
function<void(int)> build1 = [&] (int a) {
if (g[a].empty()) return;
prod[a] = (int) g[a].size();
for (auto &b : g[a]) {
build1(b);
prod[a] = prod[a] * (ll) prod[b] % MOD;
}
};
now = init;
function<void(int, int)> dfs = [&] (int a, int coef) {
/// optimize this function
vector<int> send((int) g[a].size());
{
int c = 1;
for (int i = 0; i < (int) g[a].size(); i++) {
send[i] = coef * (ll) c % MOD;
c = c * (ll) prod[g[a][i]] % MOD;
}
}
{
int c = 1;
for (int i = (int) g[a].size() - 1; i >= 0; i--) {
send[i] = send[i] * (ll) c % MOD;
c = c * (ll) prod[g[a][i]] % MOD;
}
}
for (int i = 0; i < (int) g[a].size(); i++) {
dfs(g[a][i], send[i]);
}
if (g[a].empty()) {
c[a - n] = coef;
}
};
build1(0);
dfs(0, 1);
build(1, 0, dim - 1);
}
int count_ways(int l, int r) {
op(1, 0, dim - 1, l - subtract, r - subtract);
push(1, 0, dim - 1);
return tree[1].s0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
2 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
592 KB |
Output is correct |
37 |
Correct |
1 ms |
592 KB |
Output is correct |
38 |
Correct |
1 ms |
592 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
605 ms |
5684 KB |
Output is correct |
2 |
Correct |
940 ms |
11092 KB |
Output is correct |
3 |
Correct |
678 ms |
11060 KB |
Output is correct |
4 |
Correct |
761 ms |
11060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
605 ms |
5684 KB |
Output is correct |
2 |
Correct |
940 ms |
11092 KB |
Output is correct |
3 |
Correct |
678 ms |
11060 KB |
Output is correct |
4 |
Correct |
761 ms |
11060 KB |
Output is correct |
5 |
Correct |
803 ms |
5712 KB |
Output is correct |
6 |
Correct |
811 ms |
11096 KB |
Output is correct |
7 |
Correct |
1006 ms |
11092 KB |
Output is correct |
8 |
Correct |
1129 ms |
11056 KB |
Output is correct |
9 |
Correct |
458 ms |
592 KB |
Output is correct |
10 |
Correct |
810 ms |
976 KB |
Output is correct |
11 |
Correct |
1012 ms |
848 KB |
Output is correct |
12 |
Correct |
753 ms |
976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
605 ms |
5684 KB |
Output is correct |
14 |
Correct |
940 ms |
11092 KB |
Output is correct |
15 |
Correct |
678 ms |
11060 KB |
Output is correct |
16 |
Correct |
761 ms |
11060 KB |
Output is correct |
17 |
Correct |
803 ms |
5712 KB |
Output is correct |
18 |
Correct |
811 ms |
11096 KB |
Output is correct |
19 |
Correct |
1006 ms |
11092 KB |
Output is correct |
20 |
Correct |
1129 ms |
11056 KB |
Output is correct |
21 |
Correct |
458 ms |
592 KB |
Output is correct |
22 |
Correct |
810 ms |
976 KB |
Output is correct |
23 |
Correct |
1012 ms |
848 KB |
Output is correct |
24 |
Correct |
753 ms |
976 KB |
Output is correct |
25 |
Correct |
918 ms |
16444 KB |
Output is correct |
26 |
Correct |
1134 ms |
16696 KB |
Output is correct |
27 |
Correct |
1177 ms |
16744 KB |
Output is correct |
28 |
Correct |
884 ms |
16748 KB |
Output is correct |
29 |
Correct |
1004 ms |
32316 KB |
Output is correct |
30 |
Correct |
963 ms |
32280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
2 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
592 KB |
Output is correct |
37 |
Correct |
1 ms |
592 KB |
Output is correct |
38 |
Correct |
1 ms |
592 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
511 ms |
720 KB |
Output is correct |
44 |
Correct |
1004 ms |
720 KB |
Output is correct |
45 |
Correct |
804 ms |
720 KB |
Output is correct |
46 |
Correct |
807 ms |
1104 KB |
Output is correct |
47 |
Correct |
957 ms |
1104 KB |
Output is correct |
48 |
Correct |
873 ms |
1104 KB |
Output is correct |
49 |
Correct |
901 ms |
1104 KB |
Output is correct |
50 |
Correct |
953 ms |
1104 KB |
Output is correct |
51 |
Correct |
956 ms |
848 KB |
Output is correct |
52 |
Correct |
898 ms |
848 KB |
Output is correct |
53 |
Correct |
781 ms |
1360 KB |
Output is correct |
54 |
Correct |
708 ms |
1104 KB |
Output is correct |
55 |
Correct |
918 ms |
848 KB |
Output is correct |
56 |
Correct |
959 ms |
848 KB |
Output is correct |
57 |
Correct |
853 ms |
720 KB |
Output is correct |
58 |
Correct |
948 ms |
1872 KB |
Output is correct |
59 |
Correct |
568 ms |
1872 KB |
Output is correct |
60 |
Correct |
831 ms |
2000 KB |
Output is correct |
61 |
Correct |
733 ms |
848 KB |
Output is correct |
62 |
Correct |
888 ms |
720 KB |
Output is correct |
63 |
Correct |
700 ms |
720 KB |
Output is correct |
64 |
Correct |
831 ms |
720 KB |
Output is correct |
65 |
Correct |
478 ms |
592 KB |
Output is correct |
66 |
Correct |
926 ms |
848 KB |
Output is correct |
67 |
Correct |
913 ms |
976 KB |
Output is correct |
68 |
Correct |
881 ms |
976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
2 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
592 KB |
Output is correct |
37 |
Correct |
1 ms |
592 KB |
Output is correct |
38 |
Correct |
1 ms |
592 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
605 ms |
5684 KB |
Output is correct |
44 |
Correct |
940 ms |
11092 KB |
Output is correct |
45 |
Correct |
678 ms |
11060 KB |
Output is correct |
46 |
Correct |
761 ms |
11060 KB |
Output is correct |
47 |
Correct |
803 ms |
5712 KB |
Output is correct |
48 |
Correct |
811 ms |
11096 KB |
Output is correct |
49 |
Correct |
1006 ms |
11092 KB |
Output is correct |
50 |
Correct |
1129 ms |
11056 KB |
Output is correct |
51 |
Correct |
458 ms |
592 KB |
Output is correct |
52 |
Correct |
810 ms |
976 KB |
Output is correct |
53 |
Correct |
1012 ms |
848 KB |
Output is correct |
54 |
Correct |
753 ms |
976 KB |
Output is correct |
55 |
Correct |
918 ms |
16444 KB |
Output is correct |
56 |
Correct |
1134 ms |
16696 KB |
Output is correct |
57 |
Correct |
1177 ms |
16744 KB |
Output is correct |
58 |
Correct |
884 ms |
16748 KB |
Output is correct |
59 |
Correct |
1004 ms |
32316 KB |
Output is correct |
60 |
Correct |
963 ms |
32280 KB |
Output is correct |
61 |
Correct |
511 ms |
720 KB |
Output is correct |
62 |
Correct |
1004 ms |
720 KB |
Output is correct |
63 |
Correct |
804 ms |
720 KB |
Output is correct |
64 |
Correct |
807 ms |
1104 KB |
Output is correct |
65 |
Correct |
957 ms |
1104 KB |
Output is correct |
66 |
Correct |
873 ms |
1104 KB |
Output is correct |
67 |
Correct |
901 ms |
1104 KB |
Output is correct |
68 |
Correct |
953 ms |
1104 KB |
Output is correct |
69 |
Correct |
956 ms |
848 KB |
Output is correct |
70 |
Correct |
898 ms |
848 KB |
Output is correct |
71 |
Correct |
781 ms |
1360 KB |
Output is correct |
72 |
Correct |
708 ms |
1104 KB |
Output is correct |
73 |
Correct |
918 ms |
848 KB |
Output is correct |
74 |
Correct |
959 ms |
848 KB |
Output is correct |
75 |
Correct |
853 ms |
720 KB |
Output is correct |
76 |
Correct |
948 ms |
1872 KB |
Output is correct |
77 |
Correct |
568 ms |
1872 KB |
Output is correct |
78 |
Correct |
831 ms |
2000 KB |
Output is correct |
79 |
Correct |
733 ms |
848 KB |
Output is correct |
80 |
Correct |
888 ms |
720 KB |
Output is correct |
81 |
Correct |
700 ms |
720 KB |
Output is correct |
82 |
Correct |
831 ms |
720 KB |
Output is correct |
83 |
Correct |
478 ms |
592 KB |
Output is correct |
84 |
Correct |
926 ms |
848 KB |
Output is correct |
85 |
Correct |
913 ms |
976 KB |
Output is correct |
86 |
Correct |
881 ms |
976 KB |
Output is correct |
87 |
Correct |
0 ms |
208 KB |
Output is correct |
88 |
Correct |
583 ms |
14612 KB |
Output is correct |
89 |
Correct |
1016 ms |
10312 KB |
Output is correct |
90 |
Correct |
1299 ms |
10392 KB |
Output is correct |
91 |
Correct |
1060 ms |
16852 KB |
Output is correct |
92 |
Correct |
1030 ms |
16876 KB |
Output is correct |
93 |
Correct |
1097 ms |
16780 KB |
Output is correct |
94 |
Correct |
1136 ms |
16748 KB |
Output is correct |
95 |
Correct |
1031 ms |
16752 KB |
Output is correct |
96 |
Correct |
901 ms |
10900 KB |
Output is correct |
97 |
Correct |
993 ms |
10944 KB |
Output is correct |
98 |
Correct |
796 ms |
22556 KB |
Output is correct |
99 |
Correct |
1172 ms |
16660 KB |
Output is correct |
100 |
Correct |
1106 ms |
13296 KB |
Output is correct |
101 |
Correct |
1083 ms |
12216 KB |
Output is correct |
102 |
Correct |
1083 ms |
10608 KB |
Output is correct |
103 |
Correct |
993 ms |
32292 KB |
Output is correct |
104 |
Correct |
976 ms |
33168 KB |
Output is correct |
105 |
Correct |
929 ms |
33176 KB |
Output is correct |
106 |
Correct |
889 ms |
12828 KB |
Output is correct |
107 |
Correct |
937 ms |
9544 KB |
Output is correct |
108 |
Correct |
951 ms |
9880 KB |
Output is correct |
109 |
Correct |
817 ms |
10652 KB |
Output is correct |