#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> 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);
}
void init(int n, int m, vector<int> P, vector<int> A) {
N = n, M = m;
vector<vector<int>> ch(N);
for (int i = 1; i < N + M; i++) {
ch[P[i]].push_back(i);
}
vector<int> tot(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 = 0; i < N; i++) {
int q = ch[i].size();
vector<int> pre(q + 1);
pre[0] = 1;
for (int j = 0; j < q; j++) {
pre[j + 1] = mul(pre[j], tot[ch[i][j]]);
}
vector<int> suf(q + 1);
suf[q] = 1;
for (int j = q - 1; j >= 0; j--) {
suf[j] = mul(suf[j + 1], tot[ch[i][j]]);
}
for (int j = 0; j < q; j++) {
f[ch[i][j]] = mul(f[i], mul(pre[j], suf[j + 1]));
}
}
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) {
update(0, 0, K, L - N, R - N + 1);
return S[0];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 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 |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
364 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 |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 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 |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 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 |
364 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 |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 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 |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 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 |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
1 ms |
336 KB |
Output is correct |
39 |
Correct |
1 ms |
384 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 |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
435 ms |
4916 KB |
Output is correct |
2 |
Correct |
908 ms |
9544 KB |
Output is correct |
3 |
Correct |
635 ms |
9524 KB |
Output is correct |
4 |
Correct |
682 ms |
9544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
435 ms |
4916 KB |
Output is correct |
2 |
Correct |
908 ms |
9544 KB |
Output is correct |
3 |
Correct |
635 ms |
9524 KB |
Output is correct |
4 |
Correct |
682 ms |
9544 KB |
Output is correct |
5 |
Correct |
596 ms |
4916 KB |
Output is correct |
6 |
Correct |
796 ms |
9448 KB |
Output is correct |
7 |
Correct |
791 ms |
9508 KB |
Output is correct |
8 |
Correct |
694 ms |
9496 KB |
Output is correct |
9 |
Correct |
274 ms |
564 KB |
Output is correct |
10 |
Correct |
676 ms |
856 KB |
Output is correct |
11 |
Correct |
662 ms |
852 KB |
Output is correct |
12 |
Correct |
599 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
364 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 |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 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 |
435 ms |
4916 KB |
Output is correct |
14 |
Correct |
908 ms |
9544 KB |
Output is correct |
15 |
Correct |
635 ms |
9524 KB |
Output is correct |
16 |
Correct |
682 ms |
9544 KB |
Output is correct |
17 |
Correct |
596 ms |
4916 KB |
Output is correct |
18 |
Correct |
796 ms |
9448 KB |
Output is correct |
19 |
Correct |
791 ms |
9508 KB |
Output is correct |
20 |
Correct |
694 ms |
9496 KB |
Output is correct |
21 |
Correct |
274 ms |
564 KB |
Output is correct |
22 |
Correct |
676 ms |
856 KB |
Output is correct |
23 |
Correct |
662 ms |
852 KB |
Output is correct |
24 |
Correct |
599 ms |
852 KB |
Output is correct |
25 |
Correct |
843 ms |
12616 KB |
Output is correct |
26 |
Correct |
882 ms |
12760 KB |
Output is correct |
27 |
Correct |
792 ms |
12700 KB |
Output is correct |
28 |
Correct |
618 ms |
12756 KB |
Output is correct |
29 |
Correct |
753 ms |
12728 KB |
Output is correct |
30 |
Correct |
661 ms |
12744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 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 |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 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 |
364 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 |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 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 |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 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 |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
1 ms |
336 KB |
Output is correct |
39 |
Correct |
1 ms |
384 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 |
336 KB |
Output is correct |
43 |
Correct |
460 ms |
652 KB |
Output is correct |
44 |
Correct |
750 ms |
660 KB |
Output is correct |
45 |
Correct |
690 ms |
680 KB |
Output is correct |
46 |
Correct |
595 ms |
952 KB |
Output is correct |
47 |
Correct |
415 ms |
940 KB |
Output is correct |
48 |
Correct |
483 ms |
940 KB |
Output is correct |
49 |
Correct |
744 ms |
956 KB |
Output is correct |
50 |
Correct |
597 ms |
948 KB |
Output is correct |
51 |
Correct |
657 ms |
616 KB |
Output is correct |
52 |
Correct |
689 ms |
612 KB |
Output is correct |
53 |
Correct |
635 ms |
592 KB |
Output is correct |
54 |
Correct |
733 ms |
936 KB |
Output is correct |
55 |
Correct |
698 ms |
764 KB |
Output is correct |
56 |
Correct |
815 ms |
720 KB |
Output is correct |
57 |
Correct |
703 ms |
620 KB |
Output is correct |
58 |
Correct |
712 ms |
940 KB |
Output is correct |
59 |
Correct |
710 ms |
992 KB |
Output is correct |
60 |
Correct |
711 ms |
992 KB |
Output is correct |
61 |
Correct |
652 ms |
720 KB |
Output is correct |
62 |
Correct |
686 ms |
592 KB |
Output is correct |
63 |
Correct |
693 ms |
584 KB |
Output is correct |
64 |
Correct |
598 ms |
632 KB |
Output is correct |
65 |
Correct |
387 ms |
560 KB |
Output is correct |
66 |
Correct |
654 ms |
856 KB |
Output is correct |
67 |
Correct |
748 ms |
968 KB |
Output is correct |
68 |
Correct |
737 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 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 |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 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 |
364 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 |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 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 |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 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 |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
1 ms |
336 KB |
Output is correct |
39 |
Correct |
1 ms |
384 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 |
336 KB |
Output is correct |
43 |
Correct |
435 ms |
4916 KB |
Output is correct |
44 |
Correct |
908 ms |
9544 KB |
Output is correct |
45 |
Correct |
635 ms |
9524 KB |
Output is correct |
46 |
Correct |
682 ms |
9544 KB |
Output is correct |
47 |
Correct |
596 ms |
4916 KB |
Output is correct |
48 |
Correct |
796 ms |
9448 KB |
Output is correct |
49 |
Correct |
791 ms |
9508 KB |
Output is correct |
50 |
Correct |
694 ms |
9496 KB |
Output is correct |
51 |
Correct |
274 ms |
564 KB |
Output is correct |
52 |
Correct |
676 ms |
856 KB |
Output is correct |
53 |
Correct |
662 ms |
852 KB |
Output is correct |
54 |
Correct |
599 ms |
852 KB |
Output is correct |
55 |
Correct |
843 ms |
12616 KB |
Output is correct |
56 |
Correct |
882 ms |
12760 KB |
Output is correct |
57 |
Correct |
792 ms |
12700 KB |
Output is correct |
58 |
Correct |
618 ms |
12756 KB |
Output is correct |
59 |
Correct |
753 ms |
12728 KB |
Output is correct |
60 |
Correct |
661 ms |
12744 KB |
Output is correct |
61 |
Correct |
460 ms |
652 KB |
Output is correct |
62 |
Correct |
750 ms |
660 KB |
Output is correct |
63 |
Correct |
690 ms |
680 KB |
Output is correct |
64 |
Correct |
595 ms |
952 KB |
Output is correct |
65 |
Correct |
415 ms |
940 KB |
Output is correct |
66 |
Correct |
483 ms |
940 KB |
Output is correct |
67 |
Correct |
744 ms |
956 KB |
Output is correct |
68 |
Correct |
597 ms |
948 KB |
Output is correct |
69 |
Correct |
657 ms |
616 KB |
Output is correct |
70 |
Correct |
689 ms |
612 KB |
Output is correct |
71 |
Correct |
635 ms |
592 KB |
Output is correct |
72 |
Correct |
733 ms |
936 KB |
Output is correct |
73 |
Correct |
698 ms |
764 KB |
Output is correct |
74 |
Correct |
815 ms |
720 KB |
Output is correct |
75 |
Correct |
703 ms |
620 KB |
Output is correct |
76 |
Correct |
712 ms |
940 KB |
Output is correct |
77 |
Correct |
710 ms |
992 KB |
Output is correct |
78 |
Correct |
711 ms |
992 KB |
Output is correct |
79 |
Correct |
652 ms |
720 KB |
Output is correct |
80 |
Correct |
686 ms |
592 KB |
Output is correct |
81 |
Correct |
693 ms |
584 KB |
Output is correct |
82 |
Correct |
598 ms |
632 KB |
Output is correct |
83 |
Correct |
387 ms |
560 KB |
Output is correct |
84 |
Correct |
654 ms |
856 KB |
Output is correct |
85 |
Correct |
748 ms |
968 KB |
Output is correct |
86 |
Correct |
737 ms |
852 KB |
Output is correct |
87 |
Correct |
0 ms |
208 KB |
Output is correct |
88 |
Correct |
420 ms |
11920 KB |
Output is correct |
89 |
Correct |
807 ms |
8528 KB |
Output is correct |
90 |
Correct |
617 ms |
7928 KB |
Output is correct |
91 |
Correct |
677 ms |
12828 KB |
Output is correct |
92 |
Correct |
719 ms |
12828 KB |
Output is correct |
93 |
Correct |
712 ms |
12876 KB |
Output is correct |
94 |
Correct |
887 ms |
12860 KB |
Output is correct |
95 |
Correct |
713 ms |
12824 KB |
Output is correct |
96 |
Correct |
687 ms |
6152 KB |
Output is correct |
97 |
Correct |
808 ms |
6144 KB |
Output is correct |
98 |
Correct |
671 ms |
7244 KB |
Output is correct |
99 |
Correct |
910 ms |
12696 KB |
Output is correct |
100 |
Correct |
709 ms |
9244 KB |
Output is correct |
101 |
Correct |
646 ms |
7952 KB |
Output is correct |
102 |
Correct |
747 ms |
6216 KB |
Output is correct |
103 |
Correct |
865 ms |
12724 KB |
Output is correct |
104 |
Correct |
783 ms |
13192 KB |
Output is correct |
105 |
Correct |
772 ms |
13192 KB |
Output is correct |
106 |
Correct |
690 ms |
8208 KB |
Output is correct |
107 |
Correct |
812 ms |
7836 KB |
Output is correct |
108 |
Correct |
833 ms |
7368 KB |
Output is correct |
109 |
Correct |
691 ms |
6320 KB |
Output is correct |