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>
using namespace std;
using i32 = int;
using i64 = long long;
template <typename T>
using V = vector<T>;
template <typename T>
using VV = V<V<T>>;
template <typename T>
using VVV = V<VV<T>>;
template <typename T>
bool chmin(T &x, const T &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
#define OVERRIDE4(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32)(n); ++i)
#define REP3(i, l, r) for (i32 i = (i32)(l); i < (i32)(r); ++i)
#define REP(...) OVERRIDE4(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER2(i, n) for (i32 i = (i32)(n)-1; i >= 0; --i)
#define PER3(i, l, r) for (i32 i = (i32)(r)-1; i >= (i32)(l); --i)
#define PER(...) OVERRIDE4(__VA_ARGS__, PER3, PER2)(__VA_ARGS__)
#define ALL(x) begin(x), end(x)
#define LEN(x) (i32) size(x)
void dbg(i32 x) { cerr << x; }
void dbg(i64 x) { cerr << x; }
template <typename T, typename U>
void dbg(pair<T, U> p) {
cerr << '(';
dbg(p.first);
cerr << ", ";
dbg(p.second);
cerr << ')';
}
template <typename T>
void dbg(V<T> arr) {
cerr << '[';
REP(i, LEN(arr)) {
if (i) {
cerr << ", ";
}
dbg(arr[i]);
}
cerr << ']';
}
void debug() { cerr << '\n'; }
template <typename Head, typename... Tail>
void debug(Head head, Tail... tail) {
dbg(head);
cerr << ", ";
debug(tail...);
}
#ifdef DEBUGF
#define DBG(...) \
do { \
cerr << #__VA_ARGS__ << " : "; \
debug(__VA_ARGS__); \
} while (false)
#else
#define DBG(...) (void)0
#endif
constexpr unsigned MOD = 1000002022;
struct M {
unsigned v;
M() : v(0) {}
M(unsigned v) : v(v) {}
M &operator+=(M rhs) {
if ((v += rhs.v) >= MOD) {
v -= MOD;
}
return *this;
}
M &operator-=(M rhs) {
if ((v -= rhs.v) >= MOD) {
v += MOD;
}
return *this;
}
M &operator*=(M rhs) {
v = (unsigned long long)v * rhs.v % MOD;
return *this;
}
friend M operator+(M lhs, M rhs) { return lhs += rhs; }
friend M operator-(M lhs, M rhs) { return lhs -= rhs; }
friend M operator*(M lhs, M rhs) { return lhs *= rhs; }
};
void dbg(M x) { cerr << x.v; }
#include <vector>
#include "circuit.h"
struct Dp {
M c0, c1;
};
void dbg(Dp d) {
cerr << '(';
dbg(d.c0);
cerr << ", ";
dbg(d.c1);
cerr << ')';
}
Dp merge_2(Dp l, Dp r) {
return Dp{l.c0 + r.c0, l.c1 + r.c1};
}
void toggle(Dp &d) {
swap(d.c0, d.c1);
}
i32 ceil_pow2(i32 n) {
i32 k = 1;
while (k < n) {
k *= 2;
}
return k;
}
class LazySeg {
i32 n;
V<Dp> val;
V<i32> rev;
void push(i32 cur) {
if (rev[cur]) {
rev[cur] = 0;
if (cur < n) {
toggle(val[2 * cur]);
toggle(val[2 * cur + 1]);
rev[2 * cur] ^= 1;
rev[2 * cur + 1] ^= 1;
}
}
}
void _apply(i32 cur, i32 curl, i32 curr, i32 l, i32 r) {
if (curr <= l || r <= curl) {
return;
}
push(cur);
if (l <= curl && curr <= r) {
toggle(val[cur]);
rev[cur] ^= 1;
return;
}
i32 curm = (curl + curr) / 2;
_apply(2 * cur, curl, curm, l, r);
_apply(2 * cur + 1, curm, curr, l, r);
val[cur] = merge_2(val[2 * cur], val[2 * cur + 1]);
}
public:
LazySeg() = default;
LazySeg(V<M> wt, V<i32> a) : n(ceil_pow2(LEN(wt))), val(2 * n), rev(2 * n, 0) {
REP(i, LEN(wt)) {
if (a[i] == 0) {
val[n + i].c0 = wt[i];
} else {
val[n + i].c1 = wt[i];
}
}
PER(i, 1, n) {
val[i] = merge_2(val[2 * i], val[2 * i + 1]);
}
DBG(val);
}
void apply(i32 l, i32 r) {
_apply(1, 0, n, l, r);
}
Dp get_all() const {
return val[1];
}
};
i32 n, m;
V<i32> p, a;
VV<i32> chl;
LazySeg seg;
void init(i32 _n, i32 _m, V<i32> _p, V<i32> _a) {
n = _n;
m = _m;
p = _p;
a = _a;
chl.resize(n);
REP(i, 1, n + m) { chl[p[i]].push_back(i); }
V<M> prods(n + m, M(1));
PER(i, n) {
for (i32 j : chl[i]) {
prods[i] *= prods[j];
}
prods[i] *= LEN(chl[i]);
}
DBG(prods);
V<M> wts(n + m, M(1));
REP(i, n) {
i32 c = LEN(chl[i]);
V<M> lp(c + 1, M(1)), rp(c + 1, M(1));
REP(j, c) {
lp[j + 1] = lp[j] * prods[chl[i][j]];
}
PER(j, c) {
rp[j] = rp[j + 1] * prods[chl[i][j]];
}
REP(j, c) {
wts[chl[i][j]] = wts[i] * lp[j] * rp[j + 1];
}
}
DBG(wts);
V<M> wt(m);
REP(i, m) {
wt[i] = wts[n + i];
}
DBG(wt);
seg = LazySeg(wt, a);
}
int count_ways(i32 l, i32 r) {
++r;
l -= n;
r -= n;
seg.apply(l, r);
return seg.get_all().c1.v;
}
# | 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... |