#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
struct node {
long long sum1 = 0;
long long sum2 = 0;
bool flip = false;
};
const long long mod = 1e9 + 2022;
const int maxN = 1e5 + 20;
long long cont[maxN * 2];
long long ways[maxN * 2];
node tree[maxN * 4];
vector<int> ch[maxN];
int A[maxN];
int N, M;
void dfs1(int u) {
if (ch[u].empty()) {
ways[u] = 1;
return;
}
ways[u] = (int)ch[u].size();
for (auto v: ch[u]) {
dfs1(v);
ways[u] = ways[u] * ways[v] % mod;
}
}
void dfs2(int u) {
if (ch[u].empty()) {
return;
}
int sz = ch[u].size();
vector<long long> pref(sz);
vector<long long> suf(sz);
pref[0] = ways[ch[u][0]];
for (int i = 1; i < sz; i++) {
pref[i] = pref[i - 1] * ways[ch[u][i]] % mod;
}
suf[sz - 1] = ways[ch[u][sz - 1]];
for (int i = sz - 2; i >= 0; i--) {
suf[i] = suf[i + 1] * ways[ch[u][i]] % mod;
}
for (int i = 0; i < sz; i++) {
cont[ch[u][i]] = cont[u] * (i > 0 ? pref[i - 1] : 1) % mod * (i < sz - 1 ? suf[i + 1] : 1) % mod;
dfs2(ch[u][i]);
}
}
void flip(int id) {
swap(tree[id].sum1, tree[id].sum2);
tree[id].flip ^= 1;
}
void merge(int id) {
tree[id].sum1 = (tree[id * 2].sum1 + tree[id * 2 + 1].sum1) % mod;
tree[id].sum2 = (tree[id * 2].sum2 + tree[id * 2 + 1].sum2) % mod;
tree[id].flip = false;
}
void build(int id, int lt, int rt) {
if (lt == rt) {
tree[id].sum1 = A[lt] ? cont[N + lt] : 0;
tree[id].sum2 = !A[lt] ? cont[N + lt] : 0;
tree[id].flip = false;
return;
}
int mt = (lt + rt) / 2;
build(id * 2, lt, mt);
build(id * 2 + 1, mt + 1, rt);
merge(id);
}
void update(int id, int lt, int rt, int ql, int qr) {
if (lt == ql && rt == qr) {
flip(id);
return;
}
if (tree[id].flip) {
flip(id * 2);
flip(id * 2 + 1);
}
int mt = (lt + rt) / 2;
if (qr <= mt) {
update(id * 2, lt, mt, ql, qr);
}
else if (ql >= mt + 1) {
update(id * 2 + 1, mt + 1, rt, ql, qr);
}
else {
update(id * 2, lt, mt, ql, mt);
update(id * 2 + 1, mt + 1, rt, mt + 1, qr);
}
merge(id);
}
void init(int _N, int _M, vector<int> P, vector<int> _A) {
N = _N;
M = _M;
for (int i = 2; i <= N + M; i++) {
ch[P[i - 1] + 1].push_back(i);
}
for (int i = 1; i <= M; i++) {
A[i] = _A[i - 1];
}
dfs1(1);
cont[1] = 1;
dfs2(1);
build(1, 1, M);
}
int count_ways(int L, int R) {
L -= N - 1;
R -= N - 1;
update(1, 1, M, L, R);
return tree[1].sum1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
1 ms |
2768 KB |
Output is correct |
4 |
Correct |
2 ms |
2768 KB |
Output is correct |
5 |
Correct |
1 ms |
2768 KB |
Output is correct |
6 |
Correct |
2 ms |
2768 KB |
Output is correct |
7 |
Correct |
2 ms |
2768 KB |
Output is correct |
8 |
Correct |
2 ms |
2768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2640 KB |
Output is correct |
4 |
Correct |
1 ms |
2668 KB |
Output is correct |
5 |
Correct |
1 ms |
2640 KB |
Output is correct |
6 |
Correct |
1 ms |
2768 KB |
Output is correct |
7 |
Correct |
2 ms |
2768 KB |
Output is correct |
8 |
Correct |
2 ms |
2768 KB |
Output is correct |
9 |
Correct |
2 ms |
2768 KB |
Output is correct |
10 |
Correct |
2 ms |
2896 KB |
Output is correct |
11 |
Correct |
2 ms |
2896 KB |
Output is correct |
12 |
Correct |
2 ms |
2768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
1 ms |
2768 KB |
Output is correct |
4 |
Correct |
2 ms |
2768 KB |
Output is correct |
5 |
Correct |
1 ms |
2768 KB |
Output is correct |
6 |
Correct |
2 ms |
2768 KB |
Output is correct |
7 |
Correct |
2 ms |
2768 KB |
Output is correct |
8 |
Correct |
2 ms |
2768 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
1 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
1 ms |
2668 KB |
Output is correct |
13 |
Correct |
1 ms |
2640 KB |
Output is correct |
14 |
Correct |
1 ms |
2768 KB |
Output is correct |
15 |
Correct |
2 ms |
2768 KB |
Output is correct |
16 |
Correct |
2 ms |
2768 KB |
Output is correct |
17 |
Correct |
2 ms |
2768 KB |
Output is correct |
18 |
Correct |
2 ms |
2896 KB |
Output is correct |
19 |
Correct |
2 ms |
2896 KB |
Output is correct |
20 |
Correct |
2 ms |
2768 KB |
Output is correct |
21 |
Correct |
2 ms |
2768 KB |
Output is correct |
22 |
Correct |
2 ms |
2640 KB |
Output is correct |
23 |
Correct |
2 ms |
2768 KB |
Output is correct |
24 |
Correct |
3 ms |
2768 KB |
Output is correct |
25 |
Correct |
2 ms |
2768 KB |
Output is correct |
26 |
Correct |
2 ms |
2768 KB |
Output is correct |
27 |
Correct |
2 ms |
2792 KB |
Output is correct |
28 |
Correct |
2 ms |
2768 KB |
Output is correct |
29 |
Correct |
2 ms |
2768 KB |
Output is correct |
30 |
Correct |
2 ms |
2768 KB |
Output is correct |
31 |
Correct |
2 ms |
2896 KB |
Output is correct |
32 |
Correct |
2 ms |
2768 KB |
Output is correct |
33 |
Correct |
2 ms |
2768 KB |
Output is correct |
34 |
Correct |
2 ms |
2768 KB |
Output is correct |
35 |
Correct |
1 ms |
2768 KB |
Output is correct |
36 |
Correct |
2 ms |
2896 KB |
Output is correct |
37 |
Correct |
2 ms |
3024 KB |
Output is correct |
38 |
Correct |
2 ms |
3024 KB |
Output is correct |
39 |
Correct |
1 ms |
2768 KB |
Output is correct |
40 |
Correct |
2 ms |
2768 KB |
Output is correct |
41 |
Correct |
2 ms |
2768 KB |
Output is correct |
42 |
Correct |
3 ms |
2768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
525 ms |
7168 KB |
Output is correct |
2 |
Correct |
802 ms |
11608 KB |
Output is correct |
3 |
Correct |
797 ms |
11600 KB |
Output is correct |
4 |
Correct |
673 ms |
11596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
525 ms |
7168 KB |
Output is correct |
2 |
Correct |
802 ms |
11608 KB |
Output is correct |
3 |
Correct |
797 ms |
11600 KB |
Output is correct |
4 |
Correct |
673 ms |
11596 KB |
Output is correct |
5 |
Correct |
561 ms |
7112 KB |
Output is correct |
6 |
Correct |
808 ms |
11624 KB |
Output is correct |
7 |
Correct |
759 ms |
11608 KB |
Output is correct |
8 |
Correct |
652 ms |
11584 KB |
Output is correct |
9 |
Correct |
426 ms |
2896 KB |
Output is correct |
10 |
Correct |
525 ms |
3176 KB |
Output is correct |
11 |
Correct |
637 ms |
3152 KB |
Output is correct |
12 |
Correct |
747 ms |
3164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2640 KB |
Output is correct |
4 |
Correct |
1 ms |
2668 KB |
Output is correct |
5 |
Correct |
1 ms |
2640 KB |
Output is correct |
6 |
Correct |
1 ms |
2768 KB |
Output is correct |
7 |
Correct |
2 ms |
2768 KB |
Output is correct |
8 |
Correct |
2 ms |
2768 KB |
Output is correct |
9 |
Correct |
2 ms |
2768 KB |
Output is correct |
10 |
Correct |
2 ms |
2896 KB |
Output is correct |
11 |
Correct |
2 ms |
2896 KB |
Output is correct |
12 |
Correct |
2 ms |
2768 KB |
Output is correct |
13 |
Correct |
525 ms |
7168 KB |
Output is correct |
14 |
Correct |
802 ms |
11608 KB |
Output is correct |
15 |
Correct |
797 ms |
11600 KB |
Output is correct |
16 |
Correct |
673 ms |
11596 KB |
Output is correct |
17 |
Correct |
561 ms |
7112 KB |
Output is correct |
18 |
Correct |
808 ms |
11624 KB |
Output is correct |
19 |
Correct |
759 ms |
11608 KB |
Output is correct |
20 |
Correct |
652 ms |
11584 KB |
Output is correct |
21 |
Correct |
426 ms |
2896 KB |
Output is correct |
22 |
Correct |
525 ms |
3176 KB |
Output is correct |
23 |
Correct |
637 ms |
3152 KB |
Output is correct |
24 |
Correct |
747 ms |
3164 KB |
Output is correct |
25 |
Correct |
749 ms |
17636 KB |
Output is correct |
26 |
Correct |
862 ms |
17736 KB |
Output is correct |
27 |
Correct |
856 ms |
17748 KB |
Output is correct |
28 |
Correct |
641 ms |
17744 KB |
Output is correct |
29 |
Correct |
835 ms |
38132 KB |
Output is correct |
30 |
Correct |
805 ms |
38092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
1 ms |
2768 KB |
Output is correct |
4 |
Correct |
2 ms |
2768 KB |
Output is correct |
5 |
Correct |
1 ms |
2768 KB |
Output is correct |
6 |
Correct |
2 ms |
2768 KB |
Output is correct |
7 |
Correct |
2 ms |
2768 KB |
Output is correct |
8 |
Correct |
2 ms |
2768 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
1 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
1 ms |
2668 KB |
Output is correct |
13 |
Correct |
1 ms |
2640 KB |
Output is correct |
14 |
Correct |
1 ms |
2768 KB |
Output is correct |
15 |
Correct |
2 ms |
2768 KB |
Output is correct |
16 |
Correct |
2 ms |
2768 KB |
Output is correct |
17 |
Correct |
2 ms |
2768 KB |
Output is correct |
18 |
Correct |
2 ms |
2896 KB |
Output is correct |
19 |
Correct |
2 ms |
2896 KB |
Output is correct |
20 |
Correct |
2 ms |
2768 KB |
Output is correct |
21 |
Correct |
2 ms |
2768 KB |
Output is correct |
22 |
Correct |
2 ms |
2640 KB |
Output is correct |
23 |
Correct |
2 ms |
2768 KB |
Output is correct |
24 |
Correct |
3 ms |
2768 KB |
Output is correct |
25 |
Correct |
2 ms |
2768 KB |
Output is correct |
26 |
Correct |
2 ms |
2768 KB |
Output is correct |
27 |
Correct |
2 ms |
2792 KB |
Output is correct |
28 |
Correct |
2 ms |
2768 KB |
Output is correct |
29 |
Correct |
2 ms |
2768 KB |
Output is correct |
30 |
Correct |
2 ms |
2768 KB |
Output is correct |
31 |
Correct |
2 ms |
2896 KB |
Output is correct |
32 |
Correct |
2 ms |
2768 KB |
Output is correct |
33 |
Correct |
2 ms |
2768 KB |
Output is correct |
34 |
Correct |
2 ms |
2768 KB |
Output is correct |
35 |
Correct |
1 ms |
2768 KB |
Output is correct |
36 |
Correct |
2 ms |
2896 KB |
Output is correct |
37 |
Correct |
2 ms |
3024 KB |
Output is correct |
38 |
Correct |
2 ms |
3024 KB |
Output is correct |
39 |
Correct |
1 ms |
2768 KB |
Output is correct |
40 |
Correct |
2 ms |
2768 KB |
Output is correct |
41 |
Correct |
2 ms |
2768 KB |
Output is correct |
42 |
Correct |
3 ms |
2768 KB |
Output is correct |
43 |
Correct |
416 ms |
3024 KB |
Output is correct |
44 |
Correct |
705 ms |
3152 KB |
Output is correct |
45 |
Correct |
740 ms |
3152 KB |
Output is correct |
46 |
Correct |
693 ms |
3408 KB |
Output is correct |
47 |
Correct |
828 ms |
3408 KB |
Output is correct |
48 |
Correct |
800 ms |
3408 KB |
Output is correct |
49 |
Correct |
561 ms |
3420 KB |
Output is correct |
50 |
Correct |
808 ms |
3408 KB |
Output is correct |
51 |
Correct |
831 ms |
3344 KB |
Output is correct |
52 |
Correct |
710 ms |
3348 KB |
Output is correct |
53 |
Correct |
690 ms |
3920 KB |
Output is correct |
54 |
Correct |
680 ms |
3408 KB |
Output is correct |
55 |
Correct |
732 ms |
3280 KB |
Output is correct |
56 |
Correct |
663 ms |
3280 KB |
Output is correct |
57 |
Correct |
689 ms |
3280 KB |
Output is correct |
58 |
Correct |
720 ms |
4432 KB |
Output is correct |
59 |
Correct |
649 ms |
4560 KB |
Output is correct |
60 |
Correct |
675 ms |
4560 KB |
Output is correct |
61 |
Correct |
702 ms |
3280 KB |
Output is correct |
62 |
Correct |
778 ms |
3024 KB |
Output is correct |
63 |
Correct |
715 ms |
3024 KB |
Output is correct |
64 |
Correct |
582 ms |
3280 KB |
Output is correct |
65 |
Correct |
370 ms |
2896 KB |
Output is correct |
66 |
Correct |
849 ms |
3152 KB |
Output is correct |
67 |
Correct |
601 ms |
3152 KB |
Output is correct |
68 |
Correct |
711 ms |
3152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
1 ms |
2768 KB |
Output is correct |
4 |
Correct |
2 ms |
2768 KB |
Output is correct |
5 |
Correct |
1 ms |
2768 KB |
Output is correct |
6 |
Correct |
2 ms |
2768 KB |
Output is correct |
7 |
Correct |
2 ms |
2768 KB |
Output is correct |
8 |
Correct |
2 ms |
2768 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
1 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
1 ms |
2668 KB |
Output is correct |
13 |
Correct |
1 ms |
2640 KB |
Output is correct |
14 |
Correct |
1 ms |
2768 KB |
Output is correct |
15 |
Correct |
2 ms |
2768 KB |
Output is correct |
16 |
Correct |
2 ms |
2768 KB |
Output is correct |
17 |
Correct |
2 ms |
2768 KB |
Output is correct |
18 |
Correct |
2 ms |
2896 KB |
Output is correct |
19 |
Correct |
2 ms |
2896 KB |
Output is correct |
20 |
Correct |
2 ms |
2768 KB |
Output is correct |
21 |
Correct |
2 ms |
2768 KB |
Output is correct |
22 |
Correct |
2 ms |
2640 KB |
Output is correct |
23 |
Correct |
2 ms |
2768 KB |
Output is correct |
24 |
Correct |
3 ms |
2768 KB |
Output is correct |
25 |
Correct |
2 ms |
2768 KB |
Output is correct |
26 |
Correct |
2 ms |
2768 KB |
Output is correct |
27 |
Correct |
2 ms |
2792 KB |
Output is correct |
28 |
Correct |
2 ms |
2768 KB |
Output is correct |
29 |
Correct |
2 ms |
2768 KB |
Output is correct |
30 |
Correct |
2 ms |
2768 KB |
Output is correct |
31 |
Correct |
2 ms |
2896 KB |
Output is correct |
32 |
Correct |
2 ms |
2768 KB |
Output is correct |
33 |
Correct |
2 ms |
2768 KB |
Output is correct |
34 |
Correct |
2 ms |
2768 KB |
Output is correct |
35 |
Correct |
1 ms |
2768 KB |
Output is correct |
36 |
Correct |
2 ms |
2896 KB |
Output is correct |
37 |
Correct |
2 ms |
3024 KB |
Output is correct |
38 |
Correct |
2 ms |
3024 KB |
Output is correct |
39 |
Correct |
1 ms |
2768 KB |
Output is correct |
40 |
Correct |
2 ms |
2768 KB |
Output is correct |
41 |
Correct |
2 ms |
2768 KB |
Output is correct |
42 |
Correct |
3 ms |
2768 KB |
Output is correct |
43 |
Correct |
525 ms |
7168 KB |
Output is correct |
44 |
Correct |
802 ms |
11608 KB |
Output is correct |
45 |
Correct |
797 ms |
11600 KB |
Output is correct |
46 |
Correct |
673 ms |
11596 KB |
Output is correct |
47 |
Correct |
561 ms |
7112 KB |
Output is correct |
48 |
Correct |
808 ms |
11624 KB |
Output is correct |
49 |
Correct |
759 ms |
11608 KB |
Output is correct |
50 |
Correct |
652 ms |
11584 KB |
Output is correct |
51 |
Correct |
426 ms |
2896 KB |
Output is correct |
52 |
Correct |
525 ms |
3176 KB |
Output is correct |
53 |
Correct |
637 ms |
3152 KB |
Output is correct |
54 |
Correct |
747 ms |
3164 KB |
Output is correct |
55 |
Correct |
749 ms |
17636 KB |
Output is correct |
56 |
Correct |
862 ms |
17736 KB |
Output is correct |
57 |
Correct |
856 ms |
17748 KB |
Output is correct |
58 |
Correct |
641 ms |
17744 KB |
Output is correct |
59 |
Correct |
835 ms |
38132 KB |
Output is correct |
60 |
Correct |
805 ms |
38092 KB |
Output is correct |
61 |
Correct |
416 ms |
3024 KB |
Output is correct |
62 |
Correct |
705 ms |
3152 KB |
Output is correct |
63 |
Correct |
740 ms |
3152 KB |
Output is correct |
64 |
Correct |
693 ms |
3408 KB |
Output is correct |
65 |
Correct |
828 ms |
3408 KB |
Output is correct |
66 |
Correct |
800 ms |
3408 KB |
Output is correct |
67 |
Correct |
561 ms |
3420 KB |
Output is correct |
68 |
Correct |
808 ms |
3408 KB |
Output is correct |
69 |
Correct |
831 ms |
3344 KB |
Output is correct |
70 |
Correct |
710 ms |
3348 KB |
Output is correct |
71 |
Correct |
690 ms |
3920 KB |
Output is correct |
72 |
Correct |
680 ms |
3408 KB |
Output is correct |
73 |
Correct |
732 ms |
3280 KB |
Output is correct |
74 |
Correct |
663 ms |
3280 KB |
Output is correct |
75 |
Correct |
689 ms |
3280 KB |
Output is correct |
76 |
Correct |
720 ms |
4432 KB |
Output is correct |
77 |
Correct |
649 ms |
4560 KB |
Output is correct |
78 |
Correct |
675 ms |
4560 KB |
Output is correct |
79 |
Correct |
702 ms |
3280 KB |
Output is correct |
80 |
Correct |
778 ms |
3024 KB |
Output is correct |
81 |
Correct |
715 ms |
3024 KB |
Output is correct |
82 |
Correct |
582 ms |
3280 KB |
Output is correct |
83 |
Correct |
370 ms |
2896 KB |
Output is correct |
84 |
Correct |
849 ms |
3152 KB |
Output is correct |
85 |
Correct |
601 ms |
3152 KB |
Output is correct |
86 |
Correct |
711 ms |
3152 KB |
Output is correct |
87 |
Correct |
1 ms |
2640 KB |
Output is correct |
88 |
Correct |
485 ms |
16860 KB |
Output is correct |
89 |
Correct |
871 ms |
11780 KB |
Output is correct |
90 |
Correct |
840 ms |
11336 KB |
Output is correct |
91 |
Correct |
714 ms |
17840 KB |
Output is correct |
92 |
Correct |
642 ms |
17880 KB |
Output is correct |
93 |
Correct |
714 ms |
17880 KB |
Output is correct |
94 |
Correct |
818 ms |
17908 KB |
Output is correct |
95 |
Correct |
800 ms |
17916 KB |
Output is correct |
96 |
Correct |
845 ms |
12876 KB |
Output is correct |
97 |
Correct |
801 ms |
12860 KB |
Output is correct |
98 |
Correct |
601 ms |
28380 KB |
Output is correct |
99 |
Correct |
795 ms |
17756 KB |
Output is correct |
100 |
Correct |
811 ms |
15064 KB |
Output is correct |
101 |
Correct |
757 ms |
14152 KB |
Output is correct |
102 |
Correct |
715 ms |
12824 KB |
Output is correct |
103 |
Correct |
781 ms |
38116 KB |
Output is correct |
104 |
Correct |
648 ms |
38464 KB |
Output is correct |
105 |
Correct |
728 ms |
38488 KB |
Output is correct |
106 |
Correct |
723 ms |
15764 KB |
Output is correct |
107 |
Correct |
737 ms |
12504 KB |
Output is correct |
108 |
Correct |
824 ms |
13208 KB |
Output is correct |
109 |
Correct |
700 ms |
12924 KB |
Output is correct |