#include "circuit.h"
#include "bits/stdc++.h"
using namespace std;
#define int long long
#include <vector>
const int MAXN = 2e5 + 12;
const int MOD = 1e9 + 2022;
int n, m;
vector<int> adj[MAXN];
int contrib[MAXN];
int ans;
int a[MAXN];
int lazy[MAXN];
int Y[MAXN];
const int S = 320;
int sum[S];
int state[S];
int tot[S];
void calc_y(int node) {
Y[node] = adj[node].size();
Y[node] = max(1ll, Y[node]);
for(int x: adj[node]) {
calc_y(x);
Y[node] *= Y[x];
Y[node] %= MOD;
}
}
void propagate(int node) {
for(int x: adj[node]) {
lazy[x] *= lazy[node];
lazy[x] %= MOD;
propagate(x);
}
}
void init(int32_t N, int32_t M, std::vector<int32_t> P, std::vector<int32_t> A) {
for(int i=0; i<M; i++) contrib[i] = 1;
for(int i=1; i<N+M; i++) adj[P[i]].push_back(i);
for(int i=0; i<M; i++) a[i] = A[i];
n = N;
m = M;
ans = 0;
calc_y(0);
for(int i=0; i<N+M; i++) lazy[i] = 1;
for(int i=0; i<N; i++) {
int len = adj[i].size();
int pre[len], suf[len];
for(int j=0; j<len; j++) {
pre[j] = Y[adj[i][j]];
suf[j] = Y[adj[i][j]];
}
for(int j=1; j<len; j++) {
pre[j] *= pre[j-1];
pre[j] %= MOD;
}
for(int j=len-2; j>=0; j--) {
suf[j] *= suf[j+1];
suf[j] %= MOD;
}
for(int j=0; j<len; j++) {
if(j > 0) lazy[adj[i][j]] *= pre[j-1];
lazy[adj[i][j]] %= MOD;
if(j + 1 < len) lazy[adj[i][j]] *= suf[j+1];
lazy[adj[i][j]] %= MOD;
}
}
propagate(0);
for(int i=0; i<N+M; i++) {
contrib[i] = lazy[i];
}
for(int i=0; i<M; i++) {
sum[i / S] += a[i] * contrib[i + N];
sum[i / S] %= MOD;
state[i / S] = 0;
tot[i / S] += contrib[i + N];
tot[i / S] %= MOD;
}
for(int i=N; i<N+M; i++) {
if(a[i-N]) ans = (ans + contrib[i]) % MOD;
}
}
int32_t count_ways(int32_t L, int32_t R) {
L -= n;
R -= n;
if(R - L <= 2 * S) {
for(int i=L; i<=R; i++) {
int og = a[i] ^ state[i / S];
a[i] ^= 1;
ans += MOD - og * contrib[i + n];
ans %= MOD;
sum[i / S] += MOD - og * contrib[i + n];
sum[i / S] %= MOD;
og ^= 1;
ans += og * contrib[i + n];
ans %= MOD;
sum[i / S] += og * contrib[i + n];
sum[i / S] %= MOD;
}
return ans;
}
while(L % S != 0) {
int i = L;
int og = a[i] ^ state[i / S];
a[i] ^= 1;
ans += MOD - og * contrib[i + n];
ans %= MOD;
sum[i / S] += MOD - og * contrib[i + n];
sum[i / S] %= MOD;
og ^= 1;
ans += og * contrib[i + n];
ans %= MOD;
sum[i / S] += og * contrib[i + n];
sum[i / S] %= MOD;
L++;
}
while(R % S != S - 1) {
int i = R;
int og = a[i] ^ state[i / S];
a[i] ^= 1;
ans += MOD - og * contrib[i + n];
ans %= MOD;
sum[i / S] += MOD - og * contrib[i + n];
sum[i / S] %= MOD;
og ^= 1;
ans += og * contrib[i + n];
ans %= MOD;
sum[i / S] += og * contrib[i + n];
sum[i / S] %= MOD;
R--;
}
for(int i=L; i<=R; i+=S) {
ans += MOD - sum[i / S];
ans %= MOD;
state[i / S] ^= 1;
ans += MOD + tot[i / S] - sum[i / S];
ans %= MOD;
sum[i / S] = tot[i / S] - sum[i / S];
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
11096 KB |
Output is correct |
2 |
Correct |
2 ms |
11096 KB |
Output is correct |
3 |
Correct |
3 ms |
11096 KB |
Output is correct |
4 |
Correct |
3 ms |
11096 KB |
Output is correct |
5 |
Correct |
3 ms |
11096 KB |
Output is correct |
6 |
Correct |
2 ms |
11096 KB |
Output is correct |
7 |
Correct |
4 ms |
11096 KB |
Output is correct |
8 |
Correct |
5 ms |
11096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
11104 KB |
Output is correct |
2 |
Correct |
3 ms |
11096 KB |
Output is correct |
3 |
Correct |
4 ms |
11096 KB |
Output is correct |
4 |
Correct |
3 ms |
11096 KB |
Output is correct |
5 |
Correct |
4 ms |
11096 KB |
Output is correct |
6 |
Correct |
4 ms |
11096 KB |
Output is correct |
7 |
Correct |
4 ms |
11096 KB |
Output is correct |
8 |
Correct |
3 ms |
11096 KB |
Output is correct |
9 |
Correct |
4 ms |
11096 KB |
Output is correct |
10 |
Correct |
3 ms |
11508 KB |
Output is correct |
11 |
Correct |
4 ms |
11352 KB |
Output is correct |
12 |
Correct |
3 ms |
11096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
11096 KB |
Output is correct |
2 |
Correct |
2 ms |
11096 KB |
Output is correct |
3 |
Correct |
3 ms |
11096 KB |
Output is correct |
4 |
Correct |
3 ms |
11096 KB |
Output is correct |
5 |
Correct |
3 ms |
11096 KB |
Output is correct |
6 |
Correct |
2 ms |
11096 KB |
Output is correct |
7 |
Correct |
4 ms |
11096 KB |
Output is correct |
8 |
Correct |
5 ms |
11096 KB |
Output is correct |
9 |
Correct |
4 ms |
11104 KB |
Output is correct |
10 |
Correct |
3 ms |
11096 KB |
Output is correct |
11 |
Correct |
4 ms |
11096 KB |
Output is correct |
12 |
Correct |
3 ms |
11096 KB |
Output is correct |
13 |
Correct |
4 ms |
11096 KB |
Output is correct |
14 |
Correct |
4 ms |
11096 KB |
Output is correct |
15 |
Correct |
4 ms |
11096 KB |
Output is correct |
16 |
Correct |
3 ms |
11096 KB |
Output is correct |
17 |
Correct |
4 ms |
11096 KB |
Output is correct |
18 |
Correct |
3 ms |
11508 KB |
Output is correct |
19 |
Correct |
4 ms |
11352 KB |
Output is correct |
20 |
Correct |
3 ms |
11096 KB |
Output is correct |
21 |
Correct |
4 ms |
11096 KB |
Output is correct |
22 |
Correct |
4 ms |
11224 KB |
Output is correct |
23 |
Correct |
6 ms |
11096 KB |
Output is correct |
24 |
Correct |
3 ms |
11096 KB |
Output is correct |
25 |
Correct |
4 ms |
11096 KB |
Output is correct |
26 |
Correct |
5 ms |
11096 KB |
Output is correct |
27 |
Correct |
4 ms |
11096 KB |
Output is correct |
28 |
Correct |
3 ms |
11104 KB |
Output is correct |
29 |
Correct |
4 ms |
11096 KB |
Output is correct |
30 |
Correct |
3 ms |
11096 KB |
Output is correct |
31 |
Correct |
3 ms |
11096 KB |
Output is correct |
32 |
Correct |
3 ms |
11096 KB |
Output is correct |
33 |
Correct |
3 ms |
11096 KB |
Output is correct |
34 |
Correct |
3 ms |
11096 KB |
Output is correct |
35 |
Correct |
3 ms |
11096 KB |
Output is correct |
36 |
Correct |
4 ms |
11352 KB |
Output is correct |
37 |
Correct |
4 ms |
11352 KB |
Output is correct |
38 |
Correct |
4 ms |
11352 KB |
Output is correct |
39 |
Correct |
3 ms |
11096 KB |
Output is correct |
40 |
Correct |
3 ms |
11096 KB |
Output is correct |
41 |
Correct |
3 ms |
11092 KB |
Output is correct |
42 |
Correct |
4 ms |
11096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
375 ms |
13400 KB |
Output is correct |
2 |
Correct |
526 ms |
14796 KB |
Output is correct |
3 |
Correct |
519 ms |
14808 KB |
Output is correct |
4 |
Correct |
554 ms |
14816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
375 ms |
13400 KB |
Output is correct |
2 |
Correct |
526 ms |
14796 KB |
Output is correct |
3 |
Correct |
519 ms |
14808 KB |
Output is correct |
4 |
Correct |
554 ms |
14816 KB |
Output is correct |
5 |
Correct |
688 ms |
13400 KB |
Output is correct |
6 |
Correct |
881 ms |
14936 KB |
Output is correct |
7 |
Correct |
888 ms |
14816 KB |
Output is correct |
8 |
Correct |
1093 ms |
14824 KB |
Output is correct |
9 |
Correct |
392 ms |
11352 KB |
Output is correct |
10 |
Correct |
761 ms |
11352 KB |
Output is correct |
11 |
Correct |
821 ms |
11352 KB |
Output is correct |
12 |
Correct |
990 ms |
11352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
11104 KB |
Output is correct |
2 |
Correct |
3 ms |
11096 KB |
Output is correct |
3 |
Correct |
4 ms |
11096 KB |
Output is correct |
4 |
Correct |
3 ms |
11096 KB |
Output is correct |
5 |
Correct |
4 ms |
11096 KB |
Output is correct |
6 |
Correct |
4 ms |
11096 KB |
Output is correct |
7 |
Correct |
4 ms |
11096 KB |
Output is correct |
8 |
Correct |
3 ms |
11096 KB |
Output is correct |
9 |
Correct |
4 ms |
11096 KB |
Output is correct |
10 |
Correct |
3 ms |
11508 KB |
Output is correct |
11 |
Correct |
4 ms |
11352 KB |
Output is correct |
12 |
Correct |
3 ms |
11096 KB |
Output is correct |
13 |
Correct |
375 ms |
13400 KB |
Output is correct |
14 |
Correct |
526 ms |
14796 KB |
Output is correct |
15 |
Correct |
519 ms |
14808 KB |
Output is correct |
16 |
Correct |
554 ms |
14816 KB |
Output is correct |
17 |
Correct |
688 ms |
13400 KB |
Output is correct |
18 |
Correct |
881 ms |
14936 KB |
Output is correct |
19 |
Correct |
888 ms |
14816 KB |
Output is correct |
20 |
Correct |
1093 ms |
14824 KB |
Output is correct |
21 |
Correct |
392 ms |
11352 KB |
Output is correct |
22 |
Correct |
761 ms |
11352 KB |
Output is correct |
23 |
Correct |
821 ms |
11352 KB |
Output is correct |
24 |
Correct |
990 ms |
11352 KB |
Output is correct |
25 |
Correct |
916 ms |
16732 KB |
Output is correct |
26 |
Correct |
957 ms |
16736 KB |
Output is correct |
27 |
Correct |
943 ms |
16752 KB |
Output is correct |
28 |
Correct |
986 ms |
16728 KB |
Output is correct |
29 |
Correct |
905 ms |
23152 KB |
Output is correct |
30 |
Correct |
1135 ms |
23116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
11096 KB |
Output is correct |
2 |
Correct |
2 ms |
11096 KB |
Output is correct |
3 |
Correct |
3 ms |
11096 KB |
Output is correct |
4 |
Correct |
3 ms |
11096 KB |
Output is correct |
5 |
Correct |
3 ms |
11096 KB |
Output is correct |
6 |
Correct |
2 ms |
11096 KB |
Output is correct |
7 |
Correct |
4 ms |
11096 KB |
Output is correct |
8 |
Correct |
5 ms |
11096 KB |
Output is correct |
9 |
Correct |
4 ms |
11104 KB |
Output is correct |
10 |
Correct |
3 ms |
11096 KB |
Output is correct |
11 |
Correct |
4 ms |
11096 KB |
Output is correct |
12 |
Correct |
3 ms |
11096 KB |
Output is correct |
13 |
Correct |
4 ms |
11096 KB |
Output is correct |
14 |
Correct |
4 ms |
11096 KB |
Output is correct |
15 |
Correct |
4 ms |
11096 KB |
Output is correct |
16 |
Correct |
3 ms |
11096 KB |
Output is correct |
17 |
Correct |
4 ms |
11096 KB |
Output is correct |
18 |
Correct |
3 ms |
11508 KB |
Output is correct |
19 |
Correct |
4 ms |
11352 KB |
Output is correct |
20 |
Correct |
3 ms |
11096 KB |
Output is correct |
21 |
Correct |
4 ms |
11096 KB |
Output is correct |
22 |
Correct |
4 ms |
11224 KB |
Output is correct |
23 |
Correct |
6 ms |
11096 KB |
Output is correct |
24 |
Correct |
3 ms |
11096 KB |
Output is correct |
25 |
Correct |
4 ms |
11096 KB |
Output is correct |
26 |
Correct |
5 ms |
11096 KB |
Output is correct |
27 |
Correct |
4 ms |
11096 KB |
Output is correct |
28 |
Correct |
3 ms |
11104 KB |
Output is correct |
29 |
Correct |
4 ms |
11096 KB |
Output is correct |
30 |
Correct |
3 ms |
11096 KB |
Output is correct |
31 |
Correct |
3 ms |
11096 KB |
Output is correct |
32 |
Correct |
3 ms |
11096 KB |
Output is correct |
33 |
Correct |
3 ms |
11096 KB |
Output is correct |
34 |
Correct |
3 ms |
11096 KB |
Output is correct |
35 |
Correct |
3 ms |
11096 KB |
Output is correct |
36 |
Correct |
4 ms |
11352 KB |
Output is correct |
37 |
Correct |
4 ms |
11352 KB |
Output is correct |
38 |
Correct |
4 ms |
11352 KB |
Output is correct |
39 |
Correct |
3 ms |
11096 KB |
Output is correct |
40 |
Correct |
3 ms |
11096 KB |
Output is correct |
41 |
Correct |
3 ms |
11092 KB |
Output is correct |
42 |
Correct |
4 ms |
11096 KB |
Output is correct |
43 |
Correct |
567 ms |
11352 KB |
Output is correct |
44 |
Correct |
777 ms |
11352 KB |
Output is correct |
45 |
Correct |
773 ms |
11352 KB |
Output is correct |
46 |
Correct |
524 ms |
11608 KB |
Output is correct |
47 |
Correct |
825 ms |
11608 KB |
Output is correct |
48 |
Correct |
822 ms |
11608 KB |
Output is correct |
49 |
Correct |
819 ms |
11608 KB |
Output is correct |
50 |
Correct |
906 ms |
11608 KB |
Output is correct |
51 |
Correct |
947 ms |
11512 KB |
Output is correct |
52 |
Correct |
777 ms |
11512 KB |
Output is correct |
53 |
Correct |
573 ms |
11748 KB |
Output is correct |
54 |
Correct |
859 ms |
11352 KB |
Output is correct |
55 |
Correct |
809 ms |
11352 KB |
Output is correct |
56 |
Correct |
818 ms |
11352 KB |
Output is correct |
57 |
Correct |
790 ms |
11352 KB |
Output is correct |
58 |
Correct |
806 ms |
11864 KB |
Output is correct |
59 |
Correct |
790 ms |
11980 KB |
Output is correct |
60 |
Correct |
937 ms |
11980 KB |
Output is correct |
61 |
Correct |
784 ms |
11352 KB |
Output is correct |
62 |
Correct |
805 ms |
11352 KB |
Output is correct |
63 |
Correct |
811 ms |
11352 KB |
Output is correct |
64 |
Correct |
802 ms |
11352 KB |
Output is correct |
65 |
Correct |
398 ms |
11352 KB |
Output is correct |
66 |
Correct |
805 ms |
11352 KB |
Output is correct |
67 |
Correct |
797 ms |
11352 KB |
Output is correct |
68 |
Correct |
984 ms |
11352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
11096 KB |
Output is correct |
2 |
Correct |
2 ms |
11096 KB |
Output is correct |
3 |
Correct |
3 ms |
11096 KB |
Output is correct |
4 |
Correct |
3 ms |
11096 KB |
Output is correct |
5 |
Correct |
3 ms |
11096 KB |
Output is correct |
6 |
Correct |
2 ms |
11096 KB |
Output is correct |
7 |
Correct |
4 ms |
11096 KB |
Output is correct |
8 |
Correct |
5 ms |
11096 KB |
Output is correct |
9 |
Correct |
4 ms |
11104 KB |
Output is correct |
10 |
Correct |
3 ms |
11096 KB |
Output is correct |
11 |
Correct |
4 ms |
11096 KB |
Output is correct |
12 |
Correct |
3 ms |
11096 KB |
Output is correct |
13 |
Correct |
4 ms |
11096 KB |
Output is correct |
14 |
Correct |
4 ms |
11096 KB |
Output is correct |
15 |
Correct |
4 ms |
11096 KB |
Output is correct |
16 |
Correct |
3 ms |
11096 KB |
Output is correct |
17 |
Correct |
4 ms |
11096 KB |
Output is correct |
18 |
Correct |
3 ms |
11508 KB |
Output is correct |
19 |
Correct |
4 ms |
11352 KB |
Output is correct |
20 |
Correct |
3 ms |
11096 KB |
Output is correct |
21 |
Correct |
4 ms |
11096 KB |
Output is correct |
22 |
Correct |
4 ms |
11224 KB |
Output is correct |
23 |
Correct |
6 ms |
11096 KB |
Output is correct |
24 |
Correct |
3 ms |
11096 KB |
Output is correct |
25 |
Correct |
4 ms |
11096 KB |
Output is correct |
26 |
Correct |
5 ms |
11096 KB |
Output is correct |
27 |
Correct |
4 ms |
11096 KB |
Output is correct |
28 |
Correct |
3 ms |
11104 KB |
Output is correct |
29 |
Correct |
4 ms |
11096 KB |
Output is correct |
30 |
Correct |
3 ms |
11096 KB |
Output is correct |
31 |
Correct |
3 ms |
11096 KB |
Output is correct |
32 |
Correct |
3 ms |
11096 KB |
Output is correct |
33 |
Correct |
3 ms |
11096 KB |
Output is correct |
34 |
Correct |
3 ms |
11096 KB |
Output is correct |
35 |
Correct |
3 ms |
11096 KB |
Output is correct |
36 |
Correct |
4 ms |
11352 KB |
Output is correct |
37 |
Correct |
4 ms |
11352 KB |
Output is correct |
38 |
Correct |
4 ms |
11352 KB |
Output is correct |
39 |
Correct |
3 ms |
11096 KB |
Output is correct |
40 |
Correct |
3 ms |
11096 KB |
Output is correct |
41 |
Correct |
3 ms |
11092 KB |
Output is correct |
42 |
Correct |
4 ms |
11096 KB |
Output is correct |
43 |
Correct |
375 ms |
13400 KB |
Output is correct |
44 |
Correct |
526 ms |
14796 KB |
Output is correct |
45 |
Correct |
519 ms |
14808 KB |
Output is correct |
46 |
Correct |
554 ms |
14816 KB |
Output is correct |
47 |
Correct |
688 ms |
13400 KB |
Output is correct |
48 |
Correct |
881 ms |
14936 KB |
Output is correct |
49 |
Correct |
888 ms |
14816 KB |
Output is correct |
50 |
Correct |
1093 ms |
14824 KB |
Output is correct |
51 |
Correct |
392 ms |
11352 KB |
Output is correct |
52 |
Correct |
761 ms |
11352 KB |
Output is correct |
53 |
Correct |
821 ms |
11352 KB |
Output is correct |
54 |
Correct |
990 ms |
11352 KB |
Output is correct |
55 |
Correct |
916 ms |
16732 KB |
Output is correct |
56 |
Correct |
957 ms |
16736 KB |
Output is correct |
57 |
Correct |
943 ms |
16752 KB |
Output is correct |
58 |
Correct |
986 ms |
16728 KB |
Output is correct |
59 |
Correct |
905 ms |
23152 KB |
Output is correct |
60 |
Correct |
1135 ms |
23116 KB |
Output is correct |
61 |
Correct |
567 ms |
11352 KB |
Output is correct |
62 |
Correct |
777 ms |
11352 KB |
Output is correct |
63 |
Correct |
773 ms |
11352 KB |
Output is correct |
64 |
Correct |
524 ms |
11608 KB |
Output is correct |
65 |
Correct |
825 ms |
11608 KB |
Output is correct |
66 |
Correct |
822 ms |
11608 KB |
Output is correct |
67 |
Correct |
819 ms |
11608 KB |
Output is correct |
68 |
Correct |
906 ms |
11608 KB |
Output is correct |
69 |
Correct |
947 ms |
11512 KB |
Output is correct |
70 |
Correct |
777 ms |
11512 KB |
Output is correct |
71 |
Correct |
573 ms |
11748 KB |
Output is correct |
72 |
Correct |
859 ms |
11352 KB |
Output is correct |
73 |
Correct |
809 ms |
11352 KB |
Output is correct |
74 |
Correct |
818 ms |
11352 KB |
Output is correct |
75 |
Correct |
790 ms |
11352 KB |
Output is correct |
76 |
Correct |
806 ms |
11864 KB |
Output is correct |
77 |
Correct |
790 ms |
11980 KB |
Output is correct |
78 |
Correct |
937 ms |
11980 KB |
Output is correct |
79 |
Correct |
784 ms |
11352 KB |
Output is correct |
80 |
Correct |
805 ms |
11352 KB |
Output is correct |
81 |
Correct |
811 ms |
11352 KB |
Output is correct |
82 |
Correct |
802 ms |
11352 KB |
Output is correct |
83 |
Correct |
398 ms |
11352 KB |
Output is correct |
84 |
Correct |
805 ms |
11352 KB |
Output is correct |
85 |
Correct |
797 ms |
11352 KB |
Output is correct |
86 |
Correct |
984 ms |
11352 KB |
Output is correct |
87 |
Correct |
2 ms |
11096 KB |
Output is correct |
88 |
Correct |
559 ms |
16828 KB |
Output is correct |
89 |
Correct |
862 ms |
15232 KB |
Output is correct |
90 |
Correct |
925 ms |
15448 KB |
Output is correct |
91 |
Correct |
589 ms |
17268 KB |
Output is correct |
92 |
Correct |
944 ms |
17384 KB |
Output is correct |
93 |
Correct |
962 ms |
17456 KB |
Output is correct |
94 |
Correct |
923 ms |
17720 KB |
Output is correct |
95 |
Correct |
1177 ms |
17256 KB |
Output is correct |
96 |
Correct |
1109 ms |
15308 KB |
Output is correct |
97 |
Correct |
914 ms |
15308 KB |
Output is correct |
98 |
Correct |
551 ms |
21848 KB |
Output is correct |
99 |
Correct |
933 ms |
16732 KB |
Output is correct |
100 |
Correct |
925 ms |
15696 KB |
Output is correct |
101 |
Correct |
930 ms |
15188 KB |
Output is correct |
102 |
Correct |
894 ms |
13808 KB |
Output is correct |
103 |
Correct |
870 ms |
23120 KB |
Output is correct |
104 |
Correct |
893 ms |
24052 KB |
Output is correct |
105 |
Correct |
1158 ms |
24208 KB |
Output is correct |
106 |
Correct |
878 ms |
16472 KB |
Output is correct |
107 |
Correct |
902 ms |
14344 KB |
Output is correct |
108 |
Correct |
942 ms |
14444 KB |
Output is correct |
109 |
Correct |
927 ms |
14216 KB |
Output is correct |