#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using ll = long long;
using vll = vector<ll>;
const int MOD = 1e9 + 2022;
vi W, A;
int n0;
struct AINT {
int n;
vi S0, S1, Lz;
AINT() {}
AINT(int N) : n(N), S0(4 * N, 0), S1(4 * N, 0), Lz(4 * N, 0) {}
void update(int p, int v0, int v1, int u, int s, int d) {
if(d < p || p < s) return;
if(s == d) {
S0[u] = v0;
S1[u] = v1;
return;
}
update(p, v0, v1, u << 1, s, (s + d) >> 1);
update(p, v0, v1, u << 1 | 1, ((s + d) >> 1) + 1, d);
S0[u] = S0[u << 1] + S0[u << 1 | 1];
S1[u] = S1[u << 1] + S1[u << 1 | 1];
}
void update(int p, int v0, int v1) {
update(p, v0, v1, 1, 0, n - 1);
}
void prop(int u, int s, int d) {
if(!Lz[u]) return;
swap(S0[u], S1[u]);
if(s != d) {
Lz[u << 1] ^= 1;
Lz[u << 1 | 1] ^= 1;
}
Lz[u] = 0;
}
void toggle(int l, int r, int u, int s, int d) {
prop(u, s, d);
if(r < s || d < l) return;
if(l <= s && d <= r) {
Lz[u] ^= 1;
prop(u, s, d);
return;
}
toggle(l, r, u << 1, s, (s + d) >> 1);
toggle(l, r, u << 1 | 1, ((s + d) >> 1) + 1, d);
S0[u] = S0[u << 1] + S0[u << 1 | 1];
S1[u] = S1[u << 1] + S1[u << 1 | 1];
}
void toggle(int l, int r) {
toggle(l, r, 1, 0, n - 1);
}
int query() { return S1[1]; }
};
AINT Sol;
void init(int n, int m, vi P, vi A0) {
n0 = n;
A = A0;
vector<vi> G(n + m);
vi NR(n + m, 0);
for(int i = 1; i < n + m; ++i) {
G[P[i]].push_back(i);
}
function<void(int)> dfs0 = [&](int u) {
NR[u] = 1;
if(u >= n) {
return;
}
for(auto it : G[u]) {
dfs0(it);
NR[u] = 1ll * NR[u] * NR[it] % MOD;
}
NR[u] = 1ll * NR[u] * G[u].size() % MOD;
};
dfs0(0);
W.resize(m, 0);
function<void(int, int)> dfs = [&](int u, int v) {
if(u >= n) {
W[u - n] = v;
return;
}
vi Pref, Suf;
for(auto it : G[u]) {
Pref.push_back(NR[it]);
Suf.push_back(NR[it]);
}
int nr = Pref.size();
for(int i = 1; i < nr; ++i)
Pref[i] = 1ll * Pref[i - 1] * Pref[i] % MOD;
for(int i = nr - 2; i >= 0; --i)
Suf[i] = 1ll * Suf[i + 1] * Suf[i] % MOD;
for(int i = 0; i < nr; ++i) {
ll f = 1;
if(i) f *= Pref[i - 1];
if(i + 1 < nr) f *= Suf[i + 1];
f %= MOD;
dfs(G[u][i], f * v % MOD);
}
};
dfs(0, 1);
Sol = AINT(m);
for(int i = 0; i < m; ++i) {
if(A[i]) Sol.update(i, 0, W[i]);
else Sol.update(i, W[i], 0);
}
}
int count_ways(int l, int r) {
l -= n0; r -= n0;
Sol.toggle(l, r);
return Sol.query();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
344 KB |
1st lines differ - on the 1st token, expected: '52130940', found: '-1782334720' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Incorrect |
1 ms |
344 KB |
1st lines differ - on the 1st token, expected: '52130940', found: '-1782334720' |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
318 ms |
5700 KB |
1st lines differ - on the 1st token, expected: '431985922', found: '-747097920' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
318 ms |
5700 KB |
1st lines differ - on the 1st token, expected: '431985922', found: '-747097920' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
344 KB |
1st lines differ - on the 1st token, expected: '52130940', found: '-1782334720' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Incorrect |
1 ms |
344 KB |
1st lines differ - on the 1st token, expected: '52130940', found: '-1782334720' |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Incorrect |
1 ms |
344 KB |
1st lines differ - on the 1st token, expected: '52130940', found: '-1782334720' |
11 |
Halted |
0 ms |
0 KB |
- |