#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1'000'002'022;
int n, m;
struct segTree{
ll sum[800002], rev[800002], lazy[800002];
void init(int i, int l, int r, ll *A, vector<int> &B){
lazy[i] = 0;
if(l==r){
sum[i] = A[l], rev[i] = 0;
if(!B[l-n]) swap(sum[i], rev[i]);
return;
}
int m = (l+r)>>1;
init(i*2, l, m, A, B);
init(i*2+1, m+1, r, A, B);
sum[i] = (sum[i*2] + sum[i*2+1]) % MOD;
rev[i] = (rev[i*2] + rev[i*2+1]) % MOD;
}
void propagate(int i, int l, int r){
if(!lazy[i]) return;
swap(sum[i], rev[i]);
if(l!=r) lazy[i*2] = !lazy[i*2], lazy[i*2+1] = !lazy[i*2+1];
lazy[i] = 0;
}
ll query(int i, int l, int r, int s, int e){
propagate(i, l, r);
if(r<s || e<l) return 0;
if(s<=l && r<=e) return sum[i];
int m = (l+r)>>1;
return (query(i*2, l, m, s, e) + query(i*2+1, m+1, r, s, e)) % MOD;
}
void update(int i, int l, int r, int s, int e){
propagate(i, l, r);
if(r<s || e<l) return;
if(s<=l && r<=e){
lazy[i] = 1;
propagate(i, l, r);
return;
}
int m = (l+r)>>1;
update(i*2, l, m, s, e), update(i*2+1, m+1, r, s, e);
sum[i] = (sum[i*2] + sum[i*2+1]) % MOD;
rev[i] = (rev[i*2] + rev[i*2+1]) % MOD;
}
} tree;
int par[200002];
vector<int> link[200002];
ll S[200002], W[200002];
void dfs(int x){
S[x] = max(1, (int)link[x].size());
for(auto y: link[x]){
dfs(y);
S[x] = (S[x] * S[y]) % MOD;
}
}
void dfs2(int x, ll up = 1){
int k = (int)link[x].size();
vector<ll> prf(k+1), suf(k+1);
prf[0] = suf[k] = 1;
for(int i=1; i<=k; i++) prf[i] = (prf[i-1] * S[link[x][i-1]]) % MOD;
for(int i=k-1; i>=0; i--) suf[i] = (suf[i+1] * S[link[x][i]]) % MOD;
W[x] = up;
for(int i=0; i<k; i++){
dfs2(link[x][i], up * prf[i] % MOD * suf[i+1] % MOD);
}
}
void init(int _n, int _m, vector<int> P, vector<int> A){
n = _n, m = _m;
for(int i=0; i<n+m; i++) par[i] = P[i], link[par[i]].push_back(i);
dfs(0);
dfs2(0);
tree.init(1, n, n+m-1, W, A);
}
int count_ways(int L, int R) {
tree.update(1, n, n+m-1, L, R);
return tree.query(1, n, n+m-1, n, n+m-1);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
4 ms |
4944 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 |
4 ms |
5072 KB |
Output is correct |
8 |
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 |
4 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 |
4 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 |
5328 KB |
Output is correct |
11 |
Correct |
4 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 |
4 ms |
4944 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 |
4 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
4 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 |
4 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 |
5328 KB |
Output is correct |
19 |
Correct |
4 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 |
4 ms |
5108 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 |
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 |
5072 KB |
Output is correct |
34 |
Correct |
4 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5072 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 |
5328 KB |
Output is correct |
39 |
Correct |
4 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
4 ms |
5072 KB |
Output is correct |
42 |
Correct |
4 ms |
5072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
678 ms |
9604 KB |
Output is correct |
2 |
Correct |
810 ms |
14204 KB |
Output is correct |
3 |
Correct |
861 ms |
14232 KB |
Output is correct |
4 |
Correct |
1026 ms |
14228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
678 ms |
9604 KB |
Output is correct |
2 |
Correct |
810 ms |
14204 KB |
Output is correct |
3 |
Correct |
861 ms |
14232 KB |
Output is correct |
4 |
Correct |
1026 ms |
14228 KB |
Output is correct |
5 |
Correct |
840 ms |
9552 KB |
Output is correct |
6 |
Correct |
997 ms |
14152 KB |
Output is correct |
7 |
Correct |
818 ms |
14220 KB |
Output is correct |
8 |
Correct |
925 ms |
14228 KB |
Output is correct |
9 |
Correct |
275 ms |
5256 KB |
Output is correct |
10 |
Correct |
800 ms |
5508 KB |
Output is correct |
11 |
Correct |
708 ms |
5516 KB |
Output is correct |
12 |
Correct |
1035 ms |
5552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
4 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 |
4 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 |
5328 KB |
Output is correct |
11 |
Correct |
4 ms |
5328 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
678 ms |
9604 KB |
Output is correct |
14 |
Correct |
810 ms |
14204 KB |
Output is correct |
15 |
Correct |
861 ms |
14232 KB |
Output is correct |
16 |
Correct |
1026 ms |
14228 KB |
Output is correct |
17 |
Correct |
840 ms |
9552 KB |
Output is correct |
18 |
Correct |
997 ms |
14152 KB |
Output is correct |
19 |
Correct |
818 ms |
14220 KB |
Output is correct |
20 |
Correct |
925 ms |
14228 KB |
Output is correct |
21 |
Correct |
275 ms |
5256 KB |
Output is correct |
22 |
Correct |
800 ms |
5508 KB |
Output is correct |
23 |
Correct |
708 ms |
5516 KB |
Output is correct |
24 |
Correct |
1035 ms |
5552 KB |
Output is correct |
25 |
Correct |
1075 ms |
20352 KB |
Output is correct |
26 |
Correct |
918 ms |
20544 KB |
Output is correct |
27 |
Correct |
1200 ms |
20676 KB |
Output is correct |
28 |
Correct |
631 ms |
20600 KB |
Output is correct |
29 |
Correct |
1072 ms |
40820 KB |
Output is correct |
30 |
Correct |
663 ms |
40864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
4 ms |
4944 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 |
4 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
4 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 |
4 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 |
5328 KB |
Output is correct |
19 |
Correct |
4 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 |
4 ms |
5108 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 |
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 |
5072 KB |
Output is correct |
34 |
Correct |
4 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5072 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 |
5328 KB |
Output is correct |
39 |
Correct |
4 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
4 ms |
5072 KB |
Output is correct |
42 |
Correct |
4 ms |
5072 KB |
Output is correct |
43 |
Correct |
509 ms |
5456 KB |
Output is correct |
44 |
Correct |
868 ms |
5508 KB |
Output is correct |
45 |
Correct |
715 ms |
5456 KB |
Output is correct |
46 |
Correct |
642 ms |
5840 KB |
Output is correct |
47 |
Correct |
959 ms |
5900 KB |
Output is correct |
48 |
Correct |
888 ms |
5900 KB |
Output is correct |
49 |
Correct |
915 ms |
5892 KB |
Output is correct |
50 |
Correct |
784 ms |
5900 KB |
Output is correct |
51 |
Correct |
913 ms |
5712 KB |
Output is correct |
52 |
Correct |
891 ms |
5708 KB |
Output is correct |
53 |
Correct |
923 ms |
6280 KB |
Output is correct |
54 |
Correct |
973 ms |
5764 KB |
Output is correct |
55 |
Correct |
942 ms |
5636 KB |
Output is correct |
56 |
Correct |
768 ms |
5640 KB |
Output is correct |
57 |
Correct |
1022 ms |
5508 KB |
Output is correct |
58 |
Correct |
676 ms |
6884 KB |
Output is correct |
59 |
Correct |
1016 ms |
6984 KB |
Output is correct |
60 |
Correct |
825 ms |
6992 KB |
Output is correct |
61 |
Correct |
949 ms |
5704 KB |
Output is correct |
62 |
Correct |
674 ms |
5456 KB |
Output is correct |
63 |
Correct |
862 ms |
5328 KB |
Output is correct |
64 |
Correct |
858 ms |
5644 KB |
Output is correct |
65 |
Correct |
421 ms |
5200 KB |
Output is correct |
66 |
Correct |
971 ms |
5576 KB |
Output is correct |
67 |
Correct |
860 ms |
5584 KB |
Output is correct |
68 |
Correct |
825 ms |
5512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
4 ms |
4944 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 |
4 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
4 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 |
4 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 |
5328 KB |
Output is correct |
19 |
Correct |
4 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 |
4 ms |
5108 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 |
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 |
5072 KB |
Output is correct |
34 |
Correct |
4 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5072 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 |
5328 KB |
Output is correct |
39 |
Correct |
4 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
4 ms |
5072 KB |
Output is correct |
42 |
Correct |
4 ms |
5072 KB |
Output is correct |
43 |
Correct |
678 ms |
9604 KB |
Output is correct |
44 |
Correct |
810 ms |
14204 KB |
Output is correct |
45 |
Correct |
861 ms |
14232 KB |
Output is correct |
46 |
Correct |
1026 ms |
14228 KB |
Output is correct |
47 |
Correct |
840 ms |
9552 KB |
Output is correct |
48 |
Correct |
997 ms |
14152 KB |
Output is correct |
49 |
Correct |
818 ms |
14220 KB |
Output is correct |
50 |
Correct |
925 ms |
14228 KB |
Output is correct |
51 |
Correct |
275 ms |
5256 KB |
Output is correct |
52 |
Correct |
800 ms |
5508 KB |
Output is correct |
53 |
Correct |
708 ms |
5516 KB |
Output is correct |
54 |
Correct |
1035 ms |
5552 KB |
Output is correct |
55 |
Correct |
1075 ms |
20352 KB |
Output is correct |
56 |
Correct |
918 ms |
20544 KB |
Output is correct |
57 |
Correct |
1200 ms |
20676 KB |
Output is correct |
58 |
Correct |
631 ms |
20600 KB |
Output is correct |
59 |
Correct |
1072 ms |
40820 KB |
Output is correct |
60 |
Correct |
663 ms |
40864 KB |
Output is correct |
61 |
Correct |
509 ms |
5456 KB |
Output is correct |
62 |
Correct |
868 ms |
5508 KB |
Output is correct |
63 |
Correct |
715 ms |
5456 KB |
Output is correct |
64 |
Correct |
642 ms |
5840 KB |
Output is correct |
65 |
Correct |
959 ms |
5900 KB |
Output is correct |
66 |
Correct |
888 ms |
5900 KB |
Output is correct |
67 |
Correct |
915 ms |
5892 KB |
Output is correct |
68 |
Correct |
784 ms |
5900 KB |
Output is correct |
69 |
Correct |
913 ms |
5712 KB |
Output is correct |
70 |
Correct |
891 ms |
5708 KB |
Output is correct |
71 |
Correct |
923 ms |
6280 KB |
Output is correct |
72 |
Correct |
973 ms |
5764 KB |
Output is correct |
73 |
Correct |
942 ms |
5636 KB |
Output is correct |
74 |
Correct |
768 ms |
5640 KB |
Output is correct |
75 |
Correct |
1022 ms |
5508 KB |
Output is correct |
76 |
Correct |
676 ms |
6884 KB |
Output is correct |
77 |
Correct |
1016 ms |
6984 KB |
Output is correct |
78 |
Correct |
825 ms |
6992 KB |
Output is correct |
79 |
Correct |
949 ms |
5704 KB |
Output is correct |
80 |
Correct |
674 ms |
5456 KB |
Output is correct |
81 |
Correct |
862 ms |
5328 KB |
Output is correct |
82 |
Correct |
858 ms |
5644 KB |
Output is correct |
83 |
Correct |
421 ms |
5200 KB |
Output is correct |
84 |
Correct |
971 ms |
5576 KB |
Output is correct |
85 |
Correct |
860 ms |
5584 KB |
Output is correct |
86 |
Correct |
825 ms |
5512 KB |
Output is correct |
87 |
Correct |
3 ms |
4944 KB |
Output is correct |
88 |
Correct |
572 ms |
19588 KB |
Output is correct |
89 |
Correct |
986 ms |
14408 KB |
Output is correct |
90 |
Correct |
999 ms |
14084 KB |
Output is correct |
91 |
Correct |
977 ms |
20632 KB |
Output is correct |
92 |
Correct |
1059 ms |
20596 KB |
Output is correct |
93 |
Correct |
890 ms |
20596 KB |
Output is correct |
94 |
Correct |
967 ms |
20680 KB |
Output is correct |
95 |
Correct |
706 ms |
20680 KB |
Output is correct |
96 |
Correct |
770 ms |
15136 KB |
Output is correct |
97 |
Correct |
948 ms |
15172 KB |
Output is correct |
98 |
Correct |
742 ms |
31176 KB |
Output is correct |
99 |
Correct |
959 ms |
20484 KB |
Output is correct |
100 |
Correct |
866 ms |
17528 KB |
Output is correct |
101 |
Correct |
884 ms |
16636 KB |
Output is correct |
102 |
Correct |
849 ms |
15304 KB |
Output is correct |
103 |
Correct |
911 ms |
40868 KB |
Output is correct |
104 |
Correct |
845 ms |
41304 KB |
Output is correct |
105 |
Correct |
783 ms |
41296 KB |
Output is correct |
106 |
Correct |
879 ms |
18640 KB |
Output is correct |
107 |
Correct |
937 ms |
15944 KB |
Output is correct |
108 |
Correct |
915 ms |
15816 KB |
Output is correct |
109 |
Correct |
866 ms |
15368 KB |
Output is correct |