#include "circuit.h"
#include <bits/stdc++.h>
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MOD = 1'000'002'022;
const int MAXN = 100'055;
inline void add(int &a, int b) { a += b; if(MOD <= a) a -= MOD; }
inline void sub(int &a, int b) { a -= b; if(a < 0) a += MOD; }
struct SEG {
int d[MAXN*4][2];
bitset<MAXN*4> df;
vector<int> *Q;
int *A;
void init(int i, int s, int e) {
if(s == e) {
d[i][(*Q)[s]] = A[s];
return;
}
int m = s+e >> 1;
init(i<<1, s, m);
init(i<<1|1, m+1, e);
for(int j = 2; j--;) {
d[i][j] = d[i<<1][j];
add(d[i][j], d[i<<1|1][j]);
}
}
pii f(int i, int s, int e, int p, int q) {
if(q < s || e < p) return {0, 0};
if(p <= s && e <= q) {
swap(d[i][0], d[i][1]);
df[i] = !df[i];
return {d[i][1], d[i][0]};
}
int m = s+e >> 1;
pii r = f(i<<1, s, m, p, q);
pii t = f(i<<1|1, m+1, e, p, q);
add(r.fi, t.fi); add(r.se, t.se);
for(int j = 2; j--;) {
d[i][j] = d[i<<1][j];
add(d[i][j], d[i<<1|1][j]);
}
if(df[i]) {
swap(d[i][0], d[i][1]);
swap(r.fi, r.se);
}
return r;
}
} seg;
vector<int> G[MAXN*2];
int A[MAXN*2], B[MAXN*2];
int N, M, Ans;
void dfs1(int i) {
int r = max(1, sz(G[i]));
for(int v : G[i]) {
dfs1(v);
r = ll(r) * B[v] % MOD;
}
B[i] = r;
}
void dfs2(int i) {
int u = A[i];
int dg = sz(G[i]);
vector<int> L(dg, 1), R(dg, 1);
for(int j = 1; j < dg; j++)
L[j] = ll(L[j-1]) * B[G[i][j-1]] % MOD;
for(int j = dg-2; 0 <= j; j--)
R[j] = ll(R[j+1]) * B[G[i][j+1]] % MOD;
for(int j = dg; j--;) {
int v = G[i][j];
A[v] = ll(u) * L[j] % MOD * R[j] % MOD;
dfs2(v);
}
}
void init(int N, int M, std::vector<int> P, std::vector<int> Q) {
::N = N; ::M = M;
for(int i = N+M; --i;) G[P[i]].eb(i);
dfs1(0); A[0] = 1; dfs2(0);
for(int i = 0; i < M; i++) A[i] = A[N+i];
seg.A = A; seg.Q = &Q;
seg.init(1, 0, M-1);
B[0] = 0;
for(int i = 1; i <= M; i++) {
B[i] = B[i-1];
add(B[i], A[i-1]);
}
for(int i = M; i--;) if(Q[i]) add(Ans, A[i]);
}
int count_ways(int L, int R) {
L -= N; R -= N;
int r = seg.f(1, 0, M-1, L, R).se;
add(Ans, B[R+1]); sub(Ans, B[L]);
add(r, r); sub(Ans, r);
return Ans;
}
Compilation message
circuit.cpp: In member function 'void SEG::init(int, int, int)':
circuit.cpp:30:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
30 | int m = s+e >> 1;
| ~^~
circuit.cpp: In member function 'pii SEG::f(int, int, int, int, int)':
circuit.cpp:47:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
47 | int m = s+e >> 1;
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4960 KB |
Output is correct |
3 |
Correct |
4 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
4 ms |
5072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
5072 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
5072 KB |
Output is correct |
10 |
Correct |
3 ms |
5328 KB |
Output is correct |
11 |
Correct |
3 ms |
5328 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4960 KB |
Output is correct |
3 |
Correct |
4 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
4 ms |
5072 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
5072 KB |
Output is correct |
11 |
Correct |
3 ms |
5072 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
3 ms |
5072 KB |
Output is correct |
14 |
Correct |
3 ms |
5072 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5072 KB |
Output is correct |
17 |
Correct |
3 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5328 KB |
Output is correct |
19 |
Correct |
3 ms |
5328 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
5072 KB |
Output is correct |
24 |
Correct |
3 ms |
5072 KB |
Output is correct |
25 |
Correct |
3 ms |
5072 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
4 ms |
5072 KB |
Output is correct |
28 |
Correct |
3 ms |
5072 KB |
Output is correct |
29 |
Correct |
3 ms |
5072 KB |
Output is correct |
30 |
Correct |
3 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 KB |
Output is correct |
33 |
Correct |
3 ms |
5072 KB |
Output is correct |
34 |
Correct |
3 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5108 KB |
Output is correct |
36 |
Correct |
3 ms |
5328 KB |
Output is correct |
37 |
Correct |
3 ms |
5328 KB |
Output is correct |
38 |
Correct |
3 ms |
5328 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
700 ms |
7868 KB |
Output is correct |
2 |
Correct |
948 ms |
10828 KB |
Output is correct |
3 |
Correct |
957 ms |
10692 KB |
Output is correct |
4 |
Correct |
802 ms |
10636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
700 ms |
7868 KB |
Output is correct |
2 |
Correct |
948 ms |
10828 KB |
Output is correct |
3 |
Correct |
957 ms |
10692 KB |
Output is correct |
4 |
Correct |
802 ms |
10636 KB |
Output is correct |
5 |
Correct |
780 ms |
7880 KB |
Output is correct |
6 |
Correct |
929 ms |
10620 KB |
Output is correct |
7 |
Correct |
879 ms |
10680 KB |
Output is correct |
8 |
Correct |
840 ms |
10696 KB |
Output is correct |
9 |
Correct |
470 ms |
5200 KB |
Output is correct |
10 |
Correct |
910 ms |
5328 KB |
Output is correct |
11 |
Correct |
938 ms |
5328 KB |
Output is correct |
12 |
Correct |
854 ms |
5328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
5072 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
5072 KB |
Output is correct |
10 |
Correct |
3 ms |
5328 KB |
Output is correct |
11 |
Correct |
3 ms |
5328 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
700 ms |
7868 KB |
Output is correct |
14 |
Correct |
948 ms |
10828 KB |
Output is correct |
15 |
Correct |
957 ms |
10692 KB |
Output is correct |
16 |
Correct |
802 ms |
10636 KB |
Output is correct |
17 |
Correct |
780 ms |
7880 KB |
Output is correct |
18 |
Correct |
929 ms |
10620 KB |
Output is correct |
19 |
Correct |
879 ms |
10680 KB |
Output is correct |
20 |
Correct |
840 ms |
10696 KB |
Output is correct |
21 |
Correct |
470 ms |
5200 KB |
Output is correct |
22 |
Correct |
910 ms |
5328 KB |
Output is correct |
23 |
Correct |
938 ms |
5328 KB |
Output is correct |
24 |
Correct |
854 ms |
5328 KB |
Output is correct |
25 |
Correct |
888 ms |
14008 KB |
Output is correct |
26 |
Correct |
981 ms |
14152 KB |
Output is correct |
27 |
Correct |
882 ms |
14140 KB |
Output is correct |
28 |
Correct |
721 ms |
14028 KB |
Output is correct |
29 |
Correct |
1061 ms |
34468 KB |
Output is correct |
30 |
Correct |
936 ms |
34388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4960 KB |
Output is correct |
3 |
Correct |
4 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
4 ms |
5072 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
5072 KB |
Output is correct |
11 |
Correct |
3 ms |
5072 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
3 ms |
5072 KB |
Output is correct |
14 |
Correct |
3 ms |
5072 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5072 KB |
Output is correct |
17 |
Correct |
3 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5328 KB |
Output is correct |
19 |
Correct |
3 ms |
5328 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
5072 KB |
Output is correct |
24 |
Correct |
3 ms |
5072 KB |
Output is correct |
25 |
Correct |
3 ms |
5072 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
4 ms |
5072 KB |
Output is correct |
28 |
Correct |
3 ms |
5072 KB |
Output is correct |
29 |
Correct |
3 ms |
5072 KB |
Output is correct |
30 |
Correct |
3 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 KB |
Output is correct |
33 |
Correct |
3 ms |
5072 KB |
Output is correct |
34 |
Correct |
3 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5108 KB |
Output is correct |
36 |
Correct |
3 ms |
5328 KB |
Output is correct |
37 |
Correct |
3 ms |
5328 KB |
Output is correct |
38 |
Correct |
3 ms |
5328 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
3 ms |
5072 KB |
Output is correct |
43 |
Correct |
756 ms |
5328 KB |
Output is correct |
44 |
Correct |
1109 ms |
5348 KB |
Output is correct |
45 |
Correct |
844 ms |
5368 KB |
Output is correct |
46 |
Correct |
939 ms |
5432 KB |
Output is correct |
47 |
Correct |
830 ms |
5456 KB |
Output is correct |
48 |
Correct |
912 ms |
5528 KB |
Output is correct |
49 |
Correct |
771 ms |
5552 KB |
Output is correct |
50 |
Correct |
710 ms |
5456 KB |
Output is correct |
51 |
Correct |
778 ms |
5328 KB |
Output is correct |
52 |
Correct |
847 ms |
5328 KB |
Output is correct |
53 |
Correct |
650 ms |
6224 KB |
Output is correct |
54 |
Correct |
1010 ms |
5456 KB |
Output is correct |
55 |
Correct |
945 ms |
5328 KB |
Output is correct |
56 |
Correct |
969 ms |
5328 KB |
Output is correct |
57 |
Correct |
1129 ms |
5328 KB |
Output is correct |
58 |
Correct |
1104 ms |
6528 KB |
Output is correct |
59 |
Correct |
980 ms |
6608 KB |
Output is correct |
60 |
Correct |
849 ms |
6636 KB |
Output is correct |
61 |
Correct |
1063 ms |
5584 KB |
Output is correct |
62 |
Correct |
824 ms |
5300 KB |
Output is correct |
63 |
Correct |
882 ms |
5200 KB |
Output is correct |
64 |
Correct |
1177 ms |
5328 KB |
Output is correct |
65 |
Correct |
526 ms |
5204 KB |
Output is correct |
66 |
Correct |
901 ms |
5328 KB |
Output is correct |
67 |
Correct |
975 ms |
5328 KB |
Output is correct |
68 |
Correct |
888 ms |
5376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4960 KB |
Output is correct |
3 |
Correct |
4 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
4 ms |
5072 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
5072 KB |
Output is correct |
11 |
Correct |
3 ms |
5072 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
3 ms |
5072 KB |
Output is correct |
14 |
Correct |
3 ms |
5072 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5072 KB |
Output is correct |
17 |
Correct |
3 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5328 KB |
Output is correct |
19 |
Correct |
3 ms |
5328 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
5072 KB |
Output is correct |
24 |
Correct |
3 ms |
5072 KB |
Output is correct |
25 |
Correct |
3 ms |
5072 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
4 ms |
5072 KB |
Output is correct |
28 |
Correct |
3 ms |
5072 KB |
Output is correct |
29 |
Correct |
3 ms |
5072 KB |
Output is correct |
30 |
Correct |
3 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 KB |
Output is correct |
33 |
Correct |
3 ms |
5072 KB |
Output is correct |
34 |
Correct |
3 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5108 KB |
Output is correct |
36 |
Correct |
3 ms |
5328 KB |
Output is correct |
37 |
Correct |
3 ms |
5328 KB |
Output is correct |
38 |
Correct |
3 ms |
5328 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
3 ms |
5072 KB |
Output is correct |
43 |
Correct |
700 ms |
7868 KB |
Output is correct |
44 |
Correct |
948 ms |
10828 KB |
Output is correct |
45 |
Correct |
957 ms |
10692 KB |
Output is correct |
46 |
Correct |
802 ms |
10636 KB |
Output is correct |
47 |
Correct |
780 ms |
7880 KB |
Output is correct |
48 |
Correct |
929 ms |
10620 KB |
Output is correct |
49 |
Correct |
879 ms |
10680 KB |
Output is correct |
50 |
Correct |
840 ms |
10696 KB |
Output is correct |
51 |
Correct |
470 ms |
5200 KB |
Output is correct |
52 |
Correct |
910 ms |
5328 KB |
Output is correct |
53 |
Correct |
938 ms |
5328 KB |
Output is correct |
54 |
Correct |
854 ms |
5328 KB |
Output is correct |
55 |
Correct |
888 ms |
14008 KB |
Output is correct |
56 |
Correct |
981 ms |
14152 KB |
Output is correct |
57 |
Correct |
882 ms |
14140 KB |
Output is correct |
58 |
Correct |
721 ms |
14028 KB |
Output is correct |
59 |
Correct |
1061 ms |
34468 KB |
Output is correct |
60 |
Correct |
936 ms |
34388 KB |
Output is correct |
61 |
Correct |
756 ms |
5328 KB |
Output is correct |
62 |
Correct |
1109 ms |
5348 KB |
Output is correct |
63 |
Correct |
844 ms |
5368 KB |
Output is correct |
64 |
Correct |
939 ms |
5432 KB |
Output is correct |
65 |
Correct |
830 ms |
5456 KB |
Output is correct |
66 |
Correct |
912 ms |
5528 KB |
Output is correct |
67 |
Correct |
771 ms |
5552 KB |
Output is correct |
68 |
Correct |
710 ms |
5456 KB |
Output is correct |
69 |
Correct |
778 ms |
5328 KB |
Output is correct |
70 |
Correct |
847 ms |
5328 KB |
Output is correct |
71 |
Correct |
650 ms |
6224 KB |
Output is correct |
72 |
Correct |
1010 ms |
5456 KB |
Output is correct |
73 |
Correct |
945 ms |
5328 KB |
Output is correct |
74 |
Correct |
969 ms |
5328 KB |
Output is correct |
75 |
Correct |
1129 ms |
5328 KB |
Output is correct |
76 |
Correct |
1104 ms |
6528 KB |
Output is correct |
77 |
Correct |
980 ms |
6608 KB |
Output is correct |
78 |
Correct |
849 ms |
6636 KB |
Output is correct |
79 |
Correct |
1063 ms |
5584 KB |
Output is correct |
80 |
Correct |
824 ms |
5300 KB |
Output is correct |
81 |
Correct |
882 ms |
5200 KB |
Output is correct |
82 |
Correct |
1177 ms |
5328 KB |
Output is correct |
83 |
Correct |
526 ms |
5204 KB |
Output is correct |
84 |
Correct |
901 ms |
5328 KB |
Output is correct |
85 |
Correct |
975 ms |
5328 KB |
Output is correct |
86 |
Correct |
888 ms |
5376 KB |
Output is correct |
87 |
Correct |
3 ms |
4992 KB |
Output is correct |
88 |
Correct |
684 ms |
13484 KB |
Output is correct |
89 |
Correct |
1036 ms |
10932 KB |
Output is correct |
90 |
Correct |
796 ms |
10676 KB |
Output is correct |
91 |
Correct |
1032 ms |
14248 KB |
Output is correct |
92 |
Correct |
1056 ms |
14276 KB |
Output is correct |
93 |
Correct |
1227 ms |
14260 KB |
Output is correct |
94 |
Correct |
1141 ms |
14160 KB |
Output is correct |
95 |
Correct |
1275 ms |
14160 KB |
Output is correct |
96 |
Correct |
1205 ms |
9896 KB |
Output is correct |
97 |
Correct |
976 ms |
9908 KB |
Output is correct |
98 |
Correct |
815 ms |
30004 KB |
Output is correct |
99 |
Correct |
1352 ms |
14140 KB |
Output is correct |
100 |
Correct |
1323 ms |
11716 KB |
Output is correct |
101 |
Correct |
1002 ms |
11244 KB |
Output is correct |
102 |
Correct |
1035 ms |
9960 KB |
Output is correct |
103 |
Correct |
1240 ms |
34436 KB |
Output is correct |
104 |
Correct |
1283 ms |
34848 KB |
Output is correct |
105 |
Correct |
1118 ms |
34868 KB |
Output is correct |
106 |
Correct |
1175 ms |
15212 KB |
Output is correct |
107 |
Correct |
1069 ms |
10584 KB |
Output is correct |
108 |
Correct |
1120 ms |
10384 KB |
Output is correct |
109 |
Correct |
1145 ms |
10076 KB |
Output is correct |