#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
template<class Info, class Tag>
struct LazySegmentTree {
int n;
vector<Info> info;
vector<Tag> tag;
LazySegmentTree() : n(0) {}
LazySegmentTree(int n_, Info v_ = Info()) {
init(n_, v_);
}
template<class T>
LazySegmentTree(vector<T> init_) {
init(init_);
}
void init(int n_, Info v_ = Info()) {
init(vector<Info>(n_, v_));
}
template<class T>
void init(vector<T> init_) {
n = init_.size();
info.assign(4 << __lg(n), Info());
tag.assign(4 << __lg(n), Tag());
function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) {
info[p] = init_[l];
return;
}
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
pull(p);
};
build(1, 0, n);
}
void pull(int p) {
info[p] = info[2 * p] + info[2 * p + 1];
}
void apply(int p, const Tag &v) {
info[p].apply(v);
tag[p].apply(v);
}
void push(int p) {
apply(2 * p, tag[p]);
apply(2 * p + 1, tag[p]);
tag[p] = Tag();
}
void modify(int p, int l, int r, int x, const Info &v) {
if (r - l == 1) {
info[p] = v;
return;
}
int m = (l + r) / 2;
push(p);
if (x < m) {
modify(2 * p, l, m, x, v);
} else {
modify(2 * p + 1, m, r, x, v);
}
pull(p);
}
void modify(int p, const Info &v) {
modify(1, 0, n, p, v);
}
Info rangeQuery(int p, int l, int r, int x, int y) {
if (l >= y || r <= x) {
return Info();
}
if (l >= x && r <= y) {
return info[p];
}
int m = (l + r) / 2;
push(p);
return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y);
}
Info rangeQuery(int l, int r) {
return rangeQuery(1, 0, n, l, r);
}
void rangeApply(int p, int l, int r, int x, int y, const Tag &v) {
if (l >= y || r <= x) {
return;
}
if (l >= x && r <= y) {
apply(p, v);
return;
}
int m = (l + r) / 2;
push(p);
rangeApply(2 * p, l, m, x, y, v);
rangeApply(2 * p + 1, m, r, x, y, v);
pull(p);
}
void rangeApply(int l, int r, const Tag &v) {
return rangeApply(1, 0, n, l, r, v);
}
template<class F>
int findFirst(int p, int l, int r, int x, int y, F &&pred) {
if (l >= y || r <= x) {
return -1;
}
if (l >= x && r <= y && !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findFirst(2 * p, l, m, x, y, pred);
if (res == -1) {
res = findFirst(2 * p + 1, m, r, x, y, pred);
}
return res;
}
template<class F>
int findFirst(int l, int r, F &&pred) {
return findFirst(1, 0, n, l, r, pred);
}
template<class F>
int findLast(int p, int l, int r, int x, int y, F &&pred) {
if (l >= y || r <= x) {
return -1;
}
if (l >= x && r <= y && !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findLast(2 * p + 1, m, r, x, y, pred);
if (res == -1) {
res = findLast(2 * p, l, m, x, y, pred);
}
return res;
}
template<class F>
int findLast(int l, int r, F &&pred) {
return findLast(1, 0, n, l, r, pred);
}
};
struct Tag {
int toggle = 0;
void apply(const Tag &t) & {
toggle ^= t.toggle;
}
};
const int P = (int)1e9 + 2022;
struct Info {
int sum_on = 0;
int sum_off = 0;
void apply(const Tag &t) & {
if (t.toggle) {
swap(sum_on, sum_off);
}
}
Info operator+(const Info &b) {
return {(sum_on + b.sum_on) % P, (sum_off + b.sum_off) % P};
}
};
namespace {
LazySegmentTree<Info, Tag> seg;
int n;
}
void init(int N, int M, std::vector<int> par, std::vector<int> A) {
n = N + M;
vector<vector<int>> adj(n);
for (int u = 1; u < n; u++) {
adj[par[u]].push_back(u);
}
vector<int> F(n);
auto dfs = [&](auto self, int u) -> void {
F[u] = max((int)adj[u].size(), 1);
for (int v : adj[u]) {
self(self, v);
F[u] = 1LL * F[u] * F[v] % P;
}
};
dfs(dfs, 0);
vector<int> f(n);
auto propagate = [&](auto self, int u) -> void {
vector<int> suf = {1};
for (int v : adj[u]) {
suf.push_back(1LL * suf.back() * F[v] % P);
}
reverse(adj[u].begin(), adj[u].end());
vector<int> pre = {1};
for (int v : adj[u]) {
pre.push_back(1LL * pre.back() * F[v] % P);
}
int i = 0;
for (int v : adj[u]) {
f[v] = 1LL * f[u] * pre[i] % P * suf[adj[u].size() - i - 1] % P;
self(self, v);
i++;
}
};
f[0] = 1;
propagate(propagate, 0);
vector<Info> info(n);
for (int i = 0; i < M; i++) {
info[N + i] = {f[N + i] * A[i], f[N + i] * (1 ^ A[i])};
}
seg = LazySegmentTree<Info, Tag>(info);
}
int count_ways(int L, int R) {
seg.rangeApply(L, R + 1, {1});
return seg.rangeQuery(0, n).sum_on;
}
# | 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... |