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 <iostream>
#include <vector>
using i64 = int64_t;
int n, m;
std::vector<int> p, a;
std::vector<std::vector<int>> children;
const i64 MOD = 1'000'002'022;
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
n = N;
m = M;
p = P;
a = A;
children.resize(N);
for (i64 i = 1; i < N + M; i++) {
children[P[i]].push_back(i);
}
}
auto dfs(int node) -> std::pair<i64, i64> {
if (node >= n) {
return {a[node - n] ? 1 : 0, 1};
}
i64 cs = children[node].size();
std::vector<std::pair<i64, i64>> results;
results.reserve(cs);
for (i64 c : children[node]) {
results.push_back(dfs(c));
}
std::vector<i64> xs_left {1}, xs_right {1};
xs_left.reserve(cs);
xs_right.reserve(cs);
for (i64 i = 0; i < cs; i++) {
xs_left.push_back(xs_left.back() * results[i].second % MOD);
xs_right.push_back(xs_right.back() * results[cs - i - 1].second % MOD);
}
i64 sum = 0, of = cs;
for (i64 i = 0; i < cs; i++) {
sum += results[i].first * xs_left[i] % MOD * xs_right[n - i - 1];
cs *= results[i].second;
}
return {sum, of};
}
int count_ways(int l, int r) {
r += 1;
l -= n;
r -= n;
for (i64 i = l; i < r; i++) {
a[i] = !a[i];
}
return dfs(0).first;
}
# | 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... |