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 <bits/stdc++.h>
#include "circuit.h"
using namespace std;
int modulo = (int)1e9 + 2022;
int n,m;
int t[200005];
vector<int> f[200005];
int a[100005];
int v[100005];
int dp[200005],s[200005],sp[200005];
int ff[200005];
void dfs(int nod)
{
if (nod > n)
s[nod] = 1;
else
{
for (auto fiu : f[nod])
dfs(fiu);
s[nod] = f[nod].size();
for (auto fiu : f[nod])
s[nod] = 1ll * s[nod] * s[fiu] % modulo;
vector<int> pref_prod(f[nod].size()),suf_prod(f[nod].size());
pref_prod[0] = s[f[nod][0]];
for (int i = 1; i < f[nod].size(); i++)
pref_prod[i] = 1ll * pref_prod[i - 1] * s[f[nod][i]] % modulo;
suf_prod[f[nod].size() - 1] = s[f[nod].back()];
for (int i = (int)f[nod].size() - 2; i >= 0; i--)
suf_prod[i] = 1ll * suf_prod[i + 1] * s[f[nod][i]] % modulo;
for (int i = 0; i < f[nod].size(); i++)
{
sp[f[nod][i]] = 1;
if (i != 0)
sp[i] = pref_prod[i - 1];
if (i + 1 != f[nod].size())
sp[i] = 1ll * sp[i] * suf_prod[i + 1] % modulo;
}
}
}
void dfs2(int nod)
{
if (nod == 1)
ff[nod] = 1;
else
ff[nod] = 1ll * sp[nod] * ff[t[nod]] % modulo;
for (auto fiu : f[nod])
dfs2(fiu);
}
int aint[400005],sm[400005];
bool lazy[400005];
void build(int nod,int l,int r)
{
if (l == r)
{
sm[nod] = v[l];
aint[nod] = v[l] * a[l];
}
else
{
int mij = (l + r) / 2;
build(2 * nod,l,mij);
build(2 * nod + 1,mij + 1,r);
sm[nod] = (sm[2 * nod] + sm[2 * nod + 1]) % modulo;
aint[nod] = (aint[2 * nod] + aint[2 * nod + 1]) % modulo;
}
}
void prop(int nod,int l,int r)
{
if (!lazy[nod])
return;
lazy[nod] = false;
if (l == r)
return;
aint[2 * nod] = (sm[2 * nod] + modulo - aint[2 * nod]) % modulo;
aint[2 * nod + 1] = (sm[2 * nod + 1] + modulo - aint[2 * nod + 1]) % modulo;
lazy[2 * nod] ^= 1;
lazy[2 * nod + 1] ^= 1;
}
void update(int nod,int l,int r,int st,int dr)
{
prop(nod,l,r);
if (r < st or dr < l)
return;
if (st <= l and r <= dr)
{
aint[nod] = (sm[nod] + modulo - aint[nod]) % modulo;
lazy[nod] = true;
return;
}
int mij = (l + r) / 2;
update(2 * nod,l,mij,st,dr);
update(2 * nod + 1,mij + 1,r,st,dr);
aint[nod] = (aint[2 * nod] + aint[2 * nod + 1]) % modulo;
}
void init(int N, int M, vector<int> P, vector<int> A)
{
n = N;
m = M;
for (int i = 0; i < P.size(); i++)
P[i]++;
for (int i = 0; i < P.size(); i++)
{
t[i + 1] = P[i];
f[P[i]].push_back(i + 1);
}
for (int i = 1; i <= m; i++)
a[i] = A[i - 1];
dfs(1);
dfs2(1);
for (int i = 1; i <= m; i++)
v[i] = ff[n + i];
build(1,1,m);
}
int count_ways(int L, int R)
{
L++;
R++;
L -= n;
R -= n;
//update(1,1,m,L,R);
for (int i = L; i <= R; i++)
a[i] ^= 1;
int ans = 0;
for (int i = 1; i <= m; i++)
ans = (ans + a[i] * v[i]) % modulo;
return ans;
}
Compilation message (stderr)
circuit.cpp: In function 'void dfs(int)':
circuit.cpp:29:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for (int i = 1; i < f[nod].size(); i++)
| ~~^~~~~~~~~~~~~~~
circuit.cpp:34:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for (int i = 0; i < f[nod].size(); i++)
| ~~^~~~~~~~~~~~~~~
circuit.cpp:39:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | if (i + 1 != f[nod].size())
| ~~~~~~^~~~~~~~~~~~~~~~
circuit.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>)':
circuit.cpp:109:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for (int i = 0; i < P.size(); i++)
| ~~^~~~~~~~~~
circuit.cpp:111:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | for (int i = 0; i < P.size(); i++)
| ~~^~~~~~~~~~
# | 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... |