#include "circuit.h"
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
constexpr int mod = 1e9 + 2022;
vector<int> adj[200005];
int choices[200005], coef[200005], *C;
int N, M;
struct ST
{
int pool[400005], total[400005];
int lazy[400005];
void build(int i, int l, int r)
{
if (l == r)
{
total[i] = C[l];
return;
}
int m = (l + r) / 2;
build(i << 1, l, m);
build(i << 1 | 1, m + 1, r);
total[i] = (total[i << 1] + total[i << 1 | 1]) % mod;
}
void clean(int i)
{
if (lazy[i])
{
pool[i] = (total[i] - pool[i] + mod) % mod;
lazy[i << 1] ^= 1;
lazy[i << 1 | 1] ^= 1;
lazy[i] = 0;
}
}
void flip(int i, int l, int r, int ql, int qr)
{
clean(i);
if (qr < l or ql > r)
return;
if (ql <= l and qr >= r)
{
lazy[i] = 1;
clean(i);
return;
}
int m = (l + r) / 2;
flip(i << 1, l, m, ql, qr);
flip(i << 1 | 1, m + 1, r, ql, qr);
pool[i] = (pool[i << 1] + pool[i << 1 | 1]) % mod;
}
} st;
void dfs1(int i)
{
if (i >= N)
return choices[i] = 1, void();
choices[i] = adj[i].size();
for (int j : adj[i])
{
dfs1(j);
choices[i] = 1ll * choices[i] * choices[j] % mod;
}
}
void dfs2(int i, int x)
{
if (i >= N)
{
coef[i] = x;
return;
}
int n = adj[i].size();
vector<int> l(n), r(n);
for (int j = 0; j < n; j++)
{
int w = adj[i][j];
l[j] = r[j] = choices[w];
if (j)
l[j] = 1ll * l[j] * l[j - 1] % mod;
}
for (int j = n - 2; j >= 0; j--)
r[j] = 1ll * r[j] * r[j + 1] % mod;
for (int j = 0; j < n; j++)
{
ll q = 1ll * x * (j ? l[j - 1] : 1) % mod;
dfs2(adj[i][j], q * (j != n - 1 ? r[j + 1] : 1) % mod);
}
}
void init(int N_, int M_, vector<int> P, vector<int> A)
{
N = N_, M = M_;
for (int i = 1; i < N + M; i++)
adj[P[i]].push_back(i);
dfs1(0);
dfs2(0, 1);
C = coef + N;
st.build(1, 0, M - 1);
for (int i = 0; i < M; i++)
if (A[i])
st.flip(1, 0, M - 1, i, i);
}
int count_ways(int L, int R)
{
L -= N, R -= N;
st.flip(1, 0, M - 1, L, R);
return st.pool[1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4984 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
2 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
2 ms |
5072 KB |
Output is correct |
6 |
Correct |
2 ms |
5072 KB |
Output is correct |
7 |
Correct |
2 ms |
5072 KB |
Output is correct |
8 |
Correct |
2 ms |
5052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
2 ms |
4984 KB |
Output is correct |
4 |
Correct |
2 ms |
5072 KB |
Output is correct |
5 |
Correct |
2 ms |
5072 KB |
Output is correct |
6 |
Correct |
2 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 |
2 ms |
5072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4984 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
2 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
2 ms |
5072 KB |
Output is correct |
6 |
Correct |
2 ms |
5072 KB |
Output is correct |
7 |
Correct |
2 ms |
5072 KB |
Output is correct |
8 |
Correct |
2 ms |
5052 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
2 ms |
4944 KB |
Output is correct |
11 |
Correct |
2 ms |
4984 KB |
Output is correct |
12 |
Correct |
2 ms |
5072 KB |
Output is correct |
13 |
Correct |
2 ms |
5072 KB |
Output is correct |
14 |
Correct |
2 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 |
2 ms |
5072 KB |
Output is correct |
21 |
Correct |
2 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 |
3 ms |
5072 KB |
Output is correct |
28 |
Correct |
3 ms |
5072 KB |
Output is correct |
29 |
Correct |
2 ms |
5072 KB |
Output is correct |
30 |
Correct |
2 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 |
5016 KB |
Output is correct |
36 |
Correct |
3 ms |
5328 KB |
Output is correct |
37 |
Correct |
3 ms |
5328 KB |
Output is correct |
38 |
Correct |
4 ms |
5368 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
2 ms |
5072 KB |
Output is correct |
42 |
Correct |
2 ms |
5072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
511 ms |
8076 KB |
Output is correct |
2 |
Correct |
647 ms |
11140 KB |
Output is correct |
3 |
Correct |
674 ms |
11080 KB |
Output is correct |
4 |
Correct |
645 ms |
10472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
511 ms |
8076 KB |
Output is correct |
2 |
Correct |
647 ms |
11140 KB |
Output is correct |
3 |
Correct |
674 ms |
11080 KB |
Output is correct |
4 |
Correct |
645 ms |
10472 KB |
Output is correct |
5 |
Correct |
490 ms |
8008 KB |
Output is correct |
6 |
Correct |
667 ms |
11080 KB |
Output is correct |
7 |
Correct |
660 ms |
11164 KB |
Output is correct |
8 |
Correct |
676 ms |
11084 KB |
Output is correct |
9 |
Correct |
250 ms |
5200 KB |
Output is correct |
10 |
Correct |
562 ms |
5328 KB |
Output is correct |
11 |
Correct |
611 ms |
5328 KB |
Output is correct |
12 |
Correct |
617 ms |
5328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
2 ms |
4984 KB |
Output is correct |
4 |
Correct |
2 ms |
5072 KB |
Output is correct |
5 |
Correct |
2 ms |
5072 KB |
Output is correct |
6 |
Correct |
2 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 |
2 ms |
5072 KB |
Output is correct |
13 |
Correct |
511 ms |
8076 KB |
Output is correct |
14 |
Correct |
647 ms |
11140 KB |
Output is correct |
15 |
Correct |
674 ms |
11080 KB |
Output is correct |
16 |
Correct |
645 ms |
10472 KB |
Output is correct |
17 |
Correct |
490 ms |
8008 KB |
Output is correct |
18 |
Correct |
667 ms |
11080 KB |
Output is correct |
19 |
Correct |
660 ms |
11164 KB |
Output is correct |
20 |
Correct |
676 ms |
11084 KB |
Output is correct |
21 |
Correct |
250 ms |
5200 KB |
Output is correct |
22 |
Correct |
562 ms |
5328 KB |
Output is correct |
23 |
Correct |
611 ms |
5328 KB |
Output is correct |
24 |
Correct |
617 ms |
5328 KB |
Output is correct |
25 |
Correct |
646 ms |
15260 KB |
Output is correct |
26 |
Incorrect |
737 ms |
15304 KB |
1st lines differ - on the 1st token, expected: '72622774', found: '722513690' |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4984 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
2 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
2 ms |
5072 KB |
Output is correct |
6 |
Correct |
2 ms |
5072 KB |
Output is correct |
7 |
Correct |
2 ms |
5072 KB |
Output is correct |
8 |
Correct |
2 ms |
5052 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
2 ms |
4944 KB |
Output is correct |
11 |
Correct |
2 ms |
4984 KB |
Output is correct |
12 |
Correct |
2 ms |
5072 KB |
Output is correct |
13 |
Correct |
2 ms |
5072 KB |
Output is correct |
14 |
Correct |
2 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 |
2 ms |
5072 KB |
Output is correct |
21 |
Correct |
2 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 |
3 ms |
5072 KB |
Output is correct |
28 |
Correct |
3 ms |
5072 KB |
Output is correct |
29 |
Correct |
2 ms |
5072 KB |
Output is correct |
30 |
Correct |
2 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 |
5016 KB |
Output is correct |
36 |
Correct |
3 ms |
5328 KB |
Output is correct |
37 |
Correct |
3 ms |
5328 KB |
Output is correct |
38 |
Correct |
4 ms |
5368 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
2 ms |
5072 KB |
Output is correct |
42 |
Correct |
2 ms |
5072 KB |
Output is correct |
43 |
Correct |
381 ms |
5332 KB |
Output is correct |
44 |
Correct |
670 ms |
5400 KB |
Output is correct |
45 |
Correct |
679 ms |
5328 KB |
Output is correct |
46 |
Correct |
517 ms |
5584 KB |
Output is correct |
47 |
Correct |
746 ms |
5584 KB |
Output is correct |
48 |
Correct |
768 ms |
5584 KB |
Output is correct |
49 |
Correct |
691 ms |
5584 KB |
Output is correct |
50 |
Correct |
620 ms |
5604 KB |
Output is correct |
51 |
Correct |
601 ms |
5456 KB |
Output is correct |
52 |
Correct |
716 ms |
5456 KB |
Output is correct |
53 |
Correct |
364 ms |
6144 KB |
Output is correct |
54 |
Correct |
741 ms |
5584 KB |
Output is correct |
55 |
Correct |
738 ms |
5456 KB |
Output is correct |
56 |
Correct |
708 ms |
5456 KB |
Output is correct |
57 |
Correct |
596 ms |
5328 KB |
Output is correct |
58 |
Correct |
600 ms |
6608 KB |
Output is correct |
59 |
Correct |
759 ms |
6608 KB |
Output is correct |
60 |
Correct |
764 ms |
6608 KB |
Output is correct |
61 |
Correct |
635 ms |
5584 KB |
Output is correct |
62 |
Correct |
628 ms |
5328 KB |
Output is correct |
63 |
Correct |
673 ms |
5200 KB |
Output is correct |
64 |
Correct |
767 ms |
5456 KB |
Output is correct |
65 |
Correct |
341 ms |
5200 KB |
Output is correct |
66 |
Correct |
538 ms |
5328 KB |
Output is correct |
67 |
Correct |
709 ms |
5328 KB |
Output is correct |
68 |
Correct |
441 ms |
5328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4984 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
2 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
2 ms |
5072 KB |
Output is correct |
6 |
Correct |
2 ms |
5072 KB |
Output is correct |
7 |
Correct |
2 ms |
5072 KB |
Output is correct |
8 |
Correct |
2 ms |
5052 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
2 ms |
4944 KB |
Output is correct |
11 |
Correct |
2 ms |
4984 KB |
Output is correct |
12 |
Correct |
2 ms |
5072 KB |
Output is correct |
13 |
Correct |
2 ms |
5072 KB |
Output is correct |
14 |
Correct |
2 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 |
2 ms |
5072 KB |
Output is correct |
21 |
Correct |
2 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 |
3 ms |
5072 KB |
Output is correct |
28 |
Correct |
3 ms |
5072 KB |
Output is correct |
29 |
Correct |
2 ms |
5072 KB |
Output is correct |
30 |
Correct |
2 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 |
5016 KB |
Output is correct |
36 |
Correct |
3 ms |
5328 KB |
Output is correct |
37 |
Correct |
3 ms |
5328 KB |
Output is correct |
38 |
Correct |
4 ms |
5368 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
2 ms |
5072 KB |
Output is correct |
42 |
Correct |
2 ms |
5072 KB |
Output is correct |
43 |
Correct |
511 ms |
8076 KB |
Output is correct |
44 |
Correct |
647 ms |
11140 KB |
Output is correct |
45 |
Correct |
674 ms |
11080 KB |
Output is correct |
46 |
Correct |
645 ms |
10472 KB |
Output is correct |
47 |
Correct |
490 ms |
8008 KB |
Output is correct |
48 |
Correct |
667 ms |
11080 KB |
Output is correct |
49 |
Correct |
660 ms |
11164 KB |
Output is correct |
50 |
Correct |
676 ms |
11084 KB |
Output is correct |
51 |
Correct |
250 ms |
5200 KB |
Output is correct |
52 |
Correct |
562 ms |
5328 KB |
Output is correct |
53 |
Correct |
611 ms |
5328 KB |
Output is correct |
54 |
Correct |
617 ms |
5328 KB |
Output is correct |
55 |
Correct |
646 ms |
15260 KB |
Output is correct |
56 |
Incorrect |
737 ms |
15304 KB |
1st lines differ - on the 1st token, expected: '72622774', found: '722513690' |
57 |
Halted |
0 ms |
0 KB |
- |