// correct/solution-ayaze-full.cpp
// O(N + M + Q log M)
#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
const int kMod = 1'000'002'022;
struct Node {
bool lazy;
int val[2];
Node(int v0 = 0, int v1 = 0) {
lazy = false;
val[0] = v0;
val[1] = v1;
}
void flip() {
lazy = !lazy;
swap(val[0], val[1]);
}
Node operator +(Node other) const {
Node ret;
for (int i = 0 ; i < 2 ; i++) {
ret.val[i] = (val[i] + other.val[i]) % kMod;
}
return ret;
}
};
struct SegmentTree {
vector<Node> tree;
int n;
SegmentTree() {}
SegmentTree(vector<pair<int, int>> initial_values) {
n = initial_values.size();
tree.resize(4 * n + 5);
build(1, 0, n-1, initial_values);
}
void build(int id, int l, int r, vector<pair<int, int>> &initial_values) {
if (l == r) {
pair<int, int> val = initial_values[l];
tree[id] = Node(val.first, val.second);
} else {
int m = (l + r) / 2;
int chld = id << 1;
build(chld, l, m, initial_values);
build(chld+1, m+1, r, initial_values);
tree[id] = tree[chld] + tree[chld+1];
}
}
void propagate(int id) {
if (!tree[id].lazy) return;
for (int i = 0 ; i < 2 ; i++) {
tree[2*id + i].flip();
}
tree[id].lazy = false;
}
void update(int l, int r) {
update(1, 0, n-1, l, r);
}
void update(int id, int l, int r, int x, int y) {
if (x <= l && r <= y) {
tree[id].flip();
} else {
int m = (l + r) / 2;
int chld = id << 1;
propagate(id);
if (x <= m) update(chld, l, m, x, y);
if (y > m) update(chld+1, m+1, r, x, y);
tree[id] = tree[chld] + tree[chld+1];
}
}
int query() {
return tree[1].val[1];
}
};
SegmentTree segment_tree;
vector<vector<int>> children;
vector<int> child_muls;
vector<int> contribs;
int N;
void DfsMuls(int v) {
if (v >= N) {
child_muls[v] = 1;
return;
}
child_muls[v] = children[v].size();
for (int nex : children[v]) {
DfsMuls(nex);
child_muls[v] = 1ll * child_muls[v] * child_muls[nex] % kMod;
}
}
void DfsContrib(int v, int current_contrib) {
if (v >= N) {
contribs[v-N] = current_contrib;
return;
}
vector<int> prefix_contrib(children[v].size()), suffix_contrib(children[v].size());
for (int i = 0 ; i < static_cast<int>(children[v].size()) ; i++) {
prefix_contrib[i] = child_muls[children[v][i]];
if (i > 0) {
prefix_contrib[i] = 1ll * prefix_contrib[i] * prefix_contrib[i-1] % kMod;
}
}
for (int i = static_cast<int>(children[v].size())-1 ; i >= 0 ; i--) {
suffix_contrib[i] = child_muls[children[v][i]];
if (i+1 < static_cast<int>(children[v].size())) {
suffix_contrib[i] = 1ll * suffix_contrib[i] * suffix_contrib[i+1] % kMod;
}
}
for (int i = 0 ; i < static_cast<int>(children[v].size()) ; i++) {
int nex = children[v][i];
int new_contrib = current_contrib;
if (i > 0) {
new_contrib = 1ll * new_contrib * prefix_contrib[i-1] % kMod;
}
if (i+1 < static_cast<int>(children[v].size())) {
new_contrib = 1ll * new_contrib * suffix_contrib[i+1] % kMod;
}
DfsContrib(nex, new_contrib);
}
}
void init(int _N, int M, std::vector<int> P, std::vector<int> A) {
N = _N;
children.resize(N);
contribs.resize(M);
child_muls.resize(N+M);
for (int i = 1 ; i < static_cast<int>(P.size()) ; i++) {
children[P[i]].push_back(i);
}
DfsMuls(0);
DfsContrib(0, 1);
vector<pair<int, int>> initial_values;
for (int i = 0 ; i < M ; i++) {
pair<int, int> c = {0, contribs[i]};
if (A[i] == 0) swap(c.first, c.second);
initial_values.push_back(c);
}
segment_tree = SegmentTree(initial_values);
}
int count_ways(int L, int R) {
segment_tree.update(L-N, R-N);
return segment_tree.query();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
224 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
400 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
396 KB |
Output is correct |
7 |
Correct |
1 ms |
400 KB |
Output is correct |
8 |
Correct |
1 ms |
376 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 |
384 KB |
Output is correct |
8 |
Correct |
2 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 |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
224 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
400 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
396 KB |
Output is correct |
7 |
Correct |
1 ms |
400 KB |
Output is correct |
8 |
Correct |
1 ms |
376 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 |
384 KB |
Output is correct |
16 |
Correct |
2 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 |
384 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 |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
456 KB |
Output is correct |
28 |
Correct |
1 ms |
432 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
384 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 |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
376 KB |
Output is correct |
36 |
Correct |
1 ms |
620 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 |
384 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
589 ms |
5436 KB |
Output is correct |
2 |
Correct |
1106 ms |
10420 KB |
Output is correct |
3 |
Correct |
894 ms |
10384 KB |
Output is correct |
4 |
Correct |
941 ms |
10432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
589 ms |
5436 KB |
Output is correct |
2 |
Correct |
1106 ms |
10420 KB |
Output is correct |
3 |
Correct |
894 ms |
10384 KB |
Output is correct |
4 |
Correct |
941 ms |
10432 KB |
Output is correct |
5 |
Correct |
1059 ms |
5428 KB |
Output is correct |
6 |
Correct |
1051 ms |
10468 KB |
Output is correct |
7 |
Correct |
1312 ms |
10336 KB |
Output is correct |
8 |
Correct |
1193 ms |
10448 KB |
Output is correct |
9 |
Correct |
387 ms |
624 KB |
Output is correct |
10 |
Correct |
895 ms |
848 KB |
Output is correct |
11 |
Correct |
1160 ms |
848 KB |
Output is correct |
12 |
Correct |
1114 ms |
876 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 |
384 KB |
Output is correct |
8 |
Correct |
2 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 |
589 ms |
5436 KB |
Output is correct |
14 |
Correct |
1106 ms |
10420 KB |
Output is correct |
15 |
Correct |
894 ms |
10384 KB |
Output is correct |
16 |
Correct |
941 ms |
10432 KB |
Output is correct |
17 |
Correct |
1059 ms |
5428 KB |
Output is correct |
18 |
Correct |
1051 ms |
10468 KB |
Output is correct |
19 |
Correct |
1312 ms |
10336 KB |
Output is correct |
20 |
Correct |
1193 ms |
10448 KB |
Output is correct |
21 |
Correct |
387 ms |
624 KB |
Output is correct |
22 |
Correct |
895 ms |
848 KB |
Output is correct |
23 |
Correct |
1160 ms |
848 KB |
Output is correct |
24 |
Correct |
1114 ms |
876 KB |
Output is correct |
25 |
Correct |
1122 ms |
15320 KB |
Output is correct |
26 |
Correct |
1244 ms |
15688 KB |
Output is correct |
27 |
Correct |
1183 ms |
15604 KB |
Output is correct |
28 |
Correct |
717 ms |
15804 KB |
Output is correct |
29 |
Correct |
1146 ms |
32696 KB |
Output is correct |
30 |
Correct |
1080 ms |
32740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
224 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
400 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
396 KB |
Output is correct |
7 |
Correct |
1 ms |
400 KB |
Output is correct |
8 |
Correct |
1 ms |
376 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 |
384 KB |
Output is correct |
16 |
Correct |
2 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 |
384 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 |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
456 KB |
Output is correct |
28 |
Correct |
1 ms |
432 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
384 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 |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
376 KB |
Output is correct |
36 |
Correct |
1 ms |
620 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 |
384 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
43 |
Correct |
750 ms |
728 KB |
Output is correct |
44 |
Correct |
1044 ms |
784 KB |
Output is correct |
45 |
Correct |
971 ms |
720 KB |
Output is correct |
46 |
Correct |
1078 ms |
1104 KB |
Output is correct |
47 |
Correct |
1073 ms |
1152 KB |
Output is correct |
48 |
Correct |
875 ms |
1132 KB |
Output is correct |
49 |
Correct |
906 ms |
1104 KB |
Output is correct |
50 |
Correct |
1049 ms |
1104 KB |
Output is correct |
51 |
Correct |
961 ms |
808 KB |
Output is correct |
52 |
Correct |
846 ms |
808 KB |
Output is correct |
53 |
Correct |
591 ms |
1508 KB |
Output is correct |
54 |
Correct |
982 ms |
976 KB |
Output is correct |
55 |
Correct |
825 ms |
848 KB |
Output is correct |
56 |
Correct |
997 ms |
848 KB |
Output is correct |
57 |
Correct |
918 ms |
720 KB |
Output is correct |
58 |
Correct |
926 ms |
1872 KB |
Output is correct |
59 |
Correct |
1196 ms |
1872 KB |
Output is correct |
60 |
Correct |
1012 ms |
1872 KB |
Output is correct |
61 |
Correct |
925 ms |
924 KB |
Output is correct |
62 |
Correct |
1149 ms |
720 KB |
Output is correct |
63 |
Correct |
953 ms |
768 KB |
Output is correct |
64 |
Correct |
1090 ms |
720 KB |
Output is correct |
65 |
Correct |
631 ms |
604 KB |
Output is correct |
66 |
Correct |
631 ms |
896 KB |
Output is correct |
67 |
Correct |
1104 ms |
848 KB |
Output is correct |
68 |
Correct |
799 ms |
884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
224 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
400 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
396 KB |
Output is correct |
7 |
Correct |
1 ms |
400 KB |
Output is correct |
8 |
Correct |
1 ms |
376 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 |
384 KB |
Output is correct |
16 |
Correct |
2 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 |
384 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 |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
456 KB |
Output is correct |
28 |
Correct |
1 ms |
432 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
384 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 |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
376 KB |
Output is correct |
36 |
Correct |
1 ms |
620 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 |
384 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
43 |
Correct |
589 ms |
5436 KB |
Output is correct |
44 |
Correct |
1106 ms |
10420 KB |
Output is correct |
45 |
Correct |
894 ms |
10384 KB |
Output is correct |
46 |
Correct |
941 ms |
10432 KB |
Output is correct |
47 |
Correct |
1059 ms |
5428 KB |
Output is correct |
48 |
Correct |
1051 ms |
10468 KB |
Output is correct |
49 |
Correct |
1312 ms |
10336 KB |
Output is correct |
50 |
Correct |
1193 ms |
10448 KB |
Output is correct |
51 |
Correct |
387 ms |
624 KB |
Output is correct |
52 |
Correct |
895 ms |
848 KB |
Output is correct |
53 |
Correct |
1160 ms |
848 KB |
Output is correct |
54 |
Correct |
1114 ms |
876 KB |
Output is correct |
55 |
Correct |
1122 ms |
15320 KB |
Output is correct |
56 |
Correct |
1244 ms |
15688 KB |
Output is correct |
57 |
Correct |
1183 ms |
15604 KB |
Output is correct |
58 |
Correct |
717 ms |
15804 KB |
Output is correct |
59 |
Correct |
1146 ms |
32696 KB |
Output is correct |
60 |
Correct |
1080 ms |
32740 KB |
Output is correct |
61 |
Correct |
750 ms |
728 KB |
Output is correct |
62 |
Correct |
1044 ms |
784 KB |
Output is correct |
63 |
Correct |
971 ms |
720 KB |
Output is correct |
64 |
Correct |
1078 ms |
1104 KB |
Output is correct |
65 |
Correct |
1073 ms |
1152 KB |
Output is correct |
66 |
Correct |
875 ms |
1132 KB |
Output is correct |
67 |
Correct |
906 ms |
1104 KB |
Output is correct |
68 |
Correct |
1049 ms |
1104 KB |
Output is correct |
69 |
Correct |
961 ms |
808 KB |
Output is correct |
70 |
Correct |
846 ms |
808 KB |
Output is correct |
71 |
Correct |
591 ms |
1508 KB |
Output is correct |
72 |
Correct |
982 ms |
976 KB |
Output is correct |
73 |
Correct |
825 ms |
848 KB |
Output is correct |
74 |
Correct |
997 ms |
848 KB |
Output is correct |
75 |
Correct |
918 ms |
720 KB |
Output is correct |
76 |
Correct |
926 ms |
1872 KB |
Output is correct |
77 |
Correct |
1196 ms |
1872 KB |
Output is correct |
78 |
Correct |
1012 ms |
1872 KB |
Output is correct |
79 |
Correct |
925 ms |
924 KB |
Output is correct |
80 |
Correct |
1149 ms |
720 KB |
Output is correct |
81 |
Correct |
953 ms |
768 KB |
Output is correct |
82 |
Correct |
1090 ms |
720 KB |
Output is correct |
83 |
Correct |
631 ms |
604 KB |
Output is correct |
84 |
Correct |
631 ms |
896 KB |
Output is correct |
85 |
Correct |
1104 ms |
848 KB |
Output is correct |
86 |
Correct |
799 ms |
884 KB |
Output is correct |
87 |
Correct |
1 ms |
208 KB |
Output is correct |
88 |
Correct |
903 ms |
13764 KB |
Output is correct |
89 |
Correct |
1398 ms |
9880 KB |
Output is correct |
90 |
Correct |
1004 ms |
9728 KB |
Output is correct |
91 |
Correct |
942 ms |
15812 KB |
Output is correct |
92 |
Correct |
1244 ms |
15800 KB |
Output is correct |
93 |
Correct |
1189 ms |
15720 KB |
Output is correct |
94 |
Correct |
1216 ms |
15796 KB |
Output is correct |
95 |
Correct |
941 ms |
15756 KB |
Output is correct |
96 |
Correct |
1154 ms |
9740 KB |
Output is correct |
97 |
Correct |
959 ms |
9796 KB |
Output is correct |
98 |
Correct |
838 ms |
25772 KB |
Output is correct |
99 |
Correct |
1075 ms |
15688 KB |
Output is correct |
100 |
Correct |
1167 ms |
12404 KB |
Output is correct |
101 |
Correct |
1002 ms |
11148 KB |
Output is correct |
102 |
Correct |
981 ms |
9508 KB |
Output is correct |
103 |
Correct |
1392 ms |
32756 KB |
Output is correct |
104 |
Correct |
1243 ms |
33064 KB |
Output is correct |
105 |
Correct |
833 ms |
33152 KB |
Output is correct |
106 |
Correct |
902 ms |
12464 KB |
Output is correct |
107 |
Correct |
1097 ms |
8956 KB |
Output is correct |
108 |
Correct |
1046 ms |
9156 KB |
Output is correct |
109 |
Correct |
1231 ms |
9660 KB |
Output is correct |