#include <bits/stdc++.h>
#include "circuit.h"
using namespace std;
#define int long long
const int MXN = 2e5 + 5;
const int mod = 1e9 + 2022;
int n, m, res = 0;
vector<int> A, P;
vector<int> adj[MXN];
int dp[2][MXN], SZ[MXN];
int cnt = 0;
void calc(int a)
{
dp[0][a] = dp[1][a] = 0;
if (adj[a].empty())
{
dp[0][a] = A[a] ^ 1;
dp[1][a] = A[a];
return;
}
int sz = adj[a].size();
vector<int> dp1(sz + 1, 0);
dp1[0] = 1;
for (int &v : adj[a])
{
for (int i = sz; i >= 0; i--)
{
dp1[i] = (dp1[i] * dp[0][v]) % mod;
if (i) dp1[i] = (dp1[i] + ((dp1[i - 1] * dp[1][v]) % mod)) % mod;
}
}
for (int i = 0; i <= sz; i++)
{
dp[0][a] = (dp[0][a] + (((sz - i) * dp1[i]) % mod)) % mod;
dp[1][a] = (dp[1][a] + ((i * dp1[i]) % mod)) % mod;
}
}
void dfs(int a)
{
for (int &v : adj[a])
{
dfs(v);
}
calc(a);
}
int val[MXN];
array<int, 2> st[MXN << 2];
int lz[MXN << 2];
void relax(int l, int r, int x)
{
if (!lz[x]) return;
swap(st[x][0], st[x][1]);
if (l == r)
{
lz[x] = 0;
return;
}
lz[2*x] ^= lz[x], lz[2*x + 1] ^= lz[x];
lz[x] = 0;
}
void upd(int l, int r, int x, int lx, int rx)
{
relax(l, r, x);
if (l > rx || r < lx) return;
if (l >= lx && r <= rx)
{
lz[x] ^= 1;
relax(l, r, x);
return;
}
int mid = (l + r) >> 1;
upd(l, mid, 2*x, lx, rx);
upd(mid + 1, r, 2*x + 1, lx, rx);
st[x][0] = (st[2*x][0] + st[2*x + 1][0]) % mod;
st[x][1] = (st[2*x][1] + st[2*x + 1][1]) % mod;
}
void build(int l, int r, int x)
{
if (l == r)
{
st[x][0] = A[l] * val[l];
st[x][1] = (A[l] ^ 1) * val[l];
return;
}
int mid = (l + r) >> 1;
build(l, mid, 2*x);
build(mid + 1, r, 2*x + 1);
st[x][0] = (st[2*x][0] + st[2*x + 1][0]) % mod;
st[x][1] = (st[2*x][1] + st[2*x + 1][1]) % mod;
}
void init(int32_t N, int32_t M, vector<int32_t> PP, vector<int32_t> AA)
{
n = N, m = M;
for (int32_t &i : PP) P.push_back(i);
for (int32_t &i : AA) A.push_back(i);
reverse(A.begin(), A.end());
A.resize(N + M, 0);
reverse(A.begin(), A.end());
for (int i = 1; i < N + M; i++) adj[P[i]].push_back(i);
vector<int> prev = A;
A.assign(N + M, 0);
dfs(0);
int org = dp[1][0];
for (int i = N; i < N + M; i++)
{
A[i] = 1;
dfs(0);
val[i] = (dp[1][0] - org + mod) % mod;
A[i] = 0;
}
A = prev;
build(N, N + M - 1, 1);
}
int32_t count_ways(int32_t L, int32_t R) {
upd(n, n + m - 1, 1, L, R);
return st[1][0];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13400 KB |
Output is correct |
2 |
Correct |
2 ms |
13400 KB |
Output is correct |
3 |
Correct |
2608 ms |
13628 KB |
Output is correct |
4 |
Correct |
2820 ms |
13620 KB |
Output is correct |
5 |
Correct |
2860 ms |
13628 KB |
Output is correct |
6 |
Correct |
2879 ms |
13624 KB |
Output is correct |
7 |
Correct |
2784 ms |
13624 KB |
Output is correct |
8 |
Correct |
2788 ms |
13628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13396 KB |
Output is correct |
2 |
Correct |
5 ms |
13400 KB |
Output is correct |
3 |
Correct |
15 ms |
13400 KB |
Output is correct |
4 |
Correct |
15 ms |
13644 KB |
Output is correct |
5 |
Correct |
15 ms |
13640 KB |
Output is correct |
6 |
Correct |
33 ms |
13644 KB |
Output is correct |
7 |
Correct |
62 ms |
13664 KB |
Output is correct |
8 |
Correct |
58 ms |
13680 KB |
Output is correct |
9 |
Correct |
58 ms |
13648 KB |
Output is correct |
10 |
Correct |
55 ms |
13700 KB |
Output is correct |
11 |
Correct |
54 ms |
13648 KB |
Output is correct |
12 |
Correct |
40 ms |
13656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13400 KB |
Output is correct |
2 |
Correct |
2 ms |
13400 KB |
Output is correct |
3 |
Correct |
2608 ms |
13628 KB |
Output is correct |
4 |
Correct |
2820 ms |
13620 KB |
Output is correct |
5 |
Correct |
2860 ms |
13628 KB |
Output is correct |
6 |
Correct |
2879 ms |
13624 KB |
Output is correct |
7 |
Correct |
2784 ms |
13624 KB |
Output is correct |
8 |
Correct |
2788 ms |
13628 KB |
Output is correct |
9 |
Correct |
2 ms |
13396 KB |
Output is correct |
10 |
Correct |
5 ms |
13400 KB |
Output is correct |
11 |
Correct |
15 ms |
13400 KB |
Output is correct |
12 |
Correct |
15 ms |
13644 KB |
Output is correct |
13 |
Correct |
15 ms |
13640 KB |
Output is correct |
14 |
Correct |
33 ms |
13644 KB |
Output is correct |
15 |
Correct |
62 ms |
13664 KB |
Output is correct |
16 |
Correct |
58 ms |
13680 KB |
Output is correct |
17 |
Correct |
58 ms |
13648 KB |
Output is correct |
18 |
Correct |
55 ms |
13700 KB |
Output is correct |
19 |
Correct |
54 ms |
13648 KB |
Output is correct |
20 |
Correct |
40 ms |
13656 KB |
Output is correct |
21 |
Correct |
47 ms |
13648 KB |
Output is correct |
22 |
Correct |
20 ms |
13656 KB |
Output is correct |
23 |
Correct |
21 ms |
13656 KB |
Output is correct |
24 |
Correct |
60 ms |
13688 KB |
Output is correct |
25 |
Correct |
62 ms |
13684 KB |
Output is correct |
26 |
Correct |
61 ms |
13648 KB |
Output is correct |
27 |
Correct |
62 ms |
13672 KB |
Output is correct |
28 |
Correct |
61 ms |
13684 KB |
Output is correct |
29 |
Correct |
2831 ms |
13624 KB |
Output is correct |
30 |
Correct |
2767 ms |
13396 KB |
Output is correct |
31 |
Correct |
2 ms |
13656 KB |
Output is correct |
32 |
Correct |
60 ms |
13672 KB |
Output is correct |
33 |
Correct |
40 ms |
13648 KB |
Output is correct |
34 |
Correct |
36 ms |
13644 KB |
Output is correct |
35 |
Correct |
528 ms |
13620 KB |
Output is correct |
36 |
Correct |
54 ms |
13648 KB |
Output is correct |
37 |
Correct |
2856 ms |
13704 KB |
Output is correct |
38 |
Correct |
2885 ms |
13708 KB |
Output is correct |
39 |
Correct |
14 ms |
13656 KB |
Output is correct |
40 |
Correct |
31 ms |
13644 KB |
Output is correct |
41 |
Correct |
30 ms |
13644 KB |
Output is correct |
42 |
Correct |
101 ms |
13624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3045 ms |
14676 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3045 ms |
14676 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13396 KB |
Output is correct |
2 |
Correct |
5 ms |
13400 KB |
Output is correct |
3 |
Correct |
15 ms |
13400 KB |
Output is correct |
4 |
Correct |
15 ms |
13644 KB |
Output is correct |
5 |
Correct |
15 ms |
13640 KB |
Output is correct |
6 |
Correct |
33 ms |
13644 KB |
Output is correct |
7 |
Correct |
62 ms |
13664 KB |
Output is correct |
8 |
Correct |
58 ms |
13680 KB |
Output is correct |
9 |
Correct |
58 ms |
13648 KB |
Output is correct |
10 |
Correct |
55 ms |
13700 KB |
Output is correct |
11 |
Correct |
54 ms |
13648 KB |
Output is correct |
12 |
Correct |
40 ms |
13656 KB |
Output is correct |
13 |
Execution timed out |
3045 ms |
14676 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13400 KB |
Output is correct |
2 |
Correct |
2 ms |
13400 KB |
Output is correct |
3 |
Correct |
2608 ms |
13628 KB |
Output is correct |
4 |
Correct |
2820 ms |
13620 KB |
Output is correct |
5 |
Correct |
2860 ms |
13628 KB |
Output is correct |
6 |
Correct |
2879 ms |
13624 KB |
Output is correct |
7 |
Correct |
2784 ms |
13624 KB |
Output is correct |
8 |
Correct |
2788 ms |
13628 KB |
Output is correct |
9 |
Correct |
2 ms |
13396 KB |
Output is correct |
10 |
Correct |
5 ms |
13400 KB |
Output is correct |
11 |
Correct |
15 ms |
13400 KB |
Output is correct |
12 |
Correct |
15 ms |
13644 KB |
Output is correct |
13 |
Correct |
15 ms |
13640 KB |
Output is correct |
14 |
Correct |
33 ms |
13644 KB |
Output is correct |
15 |
Correct |
62 ms |
13664 KB |
Output is correct |
16 |
Correct |
58 ms |
13680 KB |
Output is correct |
17 |
Correct |
58 ms |
13648 KB |
Output is correct |
18 |
Correct |
55 ms |
13700 KB |
Output is correct |
19 |
Correct |
54 ms |
13648 KB |
Output is correct |
20 |
Correct |
40 ms |
13656 KB |
Output is correct |
21 |
Correct |
47 ms |
13648 KB |
Output is correct |
22 |
Correct |
20 ms |
13656 KB |
Output is correct |
23 |
Correct |
21 ms |
13656 KB |
Output is correct |
24 |
Correct |
60 ms |
13688 KB |
Output is correct |
25 |
Correct |
62 ms |
13684 KB |
Output is correct |
26 |
Correct |
61 ms |
13648 KB |
Output is correct |
27 |
Correct |
62 ms |
13672 KB |
Output is correct |
28 |
Correct |
61 ms |
13684 KB |
Output is correct |
29 |
Correct |
2831 ms |
13624 KB |
Output is correct |
30 |
Correct |
2767 ms |
13396 KB |
Output is correct |
31 |
Correct |
2 ms |
13656 KB |
Output is correct |
32 |
Correct |
60 ms |
13672 KB |
Output is correct |
33 |
Correct |
40 ms |
13648 KB |
Output is correct |
34 |
Correct |
36 ms |
13644 KB |
Output is correct |
35 |
Correct |
528 ms |
13620 KB |
Output is correct |
36 |
Correct |
54 ms |
13648 KB |
Output is correct |
37 |
Correct |
2856 ms |
13704 KB |
Output is correct |
38 |
Correct |
2885 ms |
13708 KB |
Output is correct |
39 |
Correct |
14 ms |
13656 KB |
Output is correct |
40 |
Correct |
31 ms |
13644 KB |
Output is correct |
41 |
Correct |
30 ms |
13644 KB |
Output is correct |
42 |
Correct |
101 ms |
13624 KB |
Output is correct |
43 |
Correct |
888 ms |
15952 KB |
Output is correct |
44 |
Correct |
1111 ms |
15952 KB |
Output is correct |
45 |
Correct |
1106 ms |
15952 KB |
Output is correct |
46 |
Correct |
2442 ms |
16416 KB |
Output is correct |
47 |
Correct |
2508 ms |
16172 KB |
Output is correct |
48 |
Correct |
2572 ms |
16156 KB |
Output is correct |
49 |
Correct |
2410 ms |
16160 KB |
Output is correct |
50 |
Correct |
2500 ms |
15956 KB |
Output is correct |
51 |
Execution timed out |
3044 ms |
11600 KB |
Time limit exceeded |
52 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13400 KB |
Output is correct |
2 |
Correct |
2 ms |
13400 KB |
Output is correct |
3 |
Correct |
2608 ms |
13628 KB |
Output is correct |
4 |
Correct |
2820 ms |
13620 KB |
Output is correct |
5 |
Correct |
2860 ms |
13628 KB |
Output is correct |
6 |
Correct |
2879 ms |
13624 KB |
Output is correct |
7 |
Correct |
2784 ms |
13624 KB |
Output is correct |
8 |
Correct |
2788 ms |
13628 KB |
Output is correct |
9 |
Correct |
2 ms |
13396 KB |
Output is correct |
10 |
Correct |
5 ms |
13400 KB |
Output is correct |
11 |
Correct |
15 ms |
13400 KB |
Output is correct |
12 |
Correct |
15 ms |
13644 KB |
Output is correct |
13 |
Correct |
15 ms |
13640 KB |
Output is correct |
14 |
Correct |
33 ms |
13644 KB |
Output is correct |
15 |
Correct |
62 ms |
13664 KB |
Output is correct |
16 |
Correct |
58 ms |
13680 KB |
Output is correct |
17 |
Correct |
58 ms |
13648 KB |
Output is correct |
18 |
Correct |
55 ms |
13700 KB |
Output is correct |
19 |
Correct |
54 ms |
13648 KB |
Output is correct |
20 |
Correct |
40 ms |
13656 KB |
Output is correct |
21 |
Correct |
47 ms |
13648 KB |
Output is correct |
22 |
Correct |
20 ms |
13656 KB |
Output is correct |
23 |
Correct |
21 ms |
13656 KB |
Output is correct |
24 |
Correct |
60 ms |
13688 KB |
Output is correct |
25 |
Correct |
62 ms |
13684 KB |
Output is correct |
26 |
Correct |
61 ms |
13648 KB |
Output is correct |
27 |
Correct |
62 ms |
13672 KB |
Output is correct |
28 |
Correct |
61 ms |
13684 KB |
Output is correct |
29 |
Correct |
2831 ms |
13624 KB |
Output is correct |
30 |
Correct |
2767 ms |
13396 KB |
Output is correct |
31 |
Correct |
2 ms |
13656 KB |
Output is correct |
32 |
Correct |
60 ms |
13672 KB |
Output is correct |
33 |
Correct |
40 ms |
13648 KB |
Output is correct |
34 |
Correct |
36 ms |
13644 KB |
Output is correct |
35 |
Correct |
528 ms |
13620 KB |
Output is correct |
36 |
Correct |
54 ms |
13648 KB |
Output is correct |
37 |
Correct |
2856 ms |
13704 KB |
Output is correct |
38 |
Correct |
2885 ms |
13708 KB |
Output is correct |
39 |
Correct |
14 ms |
13656 KB |
Output is correct |
40 |
Correct |
31 ms |
13644 KB |
Output is correct |
41 |
Correct |
30 ms |
13644 KB |
Output is correct |
42 |
Correct |
101 ms |
13624 KB |
Output is correct |
43 |
Execution timed out |
3045 ms |
14676 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |