#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#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:33:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
33 | int m = s+e >> 1;
| ~^~
circuit.cpp: In member function 'pii SEG::f(int, int, int, int, int)':
circuit.cpp:50:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | int m = s+e >> 1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
4 ms |
4944 KB |
Output is correct |
3 |
Correct |
4 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5020 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
5 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4980 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
4 ms |
5044 KB |
Output is correct |
6 |
Correct |
5 ms |
5072 KB |
Output is correct |
7 |
Correct |
5 ms |
5072 KB |
Output is correct |
8 |
Correct |
4 ms |
5072 KB |
Output is correct |
9 |
Correct |
4 ms |
5072 KB |
Output is correct |
10 |
Correct |
4 ms |
5200 KB |
Output is correct |
11 |
Correct |
5 ms |
5200 KB |
Output is correct |
12 |
Correct |
5 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
4 ms |
4944 KB |
Output is correct |
3 |
Correct |
4 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5020 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
5 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
4 ms |
4980 KB |
Output is correct |
10 |
Correct |
3 ms |
5120 KB |
Output is correct |
11 |
Correct |
3 ms |
5072 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
4 ms |
5044 KB |
Output is correct |
14 |
Correct |
5 ms |
5072 KB |
Output is correct |
15 |
Correct |
5 ms |
5072 KB |
Output is correct |
16 |
Correct |
4 ms |
5072 KB |
Output is correct |
17 |
Correct |
4 ms |
5072 KB |
Output is correct |
18 |
Correct |
4 ms |
5200 KB |
Output is correct |
19 |
Correct |
5 ms |
5200 KB |
Output is correct |
20 |
Correct |
5 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
5 ms |
5072 KB |
Output is correct |
23 |
Correct |
4 ms |
5072 KB |
Output is correct |
24 |
Correct |
4 ms |
5128 KB |
Output is correct |
25 |
Correct |
4 ms |
5072 KB |
Output is correct |
26 |
Correct |
5 ms |
5072 KB |
Output is correct |
27 |
Correct |
5 ms |
5072 KB |
Output is correct |
28 |
Correct |
4 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 |
4 ms |
5248 KB |
Output is correct |
32 |
Correct |
4 ms |
5072 KB |
Output is correct |
33 |
Correct |
4 ms |
5120 KB |
Output is correct |
34 |
Correct |
5 ms |
5072 KB |
Output is correct |
35 |
Correct |
4 ms |
5072 KB |
Output is correct |
36 |
Correct |
4 ms |
5248 KB |
Output is correct |
37 |
Correct |
5 ms |
5200 KB |
Output is correct |
38 |
Correct |
5 ms |
5200 KB |
Output is correct |
39 |
Correct |
5 ms |
5072 KB |
Output is correct |
40 |
Correct |
4 ms |
5072 KB |
Output is correct |
41 |
Correct |
4 ms |
5072 KB |
Output is correct |
42 |
Correct |
4 ms |
5000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
845 ms |
7892 KB |
Output is correct |
2 |
Correct |
1156 ms |
10624 KB |
Output is correct |
3 |
Correct |
1137 ms |
10696 KB |
Output is correct |
4 |
Correct |
1041 ms |
10664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
845 ms |
7892 KB |
Output is correct |
2 |
Correct |
1156 ms |
10624 KB |
Output is correct |
3 |
Correct |
1137 ms |
10696 KB |
Output is correct |
4 |
Correct |
1041 ms |
10664 KB |
Output is correct |
5 |
Correct |
917 ms |
7864 KB |
Output is correct |
6 |
Correct |
1014 ms |
10692 KB |
Output is correct |
7 |
Correct |
954 ms |
10688 KB |
Output is correct |
8 |
Correct |
1018 ms |
10684 KB |
Output is correct |
9 |
Correct |
582 ms |
5200 KB |
Output is correct |
10 |
Correct |
774 ms |
5356 KB |
Output is correct |
11 |
Correct |
1010 ms |
5328 KB |
Output is correct |
12 |
Correct |
953 ms |
5360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4980 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
4 ms |
5044 KB |
Output is correct |
6 |
Correct |
5 ms |
5072 KB |
Output is correct |
7 |
Correct |
5 ms |
5072 KB |
Output is correct |
8 |
Correct |
4 ms |
5072 KB |
Output is correct |
9 |
Correct |
4 ms |
5072 KB |
Output is correct |
10 |
Correct |
4 ms |
5200 KB |
Output is correct |
11 |
Correct |
5 ms |
5200 KB |
Output is correct |
12 |
Correct |
5 ms |
5072 KB |
Output is correct |
13 |
Correct |
845 ms |
7892 KB |
Output is correct |
14 |
Correct |
1156 ms |
10624 KB |
Output is correct |
15 |
Correct |
1137 ms |
10696 KB |
Output is correct |
16 |
Correct |
1041 ms |
10664 KB |
Output is correct |
17 |
Correct |
917 ms |
7864 KB |
Output is correct |
18 |
Correct |
1014 ms |
10692 KB |
Output is correct |
19 |
Correct |
954 ms |
10688 KB |
Output is correct |
20 |
Correct |
1018 ms |
10684 KB |
Output is correct |
21 |
Correct |
582 ms |
5200 KB |
Output is correct |
22 |
Correct |
774 ms |
5356 KB |
Output is correct |
23 |
Correct |
1010 ms |
5328 KB |
Output is correct |
24 |
Correct |
953 ms |
5360 KB |
Output is correct |
25 |
Correct |
1261 ms |
14016 KB |
Output is correct |
26 |
Correct |
1338 ms |
14248 KB |
Output is correct |
27 |
Correct |
1347 ms |
14144 KB |
Output is correct |
28 |
Correct |
1072 ms |
14140 KB |
Output is correct |
29 |
Correct |
1265 ms |
29728 KB |
Output is correct |
30 |
Correct |
1074 ms |
29724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
4 ms |
4944 KB |
Output is correct |
3 |
Correct |
4 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5020 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
5 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
4 ms |
4980 KB |
Output is correct |
10 |
Correct |
3 ms |
5120 KB |
Output is correct |
11 |
Correct |
3 ms |
5072 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
4 ms |
5044 KB |
Output is correct |
14 |
Correct |
5 ms |
5072 KB |
Output is correct |
15 |
Correct |
5 ms |
5072 KB |
Output is correct |
16 |
Correct |
4 ms |
5072 KB |
Output is correct |
17 |
Correct |
4 ms |
5072 KB |
Output is correct |
18 |
Correct |
4 ms |
5200 KB |
Output is correct |
19 |
Correct |
5 ms |
5200 KB |
Output is correct |
20 |
Correct |
5 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
5 ms |
5072 KB |
Output is correct |
23 |
Correct |
4 ms |
5072 KB |
Output is correct |
24 |
Correct |
4 ms |
5128 KB |
Output is correct |
25 |
Correct |
4 ms |
5072 KB |
Output is correct |
26 |
Correct |
5 ms |
5072 KB |
Output is correct |
27 |
Correct |
5 ms |
5072 KB |
Output is correct |
28 |
Correct |
4 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 |
4 ms |
5248 KB |
Output is correct |
32 |
Correct |
4 ms |
5072 KB |
Output is correct |
33 |
Correct |
4 ms |
5120 KB |
Output is correct |
34 |
Correct |
5 ms |
5072 KB |
Output is correct |
35 |
Correct |
4 ms |
5072 KB |
Output is correct |
36 |
Correct |
4 ms |
5248 KB |
Output is correct |
37 |
Correct |
5 ms |
5200 KB |
Output is correct |
38 |
Correct |
5 ms |
5200 KB |
Output is correct |
39 |
Correct |
5 ms |
5072 KB |
Output is correct |
40 |
Correct |
4 ms |
5072 KB |
Output is correct |
41 |
Correct |
4 ms |
5072 KB |
Output is correct |
42 |
Correct |
4 ms |
5000 KB |
Output is correct |
43 |
Correct |
585 ms |
5376 KB |
Output is correct |
44 |
Correct |
801 ms |
5356 KB |
Output is correct |
45 |
Correct |
1118 ms |
5328 KB |
Output is correct |
46 |
Correct |
1135 ms |
5504 KB |
Output is correct |
47 |
Correct |
923 ms |
5544 KB |
Output is correct |
48 |
Correct |
845 ms |
5492 KB |
Output is correct |
49 |
Correct |
1085 ms |
5504 KB |
Output is correct |
50 |
Correct |
675 ms |
5504 KB |
Output is correct |
51 |
Correct |
1224 ms |
5328 KB |
Output is correct |
52 |
Correct |
1050 ms |
5344 KB |
Output is correct |
53 |
Correct |
884 ms |
5968 KB |
Output is correct |
54 |
Correct |
1064 ms |
5456 KB |
Output is correct |
55 |
Correct |
921 ms |
5328 KB |
Output is correct |
56 |
Correct |
880 ms |
5376 KB |
Output is correct |
57 |
Correct |
1064 ms |
5328 KB |
Output is correct |
58 |
Correct |
848 ms |
6224 KB |
Output is correct |
59 |
Correct |
1142 ms |
6396 KB |
Output is correct |
60 |
Correct |
872 ms |
6400 KB |
Output is correct |
61 |
Correct |
1107 ms |
5456 KB |
Output is correct |
62 |
Correct |
881 ms |
5200 KB |
Output is correct |
63 |
Correct |
915 ms |
5200 KB |
Output is correct |
64 |
Correct |
1008 ms |
5376 KB |
Output is correct |
65 |
Correct |
557 ms |
5164 KB |
Output is correct |
66 |
Correct |
928 ms |
5328 KB |
Output is correct |
67 |
Correct |
1149 ms |
5328 KB |
Output is correct |
68 |
Correct |
1008 ms |
5324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
4 ms |
4944 KB |
Output is correct |
3 |
Correct |
4 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5020 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
5 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
4 ms |
4980 KB |
Output is correct |
10 |
Correct |
3 ms |
5120 KB |
Output is correct |
11 |
Correct |
3 ms |
5072 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
4 ms |
5044 KB |
Output is correct |
14 |
Correct |
5 ms |
5072 KB |
Output is correct |
15 |
Correct |
5 ms |
5072 KB |
Output is correct |
16 |
Correct |
4 ms |
5072 KB |
Output is correct |
17 |
Correct |
4 ms |
5072 KB |
Output is correct |
18 |
Correct |
4 ms |
5200 KB |
Output is correct |
19 |
Correct |
5 ms |
5200 KB |
Output is correct |
20 |
Correct |
5 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
5 ms |
5072 KB |
Output is correct |
23 |
Correct |
4 ms |
5072 KB |
Output is correct |
24 |
Correct |
4 ms |
5128 KB |
Output is correct |
25 |
Correct |
4 ms |
5072 KB |
Output is correct |
26 |
Correct |
5 ms |
5072 KB |
Output is correct |
27 |
Correct |
5 ms |
5072 KB |
Output is correct |
28 |
Correct |
4 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 |
4 ms |
5248 KB |
Output is correct |
32 |
Correct |
4 ms |
5072 KB |
Output is correct |
33 |
Correct |
4 ms |
5120 KB |
Output is correct |
34 |
Correct |
5 ms |
5072 KB |
Output is correct |
35 |
Correct |
4 ms |
5072 KB |
Output is correct |
36 |
Correct |
4 ms |
5248 KB |
Output is correct |
37 |
Correct |
5 ms |
5200 KB |
Output is correct |
38 |
Correct |
5 ms |
5200 KB |
Output is correct |
39 |
Correct |
5 ms |
5072 KB |
Output is correct |
40 |
Correct |
4 ms |
5072 KB |
Output is correct |
41 |
Correct |
4 ms |
5072 KB |
Output is correct |
42 |
Correct |
4 ms |
5000 KB |
Output is correct |
43 |
Correct |
845 ms |
7892 KB |
Output is correct |
44 |
Correct |
1156 ms |
10624 KB |
Output is correct |
45 |
Correct |
1137 ms |
10696 KB |
Output is correct |
46 |
Correct |
1041 ms |
10664 KB |
Output is correct |
47 |
Correct |
917 ms |
7864 KB |
Output is correct |
48 |
Correct |
1014 ms |
10692 KB |
Output is correct |
49 |
Correct |
954 ms |
10688 KB |
Output is correct |
50 |
Correct |
1018 ms |
10684 KB |
Output is correct |
51 |
Correct |
582 ms |
5200 KB |
Output is correct |
52 |
Correct |
774 ms |
5356 KB |
Output is correct |
53 |
Correct |
1010 ms |
5328 KB |
Output is correct |
54 |
Correct |
953 ms |
5360 KB |
Output is correct |
55 |
Correct |
1261 ms |
14016 KB |
Output is correct |
56 |
Correct |
1338 ms |
14248 KB |
Output is correct |
57 |
Correct |
1347 ms |
14144 KB |
Output is correct |
58 |
Correct |
1072 ms |
14140 KB |
Output is correct |
59 |
Correct |
1265 ms |
29728 KB |
Output is correct |
60 |
Correct |
1074 ms |
29724 KB |
Output is correct |
61 |
Correct |
585 ms |
5376 KB |
Output is correct |
62 |
Correct |
801 ms |
5356 KB |
Output is correct |
63 |
Correct |
1118 ms |
5328 KB |
Output is correct |
64 |
Correct |
1135 ms |
5504 KB |
Output is correct |
65 |
Correct |
923 ms |
5544 KB |
Output is correct |
66 |
Correct |
845 ms |
5492 KB |
Output is correct |
67 |
Correct |
1085 ms |
5504 KB |
Output is correct |
68 |
Correct |
675 ms |
5504 KB |
Output is correct |
69 |
Correct |
1224 ms |
5328 KB |
Output is correct |
70 |
Correct |
1050 ms |
5344 KB |
Output is correct |
71 |
Correct |
884 ms |
5968 KB |
Output is correct |
72 |
Correct |
1064 ms |
5456 KB |
Output is correct |
73 |
Correct |
921 ms |
5328 KB |
Output is correct |
74 |
Correct |
880 ms |
5376 KB |
Output is correct |
75 |
Correct |
1064 ms |
5328 KB |
Output is correct |
76 |
Correct |
848 ms |
6224 KB |
Output is correct |
77 |
Correct |
1142 ms |
6396 KB |
Output is correct |
78 |
Correct |
872 ms |
6400 KB |
Output is correct |
79 |
Correct |
1107 ms |
5456 KB |
Output is correct |
80 |
Correct |
881 ms |
5200 KB |
Output is correct |
81 |
Correct |
915 ms |
5200 KB |
Output is correct |
82 |
Correct |
1008 ms |
5376 KB |
Output is correct |
83 |
Correct |
557 ms |
5164 KB |
Output is correct |
84 |
Correct |
928 ms |
5328 KB |
Output is correct |
85 |
Correct |
1149 ms |
5328 KB |
Output is correct |
86 |
Correct |
1008 ms |
5324 KB |
Output is correct |
87 |
Correct |
5 ms |
4944 KB |
Output is correct |
88 |
Correct |
809 ms |
13448 KB |
Output is correct |
89 |
Correct |
967 ms |
10924 KB |
Output is correct |
90 |
Correct |
1209 ms |
10660 KB |
Output is correct |
91 |
Correct |
1016 ms |
14248 KB |
Output is correct |
92 |
Correct |
1243 ms |
14256 KB |
Output is correct |
93 |
Correct |
993 ms |
14224 KB |
Output is correct |
94 |
Correct |
1283 ms |
14164 KB |
Output is correct |
95 |
Correct |
1232 ms |
14156 KB |
Output is correct |
96 |
Correct |
867 ms |
10028 KB |
Output is correct |
97 |
Correct |
901 ms |
10004 KB |
Output is correct |
98 |
Correct |
1071 ms |
25408 KB |
Output is correct |
99 |
Correct |
1049 ms |
14132 KB |
Output is correct |
100 |
Correct |
1238 ms |
11800 KB |
Output is correct |
101 |
Correct |
1162 ms |
11180 KB |
Output is correct |
102 |
Correct |
1088 ms |
10076 KB |
Output is correct |
103 |
Correct |
1185 ms |
29764 KB |
Output is correct |
104 |
Correct |
1042 ms |
30096 KB |
Output is correct |
105 |
Correct |
749 ms |
30180 KB |
Output is correct |
106 |
Correct |
1324 ms |
13980 KB |
Output is correct |
107 |
Correct |
1072 ms |
10632 KB |
Output is correct |
108 |
Correct |
1272 ms |
10396 KB |
Output is correct |
109 |
Correct |
1180 ms |
10036 KB |
Output is correct |