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>
using namespace std;
const int mod = 1000002022;
int n, m;
vector<int> E[200005];
int S[200005], C[200005];
void dfs1(int x) {
S[x] = max(1, int(E[x].size()));
for (int i : E[x]) {
dfs1(i);
S[x] = 1ll * S[x] * S[i] % mod;
}
}
void dfs2(int x) {
// printf("S[%d] = %d, C[%d] = %d\n", x, S[x], x, C[x]);
vector<int> R(E[x].size());
R.push_back(1);
for (int i = E[x].size(); i--; ) {
R[i] = 1ll * R[i + 1] * S[E[x][i]] % mod;
}
int L = 1;
for (int i = 0; i < int(E[x].size()); ++i) {
C[E[x][i]] = 1ll * L * R[i + 1] % mod * C[x] % mod;
dfs2(E[x][i]);
L = 1ll * L * S[E[x][i]] % mod;
}
}
int add(int x, int y) {
x += y;
if (x >= mod) return x - mod;
return x;
}
vector<int> A;
int seg[1 << 18], rev[1 << 18], lz[1 << 18];
void init(int i, int s, int e) {
if (s == e) {
seg[i] = 0;
rev[i] = C[s];
lz[i] = 0;
if (A[s - n]) swap(seg[i], rev[i]);
return;
}
int m = (s + e) / 2;
init(i + i, s, m);
init(i + i + 1, m + 1, e);
seg[i] = add(seg[i + i], seg[i + i + 1]);
rev[i] = add(rev[i + i], rev[i + i + 1]);
lz[i] = 0;
}
void update(int i, int s, int e, int x, int y) {
if (e < x || y < s) return;
if (x <= s && e <= y) {
swap(seg[i], rev[i]);
lz[i] ^= 1;
return;
}
int m = (s + e) / 2;
if (lz[i]) {
lz[i] = 0;
swap(seg[i + i], rev[i + i]);
lz[i + i] ^= 1;
swap(seg[i + i + 1], rev[i + i + 1]);
lz[i + i + 1] ^= 1;
}
update(i + i, s, m, x, y);
update(i + i + 1, m + 1, e, x, y);
seg[i] = add(seg[i + i], seg[i + i + 1]);
rev[i] = add(rev[i + i], rev[i + i + 1]);
}
void init(int N, int M, vector<int> P, vector<int> A) {
n = N;
m = M;
for (int i = 1; i < n + m; ++i) E[P[i]].push_back(i);
dfs1(0);
C[0] = 1;
dfs2(0);
::A = A;
init(1, n, n + m - 1);
}
int count_ways(int L, int R) {
update(1, n, n + m - 1, L, R);
return seg[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... |