#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5;
const int MOD = 1e9 + 2022;
bool active[MAX_N];
int value[MAX_N], dpTot[MAX_N];
vector<int> adj[MAX_N], prefix[MAX_N], suffix[MAX_N];
int n, m;
void dfsDpTot( int u ) {
for ( int v: adj[u] )
dfsDpTot( v );
dpTot[u] = 1;
if ( adj[u].size() == 0 )
return;
for ( int v: adj[u] )
dpTot[u] = (long long)dpTot[u] * dpTot[v] % MOD;
dpTot[u] = (long long)dpTot[u] * adj[u].size() % MOD;
}
void dfsCalcValues( int u, int val ) {
if ( adj[u].size() == 0 ) {
value[u] = val;
return;
}
prefix[u].resize( adj[u].size() + 1 );
suffix[u].resize( adj[u].size() + 1 );
prefix[u][0] = 1;
for ( int i = 0; i < adj[u].size(); i++ ) {
int v = adj[u][i];
prefix[u][i + 1] = (long long)prefix[u][i] * dpTot[v] % MOD;
}
suffix[u][adj[u].size()] = 1;
for ( int i = adj[u].size() - 1; i >= 0; i-- ) {
int v = adj[u][i];
suffix[u][i] = (long long)suffix[u][i + 1] * dpTot[v] % MOD;
}
for ( int i = 0; i < adj[u].size(); i++ ){
int v = adj[u][i];
dfsCalcValues( v, (long long)val * prefix[u][i] % MOD * suffix[u][i + 1] % MOD );
}
}
void init( int N, int M, vector<int> P, vector<int> A ) {
n = N + M, m = M;
for ( int v = 1; v < n; v++ )
adj[P[v]].push_back( v );
for ( int v = 0; v < m; v++ )
active[v + n - m] = A[v];
dfsDpTot( 0 );
dfsCalcValues( 0, 1 );
}
int count_ways( int L, int R ) {
for ( int i = L; i <= R; i++ )
active[i] = !active[i];
int ans = 0;
for ( int i = n - m; i < n; i++ )
ans = (ans + value[i] * active[i]) % MOD;
return ans;
}
Compilation message
circuit.cpp: In function 'void dfsCalcValues(int, int)':
circuit.cpp:37:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for ( int i = 0; i < adj[u].size(); i++ ) {
| ~~^~~~~~~~~~~~~~~
circuit.cpp:47:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for ( int i = 0; i < adj[u].size(); i++ ){
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
14936 KB |
Output is correct |
2 |
Correct |
3 ms |
14936 KB |
Output is correct |
3 |
Correct |
3 ms |
14936 KB |
Output is correct |
4 |
Correct |
3 ms |
14936 KB |
Output is correct |
5 |
Correct |
3 ms |
14936 KB |
Output is correct |
6 |
Correct |
5 ms |
14780 KB |
Output is correct |
7 |
Correct |
3 ms |
14936 KB |
Output is correct |
8 |
Correct |
3 ms |
14936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14936 KB |
Output is correct |
2 |
Correct |
3 ms |
14936 KB |
Output is correct |
3 |
Correct |
3 ms |
14936 KB |
Output is correct |
4 |
Correct |
4 ms |
14936 KB |
Output is correct |
5 |
Correct |
4 ms |
14936 KB |
Output is correct |
6 |
Correct |
4 ms |
15188 KB |
Output is correct |
7 |
Correct |
5 ms |
14932 KB |
Output is correct |
8 |
Correct |
4 ms |
14936 KB |
Output is correct |
9 |
Correct |
4 ms |
14944 KB |
Output is correct |
10 |
Correct |
5 ms |
15192 KB |
Output is correct |
11 |
Correct |
5 ms |
15192 KB |
Output is correct |
12 |
Correct |
4 ms |
14960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
14936 KB |
Output is correct |
2 |
Correct |
3 ms |
14936 KB |
Output is correct |
3 |
Correct |
3 ms |
14936 KB |
Output is correct |
4 |
Correct |
3 ms |
14936 KB |
Output is correct |
5 |
Correct |
3 ms |
14936 KB |
Output is correct |
6 |
Correct |
5 ms |
14780 KB |
Output is correct |
7 |
Correct |
3 ms |
14936 KB |
Output is correct |
8 |
Correct |
3 ms |
14936 KB |
Output is correct |
9 |
Correct |
3 ms |
14936 KB |
Output is correct |
10 |
Correct |
3 ms |
14936 KB |
Output is correct |
11 |
Correct |
3 ms |
14936 KB |
Output is correct |
12 |
Correct |
4 ms |
14936 KB |
Output is correct |
13 |
Correct |
4 ms |
14936 KB |
Output is correct |
14 |
Correct |
4 ms |
15188 KB |
Output is correct |
15 |
Correct |
5 ms |
14932 KB |
Output is correct |
16 |
Correct |
4 ms |
14936 KB |
Output is correct |
17 |
Correct |
4 ms |
14944 KB |
Output is correct |
18 |
Correct |
5 ms |
15192 KB |
Output is correct |
19 |
Correct |
5 ms |
15192 KB |
Output is correct |
20 |
Correct |
4 ms |
14960 KB |
Output is correct |
21 |
Correct |
4 ms |
14936 KB |
Output is correct |
22 |
Correct |
4 ms |
14936 KB |
Output is correct |
23 |
Correct |
4 ms |
14936 KB |
Output is correct |
24 |
Correct |
4 ms |
14936 KB |
Output is correct |
25 |
Correct |
5 ms |
14936 KB |
Output is correct |
26 |
Correct |
4 ms |
14936 KB |
Output is correct |
27 |
Correct |
4 ms |
14936 KB |
Output is correct |
28 |
Correct |
4 ms |
14936 KB |
Output is correct |
29 |
Correct |
4 ms |
15132 KB |
Output is correct |
30 |
Correct |
3 ms |
14932 KB |
Output is correct |
31 |
Correct |
3 ms |
14936 KB |
Output is correct |
32 |
Correct |
4 ms |
14936 KB |
Output is correct |
33 |
Correct |
4 ms |
14936 KB |
Output is correct |
34 |
Correct |
3 ms |
14936 KB |
Output is correct |
35 |
Correct |
4 ms |
14936 KB |
Output is correct |
36 |
Correct |
5 ms |
15192 KB |
Output is correct |
37 |
Correct |
4 ms |
15192 KB |
Output is correct |
38 |
Correct |
4 ms |
15192 KB |
Output is correct |
39 |
Correct |
4 ms |
14936 KB |
Output is correct |
40 |
Correct |
4 ms |
14936 KB |
Output is correct |
41 |
Correct |
3 ms |
14936 KB |
Output is correct |
42 |
Correct |
3 ms |
14936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3024 ms |
18956 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3024 ms |
18956 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14936 KB |
Output is correct |
2 |
Correct |
3 ms |
14936 KB |
Output is correct |
3 |
Correct |
3 ms |
14936 KB |
Output is correct |
4 |
Correct |
4 ms |
14936 KB |
Output is correct |
5 |
Correct |
4 ms |
14936 KB |
Output is correct |
6 |
Correct |
4 ms |
15188 KB |
Output is correct |
7 |
Correct |
5 ms |
14932 KB |
Output is correct |
8 |
Correct |
4 ms |
14936 KB |
Output is correct |
9 |
Correct |
4 ms |
14944 KB |
Output is correct |
10 |
Correct |
5 ms |
15192 KB |
Output is correct |
11 |
Correct |
5 ms |
15192 KB |
Output is correct |
12 |
Correct |
4 ms |
14960 KB |
Output is correct |
13 |
Execution timed out |
3024 ms |
18956 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
14936 KB |
Output is correct |
2 |
Correct |
3 ms |
14936 KB |
Output is correct |
3 |
Correct |
3 ms |
14936 KB |
Output is correct |
4 |
Correct |
3 ms |
14936 KB |
Output is correct |
5 |
Correct |
3 ms |
14936 KB |
Output is correct |
6 |
Correct |
5 ms |
14780 KB |
Output is correct |
7 |
Correct |
3 ms |
14936 KB |
Output is correct |
8 |
Correct |
3 ms |
14936 KB |
Output is correct |
9 |
Correct |
3 ms |
14936 KB |
Output is correct |
10 |
Correct |
3 ms |
14936 KB |
Output is correct |
11 |
Correct |
3 ms |
14936 KB |
Output is correct |
12 |
Correct |
4 ms |
14936 KB |
Output is correct |
13 |
Correct |
4 ms |
14936 KB |
Output is correct |
14 |
Correct |
4 ms |
15188 KB |
Output is correct |
15 |
Correct |
5 ms |
14932 KB |
Output is correct |
16 |
Correct |
4 ms |
14936 KB |
Output is correct |
17 |
Correct |
4 ms |
14944 KB |
Output is correct |
18 |
Correct |
5 ms |
15192 KB |
Output is correct |
19 |
Correct |
5 ms |
15192 KB |
Output is correct |
20 |
Correct |
4 ms |
14960 KB |
Output is correct |
21 |
Correct |
4 ms |
14936 KB |
Output is correct |
22 |
Correct |
4 ms |
14936 KB |
Output is correct |
23 |
Correct |
4 ms |
14936 KB |
Output is correct |
24 |
Correct |
4 ms |
14936 KB |
Output is correct |
25 |
Correct |
5 ms |
14936 KB |
Output is correct |
26 |
Correct |
4 ms |
14936 KB |
Output is correct |
27 |
Correct |
4 ms |
14936 KB |
Output is correct |
28 |
Correct |
4 ms |
14936 KB |
Output is correct |
29 |
Correct |
4 ms |
15132 KB |
Output is correct |
30 |
Correct |
3 ms |
14932 KB |
Output is correct |
31 |
Correct |
3 ms |
14936 KB |
Output is correct |
32 |
Correct |
4 ms |
14936 KB |
Output is correct |
33 |
Correct |
4 ms |
14936 KB |
Output is correct |
34 |
Correct |
3 ms |
14936 KB |
Output is correct |
35 |
Correct |
4 ms |
14936 KB |
Output is correct |
36 |
Correct |
5 ms |
15192 KB |
Output is correct |
37 |
Correct |
4 ms |
15192 KB |
Output is correct |
38 |
Correct |
4 ms |
15192 KB |
Output is correct |
39 |
Correct |
4 ms |
14936 KB |
Output is correct |
40 |
Correct |
4 ms |
14936 KB |
Output is correct |
41 |
Correct |
3 ms |
14936 KB |
Output is correct |
42 |
Correct |
3 ms |
14936 KB |
Output is correct |
43 |
Correct |
1169 ms |
15192 KB |
Output is correct |
44 |
Correct |
1513 ms |
15448 KB |
Output is correct |
45 |
Correct |
1683 ms |
15192 KB |
Output is correct |
46 |
Correct |
2516 ms |
15548 KB |
Output is correct |
47 |
Correct |
2636 ms |
15448 KB |
Output is correct |
48 |
Correct |
2686 ms |
15448 KB |
Output is correct |
49 |
Correct |
2683 ms |
15448 KB |
Output is correct |
50 |
Correct |
2725 ms |
15448 KB |
Output is correct |
51 |
Correct |
2620 ms |
14936 KB |
Output is correct |
52 |
Correct |
2475 ms |
14936 KB |
Output is correct |
53 |
Correct |
520 ms |
15704 KB |
Output is correct |
54 |
Correct |
2556 ms |
15448 KB |
Output is correct |
55 |
Correct |
2519 ms |
15192 KB |
Output is correct |
56 |
Correct |
2506 ms |
15192 KB |
Output is correct |
57 |
Correct |
2423 ms |
14936 KB |
Output is correct |
58 |
Correct |
2490 ms |
15960 KB |
Output is correct |
59 |
Correct |
2490 ms |
15960 KB |
Output is correct |
60 |
Correct |
2595 ms |
15960 KB |
Output is correct |
61 |
Correct |
1483 ms |
15448 KB |
Output is correct |
62 |
Correct |
1887 ms |
15192 KB |
Output is correct |
63 |
Correct |
1997 ms |
15192 KB |
Output is correct |
64 |
Correct |
2457 ms |
14936 KB |
Output is correct |
65 |
Correct |
707 ms |
15192 KB |
Output is correct |
66 |
Correct |
2169 ms |
15448 KB |
Output is correct |
67 |
Correct |
2200 ms |
15448 KB |
Output is correct |
68 |
Correct |
2251 ms |
15700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
14936 KB |
Output is correct |
2 |
Correct |
3 ms |
14936 KB |
Output is correct |
3 |
Correct |
3 ms |
14936 KB |
Output is correct |
4 |
Correct |
3 ms |
14936 KB |
Output is correct |
5 |
Correct |
3 ms |
14936 KB |
Output is correct |
6 |
Correct |
5 ms |
14780 KB |
Output is correct |
7 |
Correct |
3 ms |
14936 KB |
Output is correct |
8 |
Correct |
3 ms |
14936 KB |
Output is correct |
9 |
Correct |
3 ms |
14936 KB |
Output is correct |
10 |
Correct |
3 ms |
14936 KB |
Output is correct |
11 |
Correct |
3 ms |
14936 KB |
Output is correct |
12 |
Correct |
4 ms |
14936 KB |
Output is correct |
13 |
Correct |
4 ms |
14936 KB |
Output is correct |
14 |
Correct |
4 ms |
15188 KB |
Output is correct |
15 |
Correct |
5 ms |
14932 KB |
Output is correct |
16 |
Correct |
4 ms |
14936 KB |
Output is correct |
17 |
Correct |
4 ms |
14944 KB |
Output is correct |
18 |
Correct |
5 ms |
15192 KB |
Output is correct |
19 |
Correct |
5 ms |
15192 KB |
Output is correct |
20 |
Correct |
4 ms |
14960 KB |
Output is correct |
21 |
Correct |
4 ms |
14936 KB |
Output is correct |
22 |
Correct |
4 ms |
14936 KB |
Output is correct |
23 |
Correct |
4 ms |
14936 KB |
Output is correct |
24 |
Correct |
4 ms |
14936 KB |
Output is correct |
25 |
Correct |
5 ms |
14936 KB |
Output is correct |
26 |
Correct |
4 ms |
14936 KB |
Output is correct |
27 |
Correct |
4 ms |
14936 KB |
Output is correct |
28 |
Correct |
4 ms |
14936 KB |
Output is correct |
29 |
Correct |
4 ms |
15132 KB |
Output is correct |
30 |
Correct |
3 ms |
14932 KB |
Output is correct |
31 |
Correct |
3 ms |
14936 KB |
Output is correct |
32 |
Correct |
4 ms |
14936 KB |
Output is correct |
33 |
Correct |
4 ms |
14936 KB |
Output is correct |
34 |
Correct |
3 ms |
14936 KB |
Output is correct |
35 |
Correct |
4 ms |
14936 KB |
Output is correct |
36 |
Correct |
5 ms |
15192 KB |
Output is correct |
37 |
Correct |
4 ms |
15192 KB |
Output is correct |
38 |
Correct |
4 ms |
15192 KB |
Output is correct |
39 |
Correct |
4 ms |
14936 KB |
Output is correct |
40 |
Correct |
4 ms |
14936 KB |
Output is correct |
41 |
Correct |
3 ms |
14936 KB |
Output is correct |
42 |
Correct |
3 ms |
14936 KB |
Output is correct |
43 |
Execution timed out |
3024 ms |
18956 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |