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 pb push_back
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using ll = long long;
const ll N = 3e5 + 10;
const ll MOD = 1e9 + 2022;
ll n, m;
bool A[N];
vector<ll> g[N];
ll sz[N];
void dfs1(ll x){
sz[x] = g[x].size();
for(auto u: g[x]){
dfs1(u);
sz[x] *= sz[u];
sz[x] %= MOD;
}
if(!sz[x])sz[x] = 1;
}
ll X[N];
void dfs2(ll x,ll w){
X[x] = w;
ll k = g[x].size();
ll pref[k], suff[k];
for(ll i = 0;i<k;i++){
pref[i] = sz[g[x][i]];
if(i) pref[i] = (pref[i-1] * pref[i]) % MOD;
}
for(ll i = k - 1;i>=0;i--){
suff[i] = sz[g[x][i]];
if(i +1 < k)suff[i] = (suff[i] * suff[i+1]) % MOD;
}
for(ll i = 0;i<k;i++){
ll prod = w;
if(i)prod = (prod * pref[i-1]) % MOD;
if(i + 1 < k)prod = (prod * suff[i+1]) % MOD;
dfs2(g[x][i], prod);
}
}
array<ll, 2> t[4 * N];
bool lazy[4 * N];
void build(ll k,ll tl,ll tr){
if(tl == tr){
t[k] = {X[tl], 0};
if(A[tl])swap(t[k][0], t[k][1]);
return;
}
ll tm = (tl + tr) / 2;
build(k *2, tl, tm);
build(k*2+1, tm + 1, tr);
t[k][0] = (t[k*2][0] + t[k*2+1][0]) % MOD;
t[k][1] = (t[k*2][1] + t[k*2+1][1]) % MOD;
}
void push(ll k, ll tl,ll tr){
if(!lazy[k])return;
swap(t[k][0], t[k][1]);
if(tl != tr){
lazy[k*2] ^= 1;
lazy[k*2+1] ^= 1;
}
lazy[k] = 0;
}
void update(ll k,ll tl,ll tr,ll l,ll r){
push(k,tl, tr);
if(r < tl || tr < l)return;
if(l <= tl && tr <= r){
lazy[k] ^= 1;
push(k,tl, tr);
return;
}
ll tm = (tl + tr) / 2;
update(k * 2, tl, tm, l, r);
update(k*2+1, tm+1,tr, l, r);
t[k][0] = (t[k*2][0] + t[k*2+1][0]) % MOD;
t[k][1] = (t[k*2][1] + t[k*2+1][1]) % MOD;
}
void init(int nn, int mm, std::vector<int> P, std::vector<int> a) {
n = nn;
m = mm;
for(ll i = 1;i < n + m;i++){
g[P[i]].pb(i);
}
for(ll i = n;i<n + m;i++)A[i] = a[i - n];
dfs1(0);
dfs2(0, 1);
build(1, n, n + m - 1);
}
int count_ways(int L, int R) {
update(1, n, n + m - 1, L, R);
push(1, n, n + m - 1);
return t[1][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... |