#include "circuit.h"
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN=1e5+6;
const ll mod=1000002022;
ll arr[MAXN];
ll fastpow(ll x,ll p)
{
if(p==0)return 1;
if(p==1)return x;
ll t=fastpow(x,p/2);
t*=t;t%=mod;
if(p%2==1)
{
t*=x;
t%=mod;
}
return t;
}
struct segment_tree
{
ll tree[4*MAXN];
int lazy[4*MAXN];
void build(int l,int r,int idx)
{
if(l==r)
{
tree[idx]=arr[l];
return;
}
int mid=(l+r)/2;
build(l,mid,idx*2);
build(mid+1,r,idx*2+1);
tree[idx]=(tree[idx*2]+tree[idx*2+1])%mod;
}
void push(int l,int r,int idx)
{
if(lazy[idx]==0)return;
tree[idx]=mod-tree[idx];
tree[idx]%=mod;
if(l!=r)
{
lazy[idx*2]^=lazy[idx];
lazy[idx*2+1]^=lazy[idx];
}
lazy[idx]=0;
}
void update(int idx,int l,int r,int ql,int qr)
{
push(l,r,idx);
if(qr<l)return;
if(ql>r)return;
if(l>=ql&&r<=qr)
{
lazy[idx]^=1;
push(l,r,idx);
return;
}
int mid=(l+r)/2;
update(idx*2,l,mid,ql,qr);
update(idx*2+1,mid+1,r,ql,qr);
tree[idx]=(tree[idx*2]+tree[idx*2+1])%mod;
}
ll query(int l,int r,int idx,int ql,int qr)
{
push(l,r,idx);
if(l>qr)return 0;
if(r<ql)return 0;
if(l>=ql&&r<=qr)return tree[idx];
int mid=(l+r)/2;
return (query(l,mid,idx*2,ql,qr)+query(mid+1,r,idx*2+1,ql,qr))%mod;
}
}tr;
int m,n;
ll all_prod=1,sum1;
vector<int>g[MAXN];
ll path_prod[MAXN];
ll subtree_prod[MAXN];
void dfs_subtrees(int u)
{
subtree_prod[u]=max(1,(int)g[u].size());
for(auto xd:g[u])
{
dfs_subtrees(xd);
subtree_prod[u]*=subtree_prod[xd];
subtree_prod[u]%=mod;
}
}
void dfs_prods(int u)
{
vector<ll>pref_prods;
vector<ll>suf_prods;
for(int i=0;i<g[u].size();i++)
{
if(i==0)pref_prods.push_back(max(1,(int)g[g[u][i]].size()));
else
{
pref_prods.push_back((pref_prods.back()*max(1,(int)g[g[u][i]].size()))%mod);
}
}
for(int i=g[u].size()-1;i>=0;i--)
{
if(i==g[u].size()-1)suf_prods.push_back(max(1,(int)g[g[u][i]].size()));
else
{
suf_prods.push_back((suf_prods.back()*max(1,(int)g[g[u][i]].size()))%mod);
}
}
reverse(suf_prods.begin(),suf_prods.end());
for(int i=0;i<g[u].size();i++)
{
ll prod1=1;
if(i>0)prod1*=pref_prods[i-1];
if(i+1<g[u].size())prod1*=suf_prods[i+1];
prod1%=mod;
path_prod[g[u][i]]=(path_prod[u]*prod1)%mod;
dfs_prods(g[u][i]);
}
}
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
m=M;n=N;
for(int i=1;i<N+M;i++)
{
g[P[i]].push_back(i);
}
for(int i=0;i<N;i++)
{
ll t=g[i].size();
all_prod*=t;
all_prod%=mod;
}
path_prod[0]=1;
dfs_subtrees(0);
dfs_prods(0);
//cout<<all_prod<<endl;
for(int i=0;i<M;i++)
{
//arr[i]=(all_prod*fastpow(path_prod[N+i],mod-2))%mod;
//cout<<path_prod[N+i]<<"|"<<arr[i]<<" ";
arr[i]=path_prod[N+i];
//cout<<path_prod[N+i]<<" ";
sum1+=arr[i];sum1%=mod;
if(A[i]==0)arr[i]=-arr[i];
}
//cout<<endl;
tr.build(0,M-1,1);
//cout<<sum1<<" "<<tr.query(0,m-1,1,0,m-1)<<endl;
}
int count_ways(int L, int R) {
tr.update(1,0,m-1,L-n,R-n);
return ((sum1+tr.query(0,m-1,1,0,m-1))%mod)/2;
}
Compilation message
circuit.cpp: In function 'void dfs_prods(int)':
circuit.cpp:95:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(int i=0;i<g[u].size();i++)
| ~^~~~~~~~~~~~
circuit.cpp:105:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | if(i==g[u].size()-1)suf_prods.push_back(max(1,(int)g[g[u][i]].size()));
| ~^~~~~~~~~~~~~~~
circuit.cpp:112:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
112 | for(int i=0;i<g[u].size();i++)
| ~^~~~~~~~~~~~
circuit.cpp:116:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
116 | if(i+1<g[u].size())prod1*=suf_prods[i+1];
| ~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2640 KB |
Output is correct |
4 |
Correct |
2 ms |
2640 KB |
Output is correct |
5 |
Correct |
2 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2760 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Correct |
2 ms |
2640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2668 KB |
1st lines differ - on the 1st token, expected: '52130940', found: '16384' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2640 KB |
Output is correct |
4 |
Correct |
2 ms |
2640 KB |
Output is correct |
5 |
Correct |
2 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2760 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Correct |
2 ms |
2640 KB |
Output is correct |
9 |
Correct |
2 ms |
2640 KB |
Output is correct |
10 |
Incorrect |
2 ms |
2668 KB |
1st lines differ - on the 1st token, expected: '52130940', found: '16384' |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
600 ms |
6216 KB |
1st lines differ - on the 1st token, expected: '431985922', found: '268730368' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
600 ms |
6216 KB |
1st lines differ - on the 1st token, expected: '431985922', found: '268730368' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2668 KB |
1st lines differ - on the 1st token, expected: '52130940', found: '16384' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2640 KB |
Output is correct |
4 |
Correct |
2 ms |
2640 KB |
Output is correct |
5 |
Correct |
2 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2760 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Correct |
2 ms |
2640 KB |
Output is correct |
9 |
Correct |
2 ms |
2640 KB |
Output is correct |
10 |
Incorrect |
2 ms |
2668 KB |
1st lines differ - on the 1st token, expected: '52130940', found: '16384' |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2640 KB |
Output is correct |
4 |
Correct |
2 ms |
2640 KB |
Output is correct |
5 |
Correct |
2 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2760 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Correct |
2 ms |
2640 KB |
Output is correct |
9 |
Correct |
2 ms |
2640 KB |
Output is correct |
10 |
Incorrect |
2 ms |
2668 KB |
1st lines differ - on the 1st token, expected: '52130940', found: '16384' |
11 |
Halted |
0 ms |
0 KB |
- |