#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=(1e9)+2022;
const int maxn=(2e5)+10;
vector<int> son[maxn];
int prod[maxn],n,m;
void dfs1(int u) {
prod[u]=1;
if ((int)son[u].size()==0) return;
prod[u]=(int)son[u].size();
for (int &v : son[u]) dfs1(v),prod[u]=(ll)prod[u]*prod[v]%mod;
}
int sum[maxn];
int pre[maxn],suf[maxn];
void dfs2(int u) {
int sz=(int)son[u].size();
for (int i=0;i<sz;i++) pre[i]=(ll)(i==0?1:pre[i-1])*prod[son[u][i]]%mod;
for (int i=sz-1;i>=0;i--) suf[i]=(ll)(i==sz-1?1:suf[i+1])*prod[son[u][i]]%mod;
for (int i=0;i<sz;i++) {
int v=son[u][i];
sum[v]=sum[u];
if (i) sum[v]=(ll)sum[v]*pre[i-1]%mod;
if (i<sz-1) sum[v]=(ll)sum[v]*suf[i+1]%mod;
}
for (int &v : son[u]) {
dfs2(v);
}
}
int flip[maxn*4];
int tr[maxn*4][2],col[maxn];
void pushup(int root) {
tr[root][0]=(tr[root<<1][0]+tr[root<<1|1][0])%mod;
tr[root][1]=(tr[root<<1][1]+tr[root<<1|1][1])%mod;
}
void build(int l,int r,int root) {
if (l==r) {
tr[root][0]=(col[l]?sum[l+n]:0);
tr[root][1]=(col[l]?0:sum[l+n]);
return;
}
int mid=(l+r)>>1;
build(l,mid,root<<1),build(mid+1,r,root<<1|1);
pushup(root);
}
void init(int _n, int _m, vector<int> P, vector<int> A) {
n=_n,m=_m;
for (int i=1;i<n+m;i++) son[P[i]].push_back(i);
dfs1(0);
sum[0]=1;
dfs2(0);
for (int i=0;i<m;i++) col[i]=A[i];
build(0,m-1,1);
}
void puttag(int root) {
flip[root]^=1,swap(tr[root][0],tr[root][1]);
}
void pushdown(int root) {
if (flip[root]) puttag(root<<1),puttag(root<<1|1),flip[root]=0;
}
void add(int L,int R,int l,int r,int root) {
if (L<=l&&r<=R) { puttag(root); return; }
int mid=(l+r)>>1; pushdown(root);
if (L<=mid) add(L,R,l,mid,root<<1);
if (mid<R) add(L,R,mid+1,r,root<<1|1);
pushup(root);
}
int count_ways(int l, int r) {
l-=n,r-=n;
add(l,r,0,m-1,1);
return tr[1][0];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 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 |
5104 KB |
Output is correct |
7 |
Correct |
3 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 |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
4 ms |
5088 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 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 |
5184 KB |
Output is correct |
11 |
Correct |
3 ms |
5064 KB |
Output is correct |
12 |
Correct |
3 ms |
5124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 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 |
5104 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 |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
5072 KB |
Output is correct |
12 |
Correct |
4 ms |
5088 KB |
Output is correct |
13 |
Correct |
3 ms |
5072 KB |
Output is correct |
14 |
Correct |
3 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 |
5184 KB |
Output is correct |
19 |
Correct |
3 ms |
5064 KB |
Output is correct |
20 |
Correct |
3 ms |
5124 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
4 ms |
5108 KB |
Output is correct |
24 |
Correct |
3 ms |
5072 KB |
Output is correct |
25 |
Correct |
5 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 |
4 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5072 KB |
Output is correct |
32 |
Correct |
3 ms |
5144 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 |
5072 KB |
Output is correct |
36 |
Correct |
3 ms |
5072 KB |
Output is correct |
37 |
Correct |
4 ms |
5072 KB |
Output is correct |
38 |
Correct |
3 ms |
5200 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
656 ms |
7932 KB |
Output is correct |
2 |
Correct |
787 ms |
10896 KB |
Output is correct |
3 |
Correct |
976 ms |
10880 KB |
Output is correct |
4 |
Correct |
788 ms |
10824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
656 ms |
7932 KB |
Output is correct |
2 |
Correct |
787 ms |
10896 KB |
Output is correct |
3 |
Correct |
976 ms |
10880 KB |
Output is correct |
4 |
Correct |
788 ms |
10824 KB |
Output is correct |
5 |
Correct |
767 ms |
7888 KB |
Output is correct |
6 |
Correct |
870 ms |
10876 KB |
Output is correct |
7 |
Correct |
874 ms |
10880 KB |
Output is correct |
8 |
Correct |
1020 ms |
10876 KB |
Output is correct |
9 |
Correct |
437 ms |
5200 KB |
Output is correct |
10 |
Correct |
853 ms |
5328 KB |
Output is correct |
11 |
Correct |
891 ms |
5328 KB |
Output is correct |
12 |
Correct |
875 ms |
5328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
4 ms |
5088 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 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 |
5184 KB |
Output is correct |
11 |
Correct |
3 ms |
5064 KB |
Output is correct |
12 |
Correct |
3 ms |
5124 KB |
Output is correct |
13 |
Correct |
656 ms |
7932 KB |
Output is correct |
14 |
Correct |
787 ms |
10896 KB |
Output is correct |
15 |
Correct |
976 ms |
10880 KB |
Output is correct |
16 |
Correct |
788 ms |
10824 KB |
Output is correct |
17 |
Correct |
767 ms |
7888 KB |
Output is correct |
18 |
Correct |
870 ms |
10876 KB |
Output is correct |
19 |
Correct |
874 ms |
10880 KB |
Output is correct |
20 |
Correct |
1020 ms |
10876 KB |
Output is correct |
21 |
Correct |
437 ms |
5200 KB |
Output is correct |
22 |
Correct |
853 ms |
5328 KB |
Output is correct |
23 |
Correct |
891 ms |
5328 KB |
Output is correct |
24 |
Correct |
875 ms |
5328 KB |
Output is correct |
25 |
Correct |
862 ms |
14332 KB |
Output is correct |
26 |
Correct |
941 ms |
14460 KB |
Output is correct |
27 |
Correct |
944 ms |
14488 KB |
Output is correct |
28 |
Correct |
806 ms |
14408 KB |
Output is correct |
29 |
Correct |
1004 ms |
19100 KB |
Output is correct |
30 |
Correct |
966 ms |
19144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 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 |
5104 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 |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
5072 KB |
Output is correct |
12 |
Correct |
4 ms |
5088 KB |
Output is correct |
13 |
Correct |
3 ms |
5072 KB |
Output is correct |
14 |
Correct |
3 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 |
5184 KB |
Output is correct |
19 |
Correct |
3 ms |
5064 KB |
Output is correct |
20 |
Correct |
3 ms |
5124 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
4 ms |
5108 KB |
Output is correct |
24 |
Correct |
3 ms |
5072 KB |
Output is correct |
25 |
Correct |
5 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 |
4 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5072 KB |
Output is correct |
32 |
Correct |
3 ms |
5144 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 |
5072 KB |
Output is correct |
36 |
Correct |
3 ms |
5072 KB |
Output is correct |
37 |
Correct |
4 ms |
5072 KB |
Output is correct |
38 |
Correct |
3 ms |
5200 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
3 ms |
5072 KB |
Output is correct |
43 |
Correct |
682 ms |
5328 KB |
Output is correct |
44 |
Correct |
744 ms |
5376 KB |
Output is correct |
45 |
Correct |
852 ms |
5328 KB |
Output is correct |
46 |
Correct |
886 ms |
5572 KB |
Output is correct |
47 |
Correct |
804 ms |
5584 KB |
Output is correct |
48 |
Correct |
814 ms |
5584 KB |
Output is correct |
49 |
Correct |
849 ms |
5584 KB |
Output is correct |
50 |
Correct |
620 ms |
5568 KB |
Output is correct |
51 |
Correct |
857 ms |
5380 KB |
Output is correct |
52 |
Correct |
839 ms |
5380 KB |
Output is correct |
53 |
Correct |
859 ms |
5456 KB |
Output is correct |
54 |
Correct |
802 ms |
5584 KB |
Output is correct |
55 |
Correct |
933 ms |
5456 KB |
Output is correct |
56 |
Correct |
900 ms |
5464 KB |
Output is correct |
57 |
Correct |
871 ms |
5356 KB |
Output is correct |
58 |
Correct |
817 ms |
5780 KB |
Output is correct |
59 |
Correct |
537 ms |
5912 KB |
Output is correct |
60 |
Correct |
615 ms |
5868 KB |
Output is correct |
61 |
Correct |
979 ms |
5328 KB |
Output is correct |
62 |
Correct |
662 ms |
5256 KB |
Output is correct |
63 |
Correct |
758 ms |
5200 KB |
Output is correct |
64 |
Correct |
753 ms |
5328 KB |
Output is correct |
65 |
Correct |
456 ms |
5200 KB |
Output is correct |
66 |
Correct |
784 ms |
5328 KB |
Output is correct |
67 |
Correct |
826 ms |
5420 KB |
Output is correct |
68 |
Correct |
757 ms |
5328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 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 |
5104 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 |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
5072 KB |
Output is correct |
12 |
Correct |
4 ms |
5088 KB |
Output is correct |
13 |
Correct |
3 ms |
5072 KB |
Output is correct |
14 |
Correct |
3 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 |
5184 KB |
Output is correct |
19 |
Correct |
3 ms |
5064 KB |
Output is correct |
20 |
Correct |
3 ms |
5124 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
4 ms |
5108 KB |
Output is correct |
24 |
Correct |
3 ms |
5072 KB |
Output is correct |
25 |
Correct |
5 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 |
4 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5072 KB |
Output is correct |
32 |
Correct |
3 ms |
5144 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 |
5072 KB |
Output is correct |
36 |
Correct |
3 ms |
5072 KB |
Output is correct |
37 |
Correct |
4 ms |
5072 KB |
Output is correct |
38 |
Correct |
3 ms |
5200 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
3 ms |
5072 KB |
Output is correct |
43 |
Correct |
656 ms |
7932 KB |
Output is correct |
44 |
Correct |
787 ms |
10896 KB |
Output is correct |
45 |
Correct |
976 ms |
10880 KB |
Output is correct |
46 |
Correct |
788 ms |
10824 KB |
Output is correct |
47 |
Correct |
767 ms |
7888 KB |
Output is correct |
48 |
Correct |
870 ms |
10876 KB |
Output is correct |
49 |
Correct |
874 ms |
10880 KB |
Output is correct |
50 |
Correct |
1020 ms |
10876 KB |
Output is correct |
51 |
Correct |
437 ms |
5200 KB |
Output is correct |
52 |
Correct |
853 ms |
5328 KB |
Output is correct |
53 |
Correct |
891 ms |
5328 KB |
Output is correct |
54 |
Correct |
875 ms |
5328 KB |
Output is correct |
55 |
Correct |
862 ms |
14332 KB |
Output is correct |
56 |
Correct |
941 ms |
14460 KB |
Output is correct |
57 |
Correct |
944 ms |
14488 KB |
Output is correct |
58 |
Correct |
806 ms |
14408 KB |
Output is correct |
59 |
Correct |
1004 ms |
19100 KB |
Output is correct |
60 |
Correct |
966 ms |
19144 KB |
Output is correct |
61 |
Correct |
682 ms |
5328 KB |
Output is correct |
62 |
Correct |
744 ms |
5376 KB |
Output is correct |
63 |
Correct |
852 ms |
5328 KB |
Output is correct |
64 |
Correct |
886 ms |
5572 KB |
Output is correct |
65 |
Correct |
804 ms |
5584 KB |
Output is correct |
66 |
Correct |
814 ms |
5584 KB |
Output is correct |
67 |
Correct |
849 ms |
5584 KB |
Output is correct |
68 |
Correct |
620 ms |
5568 KB |
Output is correct |
69 |
Correct |
857 ms |
5380 KB |
Output is correct |
70 |
Correct |
839 ms |
5380 KB |
Output is correct |
71 |
Correct |
859 ms |
5456 KB |
Output is correct |
72 |
Correct |
802 ms |
5584 KB |
Output is correct |
73 |
Correct |
933 ms |
5456 KB |
Output is correct |
74 |
Correct |
900 ms |
5464 KB |
Output is correct |
75 |
Correct |
871 ms |
5356 KB |
Output is correct |
76 |
Correct |
817 ms |
5780 KB |
Output is correct |
77 |
Correct |
537 ms |
5912 KB |
Output is correct |
78 |
Correct |
615 ms |
5868 KB |
Output is correct |
79 |
Correct |
979 ms |
5328 KB |
Output is correct |
80 |
Correct |
662 ms |
5256 KB |
Output is correct |
81 |
Correct |
758 ms |
5200 KB |
Output is correct |
82 |
Correct |
753 ms |
5328 KB |
Output is correct |
83 |
Correct |
456 ms |
5200 KB |
Output is correct |
84 |
Correct |
784 ms |
5328 KB |
Output is correct |
85 |
Correct |
826 ms |
5420 KB |
Output is correct |
86 |
Correct |
757 ms |
5328 KB |
Output is correct |
87 |
Correct |
3 ms |
4944 KB |
Output is correct |
88 |
Correct |
641 ms |
13820 KB |
Output is correct |
89 |
Correct |
914 ms |
10996 KB |
Output is correct |
90 |
Correct |
967 ms |
10752 KB |
Output is correct |
91 |
Correct |
709 ms |
14536 KB |
Output is correct |
92 |
Correct |
797 ms |
14488 KB |
Output is correct |
93 |
Correct |
1006 ms |
14588 KB |
Output is correct |
94 |
Correct |
996 ms |
14592 KB |
Output is correct |
95 |
Correct |
790 ms |
14492 KB |
Output is correct |
96 |
Correct |
841 ms |
11120 KB |
Output is correct |
97 |
Correct |
667 ms |
11380 KB |
Output is correct |
98 |
Correct |
653 ms |
14416 KB |
Output is correct |
99 |
Correct |
928 ms |
14520 KB |
Output is correct |
100 |
Correct |
926 ms |
12228 KB |
Output is correct |
101 |
Correct |
901 ms |
11652 KB |
Output is correct |
102 |
Correct |
976 ms |
10564 KB |
Output is correct |
103 |
Correct |
913 ms |
19200 KB |
Output is correct |
104 |
Correct |
999 ms |
20428 KB |
Output is correct |
105 |
Correct |
887 ms |
20512 KB |
Output is correct |
106 |
Correct |
864 ms |
11780 KB |
Output is correct |
107 |
Correct |
916 ms |
11344 KB |
Output is correct |
108 |
Correct |
908 ms |
11036 KB |
Output is correct |
109 |
Correct |
823 ms |
10648 KB |
Output is correct |