#include <bits/stdc++.h>
using namespace std;
#define ll long long
constexpr ll MOD = 1000002022;
constexpr int MAXN = 2e5 + 5;
vector<int> adj[MAXN];
pair<ll, ll> dp[MAXN]; // attivo, spento
bool lazy[MAXN]; // attivo, spento
int N, M;
vector<int> A;
vector<int> P;
int PW;
bool is_seg = 0;
vector<ll> mult(vector<ll> &a, vector<ll> &b) { // b size è il minore ed è sempre 2
int dim = (a.size() - 1) + (b.size() - 1) + 1;
vector<ll> res(dim);
for (int i = 0; i < a.size(); i ++) {
for (int j = 0; j < b.size(); j ++) {
int sum = (i + j < dim) ? i + j : i + j - dim;
res[sum] += (a[i] * b[j]) % MOD;
res[sum] %= MOD;
}
}
return res;
}
void dfs(int u) {
if (u >= N) return;
vector<ll> poly;
poly.push_back(1);
for (auto x : adj[u]) {
dfs(x);
auto [acceso, spento] = dp[x];
vector<ll> curr_poly = {spento, acceso};
poly = mult(poly, curr_poly);
}
ll acceso = 0;
ll spento = 0;
for (int i = 0; i < poly.size(); i ++) {
acceso += (poly[i] * i) % MOD;
acceso %= MOD;
spento += (poly[i] * (poly.size() - 1 - i)) % MOD;
spento %= MOD;
}
dp[u] = {acceso, spento};
}
pair<ll, ll> merge(int a, int b) {
vector<ll> poly_a = {dp[a].second, dp[a].first};
vector<ll> poly_b = {dp[b].second, dp[b].first};
vector<ll> poly = mult(poly_a, poly_b);
ll acceso = 0;
ll spento = 0;
for (int i = 0; i < poly.size(); i ++) {
acceso += (poly[i] * i) % MOD;
acceso %= MOD;
spento += (poly[i] * (poly.size() - 1 - i)) % MOD;
spento %= MOD;
}
return {acceso, spento};
}
void init(int N, int M, vector<int> P, vector<int> A) {
:: N = N;
:: M = M;
:: A = A;
:: P = P;
:: PW = M;
for (int i = 1; i < N + M; i ++) {
adj[P[i]].push_back(i);
}
if (M == N + 1 && (M & (M - 1)) == 0) is_seg = 1;
if (is_seg) {
for (int i = 0; i < PW; i ++) dp[PW + i] = {A[i], A[i] ^ 1};
for (int i = PW - 1; i >= 1; i --) dp[i] = merge(2 * i, 2 * i + 1);
fill(lazy, lazy + MAXN, 0);
}
}
void push_lazy(int idx) {
if (lazy[idx] == 0) return;
lazy[idx] = 0;
auto [acceso, spento] = dp[idx];
dp[idx] = {spento, acceso};
if (idx >= PW) return;
lazy[2 * idx] ^= 1;
lazy[2 * idx + 1] ^= 1;
}
void update(int idx, int l, int r, int ql, int qr) {
// cout << "idx, l, r, ql, qr : " << idx << " " << l << " " << r << " " << ql << " " << qr << '\n';
push_lazy(idx);
if (ql <= l && r <= qr) {
lazy[idx] = 1;
push_lazy(idx);
return;
}
if (r <= ql || qr <= l) return;
push_lazy(idx);
update(2 * idx , l, (l + r) / 2, ql, qr);
update(2 * idx + 1, (l + r) / 2, r, ql, qr);
dp[idx] = merge(2 * idx, 2 * idx + 1);
}
int count_ways(int L, int R) {
if (!is_seg) {
for (int i = L - N; i <= R - N; i ++) {
A[i] ^= 1;
}
for (int i = 0; i < M; i ++) {
dp[N + i] = {A[i], A[i] ^ 1};
}
dfs(0);
return dp[0].first;
}
else {
update(1, 0, PW, L - N, R + 1 - N);
return dp[1].first;
}
}
Compilation message
circuit.cpp: In function 'std::vector<long long int> mult(std::vector<long long int>&, std::vector<long long int>&)':
circuit.cpp:21:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int i = 0; i < a.size(); i ++) {
| ~~^~~~~~~~~~
circuit.cpp:22:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int j = 0; j < b.size(); j ++) {
| ~~^~~~~~~~~~
circuit.cpp: In function 'void dfs(int)':
circuit.cpp:47:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int i = 0; i < poly.size(); i ++) {
| ~~^~~~~~~~~~~~~
circuit.cpp: In function 'std::pair<long long int, long long int> merge(int, int)':
circuit.cpp:65:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for (int i = 0; i < poly.size(); i ++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
19 ms |
5208 KB |
Output is correct |
4 |
Correct |
20 ms |
5208 KB |
Output is correct |
5 |
Correct |
20 ms |
5208 KB |
Output is correct |
6 |
Correct |
19 ms |
5152 KB |
Output is correct |
7 |
Correct |
20 ms |
5208 KB |
Output is correct |
8 |
Correct |
20 ms |
5268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
5208 KB |
Output is correct |
3 |
Correct |
2 ms |
5208 KB |
Output is correct |
4 |
Correct |
2 ms |
5208 KB |
Output is correct |
5 |
Correct |
2 ms |
5208 KB |
Output is correct |
6 |
Correct |
3 ms |
5208 KB |
Output is correct |
7 |
Correct |
3 ms |
5208 KB |
Output is correct |
8 |
Correct |
3 ms |
5208 KB |
Output is correct |
9 |
Correct |
3 ms |
5208 KB |
Output is correct |
10 |
Correct |
3 ms |
5208 KB |
Output is correct |
11 |
Correct |
3 ms |
5208 KB |
Output is correct |
12 |
Correct |
3 ms |
5208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
19 ms |
5208 KB |
Output is correct |
4 |
Correct |
20 ms |
5208 KB |
Output is correct |
5 |
Correct |
20 ms |
5208 KB |
Output is correct |
6 |
Correct |
19 ms |
5152 KB |
Output is correct |
7 |
Correct |
20 ms |
5208 KB |
Output is correct |
8 |
Correct |
20 ms |
5268 KB |
Output is correct |
9 |
Correct |
2 ms |
5208 KB |
Output is correct |
10 |
Correct |
2 ms |
5208 KB |
Output is correct |
11 |
Correct |
2 ms |
5208 KB |
Output is correct |
12 |
Correct |
2 ms |
5208 KB |
Output is correct |
13 |
Correct |
2 ms |
5208 KB |
Output is correct |
14 |
Correct |
3 ms |
5208 KB |
Output is correct |
15 |
Correct |
3 ms |
5208 KB |
Output is correct |
16 |
Correct |
3 ms |
5208 KB |
Output is correct |
17 |
Correct |
3 ms |
5208 KB |
Output is correct |
18 |
Correct |
3 ms |
5208 KB |
Output is correct |
19 |
Correct |
3 ms |
5208 KB |
Output is correct |
20 |
Correct |
3 ms |
5208 KB |
Output is correct |
21 |
Correct |
3 ms |
5208 KB |
Output is correct |
22 |
Correct |
3 ms |
5208 KB |
Output is correct |
23 |
Correct |
3 ms |
4952 KB |
Output is correct |
24 |
Correct |
4 ms |
5188 KB |
Output is correct |
25 |
Correct |
3 ms |
5208 KB |
Output is correct |
26 |
Correct |
3 ms |
5208 KB |
Output is correct |
27 |
Correct |
3 ms |
5208 KB |
Output is correct |
28 |
Correct |
4 ms |
5208 KB |
Output is correct |
29 |
Correct |
20 ms |
5208 KB |
Output is correct |
30 |
Correct |
23 ms |
5208 KB |
Output is correct |
31 |
Correct |
2 ms |
5208 KB |
Output is correct |
32 |
Correct |
3 ms |
5208 KB |
Output is correct |
33 |
Correct |
3 ms |
5208 KB |
Output is correct |
34 |
Correct |
3 ms |
4952 KB |
Output is correct |
35 |
Correct |
6 ms |
5208 KB |
Output is correct |
36 |
Correct |
3 ms |
5208 KB |
Output is correct |
37 |
Correct |
20 ms |
5464 KB |
Output is correct |
38 |
Correct |
20 ms |
5464 KB |
Output is correct |
39 |
Correct |
4 ms |
5208 KB |
Output is correct |
40 |
Correct |
3 ms |
4952 KB |
Output is correct |
41 |
Correct |
3 ms |
4952 KB |
Output is correct |
42 |
Correct |
3 ms |
4952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
495 ms |
8492 KB |
Output is correct |
2 |
Correct |
841 ms |
11608 KB |
Output is correct |
3 |
Correct |
778 ms |
11600 KB |
Output is correct |
4 |
Correct |
806 ms |
11600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
495 ms |
8492 KB |
Output is correct |
2 |
Correct |
841 ms |
11608 KB |
Output is correct |
3 |
Correct |
778 ms |
11600 KB |
Output is correct |
4 |
Correct |
806 ms |
11600 KB |
Output is correct |
5 |
Correct |
709 ms |
8536 KB |
Output is correct |
6 |
Correct |
938 ms |
11608 KB |
Output is correct |
7 |
Correct |
887 ms |
11608 KB |
Output is correct |
8 |
Correct |
855 ms |
11608 KB |
Output is correct |
9 |
Correct |
441 ms |
5464 KB |
Output is correct |
10 |
Correct |
813 ms |
5720 KB |
Output is correct |
11 |
Correct |
819 ms |
5720 KB |
Output is correct |
12 |
Correct |
763 ms |
5720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
5208 KB |
Output is correct |
3 |
Correct |
2 ms |
5208 KB |
Output is correct |
4 |
Correct |
2 ms |
5208 KB |
Output is correct |
5 |
Correct |
2 ms |
5208 KB |
Output is correct |
6 |
Correct |
3 ms |
5208 KB |
Output is correct |
7 |
Correct |
3 ms |
5208 KB |
Output is correct |
8 |
Correct |
3 ms |
5208 KB |
Output is correct |
9 |
Correct |
3 ms |
5208 KB |
Output is correct |
10 |
Correct |
3 ms |
5208 KB |
Output is correct |
11 |
Correct |
3 ms |
5208 KB |
Output is correct |
12 |
Correct |
3 ms |
5208 KB |
Output is correct |
13 |
Correct |
495 ms |
8492 KB |
Output is correct |
14 |
Correct |
841 ms |
11608 KB |
Output is correct |
15 |
Correct |
778 ms |
11600 KB |
Output is correct |
16 |
Correct |
806 ms |
11600 KB |
Output is correct |
17 |
Correct |
709 ms |
8536 KB |
Output is correct |
18 |
Correct |
938 ms |
11608 KB |
Output is correct |
19 |
Correct |
887 ms |
11608 KB |
Output is correct |
20 |
Correct |
855 ms |
11608 KB |
Output is correct |
21 |
Correct |
441 ms |
5464 KB |
Output is correct |
22 |
Correct |
813 ms |
5720 KB |
Output is correct |
23 |
Correct |
819 ms |
5720 KB |
Output is correct |
24 |
Correct |
763 ms |
5720 KB |
Output is correct |
25 |
Execution timed out |
3018 ms |
13580 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
19 ms |
5208 KB |
Output is correct |
4 |
Correct |
20 ms |
5208 KB |
Output is correct |
5 |
Correct |
20 ms |
5208 KB |
Output is correct |
6 |
Correct |
19 ms |
5152 KB |
Output is correct |
7 |
Correct |
20 ms |
5208 KB |
Output is correct |
8 |
Correct |
20 ms |
5268 KB |
Output is correct |
9 |
Correct |
2 ms |
5208 KB |
Output is correct |
10 |
Correct |
2 ms |
5208 KB |
Output is correct |
11 |
Correct |
2 ms |
5208 KB |
Output is correct |
12 |
Correct |
2 ms |
5208 KB |
Output is correct |
13 |
Correct |
2 ms |
5208 KB |
Output is correct |
14 |
Correct |
3 ms |
5208 KB |
Output is correct |
15 |
Correct |
3 ms |
5208 KB |
Output is correct |
16 |
Correct |
3 ms |
5208 KB |
Output is correct |
17 |
Correct |
3 ms |
5208 KB |
Output is correct |
18 |
Correct |
3 ms |
5208 KB |
Output is correct |
19 |
Correct |
3 ms |
5208 KB |
Output is correct |
20 |
Correct |
3 ms |
5208 KB |
Output is correct |
21 |
Correct |
3 ms |
5208 KB |
Output is correct |
22 |
Correct |
3 ms |
5208 KB |
Output is correct |
23 |
Correct |
3 ms |
4952 KB |
Output is correct |
24 |
Correct |
4 ms |
5188 KB |
Output is correct |
25 |
Correct |
3 ms |
5208 KB |
Output is correct |
26 |
Correct |
3 ms |
5208 KB |
Output is correct |
27 |
Correct |
3 ms |
5208 KB |
Output is correct |
28 |
Correct |
4 ms |
5208 KB |
Output is correct |
29 |
Correct |
20 ms |
5208 KB |
Output is correct |
30 |
Correct |
23 ms |
5208 KB |
Output is correct |
31 |
Correct |
2 ms |
5208 KB |
Output is correct |
32 |
Correct |
3 ms |
5208 KB |
Output is correct |
33 |
Correct |
3 ms |
5208 KB |
Output is correct |
34 |
Correct |
3 ms |
4952 KB |
Output is correct |
35 |
Correct |
6 ms |
5208 KB |
Output is correct |
36 |
Correct |
3 ms |
5208 KB |
Output is correct |
37 |
Correct |
20 ms |
5464 KB |
Output is correct |
38 |
Correct |
20 ms |
5464 KB |
Output is correct |
39 |
Correct |
4 ms |
5208 KB |
Output is correct |
40 |
Correct |
3 ms |
4952 KB |
Output is correct |
41 |
Correct |
3 ms |
4952 KB |
Output is correct |
42 |
Correct |
3 ms |
4952 KB |
Output is correct |
43 |
Execution timed out |
3002 ms |
5208 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
19 ms |
5208 KB |
Output is correct |
4 |
Correct |
20 ms |
5208 KB |
Output is correct |
5 |
Correct |
20 ms |
5208 KB |
Output is correct |
6 |
Correct |
19 ms |
5152 KB |
Output is correct |
7 |
Correct |
20 ms |
5208 KB |
Output is correct |
8 |
Correct |
20 ms |
5268 KB |
Output is correct |
9 |
Correct |
2 ms |
5208 KB |
Output is correct |
10 |
Correct |
2 ms |
5208 KB |
Output is correct |
11 |
Correct |
2 ms |
5208 KB |
Output is correct |
12 |
Correct |
2 ms |
5208 KB |
Output is correct |
13 |
Correct |
2 ms |
5208 KB |
Output is correct |
14 |
Correct |
3 ms |
5208 KB |
Output is correct |
15 |
Correct |
3 ms |
5208 KB |
Output is correct |
16 |
Correct |
3 ms |
5208 KB |
Output is correct |
17 |
Correct |
3 ms |
5208 KB |
Output is correct |
18 |
Correct |
3 ms |
5208 KB |
Output is correct |
19 |
Correct |
3 ms |
5208 KB |
Output is correct |
20 |
Correct |
3 ms |
5208 KB |
Output is correct |
21 |
Correct |
3 ms |
5208 KB |
Output is correct |
22 |
Correct |
3 ms |
5208 KB |
Output is correct |
23 |
Correct |
3 ms |
4952 KB |
Output is correct |
24 |
Correct |
4 ms |
5188 KB |
Output is correct |
25 |
Correct |
3 ms |
5208 KB |
Output is correct |
26 |
Correct |
3 ms |
5208 KB |
Output is correct |
27 |
Correct |
3 ms |
5208 KB |
Output is correct |
28 |
Correct |
4 ms |
5208 KB |
Output is correct |
29 |
Correct |
20 ms |
5208 KB |
Output is correct |
30 |
Correct |
23 ms |
5208 KB |
Output is correct |
31 |
Correct |
2 ms |
5208 KB |
Output is correct |
32 |
Correct |
3 ms |
5208 KB |
Output is correct |
33 |
Correct |
3 ms |
5208 KB |
Output is correct |
34 |
Correct |
3 ms |
4952 KB |
Output is correct |
35 |
Correct |
6 ms |
5208 KB |
Output is correct |
36 |
Correct |
3 ms |
5208 KB |
Output is correct |
37 |
Correct |
20 ms |
5464 KB |
Output is correct |
38 |
Correct |
20 ms |
5464 KB |
Output is correct |
39 |
Correct |
4 ms |
5208 KB |
Output is correct |
40 |
Correct |
3 ms |
4952 KB |
Output is correct |
41 |
Correct |
3 ms |
4952 KB |
Output is correct |
42 |
Correct |
3 ms |
4952 KB |
Output is correct |
43 |
Correct |
495 ms |
8492 KB |
Output is correct |
44 |
Correct |
841 ms |
11608 KB |
Output is correct |
45 |
Correct |
778 ms |
11600 KB |
Output is correct |
46 |
Correct |
806 ms |
11600 KB |
Output is correct |
47 |
Correct |
709 ms |
8536 KB |
Output is correct |
48 |
Correct |
938 ms |
11608 KB |
Output is correct |
49 |
Correct |
887 ms |
11608 KB |
Output is correct |
50 |
Correct |
855 ms |
11608 KB |
Output is correct |
51 |
Correct |
441 ms |
5464 KB |
Output is correct |
52 |
Correct |
813 ms |
5720 KB |
Output is correct |
53 |
Correct |
819 ms |
5720 KB |
Output is correct |
54 |
Correct |
763 ms |
5720 KB |
Output is correct |
55 |
Execution timed out |
3018 ms |
13580 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |