#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sep ' '
#define debug(x) cerr << #x << ": " << x << endl;
const ll MOD = 1000002022;
const ll MAXN = 1e6 + 10;
ll Z[MAXN], n, m, A[MAXN], CZ[MAXN], pw2;
vector<int> adj[MAXN];
namespace segment {
ll sg[MAXN][2], lz[MAXN];
void build(int l = 0, int r = m - 1, int v = 1) {
if (l == r) sg[v][1 - A[l]] = Z[l];
else {
int mid = (l + r) >> 1;
build(l, mid, v << 1);
build(mid + 1, r, v << 1 | 1);
sg[v][0] = (sg[v << 1][0] + sg[v << 1 | 1][0]) % MOD;
sg[v][1] = (sg[v << 1][1] + sg[v << 1 | 1][1]) % MOD;
}
}
void push(int v) {
if (lz[v]) {
swap(sg[v][0], sg[v][1]);
if ((v << 1 | 1) < MAXN) lz[v << 1] ^= 1, lz[v << 1 | 1] ^= 1;
lz[v] = 0;
}
}
void update(int ql, int qr, int l = 0, int r = m - 1, int v = 1) {
push(v);
if (ql > r || qr < l) return;
if (ql <= l && qr >= r) {
lz[v] ^= 1;
push(v);
return;
}
int mid = (l + r) >> 1;
update(ql, qr, l, mid, v << 1);
update(ql, qr, mid + 1, r, v << 1 | 1);
sg[v][0] = (sg[v << 1][0] + sg[v << 1 | 1][0]) % MOD;
sg[v][1] = (sg[v << 1][1] + sg[v << 1 | 1][1]) % MOD;
}
}
void dfs0(int v) {
CZ[v] = (v < n ? adj[v].size() : 1);
for (int u : adj[v]) {
dfs0(u);
CZ[v] = CZ[v] * CZ[u] % MOD;
}
}
void dfs1(int v, ll z) {
if (v >= n) {
Z[v - n] = z;
return;
}
int d = adj[v].size();
vector<ll> pref_z(d + 2), suff_z(d + 2);
pref_z[0] = 1;
suff_z[d + 1] = 1;
for (int i = 1; i <= d; i++)
pref_z[i] = pref_z[i - 1] * CZ[adj[v][i - 1]] % MOD;
for (int i = d; i > 0; i--)
suff_z[i] = suff_z[i + 1] * CZ[adj[v][i - 1]] % MOD;
for (int i = 1; i <= d; i++) {
dfs1(adj[v][i - 1], z * pref_z[i - 1] % MOD * suff_z[i + 1] % MOD);
}
}
void init(int N_, int M_, vector<int> P, vector<int> A_) {
n = N_, m = M_;
for (int i = 0; i < m; i++)
A[i] = A_[i];
for (int i = 1; i < n + m; i++)
adj[P[i]].push_back(i);
dfs0(0);
dfs1(0, 1);
segment::build();
pw2 = 1;
for (int i = 0; i < m; i++)
pw2 = pw2 * 2 % MOD;
}
int count_ways(int L, int R) {
L -= n, R -= n;
segment::update(L, R);
return segment::sg[1][0] % MOD;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23760 KB |
Output is correct |
2 |
Correct |
11 ms |
23792 KB |
Output is correct |
3 |
Correct |
13 ms |
23872 KB |
Output is correct |
4 |
Correct |
12 ms |
23928 KB |
Output is correct |
5 |
Correct |
14 ms |
23888 KB |
Output is correct |
6 |
Correct |
12 ms |
23888 KB |
Output is correct |
7 |
Correct |
12 ms |
23884 KB |
Output is correct |
8 |
Correct |
11 ms |
23888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23776 KB |
Output is correct |
2 |
Correct |
12 ms |
23740 KB |
Output is correct |
3 |
Correct |
12 ms |
23888 KB |
Output is correct |
4 |
Correct |
12 ms |
23788 KB |
Output is correct |
5 |
Correct |
12 ms |
23888 KB |
Output is correct |
6 |
Correct |
12 ms |
23888 KB |
Output is correct |
7 |
Correct |
13 ms |
23856 KB |
Output is correct |
8 |
Correct |
12 ms |
23960 KB |
Output is correct |
9 |
Correct |
14 ms |
23936 KB |
Output is correct |
10 |
Correct |
12 ms |
24144 KB |
Output is correct |
11 |
Correct |
12 ms |
24084 KB |
Output is correct |
12 |
Correct |
12 ms |
23860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23760 KB |
Output is correct |
2 |
Correct |
11 ms |
23792 KB |
Output is correct |
3 |
Correct |
13 ms |
23872 KB |
Output is correct |
4 |
Correct |
12 ms |
23928 KB |
Output is correct |
5 |
Correct |
14 ms |
23888 KB |
Output is correct |
6 |
Correct |
12 ms |
23888 KB |
Output is correct |
7 |
Correct |
12 ms |
23884 KB |
Output is correct |
8 |
Correct |
11 ms |
23888 KB |
Output is correct |
9 |
Correct |
12 ms |
23776 KB |
Output is correct |
10 |
Correct |
12 ms |
23740 KB |
Output is correct |
11 |
Correct |
12 ms |
23888 KB |
Output is correct |
12 |
Correct |
12 ms |
23788 KB |
Output is correct |
13 |
Correct |
12 ms |
23888 KB |
Output is correct |
14 |
Correct |
12 ms |
23888 KB |
Output is correct |
15 |
Correct |
13 ms |
23856 KB |
Output is correct |
16 |
Correct |
12 ms |
23960 KB |
Output is correct |
17 |
Correct |
14 ms |
23936 KB |
Output is correct |
18 |
Correct |
12 ms |
24144 KB |
Output is correct |
19 |
Correct |
12 ms |
24084 KB |
Output is correct |
20 |
Correct |
12 ms |
23860 KB |
Output is correct |
21 |
Correct |
12 ms |
23948 KB |
Output is correct |
22 |
Correct |
13 ms |
23888 KB |
Output is correct |
23 |
Correct |
12 ms |
23888 KB |
Output is correct |
24 |
Correct |
15 ms |
23916 KB |
Output is correct |
25 |
Correct |
12 ms |
23888 KB |
Output is correct |
26 |
Correct |
12 ms |
23888 KB |
Output is correct |
27 |
Correct |
13 ms |
23860 KB |
Output is correct |
28 |
Correct |
12 ms |
23888 KB |
Output is correct |
29 |
Correct |
12 ms |
23816 KB |
Output is correct |
30 |
Correct |
12 ms |
23868 KB |
Output is correct |
31 |
Correct |
12 ms |
24016 KB |
Output is correct |
32 |
Correct |
12 ms |
23896 KB |
Output is correct |
33 |
Correct |
12 ms |
23888 KB |
Output is correct |
34 |
Correct |
14 ms |
23828 KB |
Output is correct |
35 |
Correct |
14 ms |
23916 KB |
Output is correct |
36 |
Correct |
15 ms |
24144 KB |
Output is correct |
37 |
Correct |
12 ms |
24148 KB |
Output is correct |
38 |
Correct |
14 ms |
24124 KB |
Output is correct |
39 |
Correct |
12 ms |
23888 KB |
Output is correct |
40 |
Correct |
13 ms |
23888 KB |
Output is correct |
41 |
Correct |
12 ms |
23888 KB |
Output is correct |
42 |
Correct |
14 ms |
23888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
562 ms |
28068 KB |
Output is correct |
2 |
Correct |
653 ms |
32300 KB |
Output is correct |
3 |
Correct |
777 ms |
32296 KB |
Output is correct |
4 |
Correct |
728 ms |
32296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
562 ms |
28068 KB |
Output is correct |
2 |
Correct |
653 ms |
32300 KB |
Output is correct |
3 |
Correct |
777 ms |
32296 KB |
Output is correct |
4 |
Correct |
728 ms |
32296 KB |
Output is correct |
5 |
Correct |
637 ms |
28324 KB |
Output is correct |
6 |
Correct |
826 ms |
32892 KB |
Output is correct |
7 |
Correct |
657 ms |
32804 KB |
Output is correct |
8 |
Correct |
721 ms |
31476 KB |
Output is correct |
9 |
Correct |
259 ms |
24016 KB |
Output is correct |
10 |
Correct |
644 ms |
24304 KB |
Output is correct |
11 |
Correct |
713 ms |
24352 KB |
Output is correct |
12 |
Correct |
478 ms |
24296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23776 KB |
Output is correct |
2 |
Correct |
12 ms |
23740 KB |
Output is correct |
3 |
Correct |
12 ms |
23888 KB |
Output is correct |
4 |
Correct |
12 ms |
23788 KB |
Output is correct |
5 |
Correct |
12 ms |
23888 KB |
Output is correct |
6 |
Correct |
12 ms |
23888 KB |
Output is correct |
7 |
Correct |
13 ms |
23856 KB |
Output is correct |
8 |
Correct |
12 ms |
23960 KB |
Output is correct |
9 |
Correct |
14 ms |
23936 KB |
Output is correct |
10 |
Correct |
12 ms |
24144 KB |
Output is correct |
11 |
Correct |
12 ms |
24084 KB |
Output is correct |
12 |
Correct |
12 ms |
23860 KB |
Output is correct |
13 |
Correct |
562 ms |
28068 KB |
Output is correct |
14 |
Correct |
653 ms |
32300 KB |
Output is correct |
15 |
Correct |
777 ms |
32296 KB |
Output is correct |
16 |
Correct |
728 ms |
32296 KB |
Output is correct |
17 |
Correct |
637 ms |
28324 KB |
Output is correct |
18 |
Correct |
826 ms |
32892 KB |
Output is correct |
19 |
Correct |
657 ms |
32804 KB |
Output is correct |
20 |
Correct |
721 ms |
31476 KB |
Output is correct |
21 |
Correct |
259 ms |
24016 KB |
Output is correct |
22 |
Correct |
644 ms |
24304 KB |
Output is correct |
23 |
Correct |
713 ms |
24352 KB |
Output is correct |
24 |
Correct |
478 ms |
24296 KB |
Output is correct |
25 |
Correct |
746 ms |
39344 KB |
Output is correct |
26 |
Correct |
885 ms |
39468 KB |
Output is correct |
27 |
Correct |
826 ms |
39468 KB |
Output is correct |
28 |
Correct |
551 ms |
36460 KB |
Output is correct |
29 |
Correct |
776 ms |
62940 KB |
Output is correct |
30 |
Correct |
709 ms |
59908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23760 KB |
Output is correct |
2 |
Correct |
11 ms |
23792 KB |
Output is correct |
3 |
Correct |
13 ms |
23872 KB |
Output is correct |
4 |
Correct |
12 ms |
23928 KB |
Output is correct |
5 |
Correct |
14 ms |
23888 KB |
Output is correct |
6 |
Correct |
12 ms |
23888 KB |
Output is correct |
7 |
Correct |
12 ms |
23884 KB |
Output is correct |
8 |
Correct |
11 ms |
23888 KB |
Output is correct |
9 |
Correct |
12 ms |
23776 KB |
Output is correct |
10 |
Correct |
12 ms |
23740 KB |
Output is correct |
11 |
Correct |
12 ms |
23888 KB |
Output is correct |
12 |
Correct |
12 ms |
23788 KB |
Output is correct |
13 |
Correct |
12 ms |
23888 KB |
Output is correct |
14 |
Correct |
12 ms |
23888 KB |
Output is correct |
15 |
Correct |
13 ms |
23856 KB |
Output is correct |
16 |
Correct |
12 ms |
23960 KB |
Output is correct |
17 |
Correct |
14 ms |
23936 KB |
Output is correct |
18 |
Correct |
12 ms |
24144 KB |
Output is correct |
19 |
Correct |
12 ms |
24084 KB |
Output is correct |
20 |
Correct |
12 ms |
23860 KB |
Output is correct |
21 |
Correct |
12 ms |
23948 KB |
Output is correct |
22 |
Correct |
13 ms |
23888 KB |
Output is correct |
23 |
Correct |
12 ms |
23888 KB |
Output is correct |
24 |
Correct |
15 ms |
23916 KB |
Output is correct |
25 |
Correct |
12 ms |
23888 KB |
Output is correct |
26 |
Correct |
12 ms |
23888 KB |
Output is correct |
27 |
Correct |
13 ms |
23860 KB |
Output is correct |
28 |
Correct |
12 ms |
23888 KB |
Output is correct |
29 |
Correct |
12 ms |
23816 KB |
Output is correct |
30 |
Correct |
12 ms |
23868 KB |
Output is correct |
31 |
Correct |
12 ms |
24016 KB |
Output is correct |
32 |
Correct |
12 ms |
23896 KB |
Output is correct |
33 |
Correct |
12 ms |
23888 KB |
Output is correct |
34 |
Correct |
14 ms |
23828 KB |
Output is correct |
35 |
Correct |
14 ms |
23916 KB |
Output is correct |
36 |
Correct |
15 ms |
24144 KB |
Output is correct |
37 |
Correct |
12 ms |
24148 KB |
Output is correct |
38 |
Correct |
14 ms |
24124 KB |
Output is correct |
39 |
Correct |
12 ms |
23888 KB |
Output is correct |
40 |
Correct |
13 ms |
23888 KB |
Output is correct |
41 |
Correct |
12 ms |
23888 KB |
Output is correct |
42 |
Correct |
14 ms |
23888 KB |
Output is correct |
43 |
Correct |
428 ms |
24272 KB |
Output is correct |
44 |
Correct |
627 ms |
24340 KB |
Output is correct |
45 |
Correct |
688 ms |
24272 KB |
Output is correct |
46 |
Correct |
412 ms |
24724 KB |
Output is correct |
47 |
Correct |
773 ms |
24752 KB |
Output is correct |
48 |
Correct |
826 ms |
24732 KB |
Output is correct |
49 |
Correct |
812 ms |
24656 KB |
Output is correct |
50 |
Correct |
753 ms |
24524 KB |
Output is correct |
51 |
Correct |
730 ms |
24420 KB |
Output is correct |
52 |
Correct |
682 ms |
24644 KB |
Output is correct |
53 |
Correct |
640 ms |
25040 KB |
Output is correct |
54 |
Correct |
732 ms |
24680 KB |
Output is correct |
55 |
Correct |
696 ms |
24528 KB |
Output is correct |
56 |
Correct |
745 ms |
24528 KB |
Output is correct |
57 |
Correct |
615 ms |
24528 KB |
Output is correct |
58 |
Correct |
725 ms |
25932 KB |
Output is correct |
59 |
Correct |
677 ms |
25808 KB |
Output is correct |
60 |
Correct |
645 ms |
25680 KB |
Output is correct |
61 |
Correct |
704 ms |
24528 KB |
Output is correct |
62 |
Correct |
764 ms |
24272 KB |
Output is correct |
63 |
Correct |
776 ms |
24204 KB |
Output is correct |
64 |
Correct |
736 ms |
24528 KB |
Output is correct |
65 |
Correct |
322 ms |
24016 KB |
Output is correct |
66 |
Correct |
751 ms |
24400 KB |
Output is correct |
67 |
Correct |
691 ms |
24400 KB |
Output is correct |
68 |
Correct |
763 ms |
24228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23760 KB |
Output is correct |
2 |
Correct |
11 ms |
23792 KB |
Output is correct |
3 |
Correct |
13 ms |
23872 KB |
Output is correct |
4 |
Correct |
12 ms |
23928 KB |
Output is correct |
5 |
Correct |
14 ms |
23888 KB |
Output is correct |
6 |
Correct |
12 ms |
23888 KB |
Output is correct |
7 |
Correct |
12 ms |
23884 KB |
Output is correct |
8 |
Correct |
11 ms |
23888 KB |
Output is correct |
9 |
Correct |
12 ms |
23776 KB |
Output is correct |
10 |
Correct |
12 ms |
23740 KB |
Output is correct |
11 |
Correct |
12 ms |
23888 KB |
Output is correct |
12 |
Correct |
12 ms |
23788 KB |
Output is correct |
13 |
Correct |
12 ms |
23888 KB |
Output is correct |
14 |
Correct |
12 ms |
23888 KB |
Output is correct |
15 |
Correct |
13 ms |
23856 KB |
Output is correct |
16 |
Correct |
12 ms |
23960 KB |
Output is correct |
17 |
Correct |
14 ms |
23936 KB |
Output is correct |
18 |
Correct |
12 ms |
24144 KB |
Output is correct |
19 |
Correct |
12 ms |
24084 KB |
Output is correct |
20 |
Correct |
12 ms |
23860 KB |
Output is correct |
21 |
Correct |
12 ms |
23948 KB |
Output is correct |
22 |
Correct |
13 ms |
23888 KB |
Output is correct |
23 |
Correct |
12 ms |
23888 KB |
Output is correct |
24 |
Correct |
15 ms |
23916 KB |
Output is correct |
25 |
Correct |
12 ms |
23888 KB |
Output is correct |
26 |
Correct |
12 ms |
23888 KB |
Output is correct |
27 |
Correct |
13 ms |
23860 KB |
Output is correct |
28 |
Correct |
12 ms |
23888 KB |
Output is correct |
29 |
Correct |
12 ms |
23816 KB |
Output is correct |
30 |
Correct |
12 ms |
23868 KB |
Output is correct |
31 |
Correct |
12 ms |
24016 KB |
Output is correct |
32 |
Correct |
12 ms |
23896 KB |
Output is correct |
33 |
Correct |
12 ms |
23888 KB |
Output is correct |
34 |
Correct |
14 ms |
23828 KB |
Output is correct |
35 |
Correct |
14 ms |
23916 KB |
Output is correct |
36 |
Correct |
15 ms |
24144 KB |
Output is correct |
37 |
Correct |
12 ms |
24148 KB |
Output is correct |
38 |
Correct |
14 ms |
24124 KB |
Output is correct |
39 |
Correct |
12 ms |
23888 KB |
Output is correct |
40 |
Correct |
13 ms |
23888 KB |
Output is correct |
41 |
Correct |
12 ms |
23888 KB |
Output is correct |
42 |
Correct |
14 ms |
23888 KB |
Output is correct |
43 |
Correct |
562 ms |
28068 KB |
Output is correct |
44 |
Correct |
653 ms |
32300 KB |
Output is correct |
45 |
Correct |
777 ms |
32296 KB |
Output is correct |
46 |
Correct |
728 ms |
32296 KB |
Output is correct |
47 |
Correct |
637 ms |
28324 KB |
Output is correct |
48 |
Correct |
826 ms |
32892 KB |
Output is correct |
49 |
Correct |
657 ms |
32804 KB |
Output is correct |
50 |
Correct |
721 ms |
31476 KB |
Output is correct |
51 |
Correct |
259 ms |
24016 KB |
Output is correct |
52 |
Correct |
644 ms |
24304 KB |
Output is correct |
53 |
Correct |
713 ms |
24352 KB |
Output is correct |
54 |
Correct |
478 ms |
24296 KB |
Output is correct |
55 |
Correct |
746 ms |
39344 KB |
Output is correct |
56 |
Correct |
885 ms |
39468 KB |
Output is correct |
57 |
Correct |
826 ms |
39468 KB |
Output is correct |
58 |
Correct |
551 ms |
36460 KB |
Output is correct |
59 |
Correct |
776 ms |
62940 KB |
Output is correct |
60 |
Correct |
709 ms |
59908 KB |
Output is correct |
61 |
Correct |
428 ms |
24272 KB |
Output is correct |
62 |
Correct |
627 ms |
24340 KB |
Output is correct |
63 |
Correct |
688 ms |
24272 KB |
Output is correct |
64 |
Correct |
412 ms |
24724 KB |
Output is correct |
65 |
Correct |
773 ms |
24752 KB |
Output is correct |
66 |
Correct |
826 ms |
24732 KB |
Output is correct |
67 |
Correct |
812 ms |
24656 KB |
Output is correct |
68 |
Correct |
753 ms |
24524 KB |
Output is correct |
69 |
Correct |
730 ms |
24420 KB |
Output is correct |
70 |
Correct |
682 ms |
24644 KB |
Output is correct |
71 |
Correct |
640 ms |
25040 KB |
Output is correct |
72 |
Correct |
732 ms |
24680 KB |
Output is correct |
73 |
Correct |
696 ms |
24528 KB |
Output is correct |
74 |
Correct |
745 ms |
24528 KB |
Output is correct |
75 |
Correct |
615 ms |
24528 KB |
Output is correct |
76 |
Correct |
725 ms |
25932 KB |
Output is correct |
77 |
Correct |
677 ms |
25808 KB |
Output is correct |
78 |
Correct |
645 ms |
25680 KB |
Output is correct |
79 |
Correct |
704 ms |
24528 KB |
Output is correct |
80 |
Correct |
764 ms |
24272 KB |
Output is correct |
81 |
Correct |
776 ms |
24204 KB |
Output is correct |
82 |
Correct |
736 ms |
24528 KB |
Output is correct |
83 |
Correct |
322 ms |
24016 KB |
Output is correct |
84 |
Correct |
751 ms |
24400 KB |
Output is correct |
85 |
Correct |
691 ms |
24400 KB |
Output is correct |
86 |
Correct |
763 ms |
24228 KB |
Output is correct |
87 |
Correct |
12 ms |
23756 KB |
Output is correct |
88 |
Correct |
544 ms |
38760 KB |
Output is correct |
89 |
Correct |
728 ms |
32852 KB |
Output is correct |
90 |
Correct |
803 ms |
32644 KB |
Output is correct |
91 |
Correct |
795 ms |
39072 KB |
Output is correct |
92 |
Correct |
875 ms |
39580 KB |
Output is correct |
93 |
Correct |
893 ms |
39584 KB |
Output is correct |
94 |
Correct |
781 ms |
39576 KB |
Output is correct |
95 |
Correct |
798 ms |
36552 KB |
Output is correct |
96 |
Correct |
692 ms |
32324 KB |
Output is correct |
97 |
Correct |
930 ms |
35676 KB |
Output is correct |
98 |
Correct |
525 ms |
48872 KB |
Output is correct |
99 |
Correct |
925 ms |
39460 KB |
Output is correct |
100 |
Correct |
667 ms |
37396 KB |
Output is correct |
101 |
Correct |
741 ms |
36752 KB |
Output is correct |
102 |
Correct |
655 ms |
35684 KB |
Output is correct |
103 |
Correct |
933 ms |
62936 KB |
Output is correct |
104 |
Correct |
823 ms |
60200 KB |
Output is correct |
105 |
Correct |
708 ms |
57280 KB |
Output is correct |
106 |
Correct |
907 ms |
37384 KB |
Output is correct |
107 |
Correct |
790 ms |
35828 KB |
Output is correct |
108 |
Correct |
793 ms |
35864 KB |
Output is correct |
109 |
Correct |
757 ms |
36028 KB |
Output is correct |