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>
#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 (stderr)
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;
| ^~~~~~
# | 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... |