#include "circuit.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
const ll MOD = 1e9 + 2022;
const int mxN = 1e5 + 10;
vector<int> adj[2 * mxN];
int n, m;
bool state[2 * mxN];
ll contrib[2 * mxN];
void dfs1(int u = 0) {
if (u >= n) {
contrib[u] = 1; return;
}
contrib[u] = adj[u].size();
for (int v : adj[u]) {
dfs1(v);
contrib[u] = (contrib[u] * contrib[v]) % MOD;
}
}
void dfs2(int u = 0, ll pass = 1) {
if (u >= n) {
contrib[u] = pass; return;
}
int sz = adj[u].size();
vector<ll> pref(sz + 2), suff(sz + 2);
pref[0] = 1;
for (int i = 1; i <= sz; i++) {
pref[i] = (pref[i - 1] * contrib[adj[u][i - 1]]) % MOD;
}
suff[sz + 1] = 1;
for (int i = sz; i >= 1; i--) {
suff[i] = (suff[i + 1] * contrib[adj[u][i - 1]]) % MOD;
}
for (int i = 1; i <= sz; i++) {
ll nxt = pass;
nxt = (nxt * pref[i - 1]) % MOD;
nxt = (nxt * suff[i + 1]) % MOD;
dfs2(adj[u][i - 1], nxt);
}
}
ll seg[4 * mxN], tot[4 * mxN];
bool lazy[4 * mxN];
void pushdown(int idx) {
if (!lazy[idx]) return;
seg[(idx << 1)] = (tot[(idx << 1)] - seg[(idx << 1)] + MOD) % MOD;
seg[(idx << 1) | 1] = (tot[(idx << 1) | 1] - seg[(idx << 1) | 1] + MOD) % MOD;
lazy[(idx << 1)] ^= 1; lazy[(idx << 1) | 1] ^= 1;
lazy[idx] = false;
}
void build(int l = n, int r = n + m - 1, int idx = 1) {
if (l == r) {
seg[idx] = (state[l] ? contrib[l] : 0LL);
tot[idx] = contrib[l];
return;
}
int mid = (l + r) >> 1;
build(l, mid, (idx << 1));
build(mid + 1, r, (idx << 1) | 1);
seg[idx] = (seg[(idx << 1)] + seg[(idx << 1) | 1]) % MOD;
tot[idx] = (tot[(idx << 1)] + tot[(idx << 1) | 1]) % MOD;
}
void update(int tl, int tr, int l = n, int r = n + m - 1, int idx = 1) {
if (tl <= l && r <= tr) {
seg[idx] = (tot[idx] - seg[idx] + MOD) % MOD;
lazy[idx] ^= 1;
return;
}
pushdown(idx);
int mid = (l + r) >> 1;
if (tl <= mid) update(tl, tr, l, mid, (idx << 1));
if (tr > mid) update(tl, tr, mid + 1, r, (idx << 1) | 1);
seg[idx] = (seg[(idx << 1)] + seg[(idx << 1) | 1]) % MOD;
}
ll query() {
return seg[1];
}
void init(int N, int M, vector<int> P, vector<int> A) {
n = N; m = M;
for (int i = 1; i < (int)(P.size()); i++) adj[P[i]].pb(i);
for (int i = 0; i < (int)(A.size()); i++) state[n + i] = A[i];
dfs1(); dfs2();
build();
}
int count_ways(int L, int R) {
update(L, R);
return (int)(query());
}
/*
Observations:
Consider a particular source gate
Consider the set of solutions that toggles as the source gate toggles
(i.e. becomes valid/invalid as the source gate becomes on/off)
Then it can be shown that it is independent from other values
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11096 KB |
Output is correct |
2 |
Correct |
3 ms |
11096 KB |
Output is correct |
3 |
Correct |
2 ms |
11352 KB |
Output is correct |
4 |
Correct |
2 ms |
11352 KB |
Output is correct |
5 |
Correct |
2 ms |
11352 KB |
Output is correct |
6 |
Correct |
2 ms |
11352 KB |
Output is correct |
7 |
Correct |
2 ms |
11352 KB |
Output is correct |
8 |
Correct |
3 ms |
11352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11096 KB |
Output is correct |
2 |
Correct |
3 ms |
11188 KB |
Output is correct |
3 |
Correct |
2 ms |
11096 KB |
Output is correct |
4 |
Correct |
2 ms |
11352 KB |
Output is correct |
5 |
Correct |
2 ms |
11096 KB |
Output is correct |
6 |
Correct |
2 ms |
11352 KB |
Output is correct |
7 |
Correct |
2 ms |
11352 KB |
Output is correct |
8 |
Correct |
3 ms |
11352 KB |
Output is correct |
9 |
Correct |
3 ms |
11352 KB |
Output is correct |
10 |
Correct |
2 ms |
11352 KB |
Output is correct |
11 |
Correct |
2 ms |
11436 KB |
Output is correct |
12 |
Correct |
3 ms |
11352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11096 KB |
Output is correct |
2 |
Correct |
3 ms |
11096 KB |
Output is correct |
3 |
Correct |
2 ms |
11352 KB |
Output is correct |
4 |
Correct |
2 ms |
11352 KB |
Output is correct |
5 |
Correct |
2 ms |
11352 KB |
Output is correct |
6 |
Correct |
2 ms |
11352 KB |
Output is correct |
7 |
Correct |
2 ms |
11352 KB |
Output is correct |
8 |
Correct |
3 ms |
11352 KB |
Output is correct |
9 |
Correct |
2 ms |
11096 KB |
Output is correct |
10 |
Correct |
3 ms |
11188 KB |
Output is correct |
11 |
Correct |
2 ms |
11096 KB |
Output is correct |
12 |
Correct |
2 ms |
11352 KB |
Output is correct |
13 |
Correct |
2 ms |
11096 KB |
Output is correct |
14 |
Correct |
2 ms |
11352 KB |
Output is correct |
15 |
Correct |
2 ms |
11352 KB |
Output is correct |
16 |
Correct |
3 ms |
11352 KB |
Output is correct |
17 |
Correct |
3 ms |
11352 KB |
Output is correct |
18 |
Correct |
2 ms |
11352 KB |
Output is correct |
19 |
Correct |
2 ms |
11436 KB |
Output is correct |
20 |
Correct |
3 ms |
11352 KB |
Output is correct |
21 |
Correct |
2 ms |
11372 KB |
Output is correct |
22 |
Correct |
2 ms |
11352 KB |
Output is correct |
23 |
Correct |
4 ms |
11352 KB |
Output is correct |
24 |
Correct |
3 ms |
11352 KB |
Output is correct |
25 |
Correct |
3 ms |
11352 KB |
Output is correct |
26 |
Correct |
2 ms |
11352 KB |
Output is correct |
27 |
Correct |
2 ms |
11352 KB |
Output is correct |
28 |
Correct |
3 ms |
11604 KB |
Output is correct |
29 |
Correct |
2 ms |
11352 KB |
Output is correct |
30 |
Correct |
2 ms |
11352 KB |
Output is correct |
31 |
Correct |
2 ms |
11352 KB |
Output is correct |
32 |
Correct |
3 ms |
11352 KB |
Output is correct |
33 |
Correct |
2 ms |
11352 KB |
Output is correct |
34 |
Correct |
3 ms |
11356 KB |
Output is correct |
35 |
Correct |
2 ms |
11096 KB |
Output is correct |
36 |
Correct |
2 ms |
11352 KB |
Output is correct |
37 |
Correct |
3 ms |
11352 KB |
Output is correct |
38 |
Correct |
3 ms |
11352 KB |
Output is correct |
39 |
Correct |
3 ms |
11352 KB |
Output is correct |
40 |
Correct |
2 ms |
11352 KB |
Output is correct |
41 |
Correct |
2 ms |
11096 KB |
Output is correct |
42 |
Correct |
3 ms |
11352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
407 ms |
12888 KB |
Output is correct |
2 |
Correct |
634 ms |
16940 KB |
Output is correct |
3 |
Correct |
613 ms |
16948 KB |
Output is correct |
4 |
Correct |
578 ms |
16720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
407 ms |
12888 KB |
Output is correct |
2 |
Correct |
634 ms |
16940 KB |
Output is correct |
3 |
Correct |
613 ms |
16948 KB |
Output is correct |
4 |
Correct |
578 ms |
16720 KB |
Output is correct |
5 |
Correct |
454 ms |
12988 KB |
Output is correct |
6 |
Correct |
640 ms |
16952 KB |
Output is correct |
7 |
Correct |
584 ms |
17020 KB |
Output is correct |
8 |
Correct |
592 ms |
16960 KB |
Output is correct |
9 |
Correct |
270 ms |
11352 KB |
Output is correct |
10 |
Correct |
551 ms |
11352 KB |
Output is correct |
11 |
Correct |
558 ms |
11352 KB |
Output is correct |
12 |
Correct |
531 ms |
11352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11096 KB |
Output is correct |
2 |
Correct |
3 ms |
11188 KB |
Output is correct |
3 |
Correct |
2 ms |
11096 KB |
Output is correct |
4 |
Correct |
2 ms |
11352 KB |
Output is correct |
5 |
Correct |
2 ms |
11096 KB |
Output is correct |
6 |
Correct |
2 ms |
11352 KB |
Output is correct |
7 |
Correct |
2 ms |
11352 KB |
Output is correct |
8 |
Correct |
3 ms |
11352 KB |
Output is correct |
9 |
Correct |
3 ms |
11352 KB |
Output is correct |
10 |
Correct |
2 ms |
11352 KB |
Output is correct |
11 |
Correct |
2 ms |
11436 KB |
Output is correct |
12 |
Correct |
3 ms |
11352 KB |
Output is correct |
13 |
Correct |
407 ms |
12888 KB |
Output is correct |
14 |
Correct |
634 ms |
16940 KB |
Output is correct |
15 |
Correct |
613 ms |
16948 KB |
Output is correct |
16 |
Correct |
578 ms |
16720 KB |
Output is correct |
17 |
Correct |
454 ms |
12988 KB |
Output is correct |
18 |
Correct |
640 ms |
16952 KB |
Output is correct |
19 |
Correct |
584 ms |
17020 KB |
Output is correct |
20 |
Correct |
592 ms |
16960 KB |
Output is correct |
21 |
Correct |
270 ms |
11352 KB |
Output is correct |
22 |
Correct |
551 ms |
11352 KB |
Output is correct |
23 |
Correct |
558 ms |
11352 KB |
Output is correct |
24 |
Correct |
531 ms |
11352 KB |
Output is correct |
25 |
Correct |
653 ms |
18764 KB |
Output is correct |
26 |
Correct |
676 ms |
18616 KB |
Output is correct |
27 |
Correct |
658 ms |
18864 KB |
Output is correct |
28 |
Correct |
516 ms |
18628 KB |
Output is correct |
29 |
Correct |
671 ms |
42172 KB |
Output is correct |
30 |
Correct |
643 ms |
42176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11096 KB |
Output is correct |
2 |
Correct |
3 ms |
11096 KB |
Output is correct |
3 |
Correct |
2 ms |
11352 KB |
Output is correct |
4 |
Correct |
2 ms |
11352 KB |
Output is correct |
5 |
Correct |
2 ms |
11352 KB |
Output is correct |
6 |
Correct |
2 ms |
11352 KB |
Output is correct |
7 |
Correct |
2 ms |
11352 KB |
Output is correct |
8 |
Correct |
3 ms |
11352 KB |
Output is correct |
9 |
Correct |
2 ms |
11096 KB |
Output is correct |
10 |
Correct |
3 ms |
11188 KB |
Output is correct |
11 |
Correct |
2 ms |
11096 KB |
Output is correct |
12 |
Correct |
2 ms |
11352 KB |
Output is correct |
13 |
Correct |
2 ms |
11096 KB |
Output is correct |
14 |
Correct |
2 ms |
11352 KB |
Output is correct |
15 |
Correct |
2 ms |
11352 KB |
Output is correct |
16 |
Correct |
3 ms |
11352 KB |
Output is correct |
17 |
Correct |
3 ms |
11352 KB |
Output is correct |
18 |
Correct |
2 ms |
11352 KB |
Output is correct |
19 |
Correct |
2 ms |
11436 KB |
Output is correct |
20 |
Correct |
3 ms |
11352 KB |
Output is correct |
21 |
Correct |
2 ms |
11372 KB |
Output is correct |
22 |
Correct |
2 ms |
11352 KB |
Output is correct |
23 |
Correct |
4 ms |
11352 KB |
Output is correct |
24 |
Correct |
3 ms |
11352 KB |
Output is correct |
25 |
Correct |
3 ms |
11352 KB |
Output is correct |
26 |
Correct |
2 ms |
11352 KB |
Output is correct |
27 |
Correct |
2 ms |
11352 KB |
Output is correct |
28 |
Correct |
3 ms |
11604 KB |
Output is correct |
29 |
Correct |
2 ms |
11352 KB |
Output is correct |
30 |
Correct |
2 ms |
11352 KB |
Output is correct |
31 |
Correct |
2 ms |
11352 KB |
Output is correct |
32 |
Correct |
3 ms |
11352 KB |
Output is correct |
33 |
Correct |
2 ms |
11352 KB |
Output is correct |
34 |
Correct |
3 ms |
11356 KB |
Output is correct |
35 |
Correct |
2 ms |
11096 KB |
Output is correct |
36 |
Correct |
2 ms |
11352 KB |
Output is correct |
37 |
Correct |
3 ms |
11352 KB |
Output is correct |
38 |
Correct |
3 ms |
11352 KB |
Output is correct |
39 |
Correct |
3 ms |
11352 KB |
Output is correct |
40 |
Correct |
2 ms |
11352 KB |
Output is correct |
41 |
Correct |
2 ms |
11096 KB |
Output is correct |
42 |
Correct |
3 ms |
11352 KB |
Output is correct |
43 |
Correct |
431 ms |
11352 KB |
Output is correct |
44 |
Correct |
560 ms |
11352 KB |
Output is correct |
45 |
Correct |
569 ms |
11352 KB |
Output is correct |
46 |
Correct |
522 ms |
11608 KB |
Output is correct |
47 |
Correct |
534 ms |
11608 KB |
Output is correct |
48 |
Correct |
582 ms |
11780 KB |
Output is correct |
49 |
Correct |
566 ms |
11596 KB |
Output is correct |
50 |
Correct |
518 ms |
11608 KB |
Output is correct |
51 |
Correct |
532 ms |
11516 KB |
Output is correct |
52 |
Correct |
589 ms |
11516 KB |
Output is correct |
53 |
Correct |
562 ms |
12376 KB |
Output is correct |
54 |
Correct |
527 ms |
11608 KB |
Output is correct |
55 |
Correct |
544 ms |
11352 KB |
Output is correct |
56 |
Correct |
588 ms |
11608 KB |
Output is correct |
57 |
Correct |
551 ms |
11352 KB |
Output is correct |
58 |
Correct |
564 ms |
12632 KB |
Output is correct |
59 |
Correct |
587 ms |
12884 KB |
Output is correct |
60 |
Correct |
558 ms |
12832 KB |
Output is correct |
61 |
Correct |
542 ms |
11608 KB |
Output is correct |
62 |
Correct |
582 ms |
11352 KB |
Output is correct |
63 |
Correct |
621 ms |
11352 KB |
Output is correct |
64 |
Correct |
537 ms |
11608 KB |
Output is correct |
65 |
Correct |
279 ms |
11352 KB |
Output is correct |
66 |
Correct |
565 ms |
11352 KB |
Output is correct |
67 |
Correct |
533 ms |
11352 KB |
Output is correct |
68 |
Correct |
525 ms |
11352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11096 KB |
Output is correct |
2 |
Correct |
3 ms |
11096 KB |
Output is correct |
3 |
Correct |
2 ms |
11352 KB |
Output is correct |
4 |
Correct |
2 ms |
11352 KB |
Output is correct |
5 |
Correct |
2 ms |
11352 KB |
Output is correct |
6 |
Correct |
2 ms |
11352 KB |
Output is correct |
7 |
Correct |
2 ms |
11352 KB |
Output is correct |
8 |
Correct |
3 ms |
11352 KB |
Output is correct |
9 |
Correct |
2 ms |
11096 KB |
Output is correct |
10 |
Correct |
3 ms |
11188 KB |
Output is correct |
11 |
Correct |
2 ms |
11096 KB |
Output is correct |
12 |
Correct |
2 ms |
11352 KB |
Output is correct |
13 |
Correct |
2 ms |
11096 KB |
Output is correct |
14 |
Correct |
2 ms |
11352 KB |
Output is correct |
15 |
Correct |
2 ms |
11352 KB |
Output is correct |
16 |
Correct |
3 ms |
11352 KB |
Output is correct |
17 |
Correct |
3 ms |
11352 KB |
Output is correct |
18 |
Correct |
2 ms |
11352 KB |
Output is correct |
19 |
Correct |
2 ms |
11436 KB |
Output is correct |
20 |
Correct |
3 ms |
11352 KB |
Output is correct |
21 |
Correct |
2 ms |
11372 KB |
Output is correct |
22 |
Correct |
2 ms |
11352 KB |
Output is correct |
23 |
Correct |
4 ms |
11352 KB |
Output is correct |
24 |
Correct |
3 ms |
11352 KB |
Output is correct |
25 |
Correct |
3 ms |
11352 KB |
Output is correct |
26 |
Correct |
2 ms |
11352 KB |
Output is correct |
27 |
Correct |
2 ms |
11352 KB |
Output is correct |
28 |
Correct |
3 ms |
11604 KB |
Output is correct |
29 |
Correct |
2 ms |
11352 KB |
Output is correct |
30 |
Correct |
2 ms |
11352 KB |
Output is correct |
31 |
Correct |
2 ms |
11352 KB |
Output is correct |
32 |
Correct |
3 ms |
11352 KB |
Output is correct |
33 |
Correct |
2 ms |
11352 KB |
Output is correct |
34 |
Correct |
3 ms |
11356 KB |
Output is correct |
35 |
Correct |
2 ms |
11096 KB |
Output is correct |
36 |
Correct |
2 ms |
11352 KB |
Output is correct |
37 |
Correct |
3 ms |
11352 KB |
Output is correct |
38 |
Correct |
3 ms |
11352 KB |
Output is correct |
39 |
Correct |
3 ms |
11352 KB |
Output is correct |
40 |
Correct |
2 ms |
11352 KB |
Output is correct |
41 |
Correct |
2 ms |
11096 KB |
Output is correct |
42 |
Correct |
3 ms |
11352 KB |
Output is correct |
43 |
Correct |
407 ms |
12888 KB |
Output is correct |
44 |
Correct |
634 ms |
16940 KB |
Output is correct |
45 |
Correct |
613 ms |
16948 KB |
Output is correct |
46 |
Correct |
578 ms |
16720 KB |
Output is correct |
47 |
Correct |
454 ms |
12988 KB |
Output is correct |
48 |
Correct |
640 ms |
16952 KB |
Output is correct |
49 |
Correct |
584 ms |
17020 KB |
Output is correct |
50 |
Correct |
592 ms |
16960 KB |
Output is correct |
51 |
Correct |
270 ms |
11352 KB |
Output is correct |
52 |
Correct |
551 ms |
11352 KB |
Output is correct |
53 |
Correct |
558 ms |
11352 KB |
Output is correct |
54 |
Correct |
531 ms |
11352 KB |
Output is correct |
55 |
Correct |
653 ms |
18764 KB |
Output is correct |
56 |
Correct |
676 ms |
18616 KB |
Output is correct |
57 |
Correct |
658 ms |
18864 KB |
Output is correct |
58 |
Correct |
516 ms |
18628 KB |
Output is correct |
59 |
Correct |
671 ms |
42172 KB |
Output is correct |
60 |
Correct |
643 ms |
42176 KB |
Output is correct |
61 |
Correct |
431 ms |
11352 KB |
Output is correct |
62 |
Correct |
560 ms |
11352 KB |
Output is correct |
63 |
Correct |
569 ms |
11352 KB |
Output is correct |
64 |
Correct |
522 ms |
11608 KB |
Output is correct |
65 |
Correct |
534 ms |
11608 KB |
Output is correct |
66 |
Correct |
582 ms |
11780 KB |
Output is correct |
67 |
Correct |
566 ms |
11596 KB |
Output is correct |
68 |
Correct |
518 ms |
11608 KB |
Output is correct |
69 |
Correct |
532 ms |
11516 KB |
Output is correct |
70 |
Correct |
589 ms |
11516 KB |
Output is correct |
71 |
Correct |
562 ms |
12376 KB |
Output is correct |
72 |
Correct |
527 ms |
11608 KB |
Output is correct |
73 |
Correct |
544 ms |
11352 KB |
Output is correct |
74 |
Correct |
588 ms |
11608 KB |
Output is correct |
75 |
Correct |
551 ms |
11352 KB |
Output is correct |
76 |
Correct |
564 ms |
12632 KB |
Output is correct |
77 |
Correct |
587 ms |
12884 KB |
Output is correct |
78 |
Correct |
558 ms |
12832 KB |
Output is correct |
79 |
Correct |
542 ms |
11608 KB |
Output is correct |
80 |
Correct |
582 ms |
11352 KB |
Output is correct |
81 |
Correct |
621 ms |
11352 KB |
Output is correct |
82 |
Correct |
537 ms |
11608 KB |
Output is correct |
83 |
Correct |
279 ms |
11352 KB |
Output is correct |
84 |
Correct |
565 ms |
11352 KB |
Output is correct |
85 |
Correct |
533 ms |
11352 KB |
Output is correct |
86 |
Correct |
525 ms |
11352 KB |
Output is correct |
87 |
Correct |
2 ms |
11096 KB |
Output is correct |
88 |
Correct |
371 ms |
18196 KB |
Output is correct |
89 |
Correct |
599 ms |
17128 KB |
Output is correct |
90 |
Correct |
668 ms |
16872 KB |
Output is correct |
91 |
Correct |
639 ms |
18976 KB |
Output is correct |
92 |
Correct |
734 ms |
18876 KB |
Output is correct |
93 |
Correct |
664 ms |
19148 KB |
Output is correct |
94 |
Correct |
691 ms |
18884 KB |
Output is correct |
95 |
Correct |
622 ms |
18936 KB |
Output is correct |
96 |
Correct |
564 ms |
15280 KB |
Output is correct |
97 |
Correct |
615 ms |
15280 KB |
Output is correct |
98 |
Correct |
563 ms |
35524 KB |
Output is correct |
99 |
Correct |
666 ms |
18632 KB |
Output is correct |
100 |
Correct |
669 ms |
16904 KB |
Output is correct |
101 |
Correct |
665 ms |
16208 KB |
Output is correct |
102 |
Correct |
687 ms |
15448 KB |
Output is correct |
103 |
Correct |
706 ms |
42176 KB |
Output is correct |
104 |
Correct |
657 ms |
39468 KB |
Output is correct |
105 |
Correct |
612 ms |
39464 KB |
Output is correct |
106 |
Correct |
626 ms |
21896 KB |
Output is correct |
107 |
Correct |
649 ms |
16536 KB |
Output is correct |
108 |
Correct |
627 ms |
15616 KB |
Output is correct |
109 |
Correct |
707 ms |
15692 KB |
Output is correct |