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>
#define int long long
using namespace std;
const int MOD = (int)1e9 + 2022;
const int MAX = 1e6;
int par[MAX];
vector<int> childs[MAX];
int cntParams[MAX];
int nbInternes, nbFeuilles;
int coeff[MAX];
int iDeb[MAX], iFin[MAX];
int sum[MAX], sumCoeffs[MAX], lazy[MAX];
void pull(int node) {
sum[node] = sum[2 * node] + sum[2 * node + 1];
if (sum[node] >= MOD)
sum[node] -= MOD;
}
void push(int node) {
if (!lazy[node])
return;
sum[node] = sumCoeffs[node] - sum[node];
if (sum[node] < 0)
sum[node] += MOD;
if (iDeb[node] < iFin[node])
lazy[2 * node] ^= 1, lazy[2 * node + 1] ^= 1;
lazy[node] = 0;
}
void initSeg(int node, int l, int r, vector<signed> &valInit) {
iDeb[node] = l, iFin[node] = r;
if (l == r) {
sumCoeffs[node] = coeff[l];
sum[node] = valInit[l] * coeff[l];
return;
}
initSeg(2 * node, l, (l + r) / 2, valInit);
initSeg(2 * node + 1, (l + r) / 2 + 1, r, valInit);
pull(node);
sumCoeffs[node] = sumCoeffs[2 * node] + sumCoeffs[2 * node + 1];
if (sumCoeffs[node] >= MOD)
sumCoeffs[node] -= MOD;
}
void upd(int node, int l, int r) {
push(node);
if (l > iFin[node] or r < iDeb[node])
return;
if (l <= iDeb[node] and r >= iFin[node]) {
lazy[node] ^= 1;
push(node);
return;
}
upd(2 * node, l, r);
upd(2 * node + 1, l, r);
pull(node);
}
void setParams(int u) {
if (childs[u].empty()) {
cntParams[u] = 1;
return;
}
cntParams[u] = childs[u].size();
for (int v : childs[u]) {
setParams(v);
cntParams[u] = cntParams[u] * cntParams[v] % MOD;
}
}
void setCoeffs(int u, int bef) {
if (u >= nbInternes) {
coeff[u - nbInternes] = bef;
return;
}
int deg = childs[u].size();
vector<int> prefProd(deg + 1, 1), suffProd(deg + 1, 1);
for (int i = 0; i < deg; ++i)
prefProd[i + 1] = prefProd[i] * cntParams[childs[u][i]] % MOD;
for (int i = deg - 1; i >= 0; --i)
suffProd[i] = suffProd[i + 1] * cntParams[childs[u][i]] % MOD;
for (int i = 0; i < deg; ++i)
setCoeffs(childs[u][i], bef * prefProd[i] % MOD * suffProd[i + 1] % MOD);
}
void init(signed N, signed M, vector<signed> P, std::vector<signed> A) {
nbInternes = N, nbFeuilles = M;
int nbSommets = N + M;
for (int i = 1; i < nbSommets; ++i)
childs[P[i]].push_back(i);
for (int i = nbSommets - 1; i >= 0; --i) {
if (i >= nbInternes)
cntParams[i] = 1;
else {
cntParams[i] = childs[i].size();
for (int j : childs[i])
if (j < nbInternes)
cntParams[i] = cntParams[i] * cntParams[j] % MOD;
}
}
setCoeffs(0, 1);
initSeg(1, 0, nbFeuilles - 1, A);
}
signed count_ways(signed L, signed R) {
L -= nbInternes, R -= nbInternes;
upd(1, L, R);
return sum[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... |