#include "circuit.h"
typedef long long ll;
#include <bits/stdc++.h>
using namespace std;
const ll MOD=1e9+2022;
const int N=2e5+5;
struct tree
{
ll one, zero, lazy;
}t[4*N];
int nw[N], cnt, old[N], n, m;
ll suff[N], prv=1, a[N];
vector<int>g[N];
void dfs(int v)
{
if(v<n)
{
old[cnt]=v;
nw[v]=cnt++;
}
for(int u : g[v])
dfs(u);
}
void calc(int v)
{
if(v>=n)
{
a[v-n]=(1ll*prv*suff[cnt])%MOD;
return;
}
else cnt++;
for(int u : g[v])
calc(u);
if(v<n) prv=(1ll*prv*g[v].size())%MOD;
}
void build(int v, int l, int r, vector<int>&state)
{
if(l==r)
{
if(state[l]) t[v]={a[l], 0, 0};
else t[v]={0, a[l], 0};
return;
}
int mid=(l+r)>>1;
build(v<<1, l, mid, state);
build(v<<1|1, mid+1, r, state);
t[v]={(t[v<<1].one+t[v<<1|1].one)%MOD, (t[v<<1].zero+t[v<<1|1].zero)%MOD, 0};
}
void push(int v, int l, int r)
{
swap(t[v].zero, t[v].one);
t[v].lazy=0;
if(l!=r)
{
t[v<<1].lazy^=1;
t[v<<1|1].lazy^=1;
}
}
void update(int v, int l, int r, int tl, int tr)
{
if(t[v].lazy) push(v, l, r);
if(r<tl or tr<l) return;
if(tl<=l and r<=tr)
{
push(v, l, r);
return;
}
int mid=(l+r)>>1;
update(v<<1, l, mid, tl, tr);
update(v<<1|1, mid+1, r, tl, tr);
t[v]={(t[v<<1].one+t[v<<1|1].one)%MOD, (t[v<<1].zero+t[v<<1|1].zero)%MOD, 0};
}
void init(int N, int M, vector<int> p, vector<int> state)
{
n=N; m=M;
for(int i=1; i<n+m; i++)
g[p[i]].push_back(i);
dfs(0);
suff[n]=1;
for(int i=n-1; i>=0; i--)
{
suff[i]=1ll*suff[i+1]*g[old[i]].size();
suff[i]%=MOD;
}
cnt=0;
calc(0);
build(1, 0, m-1, state);
}
int count_ways(int l, int r)
{
update(1, 0, m-1, l-n, r-n);
return t[1].one;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
2 ms |
5072 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 |
2 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 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 |
2 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5148 KB |
Output is correct |
9 |
Correct |
3 ms |
5028 KB |
Output is correct |
10 |
Correct |
3 ms |
5200 KB |
Output is correct |
11 |
Correct |
3 ms |
5072 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
2 ms |
5072 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 |
2 ms |
5072 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
2 ms |
4944 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 |
2 ms |
5072 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5148 KB |
Output is correct |
17 |
Correct |
3 ms |
5028 KB |
Output is correct |
18 |
Correct |
3 ms |
5200 KB |
Output is correct |
19 |
Correct |
3 ms |
5072 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
2 ms |
5072 KB |
Output is correct |
23 |
Correct |
2 ms |
5072 KB |
Output is correct |
24 |
Correct |
4 ms |
5200 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 |
2 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 |
2 ms |
5072 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 KB |
Output is correct |
33 |
Correct |
2 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 |
5200 KB |
Output is correct |
37 |
Correct |
3 ms |
5200 KB |
Output is correct |
38 |
Correct |
3 ms |
5200 KB |
Output is correct |
39 |
Correct |
2 ms |
5072 KB |
Output is correct |
40 |
Correct |
2 ms |
5072 KB |
Output is correct |
41 |
Correct |
2 ms |
5072 KB |
Output is correct |
42 |
Correct |
2 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
489 ms |
9060 KB |
Output is correct |
2 |
Correct |
913 ms |
13212 KB |
Output is correct |
3 |
Correct |
736 ms |
13184 KB |
Output is correct |
4 |
Correct |
698 ms |
13128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
489 ms |
9060 KB |
Output is correct |
2 |
Correct |
913 ms |
13212 KB |
Output is correct |
3 |
Correct |
736 ms |
13184 KB |
Output is correct |
4 |
Correct |
698 ms |
13128 KB |
Output is correct |
5 |
Correct |
586 ms |
9032 KB |
Output is correct |
6 |
Correct |
864 ms |
13144 KB |
Output is correct |
7 |
Correct |
774 ms |
13212 KB |
Output is correct |
8 |
Correct |
649 ms |
13212 KB |
Output is correct |
9 |
Correct |
267 ms |
5200 KB |
Output is correct |
10 |
Correct |
746 ms |
5456 KB |
Output is correct |
11 |
Correct |
665 ms |
5456 KB |
Output is correct |
12 |
Correct |
697 ms |
5456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 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 |
2 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5148 KB |
Output is correct |
9 |
Correct |
3 ms |
5028 KB |
Output is correct |
10 |
Correct |
3 ms |
5200 KB |
Output is correct |
11 |
Correct |
3 ms |
5072 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
489 ms |
9060 KB |
Output is correct |
14 |
Correct |
913 ms |
13212 KB |
Output is correct |
15 |
Correct |
736 ms |
13184 KB |
Output is correct |
16 |
Correct |
698 ms |
13128 KB |
Output is correct |
17 |
Correct |
586 ms |
9032 KB |
Output is correct |
18 |
Correct |
864 ms |
13144 KB |
Output is correct |
19 |
Correct |
774 ms |
13212 KB |
Output is correct |
20 |
Correct |
649 ms |
13212 KB |
Output is correct |
21 |
Correct |
267 ms |
5200 KB |
Output is correct |
22 |
Correct |
746 ms |
5456 KB |
Output is correct |
23 |
Correct |
665 ms |
5456 KB |
Output is correct |
24 |
Correct |
697 ms |
5456 KB |
Output is correct |
25 |
Correct |
732 ms |
18820 KB |
Output is correct |
26 |
Correct |
772 ms |
18952 KB |
Output is correct |
27 |
Correct |
738 ms |
18996 KB |
Output is correct |
28 |
Correct |
619 ms |
18984 KB |
Output is correct |
29 |
Correct |
849 ms |
23688 KB |
Output is correct |
30 |
Correct |
645 ms |
23684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
2 ms |
5072 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 |
2 ms |
5072 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
2 ms |
4944 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 |
2 ms |
5072 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5148 KB |
Output is correct |
17 |
Correct |
3 ms |
5028 KB |
Output is correct |
18 |
Correct |
3 ms |
5200 KB |
Output is correct |
19 |
Correct |
3 ms |
5072 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
2 ms |
5072 KB |
Output is correct |
23 |
Correct |
2 ms |
5072 KB |
Output is correct |
24 |
Correct |
4 ms |
5200 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 |
2 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 |
2 ms |
5072 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 KB |
Output is correct |
33 |
Correct |
2 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 |
5200 KB |
Output is correct |
37 |
Correct |
3 ms |
5200 KB |
Output is correct |
38 |
Correct |
3 ms |
5200 KB |
Output is correct |
39 |
Correct |
2 ms |
5072 KB |
Output is correct |
40 |
Correct |
2 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 |
614 ms |
5328 KB |
Output is correct |
44 |
Correct |
697 ms |
5456 KB |
Output is correct |
45 |
Correct |
614 ms |
5456 KB |
Output is correct |
46 |
Correct |
518 ms |
5712 KB |
Output is correct |
47 |
Correct |
648 ms |
5700 KB |
Output is correct |
48 |
Correct |
583 ms |
5712 KB |
Output is correct |
49 |
Correct |
818 ms |
5712 KB |
Output is correct |
50 |
Correct |
503 ms |
5712 KB |
Output is correct |
51 |
Correct |
565 ms |
5584 KB |
Output is correct |
52 |
Correct |
779 ms |
5584 KB |
Output is correct |
53 |
Correct |
789 ms |
5456 KB |
Output is correct |
54 |
Correct |
622 ms |
5712 KB |
Output is correct |
55 |
Correct |
854 ms |
5584 KB |
Output is correct |
56 |
Correct |
574 ms |
5584 KB |
Output is correct |
57 |
Correct |
870 ms |
5456 KB |
Output is correct |
58 |
Correct |
857 ms |
5968 KB |
Output is correct |
59 |
Correct |
554 ms |
6076 KB |
Output is correct |
60 |
Correct |
614 ms |
6072 KB |
Output is correct |
61 |
Correct |
865 ms |
5456 KB |
Output is correct |
62 |
Correct |
843 ms |
5328 KB |
Output is correct |
63 |
Correct |
703 ms |
5328 KB |
Output is correct |
64 |
Correct |
549 ms |
5456 KB |
Output is correct |
65 |
Correct |
312 ms |
5200 KB |
Output is correct |
66 |
Correct |
777 ms |
5456 KB |
Output is correct |
67 |
Correct |
628 ms |
5456 KB |
Output is correct |
68 |
Correct |
659 ms |
5456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
2 ms |
5072 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 |
2 ms |
5072 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
2 ms |
4944 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 |
2 ms |
5072 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5148 KB |
Output is correct |
17 |
Correct |
3 ms |
5028 KB |
Output is correct |
18 |
Correct |
3 ms |
5200 KB |
Output is correct |
19 |
Correct |
3 ms |
5072 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
2 ms |
5072 KB |
Output is correct |
23 |
Correct |
2 ms |
5072 KB |
Output is correct |
24 |
Correct |
4 ms |
5200 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 |
2 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 |
2 ms |
5072 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 KB |
Output is correct |
33 |
Correct |
2 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 |
5200 KB |
Output is correct |
37 |
Correct |
3 ms |
5200 KB |
Output is correct |
38 |
Correct |
3 ms |
5200 KB |
Output is correct |
39 |
Correct |
2 ms |
5072 KB |
Output is correct |
40 |
Correct |
2 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 |
489 ms |
9060 KB |
Output is correct |
44 |
Correct |
913 ms |
13212 KB |
Output is correct |
45 |
Correct |
736 ms |
13184 KB |
Output is correct |
46 |
Correct |
698 ms |
13128 KB |
Output is correct |
47 |
Correct |
586 ms |
9032 KB |
Output is correct |
48 |
Correct |
864 ms |
13144 KB |
Output is correct |
49 |
Correct |
774 ms |
13212 KB |
Output is correct |
50 |
Correct |
649 ms |
13212 KB |
Output is correct |
51 |
Correct |
267 ms |
5200 KB |
Output is correct |
52 |
Correct |
746 ms |
5456 KB |
Output is correct |
53 |
Correct |
665 ms |
5456 KB |
Output is correct |
54 |
Correct |
697 ms |
5456 KB |
Output is correct |
55 |
Correct |
732 ms |
18820 KB |
Output is correct |
56 |
Correct |
772 ms |
18952 KB |
Output is correct |
57 |
Correct |
738 ms |
18996 KB |
Output is correct |
58 |
Correct |
619 ms |
18984 KB |
Output is correct |
59 |
Correct |
849 ms |
23688 KB |
Output is correct |
60 |
Correct |
645 ms |
23684 KB |
Output is correct |
61 |
Correct |
614 ms |
5328 KB |
Output is correct |
62 |
Correct |
697 ms |
5456 KB |
Output is correct |
63 |
Correct |
614 ms |
5456 KB |
Output is correct |
64 |
Correct |
518 ms |
5712 KB |
Output is correct |
65 |
Correct |
648 ms |
5700 KB |
Output is correct |
66 |
Correct |
583 ms |
5712 KB |
Output is correct |
67 |
Correct |
818 ms |
5712 KB |
Output is correct |
68 |
Correct |
503 ms |
5712 KB |
Output is correct |
69 |
Correct |
565 ms |
5584 KB |
Output is correct |
70 |
Correct |
779 ms |
5584 KB |
Output is correct |
71 |
Correct |
789 ms |
5456 KB |
Output is correct |
72 |
Correct |
622 ms |
5712 KB |
Output is correct |
73 |
Correct |
854 ms |
5584 KB |
Output is correct |
74 |
Correct |
574 ms |
5584 KB |
Output is correct |
75 |
Correct |
870 ms |
5456 KB |
Output is correct |
76 |
Correct |
857 ms |
5968 KB |
Output is correct |
77 |
Correct |
554 ms |
6076 KB |
Output is correct |
78 |
Correct |
614 ms |
6072 KB |
Output is correct |
79 |
Correct |
865 ms |
5456 KB |
Output is correct |
80 |
Correct |
843 ms |
5328 KB |
Output is correct |
81 |
Correct |
703 ms |
5328 KB |
Output is correct |
82 |
Correct |
549 ms |
5456 KB |
Output is correct |
83 |
Correct |
312 ms |
5200 KB |
Output is correct |
84 |
Correct |
777 ms |
5456 KB |
Output is correct |
85 |
Correct |
628 ms |
5456 KB |
Output is correct |
86 |
Correct |
659 ms |
5456 KB |
Output is correct |
87 |
Correct |
3 ms |
4944 KB |
Output is correct |
88 |
Correct |
532 ms |
18228 KB |
Output is correct |
89 |
Correct |
883 ms |
13512 KB |
Output is correct |
90 |
Correct |
849 ms |
13156 KB |
Output is correct |
91 |
Correct |
559 ms |
19016 KB |
Output is correct |
92 |
Correct |
818 ms |
19072 KB |
Output is correct |
93 |
Correct |
963 ms |
19232 KB |
Output is correct |
94 |
Correct |
696 ms |
19104 KB |
Output is correct |
95 |
Correct |
785 ms |
19024 KB |
Output is correct |
96 |
Correct |
782 ms |
13964 KB |
Output is correct |
97 |
Correct |
784 ms |
13944 KB |
Output is correct |
98 |
Correct |
609 ms |
15184 KB |
Output is correct |
99 |
Correct |
758 ms |
18988 KB |
Output is correct |
100 |
Correct |
744 ms |
16260 KB |
Output is correct |
101 |
Correct |
779 ms |
15384 KB |
Output is correct |
102 |
Correct |
895 ms |
14032 KB |
Output is correct |
103 |
Correct |
702 ms |
23644 KB |
Output is correct |
104 |
Correct |
874 ms |
24104 KB |
Output is correct |
105 |
Correct |
714 ms |
24224 KB |
Output is correct |
106 |
Correct |
774 ms |
14220 KB |
Output is correct |
107 |
Correct |
656 ms |
14152 KB |
Output is correct |
108 |
Correct |
731 ms |
14664 KB |
Output is correct |
109 |
Correct |
705 ms |
13980 KB |
Output is correct |