#include <bits/stdc++.h>
#ifndef LOCAL
#include "circuit.h"
#endif // LOCAL
using namespace std;
#define rep(i, l, r) for(int i = (l), _r = (r); i < _r; ++i)
#define FOR(i, l, r) for(int i = (l), _r = (r); i <= _r; ++i)
#define ROF(i, r, l) for(int i = (r), _l = (l); i >= _l; --i)
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define sz(v) (int)v.size()
#define dbg(x) "[" #x " = " << (x) << "]"
#define file(name) if(fopen(name".inp", "r")) freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
template<typename T>
bool minimize(T& a, const T& b){
if(a > b) return a = b, true; return false;
}
template<typename T>
bool maximize(T& a, const T& b){
if(a < b) return a = b, true; return false;
}
using ll = long long;
using ld = long double;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T> T random_int(T l, T r){ return uniform_int_distribution<T>(l, r)(rng); }
template<typename T> T random_real(T l, T r){ return uniform_real_distribution<T>(l, r)(rng); }
const int MAX = 2e5 + 5;
const int mod = 1e9 + 2022;
int n, m;
vector<int> adj[MAX];
bool state[MAX];
int dp[MAX][2], f[MAX];
int add(int a, int b){
return a + b < mod ? a + b : a + b - mod;
}
int multiply(int a, int b){
return 1LL * a * b % mod;
}
bool isLeaf(int u){
return n <= u;
}
void dfs(int u){
if(isLeaf(u)){
dp[u][state[u - n]] = 1;
dp[u][!state[u - n]] = 0;
return;
}
for(int v : adj[u]){
dfs(v);
}
int k = sz(adj[u]);
fill(f, f + k + 1, 0);
f[0] = 1;
dp[u][0] = 0; dp[u][1] = 0;
for(int v : adj[u]){
for(int i = k; i >= 0; --i){
f[i] = multiply(f[i], dp[v][0]);
if(i > 0) f[i] = add(f[i], multiply(f[i - 1], dp[v][1]));
}
}
for(int i = 1, s = f[0]; i <= k; ++i){
dp[u][0] = add(dp[u][0], s);
s = add(s, f[i]);
}
for(int i = k, s = 0; i >= 1; --i){
s = add(s, f[i]);
dp[u][1] = add(dp[u][1], s);
}
}
void init(int N, int M, vector<int> P, vector<int> A){
n = N;
m = M;
rep(i, 1, n + m){
adj[P[i]].push_back(i);
}
rep(i, 0, m){
state[i] = A[i];
}
}
int count_ways(int L, int R){
L -= n; R -= n;
FOR(i, L, R) state[i] ^= 1;
dfs(0);
return dp[0][1];
}
#ifdef LOCAL
int main(){
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
auto random = [&](int l, int r){
return uniform_int_distribution<int>(l, r)(rng);
};
for(int itest = 1; itest <= 1; ++itest){
//example testcase
int N = 3, M = 4;
vector<int> P(N + M);
P = {-1, 0, 1, 2, 1, 1, 0};
vector<int> A(M);
A = {1, 0, 1, 0};
init(N, M, P, A);
cout << count_ways(3, 4) << '\n'; //should be 2
cout << count_ways(4, 5) << '\n'; //should be 0
cout << count_ways(3, 6) << '\n'; //should 6
}
return 0;
}
#endif //LOCAL
Compilation message
circuit.cpp: In function 'bool minimize(T&, const T&)':
circuit.cpp:20:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
20 | if(a > b) return a = b, true; return false;
| ^~
circuit.cpp:20:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
20 | if(a > b) return a = b, true; return false;
| ^~~~~~
circuit.cpp: In function 'bool maximize(T&, const T&)':
circuit.cpp:25:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
25 | if(a < b) return a = b, true; return false;
| ^~
circuit.cpp:25:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
25 | if(a < b) return a = b, true; return false;
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7504 KB |
Output is correct |
2 |
Correct |
3 ms |
7504 KB |
Output is correct |
3 |
Correct |
19 ms |
7504 KB |
Output is correct |
4 |
Correct |
20 ms |
7676 KB |
Output is correct |
5 |
Correct |
20 ms |
7504 KB |
Output is correct |
6 |
Correct |
20 ms |
7504 KB |
Output is correct |
7 |
Correct |
20 ms |
7672 KB |
Output is correct |
8 |
Correct |
21 ms |
7504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7504 KB |
Output is correct |
2 |
Correct |
2 ms |
7504 KB |
Output is correct |
3 |
Correct |
3 ms |
7676 KB |
Output is correct |
4 |
Correct |
3 ms |
7504 KB |
Output is correct |
5 |
Correct |
3 ms |
7504 KB |
Output is correct |
6 |
Correct |
3 ms |
7504 KB |
Output is correct |
7 |
Correct |
3 ms |
7504 KB |
Output is correct |
8 |
Correct |
4 ms |
7504 KB |
Output is correct |
9 |
Correct |
3 ms |
7504 KB |
Output is correct |
10 |
Correct |
3 ms |
7504 KB |
Output is correct |
11 |
Correct |
2 ms |
7504 KB |
Output is correct |
12 |
Correct |
3 ms |
7504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7504 KB |
Output is correct |
2 |
Correct |
3 ms |
7504 KB |
Output is correct |
3 |
Correct |
19 ms |
7504 KB |
Output is correct |
4 |
Correct |
20 ms |
7676 KB |
Output is correct |
5 |
Correct |
20 ms |
7504 KB |
Output is correct |
6 |
Correct |
20 ms |
7504 KB |
Output is correct |
7 |
Correct |
20 ms |
7672 KB |
Output is correct |
8 |
Correct |
21 ms |
7504 KB |
Output is correct |
9 |
Correct |
3 ms |
7504 KB |
Output is correct |
10 |
Correct |
2 ms |
7504 KB |
Output is correct |
11 |
Correct |
3 ms |
7676 KB |
Output is correct |
12 |
Correct |
3 ms |
7504 KB |
Output is correct |
13 |
Correct |
3 ms |
7504 KB |
Output is correct |
14 |
Correct |
3 ms |
7504 KB |
Output is correct |
15 |
Correct |
3 ms |
7504 KB |
Output is correct |
16 |
Correct |
4 ms |
7504 KB |
Output is correct |
17 |
Correct |
3 ms |
7504 KB |
Output is correct |
18 |
Correct |
3 ms |
7504 KB |
Output is correct |
19 |
Correct |
2 ms |
7504 KB |
Output is correct |
20 |
Correct |
3 ms |
7504 KB |
Output is correct |
21 |
Correct |
2 ms |
7504 KB |
Output is correct |
22 |
Correct |
2 ms |
7504 KB |
Output is correct |
23 |
Correct |
2 ms |
7504 KB |
Output is correct |
24 |
Correct |
4 ms |
7504 KB |
Output is correct |
25 |
Correct |
3 ms |
7504 KB |
Output is correct |
26 |
Correct |
4 ms |
7504 KB |
Output is correct |
27 |
Correct |
3 ms |
7504 KB |
Output is correct |
28 |
Correct |
3 ms |
7504 KB |
Output is correct |
29 |
Correct |
19 ms |
7504 KB |
Output is correct |
30 |
Correct |
20 ms |
7504 KB |
Output is correct |
31 |
Correct |
3 ms |
7504 KB |
Output is correct |
32 |
Correct |
3 ms |
7504 KB |
Output is correct |
33 |
Correct |
3 ms |
7504 KB |
Output is correct |
34 |
Correct |
3 ms |
7504 KB |
Output is correct |
35 |
Correct |
5 ms |
7504 KB |
Output is correct |
36 |
Correct |
2 ms |
7504 KB |
Output is correct |
37 |
Correct |
28 ms |
7648 KB |
Output is correct |
38 |
Correct |
21 ms |
7504 KB |
Output is correct |
39 |
Correct |
2 ms |
7504 KB |
Output is correct |
40 |
Correct |
3 ms |
7504 KB |
Output is correct |
41 |
Correct |
3 ms |
7504 KB |
Output is correct |
42 |
Correct |
3 ms |
7504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3030 ms |
8904 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3030 ms |
8904 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7504 KB |
Output is correct |
2 |
Correct |
2 ms |
7504 KB |
Output is correct |
3 |
Correct |
3 ms |
7676 KB |
Output is correct |
4 |
Correct |
3 ms |
7504 KB |
Output is correct |
5 |
Correct |
3 ms |
7504 KB |
Output is correct |
6 |
Correct |
3 ms |
7504 KB |
Output is correct |
7 |
Correct |
3 ms |
7504 KB |
Output is correct |
8 |
Correct |
4 ms |
7504 KB |
Output is correct |
9 |
Correct |
3 ms |
7504 KB |
Output is correct |
10 |
Correct |
3 ms |
7504 KB |
Output is correct |
11 |
Correct |
2 ms |
7504 KB |
Output is correct |
12 |
Correct |
3 ms |
7504 KB |
Output is correct |
13 |
Execution timed out |
3030 ms |
8904 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7504 KB |
Output is correct |
2 |
Correct |
3 ms |
7504 KB |
Output is correct |
3 |
Correct |
19 ms |
7504 KB |
Output is correct |
4 |
Correct |
20 ms |
7676 KB |
Output is correct |
5 |
Correct |
20 ms |
7504 KB |
Output is correct |
6 |
Correct |
20 ms |
7504 KB |
Output is correct |
7 |
Correct |
20 ms |
7672 KB |
Output is correct |
8 |
Correct |
21 ms |
7504 KB |
Output is correct |
9 |
Correct |
3 ms |
7504 KB |
Output is correct |
10 |
Correct |
2 ms |
7504 KB |
Output is correct |
11 |
Correct |
3 ms |
7676 KB |
Output is correct |
12 |
Correct |
3 ms |
7504 KB |
Output is correct |
13 |
Correct |
3 ms |
7504 KB |
Output is correct |
14 |
Correct |
3 ms |
7504 KB |
Output is correct |
15 |
Correct |
3 ms |
7504 KB |
Output is correct |
16 |
Correct |
4 ms |
7504 KB |
Output is correct |
17 |
Correct |
3 ms |
7504 KB |
Output is correct |
18 |
Correct |
3 ms |
7504 KB |
Output is correct |
19 |
Correct |
2 ms |
7504 KB |
Output is correct |
20 |
Correct |
3 ms |
7504 KB |
Output is correct |
21 |
Correct |
2 ms |
7504 KB |
Output is correct |
22 |
Correct |
2 ms |
7504 KB |
Output is correct |
23 |
Correct |
2 ms |
7504 KB |
Output is correct |
24 |
Correct |
4 ms |
7504 KB |
Output is correct |
25 |
Correct |
3 ms |
7504 KB |
Output is correct |
26 |
Correct |
4 ms |
7504 KB |
Output is correct |
27 |
Correct |
3 ms |
7504 KB |
Output is correct |
28 |
Correct |
3 ms |
7504 KB |
Output is correct |
29 |
Correct |
19 ms |
7504 KB |
Output is correct |
30 |
Correct |
20 ms |
7504 KB |
Output is correct |
31 |
Correct |
3 ms |
7504 KB |
Output is correct |
32 |
Correct |
3 ms |
7504 KB |
Output is correct |
33 |
Correct |
3 ms |
7504 KB |
Output is correct |
34 |
Correct |
3 ms |
7504 KB |
Output is correct |
35 |
Correct |
5 ms |
7504 KB |
Output is correct |
36 |
Correct |
2 ms |
7504 KB |
Output is correct |
37 |
Correct |
28 ms |
7648 KB |
Output is correct |
38 |
Correct |
21 ms |
7504 KB |
Output is correct |
39 |
Correct |
2 ms |
7504 KB |
Output is correct |
40 |
Correct |
3 ms |
7504 KB |
Output is correct |
41 |
Correct |
3 ms |
7504 KB |
Output is correct |
42 |
Correct |
3 ms |
7504 KB |
Output is correct |
43 |
Execution timed out |
3023 ms |
7504 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7504 KB |
Output is correct |
2 |
Correct |
3 ms |
7504 KB |
Output is correct |
3 |
Correct |
19 ms |
7504 KB |
Output is correct |
4 |
Correct |
20 ms |
7676 KB |
Output is correct |
5 |
Correct |
20 ms |
7504 KB |
Output is correct |
6 |
Correct |
20 ms |
7504 KB |
Output is correct |
7 |
Correct |
20 ms |
7672 KB |
Output is correct |
8 |
Correct |
21 ms |
7504 KB |
Output is correct |
9 |
Correct |
3 ms |
7504 KB |
Output is correct |
10 |
Correct |
2 ms |
7504 KB |
Output is correct |
11 |
Correct |
3 ms |
7676 KB |
Output is correct |
12 |
Correct |
3 ms |
7504 KB |
Output is correct |
13 |
Correct |
3 ms |
7504 KB |
Output is correct |
14 |
Correct |
3 ms |
7504 KB |
Output is correct |
15 |
Correct |
3 ms |
7504 KB |
Output is correct |
16 |
Correct |
4 ms |
7504 KB |
Output is correct |
17 |
Correct |
3 ms |
7504 KB |
Output is correct |
18 |
Correct |
3 ms |
7504 KB |
Output is correct |
19 |
Correct |
2 ms |
7504 KB |
Output is correct |
20 |
Correct |
3 ms |
7504 KB |
Output is correct |
21 |
Correct |
2 ms |
7504 KB |
Output is correct |
22 |
Correct |
2 ms |
7504 KB |
Output is correct |
23 |
Correct |
2 ms |
7504 KB |
Output is correct |
24 |
Correct |
4 ms |
7504 KB |
Output is correct |
25 |
Correct |
3 ms |
7504 KB |
Output is correct |
26 |
Correct |
4 ms |
7504 KB |
Output is correct |
27 |
Correct |
3 ms |
7504 KB |
Output is correct |
28 |
Correct |
3 ms |
7504 KB |
Output is correct |
29 |
Correct |
19 ms |
7504 KB |
Output is correct |
30 |
Correct |
20 ms |
7504 KB |
Output is correct |
31 |
Correct |
3 ms |
7504 KB |
Output is correct |
32 |
Correct |
3 ms |
7504 KB |
Output is correct |
33 |
Correct |
3 ms |
7504 KB |
Output is correct |
34 |
Correct |
3 ms |
7504 KB |
Output is correct |
35 |
Correct |
5 ms |
7504 KB |
Output is correct |
36 |
Correct |
2 ms |
7504 KB |
Output is correct |
37 |
Correct |
28 ms |
7648 KB |
Output is correct |
38 |
Correct |
21 ms |
7504 KB |
Output is correct |
39 |
Correct |
2 ms |
7504 KB |
Output is correct |
40 |
Correct |
3 ms |
7504 KB |
Output is correct |
41 |
Correct |
3 ms |
7504 KB |
Output is correct |
42 |
Correct |
3 ms |
7504 KB |
Output is correct |
43 |
Execution timed out |
3030 ms |
8904 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |