This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int,int>
#define fi first
#define se second
#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back
#define lb lower_bound
#define ub upper_bound
#define rep(x,s,e) for (int x=(s)-((s)>(e));x!=(e)-((s)>(e));((s)<(e))?x++:x--)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
const int MOD=1000002022;
struct node{
int s,e,m;
ii val={0,0};
bool lazy=false;
node *l,*r;
node (int _s,int _e){
s=_s,e=_e,m=s+e>>1;
if (s!=e){
l=new node(s,m);
r=new node(m+1,e);
}
}
void propo(){
if (!lazy) return;
swap(val.fi,val.se);
if (s!=e){
l->lazy^=true;
r->lazy^=true;
}
lazy=false;
}
void update(int i,ii k){
if (s==e) val=k;
else{
if (i<=m) l->update(i,k);
else r->update(i,k);
val={(l->val.fi+r->val.fi)%MOD,(l->val.se+r->val.se)%MOD};
}
}
void update(int i,int j){
propo();
if (s==i && e==j) lazy^=true;
else{
if (j<=m) l->update(i,j);
else if (m<i) r->update(i,j);
else l->update(i,m),r->update(m+1,j);
l->propo(),r->propo();
val={(l->val.fi+r->val.fi)%MOD,(l->val.se+r->val.se)%MOD};
}
}
} *root=new node(0,100005);
int n,m;
vector<int> al[100005];
int coef[100005];
int arr[100005];
int ss[100005];
void dfs_ss(int i){
if (i<n){
ss[i]=sz(al[i]);
for (auto it:al[i]){
dfs_ss(it);
ss[i]=ss[i]*ss[it]%MOD;
}
}
else ss[i]=1;
}
void dfs(int i,int ans){
if (i<n){
vector<int> v;
for (auto it:al[i]) v.pub(ss[it]);
vector<int> f={1},b={1};
rep(x,0,sz(v)) f.pub(f.back()*v[x]%MOD);
rep(x,sz(v),0) b.pub(b.back()*v[x]%MOD);
reverse(all(b));
rep(x,0,sz(v)) dfs(al[i][x],ans*f[x]%MOD*b[x+1]%MOD);
}
else coef[i-n]=ans;
}
void init(signed N, signed M, vector<signed> P, vector<signed> A) {
n=N,m=M;
rep(x,1,n+m) al[P[x]].pub(x);
rep(x,0,m) arr[x]=A[x];
dfs_ss(0);
dfs(0,1);
//rep(x,0,m) cout<<coef[x]<<" "; cout<<endl;
rep(x,0,m){
if (A[x]) root->update(x,{coef[x],0});
else root->update(x,{0,coef[x]});
}
}
signed count_ways(signed a, signed b) {
root->update(a-n,b-n);
return root->val.fi;
}
Compilation message (stderr)
circuit.cpp: In constructor 'node::node(long long int, long long int)':
circuit.cpp:33:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
33 | s=_s,e=_e,m=s+e>>1;
| ~^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |