#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int mod = 1000002022;
int add(int x, int y) {
int z = x + y;
if (z >= mod) {
z -= mod;
}
return z;
}
int sub(int x, int y) {
int z = x - y;
if (z < 0) {
z += mod;
}
return z;
}
int mul(int x, int y) {
return (ll)x * y % mod;
}
int N, M, K;
vector<int> P, A, tot, lazy, S, T;
void apply(int p, int v) {
if (v) {
S[p] = sub(T[p], S[p]);
lazy[p] ^= 1;
}
}
void push(int p) {
apply(p * 2 + 1, lazy[p]);
apply(p * 2 + 2, lazy[p]);
lazy[p] = 0;
}
void pull(int p) {
S[p] = add(S[p * 2 + 1], S[p * 2 + 2]);
}
void update(int p, int l, int r, int L, int R) {
if (R <= l || r <= L) {
return;
}
if (L <= l && r <= R) {
apply(p, 1);
return;
}
int m = (l + r + 1) / 2;
push(p);
update(p * 2 + 1, l, m, L, R);
update(p * 2 + 2, m, r, L, R);
pull(p);
}
int query(int p, int l, int r, int L, int R) {
if (R <= l || r <= L) {
return 0;
}
if (L <= l && r <= R) {
return S[p];
}
int m = (l + r + 1) / 2;
push(p);
return add(query(p * 2 + 1, l, m, L, R), query(p * 2 + 2, m, r, L, R));
}
void init(int n, int m, vector<int> p, vector<int> a) {
N = n, M = m;
P = p, A = a;
vector<vector<int>> ch(N);
for (int i = 1; i < N + M; i++) {
ch[P[i]].push_back(i);
}
tot.resize(N + M);
for (int i = N; i < N + M; i++) {
tot[i] = 1;
}
for (int i = N - 1; i >= 0; i--) {
tot[i] = ch[i].size();
for (int j : ch[i]) {
tot[i] = mul(tot[i], tot[j]);
}
}
vector<int> f(N + M);
f[0] = 1;
for (int i = 1; i < N + M; i++) {
f[i] = f[P[i]];
for (int j : ch[P[i]]) {
if (i != j) {
f[i] = mul(f[i], tot[j]);
}
}
}
K = 2 << __lg(M);
S.resize(2 * K - 1);
T.resize(2 * K - 1);
lazy.resize(2 * K - 1);
for (int i = 0; i < M; i++) {
if (A[i] == 1) {
S[i + K - 1] = f[i + N];
}
T[i + K - 1] = f[i + N];
}
for (int i = K - 2; i >= 0; i--) {
S[i] = add(S[i * 2 + 1], S[i * 2 + 2]);
T[i] = add(T[i * 2 + 1], T[i * 2 + 2]);
}
}
int count_ways(int L, int R) {
if (N <= 1000 && M <= 1000) {
vector dp(N, vector{1});
vector<int> on(N + M), off(N + M);
for (int i = L; i <= R; i++) {
A[i - N] ^= 1;
}
for (int i = N + M - 1; i >= 0; i--) {
if (i >= N) {
on[i] = A[i - N];
off[i] = tot[i] - on[i];
} else {
int sum = 0;
for (int j = dp[i].size() - 1; j; j--) {
sum = add(sum, dp[i][j]);
on[i] = add(on[i], sum);
}
off[i] = sub(tot[i], on[i]);
}
if (i) {
int K = dp[P[i]].size();
dp[P[i]].resize(K + 1);
for (int j = K; j; j--) {
dp[P[i]][j] = add(mul(dp[P[i]][j], off[i]), mul(dp[P[i]][j - 1], on[i]));
}
dp[P[i]][0] = mul(dp[P[i]][0], off[i]);
}
}
return on[0];
}
update(0, 0, K, L - N, R - N + 1);
return query(0, 0, K, 0, M);
}
# |
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 |
16 ms |
336 KB |
Output is correct |
4 |
Correct |
17 ms |
364 KB |
Output is correct |
5 |
Correct |
17 ms |
332 KB |
Output is correct |
6 |
Correct |
17 ms |
352 KB |
Output is correct |
7 |
Correct |
19 ms |
332 KB |
Output is correct |
8 |
Correct |
17 ms |
328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
228 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 |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
376 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 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 |
16 ms |
336 KB |
Output is correct |
4 |
Correct |
17 ms |
364 KB |
Output is correct |
5 |
Correct |
17 ms |
332 KB |
Output is correct |
6 |
Correct |
17 ms |
352 KB |
Output is correct |
7 |
Correct |
19 ms |
332 KB |
Output is correct |
8 |
Correct |
17 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
228 KB |
Output is correct |
10 |
Correct |
0 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 |
376 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 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 |
2 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
2 ms |
464 KB |
Output is correct |
28 |
Correct |
2 ms |
432 KB |
Output is correct |
29 |
Correct |
17 ms |
336 KB |
Output is correct |
30 |
Correct |
17 ms |
328 KB |
Output is correct |
31 |
Correct |
1 ms |
336 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 |
5 ms |
336 KB |
Output is correct |
36 |
Correct |
2 ms |
336 KB |
Output is correct |
37 |
Correct |
17 ms |
432 KB |
Output is correct |
38 |
Correct |
17 ms |
336 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 |
240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
534 ms |
5336 KB |
Output is correct |
2 |
Correct |
633 ms |
10264 KB |
Output is correct |
3 |
Correct |
703 ms |
10284 KB |
Output is correct |
4 |
Correct |
820 ms |
10268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
534 ms |
5336 KB |
Output is correct |
2 |
Correct |
633 ms |
10264 KB |
Output is correct |
3 |
Correct |
703 ms |
10284 KB |
Output is correct |
4 |
Correct |
820 ms |
10268 KB |
Output is correct |
5 |
Correct |
619 ms |
5296 KB |
Output is correct |
6 |
Correct |
867 ms |
10320 KB |
Output is correct |
7 |
Correct |
776 ms |
10312 KB |
Output is correct |
8 |
Correct |
704 ms |
10276 KB |
Output is correct |
9 |
Correct |
292 ms |
592 KB |
Output is correct |
10 |
Correct |
672 ms |
904 KB |
Output is correct |
11 |
Correct |
758 ms |
916 KB |
Output is correct |
12 |
Correct |
582 ms |
896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
228 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 |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
376 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
534 ms |
5336 KB |
Output is correct |
14 |
Correct |
633 ms |
10264 KB |
Output is correct |
15 |
Correct |
703 ms |
10284 KB |
Output is correct |
16 |
Correct |
820 ms |
10268 KB |
Output is correct |
17 |
Correct |
619 ms |
5296 KB |
Output is correct |
18 |
Correct |
867 ms |
10320 KB |
Output is correct |
19 |
Correct |
776 ms |
10312 KB |
Output is correct |
20 |
Correct |
704 ms |
10276 KB |
Output is correct |
21 |
Correct |
292 ms |
592 KB |
Output is correct |
22 |
Correct |
672 ms |
904 KB |
Output is correct |
23 |
Correct |
758 ms |
916 KB |
Output is correct |
24 |
Correct |
582 ms |
896 KB |
Output is correct |
25 |
Correct |
866 ms |
13720 KB |
Output is correct |
26 |
Correct |
776 ms |
13852 KB |
Output is correct |
27 |
Correct |
889 ms |
13888 KB |
Output is correct |
28 |
Correct |
690 ms |
13920 KB |
Output is correct |
29 |
Correct |
749 ms |
13896 KB |
Output is correct |
30 |
Correct |
877 ms |
13952 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 |
16 ms |
336 KB |
Output is correct |
4 |
Correct |
17 ms |
364 KB |
Output is correct |
5 |
Correct |
17 ms |
332 KB |
Output is correct |
6 |
Correct |
17 ms |
352 KB |
Output is correct |
7 |
Correct |
19 ms |
332 KB |
Output is correct |
8 |
Correct |
17 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
228 KB |
Output is correct |
10 |
Correct |
0 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 |
376 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 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 |
2 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
2 ms |
464 KB |
Output is correct |
28 |
Correct |
2 ms |
432 KB |
Output is correct |
29 |
Correct |
17 ms |
336 KB |
Output is correct |
30 |
Correct |
17 ms |
328 KB |
Output is correct |
31 |
Correct |
1 ms |
336 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 |
5 ms |
336 KB |
Output is correct |
36 |
Correct |
2 ms |
336 KB |
Output is correct |
37 |
Correct |
17 ms |
432 KB |
Output is correct |
38 |
Correct |
17 ms |
336 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 |
240 KB |
Output is correct |
43 |
Correct |
431 ms |
680 KB |
Output is correct |
44 |
Correct |
715 ms |
672 KB |
Output is correct |
45 |
Correct |
590 ms |
712 KB |
Output is correct |
46 |
Correct |
390 ms |
996 KB |
Output is correct |
47 |
Correct |
788 ms |
1000 KB |
Output is correct |
48 |
Correct |
765 ms |
1004 KB |
Output is correct |
49 |
Correct |
722 ms |
996 KB |
Output is correct |
50 |
Correct |
609 ms |
1004 KB |
Output is correct |
51 |
Correct |
964 ms |
660 KB |
Output is correct |
52 |
Correct |
989 ms |
660 KB |
Output is correct |
53 |
Correct |
584 ms |
592 KB |
Output is correct |
54 |
Correct |
718 ms |
1000 KB |
Output is correct |
55 |
Correct |
796 ms |
816 KB |
Output is correct |
56 |
Correct |
704 ms |
752 KB |
Output is correct |
57 |
Correct |
644 ms |
652 KB |
Output is correct |
58 |
Correct |
663 ms |
996 KB |
Output is correct |
59 |
Correct |
948 ms |
1052 KB |
Output is correct |
60 |
Correct |
915 ms |
1052 KB |
Output is correct |
61 |
Correct |
758 ms |
752 KB |
Output is correct |
62 |
Correct |
551 ms |
644 KB |
Output is correct |
63 |
Correct |
753 ms |
584 KB |
Output is correct |
64 |
Correct |
693 ms |
664 KB |
Output is correct |
65 |
Correct |
405 ms |
544 KB |
Output is correct |
66 |
Correct |
684 ms |
896 KB |
Output is correct |
67 |
Correct |
759 ms |
904 KB |
Output is correct |
68 |
Correct |
738 ms |
904 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 |
16 ms |
336 KB |
Output is correct |
4 |
Correct |
17 ms |
364 KB |
Output is correct |
5 |
Correct |
17 ms |
332 KB |
Output is correct |
6 |
Correct |
17 ms |
352 KB |
Output is correct |
7 |
Correct |
19 ms |
332 KB |
Output is correct |
8 |
Correct |
17 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
228 KB |
Output is correct |
10 |
Correct |
0 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 |
376 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 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 |
2 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
2 ms |
464 KB |
Output is correct |
28 |
Correct |
2 ms |
432 KB |
Output is correct |
29 |
Correct |
17 ms |
336 KB |
Output is correct |
30 |
Correct |
17 ms |
328 KB |
Output is correct |
31 |
Correct |
1 ms |
336 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 |
5 ms |
336 KB |
Output is correct |
36 |
Correct |
2 ms |
336 KB |
Output is correct |
37 |
Correct |
17 ms |
432 KB |
Output is correct |
38 |
Correct |
17 ms |
336 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 |
240 KB |
Output is correct |
43 |
Correct |
534 ms |
5336 KB |
Output is correct |
44 |
Correct |
633 ms |
10264 KB |
Output is correct |
45 |
Correct |
703 ms |
10284 KB |
Output is correct |
46 |
Correct |
820 ms |
10268 KB |
Output is correct |
47 |
Correct |
619 ms |
5296 KB |
Output is correct |
48 |
Correct |
867 ms |
10320 KB |
Output is correct |
49 |
Correct |
776 ms |
10312 KB |
Output is correct |
50 |
Correct |
704 ms |
10276 KB |
Output is correct |
51 |
Correct |
292 ms |
592 KB |
Output is correct |
52 |
Correct |
672 ms |
904 KB |
Output is correct |
53 |
Correct |
758 ms |
916 KB |
Output is correct |
54 |
Correct |
582 ms |
896 KB |
Output is correct |
55 |
Correct |
866 ms |
13720 KB |
Output is correct |
56 |
Correct |
776 ms |
13852 KB |
Output is correct |
57 |
Correct |
889 ms |
13888 KB |
Output is correct |
58 |
Correct |
690 ms |
13920 KB |
Output is correct |
59 |
Correct |
749 ms |
13896 KB |
Output is correct |
60 |
Correct |
877 ms |
13952 KB |
Output is correct |
61 |
Correct |
431 ms |
680 KB |
Output is correct |
62 |
Correct |
715 ms |
672 KB |
Output is correct |
63 |
Correct |
590 ms |
712 KB |
Output is correct |
64 |
Correct |
390 ms |
996 KB |
Output is correct |
65 |
Correct |
788 ms |
1000 KB |
Output is correct |
66 |
Correct |
765 ms |
1004 KB |
Output is correct |
67 |
Correct |
722 ms |
996 KB |
Output is correct |
68 |
Correct |
609 ms |
1004 KB |
Output is correct |
69 |
Correct |
964 ms |
660 KB |
Output is correct |
70 |
Correct |
989 ms |
660 KB |
Output is correct |
71 |
Correct |
584 ms |
592 KB |
Output is correct |
72 |
Correct |
718 ms |
1000 KB |
Output is correct |
73 |
Correct |
796 ms |
816 KB |
Output is correct |
74 |
Correct |
704 ms |
752 KB |
Output is correct |
75 |
Correct |
644 ms |
652 KB |
Output is correct |
76 |
Correct |
663 ms |
996 KB |
Output is correct |
77 |
Correct |
948 ms |
1052 KB |
Output is correct |
78 |
Correct |
915 ms |
1052 KB |
Output is correct |
79 |
Correct |
758 ms |
752 KB |
Output is correct |
80 |
Correct |
551 ms |
644 KB |
Output is correct |
81 |
Correct |
753 ms |
584 KB |
Output is correct |
82 |
Correct |
693 ms |
664 KB |
Output is correct |
83 |
Correct |
405 ms |
544 KB |
Output is correct |
84 |
Correct |
684 ms |
896 KB |
Output is correct |
85 |
Correct |
759 ms |
904 KB |
Output is correct |
86 |
Correct |
738 ms |
904 KB |
Output is correct |
87 |
Correct |
0 ms |
208 KB |
Output is correct |
88 |
Correct |
429 ms |
12944 KB |
Output is correct |
89 |
Correct |
778 ms |
9224 KB |
Output is correct |
90 |
Correct |
771 ms |
8652 KB |
Output is correct |
91 |
Correct |
784 ms |
14024 KB |
Output is correct |
92 |
Correct |
789 ms |
13968 KB |
Output is correct |
93 |
Correct |
717 ms |
14024 KB |
Output is correct |
94 |
Correct |
872 ms |
13976 KB |
Output is correct |
95 |
Correct |
756 ms |
14056 KB |
Output is correct |
96 |
Execution timed out |
3028 ms |
3912 KB |
Time limit exceeded |
97 |
Halted |
0 ms |
0 KB |
- |