// model_solution/solution.cpp
// O(N + M + Q log M)
#include "circuit.h"
#include <functional>
#include <utility>
#include <vector>
const int kMod = 1000002022;
int N;
struct SegTree {
private:
int n;
struct Node {
int val[2];
bool lazy;
Node(int v0=0, int v1=0) {
val[0] = v0;
val[1] = v1;
lazy = false;
}
Node operator + (const Node &other) {
return Node((val[0] + other.val[0]) % kMod,
(val[1] + other.val[1]) % kMod);
}
};
std::vector<Node> nodes;
inline void toggle(int idx) {
std::swap(nodes[idx].val[0], nodes[idx].val[1]);
nodes[idx].lazy = !nodes[idx].lazy;
}
inline void pull(int idx, int l, int r) {
int mid = (l + r) >> 1;
nodes[idx] = nodes[idx + 1] + nodes[idx + (mid - l) * 2];
}
inline void push(int idx, int l, int r) {
if (!nodes[idx].lazy) return;
int mid = (l + r) >> 1;
toggle(idx + 1);
toggle(idx + (mid - l) * 2);
nodes[idx].lazy = false;
}
void build(int idx, int l, int r, const std::vector<std::pair<int, int>> &v) {
if (l + 1 == r) {
nodes[idx] = Node(v[l].first, v[l].second);
return;
}
int mid = (l + r) >> 1;
build(idx + 1, l, mid, v);
build(idx + (mid - l) * 2, mid, r, v);
pull(idx, l, r);
}
void update(int idx, int l, int r, int ll, int rr) {
if (l >= rr || r <= ll) {
return;
}
if (l >= ll && r <= rr) {
return toggle(idx);
}
push(idx, l, r);
int mid = (l + r) >> 1;
update(idx + 1, l, mid, ll, rr);
update(idx + (mid - l) * 2, mid, r, ll, rr);
pull(idx, l, r);
}
public:
void init(const std::vector<std::pair<int, int>> &v) {
n = static_cast<int>(v.size());
nodes.resize(2 * n - 1);
build(0, 0, n, v);
}
void update(int l, int r) {
update(0, 0, n, l, r);
}
int query() {
return nodes[0].val[1];
}
} segtree;
void init(int _N, int M, std::vector<int> P, std::vector<int> A) {
N = _N;
std::vector<std::vector<int>> adj(N + M);
for (int i = 1; i < N + M; ++i) {
adj[P[i]].push_back(i);
}
std::vector<int> prod(N + M);
std::function<int(int)> dfs_prod = [&](int u) {
if (u >= N) {
return prod[u] = 1;
}
prod[u] = static_cast<int>(adj[u].size());
for (int v : adj[u]) {
prod[u] = 1LL * prod[u] * dfs_prod(v) % kMod;
}
return prod[u];
};
dfs_prod(0);
std::vector<int> contribution(M);
std::function<void(int, int)> dfs_contrib = [&](int u, int product) {
if (u >= N) {
contribution[u - N] = product;
return;
}
std::vector<int> prefix(adj[u].size()), suffix(adj[u].size());
for (int i = 0; i < static_cast<int>(adj[u].size()); ++i) {
prefix[i] = prod[adj[u][i]];
if (i > 0) {
prefix[i] = 1LL * prefix[i] * prefix[i - 1] % kMod;
}
}
for (int i = static_cast<int>(adj[u].size()) - 1; i >= 0; --i) {
suffix[i] = prod[adj[u][i]];
if (i + 1 < static_cast<int>(adj[u].size())) {
suffix[i] = 1LL * suffix[i] * suffix[i + 1] % kMod;
}
}
for (int i = 0; i < static_cast<int>(adj[u].size()); ++i) {
int next_product = product;
if (i > 0) {
next_product = 1LL * next_product * prefix[i - 1] % kMod;
}
if (i + 1 < static_cast<int>(adj[u].size())) {
next_product = 1LL * next_product * suffix[i + 1] % kMod;
}
dfs_contrib(adj[u][i], next_product);
}
};
dfs_contrib(0, 1);
std::vector<std::pair<int, int>> v(M);
for (int i = 0; i < M; ++i) {
if (A[i]) {
v[i].second = contribution[i];
} else {
v[i].first = contribution[i];
}
}
segtree.init(v);
}
int count_ways(int L, int R) {
segtree.update(L - N, R - N + 1);
return segtree.query();
}
# |
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 |
0 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 |
0 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 |
0 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 |
0 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 |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 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 |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
0 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 |
671 ms |
5028 KB |
Output is correct |
2 |
Correct |
947 ms |
9760 KB |
Output is correct |
3 |
Correct |
841 ms |
9672 KB |
Output is correct |
4 |
Correct |
911 ms |
9800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
671 ms |
5028 KB |
Output is correct |
2 |
Correct |
947 ms |
9760 KB |
Output is correct |
3 |
Correct |
841 ms |
9672 KB |
Output is correct |
4 |
Correct |
911 ms |
9800 KB |
Output is correct |
5 |
Correct |
664 ms |
5052 KB |
Output is correct |
6 |
Correct |
839 ms |
9760 KB |
Output is correct |
7 |
Correct |
745 ms |
9800 KB |
Output is correct |
8 |
Correct |
896 ms |
9800 KB |
Output is correct |
9 |
Correct |
407 ms |
556 KB |
Output is correct |
10 |
Correct |
733 ms |
848 KB |
Output is correct |
11 |
Correct |
929 ms |
848 KB |
Output is correct |
12 |
Correct |
798 ms |
848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
671 ms |
5028 KB |
Output is correct |
14 |
Correct |
947 ms |
9760 KB |
Output is correct |
15 |
Correct |
841 ms |
9672 KB |
Output is correct |
16 |
Correct |
911 ms |
9800 KB |
Output is correct |
17 |
Correct |
664 ms |
5052 KB |
Output is correct |
18 |
Correct |
839 ms |
9760 KB |
Output is correct |
19 |
Correct |
745 ms |
9800 KB |
Output is correct |
20 |
Correct |
896 ms |
9800 KB |
Output is correct |
21 |
Correct |
407 ms |
556 KB |
Output is correct |
22 |
Correct |
733 ms |
848 KB |
Output is correct |
23 |
Correct |
929 ms |
848 KB |
Output is correct |
24 |
Correct |
798 ms |
848 KB |
Output is correct |
25 |
Correct |
757 ms |
14508 KB |
Output is correct |
26 |
Correct |
819 ms |
14732 KB |
Output is correct |
27 |
Correct |
917 ms |
14724 KB |
Output is correct |
28 |
Correct |
714 ms |
14732 KB |
Output is correct |
29 |
Correct |
851 ms |
33452 KB |
Output is correct |
30 |
Correct |
904 ms |
33444 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 |
0 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 |
0 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 |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 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 |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
0 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 |
408 ms |
592 KB |
Output is correct |
44 |
Correct |
804 ms |
720 KB |
Output is correct |
45 |
Correct |
803 ms |
720 KB |
Output is correct |
46 |
Correct |
720 ms |
976 KB |
Output is correct |
47 |
Correct |
704 ms |
976 KB |
Output is correct |
48 |
Correct |
875 ms |
976 KB |
Output is correct |
49 |
Correct |
916 ms |
976 KB |
Output is correct |
50 |
Correct |
856 ms |
976 KB |
Output is correct |
51 |
Correct |
565 ms |
692 KB |
Output is correct |
52 |
Correct |
620 ms |
692 KB |
Output is correct |
53 |
Correct |
824 ms |
1616 KB |
Output is correct |
54 |
Correct |
780 ms |
976 KB |
Output is correct |
55 |
Correct |
798 ms |
848 KB |
Output is correct |
56 |
Correct |
845 ms |
720 KB |
Output is correct |
57 |
Correct |
797 ms |
688 KB |
Output is correct |
58 |
Correct |
766 ms |
1872 KB |
Output is correct |
59 |
Correct |
755 ms |
2000 KB |
Output is correct |
60 |
Correct |
796 ms |
2004 KB |
Output is correct |
61 |
Correct |
701 ms |
848 KB |
Output is correct |
62 |
Correct |
763 ms |
592 KB |
Output is correct |
63 |
Correct |
903 ms |
656 KB |
Output is correct |
64 |
Correct |
816 ms |
696 KB |
Output is correct |
65 |
Correct |
436 ms |
560 KB |
Output is correct |
66 |
Correct |
785 ms |
848 KB |
Output is correct |
67 |
Correct |
692 ms |
848 KB |
Output is correct |
68 |
Correct |
612 ms |
848 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 |
0 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 |
0 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 |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 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 |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
0 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 |
671 ms |
5028 KB |
Output is correct |
44 |
Correct |
947 ms |
9760 KB |
Output is correct |
45 |
Correct |
841 ms |
9672 KB |
Output is correct |
46 |
Correct |
911 ms |
9800 KB |
Output is correct |
47 |
Correct |
664 ms |
5052 KB |
Output is correct |
48 |
Correct |
839 ms |
9760 KB |
Output is correct |
49 |
Correct |
745 ms |
9800 KB |
Output is correct |
50 |
Correct |
896 ms |
9800 KB |
Output is correct |
51 |
Correct |
407 ms |
556 KB |
Output is correct |
52 |
Correct |
733 ms |
848 KB |
Output is correct |
53 |
Correct |
929 ms |
848 KB |
Output is correct |
54 |
Correct |
798 ms |
848 KB |
Output is correct |
55 |
Correct |
757 ms |
14508 KB |
Output is correct |
56 |
Correct |
819 ms |
14732 KB |
Output is correct |
57 |
Correct |
917 ms |
14724 KB |
Output is correct |
58 |
Correct |
714 ms |
14732 KB |
Output is correct |
59 |
Correct |
851 ms |
33452 KB |
Output is correct |
60 |
Correct |
904 ms |
33444 KB |
Output is correct |
61 |
Correct |
408 ms |
592 KB |
Output is correct |
62 |
Correct |
804 ms |
720 KB |
Output is correct |
63 |
Correct |
803 ms |
720 KB |
Output is correct |
64 |
Correct |
720 ms |
976 KB |
Output is correct |
65 |
Correct |
704 ms |
976 KB |
Output is correct |
66 |
Correct |
875 ms |
976 KB |
Output is correct |
67 |
Correct |
916 ms |
976 KB |
Output is correct |
68 |
Correct |
856 ms |
976 KB |
Output is correct |
69 |
Correct |
565 ms |
692 KB |
Output is correct |
70 |
Correct |
620 ms |
692 KB |
Output is correct |
71 |
Correct |
824 ms |
1616 KB |
Output is correct |
72 |
Correct |
780 ms |
976 KB |
Output is correct |
73 |
Correct |
798 ms |
848 KB |
Output is correct |
74 |
Correct |
845 ms |
720 KB |
Output is correct |
75 |
Correct |
797 ms |
688 KB |
Output is correct |
76 |
Correct |
766 ms |
1872 KB |
Output is correct |
77 |
Correct |
755 ms |
2000 KB |
Output is correct |
78 |
Correct |
796 ms |
2004 KB |
Output is correct |
79 |
Correct |
701 ms |
848 KB |
Output is correct |
80 |
Correct |
763 ms |
592 KB |
Output is correct |
81 |
Correct |
903 ms |
656 KB |
Output is correct |
82 |
Correct |
816 ms |
696 KB |
Output is correct |
83 |
Correct |
436 ms |
560 KB |
Output is correct |
84 |
Correct |
785 ms |
848 KB |
Output is correct |
85 |
Correct |
692 ms |
848 KB |
Output is correct |
86 |
Correct |
612 ms |
848 KB |
Output is correct |
87 |
Correct |
0 ms |
208 KB |
Output is correct |
88 |
Correct |
609 ms |
13004 KB |
Output is correct |
89 |
Correct |
976 ms |
9356 KB |
Output is correct |
90 |
Correct |
901 ms |
9160 KB |
Output is correct |
91 |
Correct |
800 ms |
14860 KB |
Output is correct |
92 |
Correct |
1022 ms |
14824 KB |
Output is correct |
93 |
Correct |
1039 ms |
14792 KB |
Output is correct |
94 |
Correct |
852 ms |
14920 KB |
Output is correct |
95 |
Correct |
808 ms |
14852 KB |
Output is correct |
96 |
Correct |
922 ms |
8580 KB |
Output is correct |
97 |
Correct |
951 ms |
8580 KB |
Output is correct |
98 |
Correct |
751 ms |
28832 KB |
Output is correct |
99 |
Correct |
1123 ms |
14756 KB |
Output is correct |
100 |
Correct |
1224 ms |
11368 KB |
Output is correct |
101 |
Correct |
713 ms |
10192 KB |
Output is correct |
102 |
Correct |
1090 ms |
8616 KB |
Output is correct |
103 |
Correct |
1081 ms |
33536 KB |
Output is correct |
104 |
Correct |
929 ms |
34704 KB |
Output is correct |
105 |
Correct |
1303 ms |
34656 KB |
Output is correct |
106 |
Correct |
1167 ms |
12068 KB |
Output is correct |
107 |
Correct |
1314 ms |
8120 KB |
Output is correct |
108 |
Correct |
1263 ms |
8464 KB |
Output is correct |
109 |
Correct |
956 ms |
8732 KB |
Output is correct |