#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll MOD = 1000002022;
const int MAXN = 2e5+5;
vector<int>adj[MAXN];
ll c[MAXN],sub[MAXN];
int n,m;
void dfs(int v){
if(adj[v].empty())sub[v] = 1;
else sub[v] = adj[v].size();
for(int x:adj[v]){
dfs(x);
sub[v] = sub[v]*sub[x]%MOD;
}
}
void dfs(int v,ll cur){
if(adj[v].empty()){
c[v-n] = cur;
return;
}
int s = adj[v].size();
vector<ll>pref(s,1),suff(s,1);
for(int i=0;i<s;i++){
pref[i] = sub[adj[v][i]];
suff[i] = sub[adj[v][i]];
if(i)pref[i] = pref[i]*pref[i-1]%MOD;
}
for(int i=s-2;i>=0;i--)suff[i] = suff[i]*suff[i+1]%MOD;
for(int i=0;i<s;i++){
ll k = 1;
if(i)k = pref[i-1]*k%MOD;
if(i!=s-1)k = suff[i+1]*k%MOD;
dfs(adj[v][i],(cur*k)%MOD);
}
}
ll ans = 0;
vector<int>a;
ll t[4*MAXN],sum[4*MAXN];
bool lazy[4*MAXN];
void pushdown(int v){
if(lazy[v]){
t[2*v] = sum[2*v] - t[2*v] + MOD;
t[2*v+1] = sum[2*v+1] - t[2*v+1] + MOD;
if(t[2*v]>=MOD)t[2*v]-=MOD;
if(t[2*v+1]>=MOD)t[2*v+1]-=MOD;
lazy[2*v]^=1;
lazy[2*v+1]^=1;
lazy[v] = 0;
}
}
void upd(int v,int l,int r,int tl,int tr){
if(l>r)return;
if(l==tl && r==tr){
lazy[v]^=1;
t[v] = sum[v]-t[v]+MOD;
if(t[v]>=MOD)t[v]-=MOD;
return;
}
int tm = (tl+tr)/2;
pushdown(v);
upd(2*v,l,min(r,tm),tl,tm);
upd(2*v+1,max(tm+1,l),r,tm+1,tr);
t[v] = t[2*v] + t[2*v+1];
if(t[v]>=MOD)t[v]-=MOD;
}
void build(int v,int l,int r){
if(l==r){
sum[v] = c[l];
if(a[l])t[v] = c[l];
return;
}
int tm = (l+r)/2;
build(2*v,l,tm);
build(2*v+1,tm+1,r);
t[v] = t[2*v]+t[2*v+1];
sum[v] = sum[2*v]+sum[2*v+1];
if(sum[v]>=MOD)sum[v]-=MOD;
if(t[v]>=MOD)t[v]-=MOD;
}
void init(int N, int M, vector<int> p, vector<int> A) {
n = N;
m = M;
a = A;
for(int i=1;i<n+m;i++)adj[p[i]].push_back(i);
dfs(0);
dfs(0,1);
build(1,0,m-1);
}
int count_ways(int L, int R) {
L-=n;
R-=n;
upd(1,L,R,0,m-1);
return t[1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 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 |
5124 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 |
4 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 |
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 |
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 |
4 ms |
5328 KB |
Output is correct |
11 |
Correct |
3 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 |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5124 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 |
4 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 |
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 |
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 |
4 ms |
5328 KB |
Output is correct |
19 |
Correct |
3 ms |
5328 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
4 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 |
5072 KB |
Output is correct |
25 |
Correct |
3 ms |
5072 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
4 ms |
5072 KB |
Output is correct |
28 |
Correct |
4 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 |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 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 |
5328 KB |
Output is correct |
37 |
Correct |
3 ms |
5320 KB |
Output is correct |
38 |
Correct |
3 ms |
5328 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 |
522 ms |
8752 KB |
Output is correct |
2 |
Correct |
684 ms |
12360 KB |
Output is correct |
3 |
Correct |
872 ms |
12424 KB |
Output is correct |
4 |
Correct |
908 ms |
11928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
522 ms |
8752 KB |
Output is correct |
2 |
Correct |
684 ms |
12360 KB |
Output is correct |
3 |
Correct |
872 ms |
12424 KB |
Output is correct |
4 |
Correct |
908 ms |
11928 KB |
Output is correct |
5 |
Correct |
794 ms |
8700 KB |
Output is correct |
6 |
Correct |
991 ms |
12424 KB |
Output is correct |
7 |
Correct |
886 ms |
12428 KB |
Output is correct |
8 |
Correct |
737 ms |
12372 KB |
Output is correct |
9 |
Correct |
486 ms |
5268 KB |
Output is correct |
10 |
Correct |
875 ms |
5452 KB |
Output is correct |
11 |
Correct |
877 ms |
5396 KB |
Output is correct |
12 |
Correct |
846 ms |
5456 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 |
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 |
4 ms |
5328 KB |
Output is correct |
11 |
Correct |
3 ms |
5328 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
522 ms |
8752 KB |
Output is correct |
14 |
Correct |
684 ms |
12360 KB |
Output is correct |
15 |
Correct |
872 ms |
12424 KB |
Output is correct |
16 |
Correct |
908 ms |
11928 KB |
Output is correct |
17 |
Correct |
794 ms |
8700 KB |
Output is correct |
18 |
Correct |
991 ms |
12424 KB |
Output is correct |
19 |
Correct |
886 ms |
12428 KB |
Output is correct |
20 |
Correct |
737 ms |
12372 KB |
Output is correct |
21 |
Correct |
486 ms |
5268 KB |
Output is correct |
22 |
Correct |
875 ms |
5452 KB |
Output is correct |
23 |
Correct |
877 ms |
5396 KB |
Output is correct |
24 |
Correct |
846 ms |
5456 KB |
Output is correct |
25 |
Correct |
985 ms |
17172 KB |
Output is correct |
26 |
Correct |
1094 ms |
17228 KB |
Output is correct |
27 |
Correct |
939 ms |
17224 KB |
Output is correct |
28 |
Correct |
833 ms |
17308 KB |
Output is correct |
29 |
Correct |
861 ms |
37652 KB |
Output is correct |
30 |
Correct |
867 ms |
37624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 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 |
5124 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 |
4 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 |
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 |
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 |
4 ms |
5328 KB |
Output is correct |
19 |
Correct |
3 ms |
5328 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
4 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 |
5072 KB |
Output is correct |
25 |
Correct |
3 ms |
5072 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
4 ms |
5072 KB |
Output is correct |
28 |
Correct |
4 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 |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 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 |
5328 KB |
Output is correct |
37 |
Correct |
3 ms |
5320 KB |
Output is correct |
38 |
Correct |
3 ms |
5328 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 |
680 ms |
5328 KB |
Output is correct |
44 |
Correct |
956 ms |
5328 KB |
Output is correct |
45 |
Correct |
788 ms |
5328 KB |
Output is correct |
46 |
Correct |
805 ms |
5712 KB |
Output is correct |
47 |
Correct |
811 ms |
5712 KB |
Output is correct |
48 |
Correct |
910 ms |
5712 KB |
Output is correct |
49 |
Correct |
782 ms |
5712 KB |
Output is correct |
50 |
Correct |
866 ms |
5712 KB |
Output is correct |
51 |
Correct |
853 ms |
5704 KB |
Output is correct |
52 |
Correct |
814 ms |
5608 KB |
Output is correct |
53 |
Correct |
823 ms |
6224 KB |
Output is correct |
54 |
Correct |
953 ms |
5712 KB |
Output is correct |
55 |
Correct |
838 ms |
5584 KB |
Output is correct |
56 |
Correct |
930 ms |
5456 KB |
Output is correct |
57 |
Correct |
831 ms |
5456 KB |
Output is correct |
58 |
Correct |
946 ms |
6608 KB |
Output is correct |
59 |
Correct |
688 ms |
6736 KB |
Output is correct |
60 |
Correct |
761 ms |
6736 KB |
Output is correct |
61 |
Correct |
778 ms |
5664 KB |
Output is correct |
62 |
Correct |
907 ms |
5320 KB |
Output is correct |
63 |
Correct |
673 ms |
5328 KB |
Output is correct |
64 |
Correct |
838 ms |
5456 KB |
Output is correct |
65 |
Correct |
495 ms |
5200 KB |
Output is correct |
66 |
Correct |
853 ms |
5456 KB |
Output is correct |
67 |
Correct |
810 ms |
5456 KB |
Output is correct |
68 |
Correct |
687 ms |
5456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 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 |
5124 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 |
4 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 |
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 |
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 |
4 ms |
5328 KB |
Output is correct |
19 |
Correct |
3 ms |
5328 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
4 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 |
5072 KB |
Output is correct |
25 |
Correct |
3 ms |
5072 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
4 ms |
5072 KB |
Output is correct |
28 |
Correct |
4 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 |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 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 |
5328 KB |
Output is correct |
37 |
Correct |
3 ms |
5320 KB |
Output is correct |
38 |
Correct |
3 ms |
5328 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 |
522 ms |
8752 KB |
Output is correct |
44 |
Correct |
684 ms |
12360 KB |
Output is correct |
45 |
Correct |
872 ms |
12424 KB |
Output is correct |
46 |
Correct |
908 ms |
11928 KB |
Output is correct |
47 |
Correct |
794 ms |
8700 KB |
Output is correct |
48 |
Correct |
991 ms |
12424 KB |
Output is correct |
49 |
Correct |
886 ms |
12428 KB |
Output is correct |
50 |
Correct |
737 ms |
12372 KB |
Output is correct |
51 |
Correct |
486 ms |
5268 KB |
Output is correct |
52 |
Correct |
875 ms |
5452 KB |
Output is correct |
53 |
Correct |
877 ms |
5396 KB |
Output is correct |
54 |
Correct |
846 ms |
5456 KB |
Output is correct |
55 |
Correct |
985 ms |
17172 KB |
Output is correct |
56 |
Correct |
1094 ms |
17228 KB |
Output is correct |
57 |
Correct |
939 ms |
17224 KB |
Output is correct |
58 |
Correct |
833 ms |
17308 KB |
Output is correct |
59 |
Correct |
861 ms |
37652 KB |
Output is correct |
60 |
Correct |
867 ms |
37624 KB |
Output is correct |
61 |
Correct |
680 ms |
5328 KB |
Output is correct |
62 |
Correct |
956 ms |
5328 KB |
Output is correct |
63 |
Correct |
788 ms |
5328 KB |
Output is correct |
64 |
Correct |
805 ms |
5712 KB |
Output is correct |
65 |
Correct |
811 ms |
5712 KB |
Output is correct |
66 |
Correct |
910 ms |
5712 KB |
Output is correct |
67 |
Correct |
782 ms |
5712 KB |
Output is correct |
68 |
Correct |
866 ms |
5712 KB |
Output is correct |
69 |
Correct |
853 ms |
5704 KB |
Output is correct |
70 |
Correct |
814 ms |
5608 KB |
Output is correct |
71 |
Correct |
823 ms |
6224 KB |
Output is correct |
72 |
Correct |
953 ms |
5712 KB |
Output is correct |
73 |
Correct |
838 ms |
5584 KB |
Output is correct |
74 |
Correct |
930 ms |
5456 KB |
Output is correct |
75 |
Correct |
831 ms |
5456 KB |
Output is correct |
76 |
Correct |
946 ms |
6608 KB |
Output is correct |
77 |
Correct |
688 ms |
6736 KB |
Output is correct |
78 |
Correct |
761 ms |
6736 KB |
Output is correct |
79 |
Correct |
778 ms |
5664 KB |
Output is correct |
80 |
Correct |
907 ms |
5320 KB |
Output is correct |
81 |
Correct |
673 ms |
5328 KB |
Output is correct |
82 |
Correct |
838 ms |
5456 KB |
Output is correct |
83 |
Correct |
495 ms |
5200 KB |
Output is correct |
84 |
Correct |
853 ms |
5456 KB |
Output is correct |
85 |
Correct |
810 ms |
5456 KB |
Output is correct |
86 |
Correct |
687 ms |
5456 KB |
Output is correct |
87 |
Correct |
3 ms |
4944 KB |
Output is correct |
88 |
Correct |
607 ms |
16396 KB |
Output is correct |
89 |
Correct |
937 ms |
12428 KB |
Output is correct |
90 |
Correct |
913 ms |
12252 KB |
Output is correct |
91 |
Correct |
936 ms |
17436 KB |
Output is correct |
92 |
Correct |
1054 ms |
17352 KB |
Output is correct |
93 |
Correct |
814 ms |
16616 KB |
Output is correct |
94 |
Correct |
1122 ms |
17416 KB |
Output is correct |
95 |
Correct |
1056 ms |
17400 KB |
Output is correct |
96 |
Correct |
972 ms |
13072 KB |
Output is correct |
97 |
Correct |
900 ms |
13088 KB |
Output is correct |
98 |
Correct |
971 ms |
29976 KB |
Output is correct |
99 |
Correct |
902 ms |
17312 KB |
Output is correct |
100 |
Correct |
997 ms |
14920 KB |
Output is correct |
101 |
Correct |
909 ms |
14280 KB |
Output is correct |
102 |
Correct |
921 ms |
13068 KB |
Output is correct |
103 |
Correct |
944 ms |
37636 KB |
Output is correct |
104 |
Correct |
1031 ms |
38064 KB |
Output is correct |
105 |
Correct |
860 ms |
38020 KB |
Output is correct |
106 |
Correct |
1045 ms |
16564 KB |
Output is correct |
107 |
Correct |
913 ms |
13316 KB |
Output is correct |
108 |
Correct |
1016 ms |
13324 KB |
Output is correct |
109 |
Correct |
1048 ms |
13256 KB |
Output is correct |