#include "circuit.h"
#include <bits/stdc++.h>
#define N 100020
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using namespace std;
typedef long long ll;
const ll mod = 1000002022;
ll yes[N], no[N], state[N],pai[N], n,m;
ll save[N], sum[N];
vector<ll> grafo[N];
ll pref[N], ans = 0;
void solve(){
for(ll x=n;x<n+m+1;x++)yes[x]=state[x],no[x]=1-state[x],save[x]=sum[x]=1;
pref[0]=1;
for(ll x = n-1; x >= 0; x--){
ll sum_cost = 1, C = sz(grafo[x]);
no[x]=yes[x]=0;
for(auto v: grafo[x]){
ll old_yes = yes[x];
yes[x] = (1LL*old_yes*sum[v])%mod + (1LL*sum_cost*yes[v])%mod;
yes[x] %= mod;
pref[v] = sum_cost;
sum_cost = (1LL*sum_cost*sum[v])%mod;
}
reverse(all(grafo[x]));
ll suf = 1;
for(auto v: grafo[x]){
pref[v] = (1LL*pref[v]*suf)%mod;
suf = (1LL*suf*sum[v])%mod;
}
if(save[x] == -1) save[x] = sum_cost;
assert(save[x] == sum_cost);
no[x] = (1LL*C*sum_cost%mod - yes[x])%mod;
no[x] = (no[x] + mod)%mod;
sum[x] = (yes[x] + no[x])%mod;
}
for(ll i = 0; i < n+m; i++){
if(i)pref[i] = (pref[i] * pref[pai[i]])%mod;
}
}
ll cnt[4*N][2],lazy[4*N],custo[N];
#define mid ((a+b)/2)
void build(int nod, int a, int b){
if(a==b){
cnt[nod][custo[a]] = pref[a+n];
return;
}
build(2*nod,a,mid); build(2*nod+1,mid+1,b);
cnt[nod][0] = (cnt[2*nod][0]+cnt[2*nod+1][0])%mod;
cnt[nod][1] = (cnt[2*nod][1]+cnt[2*nod+1][1])%mod;
}
void prop(int nod, int a, int b){
if(!lazy[nod]) return;
swap(cnt[nod][0],cnt[nod][1]);
lazy[nod]=0;
if(a != b){
lazy[2*nod] ^= 1;
lazy[2*nod+1] ^= 1;
}
}
void upd(int nod, int a, int b, int i, int j){
prop(nod,a,b);
if(j < a or i > b) return;
if(i <= a and j >= b){
lazy[nod] ^= 1;
prop(nod,a,b);
return;
}
upd(2*nod,a,mid,i,j); upd(2*nod+1,mid+1,b,i,j);
cnt[nod][0] = (cnt[2*nod][0] + cnt[2*nod+1][0])%mod;
cnt[nod][1] = (cnt[2*nod][1] + cnt[2*nod+1][1])%mod;
}
void init(int n_, int m_, vector<int> P, vector<int> A) {
memset(save,-1,sizeof save);
n = n_, m = m_;
for(int i = 1; i < n+m; i++){
grafo[P[i]].pb(i);
pai[i] = P[i];
}
for(int i = 0;i<m;i++) state[i+n] = A[i],custo[i]=A[i];
solve();
build(1,0,m-1);
}
int count_ways(int L, int R) {
upd(1,0,m-1,L-n,R-n);
prop(1,0,m-1);
return cnt[1][1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3408 KB |
Output is correct |
2 |
Correct |
2 ms |
3408 KB |
Output is correct |
3 |
Correct |
2 ms |
3536 KB |
Output is correct |
4 |
Correct |
2 ms |
3536 KB |
Output is correct |
5 |
Correct |
2 ms |
3536 KB |
Output is correct |
6 |
Correct |
2 ms |
3536 KB |
Output is correct |
7 |
Correct |
2 ms |
3536 KB |
Output is correct |
8 |
Correct |
4 ms |
3536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3408 KB |
Output is correct |
2 |
Correct |
2 ms |
3536 KB |
Output is correct |
3 |
Correct |
2 ms |
3536 KB |
Output is correct |
4 |
Correct |
2 ms |
3536 KB |
Output is correct |
5 |
Correct |
2 ms |
3536 KB |
Output is correct |
6 |
Correct |
3 ms |
3536 KB |
Output is correct |
7 |
Correct |
3 ms |
3664 KB |
Output is correct |
8 |
Correct |
3 ms |
3664 KB |
Output is correct |
9 |
Correct |
2 ms |
3664 KB |
Output is correct |
10 |
Correct |
2 ms |
3664 KB |
Output is correct |
11 |
Correct |
3 ms |
3664 KB |
Output is correct |
12 |
Correct |
2 ms |
3536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3408 KB |
Output is correct |
2 |
Correct |
2 ms |
3408 KB |
Output is correct |
3 |
Correct |
2 ms |
3536 KB |
Output is correct |
4 |
Correct |
2 ms |
3536 KB |
Output is correct |
5 |
Correct |
2 ms |
3536 KB |
Output is correct |
6 |
Correct |
2 ms |
3536 KB |
Output is correct |
7 |
Correct |
2 ms |
3536 KB |
Output is correct |
8 |
Correct |
4 ms |
3536 KB |
Output is correct |
9 |
Correct |
2 ms |
3408 KB |
Output is correct |
10 |
Correct |
2 ms |
3536 KB |
Output is correct |
11 |
Correct |
2 ms |
3536 KB |
Output is correct |
12 |
Correct |
2 ms |
3536 KB |
Output is correct |
13 |
Correct |
2 ms |
3536 KB |
Output is correct |
14 |
Correct |
3 ms |
3536 KB |
Output is correct |
15 |
Correct |
3 ms |
3664 KB |
Output is correct |
16 |
Correct |
3 ms |
3664 KB |
Output is correct |
17 |
Correct |
2 ms |
3664 KB |
Output is correct |
18 |
Correct |
2 ms |
3664 KB |
Output is correct |
19 |
Correct |
3 ms |
3664 KB |
Output is correct |
20 |
Correct |
2 ms |
3536 KB |
Output is correct |
21 |
Correct |
2 ms |
3664 KB |
Output is correct |
22 |
Correct |
2 ms |
3536 KB |
Output is correct |
23 |
Correct |
2 ms |
3536 KB |
Output is correct |
24 |
Correct |
3 ms |
3664 KB |
Output is correct |
25 |
Correct |
3 ms |
3664 KB |
Output is correct |
26 |
Correct |
2 ms |
3664 KB |
Output is correct |
27 |
Correct |
2 ms |
3664 KB |
Output is correct |
28 |
Correct |
2 ms |
3664 KB |
Output is correct |
29 |
Correct |
2 ms |
3536 KB |
Output is correct |
30 |
Correct |
2 ms |
3536 KB |
Output is correct |
31 |
Correct |
3 ms |
3536 KB |
Output is correct |
32 |
Correct |
2 ms |
3664 KB |
Output is correct |
33 |
Correct |
2 ms |
3536 KB |
Output is correct |
34 |
Correct |
2 ms |
3536 KB |
Output is correct |
35 |
Correct |
2 ms |
3536 KB |
Output is correct |
36 |
Correct |
2 ms |
3664 KB |
Output is correct |
37 |
Correct |
2 ms |
3664 KB |
Output is correct |
38 |
Correct |
2 ms |
3664 KB |
Output is correct |
39 |
Correct |
2 ms |
3536 KB |
Output is correct |
40 |
Correct |
2 ms |
3624 KB |
Output is correct |
41 |
Correct |
2 ms |
3536 KB |
Output is correct |
42 |
Correct |
3 ms |
3536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
612 ms |
9324 KB |
Output is correct |
2 |
Runtime error |
36 ms |
18260 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
612 ms |
9324 KB |
Output is correct |
2 |
Runtime error |
36 ms |
18260 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3408 KB |
Output is correct |
2 |
Correct |
2 ms |
3536 KB |
Output is correct |
3 |
Correct |
2 ms |
3536 KB |
Output is correct |
4 |
Correct |
2 ms |
3536 KB |
Output is correct |
5 |
Correct |
2 ms |
3536 KB |
Output is correct |
6 |
Correct |
3 ms |
3536 KB |
Output is correct |
7 |
Correct |
3 ms |
3664 KB |
Output is correct |
8 |
Correct |
3 ms |
3664 KB |
Output is correct |
9 |
Correct |
2 ms |
3664 KB |
Output is correct |
10 |
Correct |
2 ms |
3664 KB |
Output is correct |
11 |
Correct |
3 ms |
3664 KB |
Output is correct |
12 |
Correct |
2 ms |
3536 KB |
Output is correct |
13 |
Correct |
612 ms |
9324 KB |
Output is correct |
14 |
Runtime error |
36 ms |
18260 KB |
Execution killed with signal 11 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3408 KB |
Output is correct |
2 |
Correct |
2 ms |
3408 KB |
Output is correct |
3 |
Correct |
2 ms |
3536 KB |
Output is correct |
4 |
Correct |
2 ms |
3536 KB |
Output is correct |
5 |
Correct |
2 ms |
3536 KB |
Output is correct |
6 |
Correct |
2 ms |
3536 KB |
Output is correct |
7 |
Correct |
2 ms |
3536 KB |
Output is correct |
8 |
Correct |
4 ms |
3536 KB |
Output is correct |
9 |
Correct |
2 ms |
3408 KB |
Output is correct |
10 |
Correct |
2 ms |
3536 KB |
Output is correct |
11 |
Correct |
2 ms |
3536 KB |
Output is correct |
12 |
Correct |
2 ms |
3536 KB |
Output is correct |
13 |
Correct |
2 ms |
3536 KB |
Output is correct |
14 |
Correct |
3 ms |
3536 KB |
Output is correct |
15 |
Correct |
3 ms |
3664 KB |
Output is correct |
16 |
Correct |
3 ms |
3664 KB |
Output is correct |
17 |
Correct |
2 ms |
3664 KB |
Output is correct |
18 |
Correct |
2 ms |
3664 KB |
Output is correct |
19 |
Correct |
3 ms |
3664 KB |
Output is correct |
20 |
Correct |
2 ms |
3536 KB |
Output is correct |
21 |
Correct |
2 ms |
3664 KB |
Output is correct |
22 |
Correct |
2 ms |
3536 KB |
Output is correct |
23 |
Correct |
2 ms |
3536 KB |
Output is correct |
24 |
Correct |
3 ms |
3664 KB |
Output is correct |
25 |
Correct |
3 ms |
3664 KB |
Output is correct |
26 |
Correct |
2 ms |
3664 KB |
Output is correct |
27 |
Correct |
2 ms |
3664 KB |
Output is correct |
28 |
Correct |
2 ms |
3664 KB |
Output is correct |
29 |
Correct |
2 ms |
3536 KB |
Output is correct |
30 |
Correct |
2 ms |
3536 KB |
Output is correct |
31 |
Correct |
3 ms |
3536 KB |
Output is correct |
32 |
Correct |
2 ms |
3664 KB |
Output is correct |
33 |
Correct |
2 ms |
3536 KB |
Output is correct |
34 |
Correct |
2 ms |
3536 KB |
Output is correct |
35 |
Correct |
2 ms |
3536 KB |
Output is correct |
36 |
Correct |
2 ms |
3664 KB |
Output is correct |
37 |
Correct |
2 ms |
3664 KB |
Output is correct |
38 |
Correct |
2 ms |
3664 KB |
Output is correct |
39 |
Correct |
2 ms |
3536 KB |
Output is correct |
40 |
Correct |
2 ms |
3624 KB |
Output is correct |
41 |
Correct |
2 ms |
3536 KB |
Output is correct |
42 |
Correct |
3 ms |
3536 KB |
Output is correct |
43 |
Correct |
707 ms |
4072 KB |
Output is correct |
44 |
Correct |
841 ms |
4172 KB |
Output is correct |
45 |
Correct |
678 ms |
4048 KB |
Output is correct |
46 |
Correct |
1136 ms |
4560 KB |
Output is correct |
47 |
Correct |
841 ms |
4560 KB |
Output is correct |
48 |
Correct |
776 ms |
4560 KB |
Output is correct |
49 |
Correct |
837 ms |
4536 KB |
Output is correct |
50 |
Correct |
955 ms |
4432 KB |
Output is correct |
51 |
Correct |
749 ms |
4200 KB |
Output is correct |
52 |
Correct |
778 ms |
4308 KB |
Output is correct |
53 |
Correct |
834 ms |
3792 KB |
Output is correct |
54 |
Correct |
851 ms |
4560 KB |
Output is correct |
55 |
Correct |
873 ms |
4432 KB |
Output is correct |
56 |
Correct |
800 ms |
4304 KB |
Output is correct |
57 |
Correct |
890 ms |
4176 KB |
Output is correct |
58 |
Correct |
837 ms |
4560 KB |
Output is correct |
59 |
Correct |
567 ms |
4708 KB |
Output is correct |
60 |
Correct |
875 ms |
4592 KB |
Output is correct |
61 |
Correct |
679 ms |
4048 KB |
Output is correct |
62 |
Correct |
871 ms |
4068 KB |
Output is correct |
63 |
Correct |
830 ms |
4048 KB |
Output is correct |
64 |
Correct |
778 ms |
4176 KB |
Output is correct |
65 |
Correct |
369 ms |
3792 KB |
Output is correct |
66 |
Correct |
773 ms |
4176 KB |
Output is correct |
67 |
Correct |
861 ms |
4176 KB |
Output is correct |
68 |
Correct |
880 ms |
4176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3408 KB |
Output is correct |
2 |
Correct |
2 ms |
3408 KB |
Output is correct |
3 |
Correct |
2 ms |
3536 KB |
Output is correct |
4 |
Correct |
2 ms |
3536 KB |
Output is correct |
5 |
Correct |
2 ms |
3536 KB |
Output is correct |
6 |
Correct |
2 ms |
3536 KB |
Output is correct |
7 |
Correct |
2 ms |
3536 KB |
Output is correct |
8 |
Correct |
4 ms |
3536 KB |
Output is correct |
9 |
Correct |
2 ms |
3408 KB |
Output is correct |
10 |
Correct |
2 ms |
3536 KB |
Output is correct |
11 |
Correct |
2 ms |
3536 KB |
Output is correct |
12 |
Correct |
2 ms |
3536 KB |
Output is correct |
13 |
Correct |
2 ms |
3536 KB |
Output is correct |
14 |
Correct |
3 ms |
3536 KB |
Output is correct |
15 |
Correct |
3 ms |
3664 KB |
Output is correct |
16 |
Correct |
3 ms |
3664 KB |
Output is correct |
17 |
Correct |
2 ms |
3664 KB |
Output is correct |
18 |
Correct |
2 ms |
3664 KB |
Output is correct |
19 |
Correct |
3 ms |
3664 KB |
Output is correct |
20 |
Correct |
2 ms |
3536 KB |
Output is correct |
21 |
Correct |
2 ms |
3664 KB |
Output is correct |
22 |
Correct |
2 ms |
3536 KB |
Output is correct |
23 |
Correct |
2 ms |
3536 KB |
Output is correct |
24 |
Correct |
3 ms |
3664 KB |
Output is correct |
25 |
Correct |
3 ms |
3664 KB |
Output is correct |
26 |
Correct |
2 ms |
3664 KB |
Output is correct |
27 |
Correct |
2 ms |
3664 KB |
Output is correct |
28 |
Correct |
2 ms |
3664 KB |
Output is correct |
29 |
Correct |
2 ms |
3536 KB |
Output is correct |
30 |
Correct |
2 ms |
3536 KB |
Output is correct |
31 |
Correct |
3 ms |
3536 KB |
Output is correct |
32 |
Correct |
2 ms |
3664 KB |
Output is correct |
33 |
Correct |
2 ms |
3536 KB |
Output is correct |
34 |
Correct |
2 ms |
3536 KB |
Output is correct |
35 |
Correct |
2 ms |
3536 KB |
Output is correct |
36 |
Correct |
2 ms |
3664 KB |
Output is correct |
37 |
Correct |
2 ms |
3664 KB |
Output is correct |
38 |
Correct |
2 ms |
3664 KB |
Output is correct |
39 |
Correct |
2 ms |
3536 KB |
Output is correct |
40 |
Correct |
2 ms |
3624 KB |
Output is correct |
41 |
Correct |
2 ms |
3536 KB |
Output is correct |
42 |
Correct |
3 ms |
3536 KB |
Output is correct |
43 |
Correct |
612 ms |
9324 KB |
Output is correct |
44 |
Runtime error |
36 ms |
18260 KB |
Execution killed with signal 11 |
45 |
Halted |
0 ms |
0 KB |
- |