#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define all(a) (a).begin(), (a).end()
#define ll long long
const int mod = 1000002022;
vector<int> state, p;
int n, m;
vector<vector<int>> g;
template<typename T>
int len(T &a){return a.size();}
vector<array<ll, 2>> dp;
void build(int i){
if(i >= n){
dp[i][0] = state[i - n] ^ 1;
dp[i][1] = state[i - n];
return;
}
for(auto u : g[i]) build(u);
vector<ll> prep(len(g[i]) + 1);
prep[0] = 1;
ll al = len(g[i]);
int cur = 0;
for(auto u : g[i]){
cur ++;
al = al * (dp[u][0] + dp[u][1]) % mod;
for(int j = cur; j >= 0; j --){
prep[j] = prep[j] * dp[u][0] % mod;
if(j) prep[j] += prep[j - 1] * dp[u][1] % mod;
}
}
dp[i][1] = 0;
for(int j = 1; j <= len(g[i]); j ++){
dp[i][1] = (dp[i][1] + prep[j] * j) % mod;
}
dp[i][0] = al - dp[i][1];
if(dp[i][0] < 0) dp[i][0] += mod;
}
void upd(int i, int l, int r, int ind){
if(l == r){
swap(dp[i][0], dp[i][1]);
return;
}
int m = (l + r) / 2;
if(ind <= m){
upd(g[i][0], l, m, ind);
}else upd(g[i][1], m + 1, r, ind);
vector<ll> prep(len(g[i]) + 1);
prep[0] = 1;
ll al = len(g[i]);
int cur = 0;
for(auto u : g[i]){
cur ++;
al = al * (dp[u][0] + dp[u][1]) % mod;
for(int j = cur; j >= 0; j --){
prep[j] = prep[j] * dp[u][0] % mod;
if(j) prep[j] += prep[j - 1] * dp[u][1] % mod;
}
}
dp[i][1] = 0;
for(int j = 1; j <= len(g[i]); j ++){
dp[i][1] = (dp[i][1] + prep[j] * j) % mod;
}
dp[i][0] = al - dp[i][1];
if(dp[i][0] < 0) dp[i][0] += mod;
}
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
n = N, m = M;
g.resize(n);
state = A;
p = P;
for(int i = 1; i < n + m; i ++){
g[p[i]].push_back(i);
}
dp.resize(n + m);
build(0);
}
int count_ways(int l, int r){
upd(0, n, n + m - 1, l);
return dp[0][1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
3rd lines differ - on the 1st token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
3rd lines differ - on the 1st token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
3rd lines differ - on the 1st token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
534 ms |
4184 KB |
Output is correct |
2 |
Correct |
685 ms |
8272 KB |
Output is correct |
3 |
Correct |
729 ms |
8280 KB |
Output is correct |
4 |
Correct |
749 ms |
8280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
534 ms |
4184 KB |
Output is correct |
2 |
Correct |
685 ms |
8272 KB |
Output is correct |
3 |
Correct |
729 ms |
8280 KB |
Output is correct |
4 |
Correct |
749 ms |
8280 KB |
Output is correct |
5 |
Incorrect |
560 ms |
4440 KB |
1st lines differ - on the 1st token, expected: '105182172', found: '2777846' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
3rd lines differ - on the 1st token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
3rd lines differ - on the 1st token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
3rd lines differ - on the 1st token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |