#include "circuit.h"
#include<bits/stdc++.h>
#include <vector>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define ll long long
const int N=1e5+500;
const ll mod=1000002022;
ll Puta(ll a,ll b){a%=mod,b%=mod;ll x=a*b;x%=mod;return x;}
ll Plus(ll a,ll b){a%=mod,b%=mod;ll x=a+b;x%=mod;return x;}
vector<int>E[2*N+500];
int a[N+500],n,m,P[2*N+500];
ll res[N+500],dp[2*N+500][2],F[2*N+500],F1[2*N+500];
bool stepen2;
void DFSsetup(int u){
if(E[u].empty()) return;
F[u]=E[u].size();
for(auto i:E[u]){
DFSsetup(i);
F[u]=Puta(F[u],F[i]);
}
for(auto i:E[u]){
F1[i]=1;
for(auto j:E[u]){
if(j!=i) F1[i]=Puta(F1[i],F[j]);
}
}
}
void DFS(int u){
if(E[u].empty()) return;
for(auto i:E[u]) DFS(i);
dp[u][1]=0;
for(auto i:E[u]) dp[u][1]=Plus(dp[u][1],Puta(dp[i][1],F1[i]));
//printf("%i: %lld %lld\n",u,dp[u][1],F[u]);
/*int mask=0;
while(mask< (1<<(E[u].size()))){
ll temp1=1,sum=0;
for(int i=0,temp=mask;i<E[u].size();i++,temp/=2){
temp1=Puta(temp1,dp[E[u][i]][temp%2]);sum+=temp%2;
}
if(mask!=0) dp[u][1]=Plus(dp[u][1],Puta(sum,temp1));
dp[u][0]=Plus(dp[u][0],Puta(E[u].size()-sum,temp1));
mask++;
}*/
//dp[u][0]=F[u]-dp[u][1];
}
void Update1(int v){
dp[v][1]=1-dp[v][1];
dp[v][0]=1-dp[v][0];
int u=P[v];
while(u!=-1){
dp[u][1]=0;
for(auto i:E[u]) dp[u][1]=Plus(dp[u][1],Puta(dp[i][1],F1[i]));
/*dp[u][1]=dp[u][0]=0;
int mask=0;
while(mask< (1<<(E[u].size()))){
ll temp1=1,sum=0;
for(int i=0,temp=mask;i<E[u].size();i++,temp/=2){
temp1=Puta(temp1,dp[E[u][i]][temp%2]);sum+=temp%2;
}
if(mask!=0) dp[u][1]=Plus(dp[u][1],Puta(sum,temp1));
dp[u][0]=Plus(dp[u][0],Puta(E[u].size()-sum,temp1));
mask++;
}*/
u=P[u];
}
}
int nc,root,lc[2*N],rc[2*N],sum[2*N],lazy[2*N];
void update(int &c,int ss,int se,int qval){
if(!c) c=++nc;
if(qval==0) return;
sum[c]=se-ss+1-sum[c];
lazy[c]++;lazy[c]%=2;
}
void push(int &c,int ss,int se){
int mid=(ss+se)/2;
update(lc[c],ss,mid,lazy[c]);
update(rc[c],mid+1,se,lazy[c]);
lazy[c]=0;
}
void Update(int &c,int ss,int se,int qs,int qe){
if(!c) c=++nc;
if(qs<=ss && se<=qe){
update(c,ss,se,1);
//printf("%i %i %i: %i\n",c,ss,se,sum[c]);
return;
}
if(qe<ss || se<qs) return;
int mid=(ss+se)/2;
push(c,ss,se);
Update(lc[c],ss,mid,qs,qe);Update(rc[c],mid+1,se,qs,qe);
sum[c]=sum[lc[c]]+sum[rc[c]];
//printf("%i %i %i: %i\n",c,ss,se,sum[c]);
}
int Get(int c,int ss,int se,int qs,int qe){
//printf("%i %i %i: %i\n",c,ss,se,sum[c]);
if(qs<=ss && se<=qe) return sum[c];
if(qe<ss || se<qs) return 0;
int mid=(ss+se)/2;
push(c,ss,se);
return Get(lc[c],ss,mid,qs,qe)+Get(rc[c],mid+1,se,qs,qe);
}
void init(int N1, int M1, std::vector<int> P1, std::vector<int> A) {
n=N1,m=M1;
for(int i=0;i<P1.size();i++) P[i]=P1[i];
for(int i=0;i<P1.size();i++){
E[P[i]].pb(i);
}
for(int i=0;i<m;i++){
dp[n+i][1]=A[i],dp[n+i][0]=1-A[i];
a[i]=A[i];
F[n+i]=1;
}
DFSsetup(0);
DFS(0);
int temp=m,ct=0;while(temp>0) ct+=temp%2,temp/=2;if(ct==1) stepen2=true;
if(!stepen2) return;
for(int i=n;i<n+m;i++) dp[i][1]=0;
DFS(0);
res[0]=dp[0][1];
for(int i=0;i<m;i++){
Update1(n+i);
res[i+1]=dp[0][1];
//for(int j=0;j<n+m;j++) printf("%i %i: %i\n",i+1,j,dp[j][1]);
}
//for(int i=0;i<=m;i++) printf("%i ",res[i]);
//printf("\n");
for(int i=0;i<m;i++) if(a[i]==1) Update(root,0,m-1,i,i);
}
int count_ways(int L, int R) {
if(stepen2){
Update(root,0,m-1,L-n,R-n);
return res[Get(root,0,m-1,0,m-1)];
}
/*if(n==1){
int ct=0;
for(int i=L;i<=R;i++) a[i-1]=1-a[i-1];
for(int i=0;i<=m-1;i++) ct+=a[i];
return ct;
}*/
for(int i=L;i<=R;i++) Update1(i);
//DFS(0);
/*for(int i=0;i<=n+m-1;i++){
printf("%i: %i %i\n",i,dp[i][0],dp[i][1]);
}*/
return dp[0][1];
}
Compilation message
circuit.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>)':
circuit.cpp:107:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for(int i=0;i<P1.size();i++) P[i]=P1[i];
| ~^~~~~~~~~~
circuit.cpp:108:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | for(int i=0;i<P1.size();i++){
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12120 KB |
Output is correct |
2 |
Correct |
3 ms |
12120 KB |
Output is correct |
3 |
Correct |
15 ms |
12120 KB |
Output is correct |
4 |
Correct |
16 ms |
12372 KB |
Output is correct |
5 |
Correct |
14 ms |
12120 KB |
Output is correct |
6 |
Correct |
16 ms |
12120 KB |
Output is correct |
7 |
Correct |
33 ms |
12340 KB |
Output is correct |
8 |
Correct |
10 ms |
12120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12120 KB |
Output is correct |
2 |
Correct |
4 ms |
12120 KB |
Output is correct |
3 |
Correct |
4 ms |
12120 KB |
Output is correct |
4 |
Correct |
3 ms |
12120 KB |
Output is correct |
5 |
Correct |
3 ms |
12120 KB |
Output is correct |
6 |
Correct |
2 ms |
12120 KB |
Output is correct |
7 |
Correct |
4 ms |
14424 KB |
Output is correct |
8 |
Correct |
3 ms |
14420 KB |
Output is correct |
9 |
Correct |
4 ms |
14424 KB |
Output is correct |
10 |
Correct |
25 ms |
14504 KB |
Output is correct |
11 |
Correct |
44 ms |
14496 KB |
Output is correct |
12 |
Correct |
3 ms |
12120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12120 KB |
Output is correct |
2 |
Correct |
3 ms |
12120 KB |
Output is correct |
3 |
Correct |
15 ms |
12120 KB |
Output is correct |
4 |
Correct |
16 ms |
12372 KB |
Output is correct |
5 |
Correct |
14 ms |
12120 KB |
Output is correct |
6 |
Correct |
16 ms |
12120 KB |
Output is correct |
7 |
Correct |
33 ms |
12340 KB |
Output is correct |
8 |
Correct |
10 ms |
12120 KB |
Output is correct |
9 |
Correct |
2 ms |
12120 KB |
Output is correct |
10 |
Correct |
4 ms |
12120 KB |
Output is correct |
11 |
Correct |
4 ms |
12120 KB |
Output is correct |
12 |
Correct |
3 ms |
12120 KB |
Output is correct |
13 |
Correct |
3 ms |
12120 KB |
Output is correct |
14 |
Correct |
2 ms |
12120 KB |
Output is correct |
15 |
Correct |
4 ms |
14424 KB |
Output is correct |
16 |
Correct |
3 ms |
14420 KB |
Output is correct |
17 |
Correct |
4 ms |
14424 KB |
Output is correct |
18 |
Correct |
25 ms |
14504 KB |
Output is correct |
19 |
Correct |
44 ms |
14496 KB |
Output is correct |
20 |
Correct |
3 ms |
12120 KB |
Output is correct |
21 |
Correct |
4 ms |
12120 KB |
Output is correct |
22 |
Correct |
3 ms |
12120 KB |
Output is correct |
23 |
Correct |
3 ms |
12120 KB |
Output is correct |
24 |
Correct |
3 ms |
14424 KB |
Output is correct |
25 |
Correct |
4 ms |
14424 KB |
Output is correct |
26 |
Correct |
4 ms |
14676 KB |
Output is correct |
27 |
Correct |
4 ms |
14424 KB |
Output is correct |
28 |
Correct |
4 ms |
14424 KB |
Output is correct |
29 |
Correct |
33 ms |
12332 KB |
Output is correct |
30 |
Correct |
21 ms |
12340 KB |
Output is correct |
31 |
Correct |
3 ms |
12376 KB |
Output is correct |
32 |
Correct |
3 ms |
14424 KB |
Output is correct |
33 |
Correct |
3 ms |
12316 KB |
Output is correct |
34 |
Correct |
3 ms |
12120 KB |
Output is correct |
35 |
Correct |
8 ms |
12120 KB |
Output is correct |
36 |
Correct |
16 ms |
14424 KB |
Output is correct |
37 |
Correct |
33 ms |
14484 KB |
Output is correct |
38 |
Correct |
96 ms |
14488 KB |
Output is correct |
39 |
Correct |
4 ms |
12120 KB |
Output is correct |
40 |
Correct |
3 ms |
12120 KB |
Output is correct |
41 |
Correct |
3 ms |
12120 KB |
Output is correct |
42 |
Correct |
4 ms |
12208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
399 ms |
17036 KB |
Output is correct |
2 |
Correct |
604 ms |
19620 KB |
Output is correct |
3 |
Correct |
640 ms |
19280 KB |
Output is correct |
4 |
Correct |
666 ms |
19688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
399 ms |
17036 KB |
Output is correct |
2 |
Correct |
604 ms |
19620 KB |
Output is correct |
3 |
Correct |
640 ms |
19280 KB |
Output is correct |
4 |
Correct |
666 ms |
19688 KB |
Output is correct |
5 |
Correct |
539 ms |
17276 KB |
Output is correct |
6 |
Correct |
640 ms |
19732 KB |
Output is correct |
7 |
Correct |
697 ms |
19280 KB |
Output is correct |
8 |
Correct |
638 ms |
19220 KB |
Output is correct |
9 |
Correct |
325 ms |
14684 KB |
Output is correct |
10 |
Correct |
548 ms |
15184 KB |
Output is correct |
11 |
Correct |
604 ms |
14680 KB |
Output is correct |
12 |
Correct |
618 ms |
14680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12120 KB |
Output is correct |
2 |
Correct |
4 ms |
12120 KB |
Output is correct |
3 |
Correct |
4 ms |
12120 KB |
Output is correct |
4 |
Correct |
3 ms |
12120 KB |
Output is correct |
5 |
Correct |
3 ms |
12120 KB |
Output is correct |
6 |
Correct |
2 ms |
12120 KB |
Output is correct |
7 |
Correct |
4 ms |
14424 KB |
Output is correct |
8 |
Correct |
3 ms |
14420 KB |
Output is correct |
9 |
Correct |
4 ms |
14424 KB |
Output is correct |
10 |
Correct |
25 ms |
14504 KB |
Output is correct |
11 |
Correct |
44 ms |
14496 KB |
Output is correct |
12 |
Correct |
3 ms |
12120 KB |
Output is correct |
13 |
Correct |
399 ms |
17036 KB |
Output is correct |
14 |
Correct |
604 ms |
19620 KB |
Output is correct |
15 |
Correct |
640 ms |
19280 KB |
Output is correct |
16 |
Correct |
666 ms |
19688 KB |
Output is correct |
17 |
Correct |
539 ms |
17276 KB |
Output is correct |
18 |
Correct |
640 ms |
19732 KB |
Output is correct |
19 |
Correct |
697 ms |
19280 KB |
Output is correct |
20 |
Correct |
638 ms |
19220 KB |
Output is correct |
21 |
Correct |
325 ms |
14684 KB |
Output is correct |
22 |
Correct |
548 ms |
15184 KB |
Output is correct |
23 |
Correct |
604 ms |
14680 KB |
Output is correct |
24 |
Correct |
618 ms |
14680 KB |
Output is correct |
25 |
Execution timed out |
3015 ms |
19588 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12120 KB |
Output is correct |
2 |
Correct |
3 ms |
12120 KB |
Output is correct |
3 |
Correct |
15 ms |
12120 KB |
Output is correct |
4 |
Correct |
16 ms |
12372 KB |
Output is correct |
5 |
Correct |
14 ms |
12120 KB |
Output is correct |
6 |
Correct |
16 ms |
12120 KB |
Output is correct |
7 |
Correct |
33 ms |
12340 KB |
Output is correct |
8 |
Correct |
10 ms |
12120 KB |
Output is correct |
9 |
Correct |
2 ms |
12120 KB |
Output is correct |
10 |
Correct |
4 ms |
12120 KB |
Output is correct |
11 |
Correct |
4 ms |
12120 KB |
Output is correct |
12 |
Correct |
3 ms |
12120 KB |
Output is correct |
13 |
Correct |
3 ms |
12120 KB |
Output is correct |
14 |
Correct |
2 ms |
12120 KB |
Output is correct |
15 |
Correct |
4 ms |
14424 KB |
Output is correct |
16 |
Correct |
3 ms |
14420 KB |
Output is correct |
17 |
Correct |
4 ms |
14424 KB |
Output is correct |
18 |
Correct |
25 ms |
14504 KB |
Output is correct |
19 |
Correct |
44 ms |
14496 KB |
Output is correct |
20 |
Correct |
3 ms |
12120 KB |
Output is correct |
21 |
Correct |
4 ms |
12120 KB |
Output is correct |
22 |
Correct |
3 ms |
12120 KB |
Output is correct |
23 |
Correct |
3 ms |
12120 KB |
Output is correct |
24 |
Correct |
3 ms |
14424 KB |
Output is correct |
25 |
Correct |
4 ms |
14424 KB |
Output is correct |
26 |
Correct |
4 ms |
14676 KB |
Output is correct |
27 |
Correct |
4 ms |
14424 KB |
Output is correct |
28 |
Correct |
4 ms |
14424 KB |
Output is correct |
29 |
Correct |
33 ms |
12332 KB |
Output is correct |
30 |
Correct |
21 ms |
12340 KB |
Output is correct |
31 |
Correct |
3 ms |
12376 KB |
Output is correct |
32 |
Correct |
3 ms |
14424 KB |
Output is correct |
33 |
Correct |
3 ms |
12316 KB |
Output is correct |
34 |
Correct |
3 ms |
12120 KB |
Output is correct |
35 |
Correct |
8 ms |
12120 KB |
Output is correct |
36 |
Correct |
16 ms |
14424 KB |
Output is correct |
37 |
Correct |
33 ms |
14484 KB |
Output is correct |
38 |
Correct |
96 ms |
14488 KB |
Output is correct |
39 |
Correct |
4 ms |
12120 KB |
Output is correct |
40 |
Correct |
3 ms |
12120 KB |
Output is correct |
41 |
Correct |
3 ms |
12120 KB |
Output is correct |
42 |
Correct |
4 ms |
12208 KB |
Output is correct |
43 |
Execution timed out |
3033 ms |
14424 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12120 KB |
Output is correct |
2 |
Correct |
3 ms |
12120 KB |
Output is correct |
3 |
Correct |
15 ms |
12120 KB |
Output is correct |
4 |
Correct |
16 ms |
12372 KB |
Output is correct |
5 |
Correct |
14 ms |
12120 KB |
Output is correct |
6 |
Correct |
16 ms |
12120 KB |
Output is correct |
7 |
Correct |
33 ms |
12340 KB |
Output is correct |
8 |
Correct |
10 ms |
12120 KB |
Output is correct |
9 |
Correct |
2 ms |
12120 KB |
Output is correct |
10 |
Correct |
4 ms |
12120 KB |
Output is correct |
11 |
Correct |
4 ms |
12120 KB |
Output is correct |
12 |
Correct |
3 ms |
12120 KB |
Output is correct |
13 |
Correct |
3 ms |
12120 KB |
Output is correct |
14 |
Correct |
2 ms |
12120 KB |
Output is correct |
15 |
Correct |
4 ms |
14424 KB |
Output is correct |
16 |
Correct |
3 ms |
14420 KB |
Output is correct |
17 |
Correct |
4 ms |
14424 KB |
Output is correct |
18 |
Correct |
25 ms |
14504 KB |
Output is correct |
19 |
Correct |
44 ms |
14496 KB |
Output is correct |
20 |
Correct |
3 ms |
12120 KB |
Output is correct |
21 |
Correct |
4 ms |
12120 KB |
Output is correct |
22 |
Correct |
3 ms |
12120 KB |
Output is correct |
23 |
Correct |
3 ms |
12120 KB |
Output is correct |
24 |
Correct |
3 ms |
14424 KB |
Output is correct |
25 |
Correct |
4 ms |
14424 KB |
Output is correct |
26 |
Correct |
4 ms |
14676 KB |
Output is correct |
27 |
Correct |
4 ms |
14424 KB |
Output is correct |
28 |
Correct |
4 ms |
14424 KB |
Output is correct |
29 |
Correct |
33 ms |
12332 KB |
Output is correct |
30 |
Correct |
21 ms |
12340 KB |
Output is correct |
31 |
Correct |
3 ms |
12376 KB |
Output is correct |
32 |
Correct |
3 ms |
14424 KB |
Output is correct |
33 |
Correct |
3 ms |
12316 KB |
Output is correct |
34 |
Correct |
3 ms |
12120 KB |
Output is correct |
35 |
Correct |
8 ms |
12120 KB |
Output is correct |
36 |
Correct |
16 ms |
14424 KB |
Output is correct |
37 |
Correct |
33 ms |
14484 KB |
Output is correct |
38 |
Correct |
96 ms |
14488 KB |
Output is correct |
39 |
Correct |
4 ms |
12120 KB |
Output is correct |
40 |
Correct |
3 ms |
12120 KB |
Output is correct |
41 |
Correct |
3 ms |
12120 KB |
Output is correct |
42 |
Correct |
4 ms |
12208 KB |
Output is correct |
43 |
Correct |
399 ms |
17036 KB |
Output is correct |
44 |
Correct |
604 ms |
19620 KB |
Output is correct |
45 |
Correct |
640 ms |
19280 KB |
Output is correct |
46 |
Correct |
666 ms |
19688 KB |
Output is correct |
47 |
Correct |
539 ms |
17276 KB |
Output is correct |
48 |
Correct |
640 ms |
19732 KB |
Output is correct |
49 |
Correct |
697 ms |
19280 KB |
Output is correct |
50 |
Correct |
638 ms |
19220 KB |
Output is correct |
51 |
Correct |
325 ms |
14684 KB |
Output is correct |
52 |
Correct |
548 ms |
15184 KB |
Output is correct |
53 |
Correct |
604 ms |
14680 KB |
Output is correct |
54 |
Correct |
618 ms |
14680 KB |
Output is correct |
55 |
Execution timed out |
3015 ms |
19588 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |