#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
typedef long long ll;
const int N = 200005, M = 1000002022;
int n, m;
vector<int> g[N];
int s[N];
int a[N];
void dfs0(int x)
{
a[x] = 1;
if (g[x].empty())
return;
for (int i = 0; i < g[x].size(); ++i)
{
int h = g[x][i];
dfs0(h);
a[x] = (a[x] * 1LL * a[h]) % M;
}
a[x] = (a[x] * 1LL * sz(g[x])) % M;
}
int u[N];
void dfs1(int x, int aa)
{
if (g[x].empty())
{
u[x - n] = aa;
return;
}
vector<int> p;
for (int i = 0; i < g[x].size(); ++i)
{
int h = g[x][i];
if (p.empty())
p.push_back(a[h]);
else
p.push_back((p.back() * 1LL * a[h]) % M);
}
vector<int> s;
for (int i = sz(g[x]) - 1; i >= 0; --i)
{
int h = g[x][i];
if (s.empty())
s.push_back(a[h]);
else
s.push_back((s.back() * 1LL * a[h]) % M);
}
reverse(all(s));
for (int i = 0; i < g[x].size(); ++i)
{
int h = g[x][i];
int yaa = aa;
if (i)
yaa = (yaa * 1LL * p[i - 1]) % M;
if (i + 1 < sz(g[x]))
yaa = (yaa * 1LL * s[i + 1]) % M;
dfs1(h, yaa);
}
}
void init(int NN, int MM, std::vector<int> P, std::vector<int> A)
{
n = NN;
m = MM;
for (int i = 1; i < n + m; ++i)
g[P[i]].push_back(i);
for (int i = 0; i < m; ++i)
s[i] = A[i];
dfs0(0);
dfs1(0, 1);
}
int count_ways(int L, int R)
{
L -= n;
R -= n;
for (int i = L; i <= R; ++i)
s[i] ^= 1;
int ans = 0;
for (int i = 0; i < m; ++i)
{
ans = (ans + s[i] * u[i]) % M;
}
return ans;
}
/*
3 4 3
-1 0 1 2 1 1 0
1 0 1 0
3 4
4 5
3 6
*/
Compilation message
circuit.cpp: In function 'void dfs0(int)':
circuit.cpp:22:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int i = 0; i < g[x].size(); ++i)
| ~~^~~~~~~~~~~~~
circuit.cpp: In function 'void dfs1(int, int)':
circuit.cpp:41:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for (int i = 0; i < g[x].size(); ++i)
| ~~^~~~~~~~~~~~~
circuit.cpp:60:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int i = 0; i < g[x].size(); ++i)
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
2 ms |
4944 KB |
Output is correct |
4 |
Correct |
3 ms |
4944 KB |
Output is correct |
5 |
Correct |
2 ms |
4944 KB |
Output is correct |
6 |
Correct |
2 ms |
4944 KB |
Output is correct |
7 |
Correct |
3 ms |
4944 KB |
Output is correct |
8 |
Correct |
2 ms |
4944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
2 ms |
4944 KB |
Output is correct |
4 |
Correct |
2 ms |
4944 KB |
Output is correct |
5 |
Correct |
2 ms |
4944 KB |
Output is correct |
6 |
Correct |
2 ms |
4944 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
2 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
5072 KB |
Output is correct |
10 |
Correct |
3 ms |
5200 KB |
Output is correct |
11 |
Correct |
2 ms |
5200 KB |
Output is correct |
12 |
Correct |
2 ms |
4944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
2 ms |
4944 KB |
Output is correct |
4 |
Correct |
3 ms |
4944 KB |
Output is correct |
5 |
Correct |
2 ms |
4944 KB |
Output is correct |
6 |
Correct |
2 ms |
4944 KB |
Output is correct |
7 |
Correct |
3 ms |
4944 KB |
Output is correct |
8 |
Correct |
2 ms |
4944 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
2 ms |
4944 KB |
Output is correct |
11 |
Correct |
2 ms |
4944 KB |
Output is correct |
12 |
Correct |
2 ms |
4944 KB |
Output is correct |
13 |
Correct |
2 ms |
4944 KB |
Output is correct |
14 |
Correct |
2 ms |
4944 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
2 ms |
5072 KB |
Output is correct |
17 |
Correct |
3 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5200 KB |
Output is correct |
19 |
Correct |
2 ms |
5200 KB |
Output is correct |
20 |
Correct |
2 ms |
4944 KB |
Output is correct |
21 |
Correct |
2 ms |
4944 KB |
Output is correct |
22 |
Correct |
2 ms |
4944 KB |
Output is correct |
23 |
Correct |
2 ms |
4944 KB |
Output is correct |
24 |
Correct |
3 ms |
5072 KB |
Output is correct |
25 |
Correct |
2 ms |
5072 KB |
Output is correct |
26 |
Correct |
2 ms |
5072 KB |
Output is correct |
27 |
Correct |
3 ms |
5072 KB |
Output is correct |
28 |
Correct |
2 ms |
5072 KB |
Output is correct |
29 |
Correct |
2 ms |
4944 KB |
Output is correct |
30 |
Correct |
2 ms |
4944 KB |
Output is correct |
31 |
Correct |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 KB |
Output is correct |
33 |
Correct |
3 ms |
4944 KB |
Output is correct |
34 |
Correct |
2 ms |
4944 KB |
Output is correct |
35 |
Correct |
2 ms |
4944 KB |
Output is correct |
36 |
Correct |
3 ms |
5200 KB |
Output is correct |
37 |
Correct |
2 ms |
5200 KB |
Output is correct |
38 |
Correct |
3 ms |
5200 KB |
Output is correct |
39 |
Correct |
2 ms |
5072 KB |
Output is correct |
40 |
Correct |
2 ms |
4944 KB |
Output is correct |
41 |
Correct |
2 ms |
4944 KB |
Output is correct |
42 |
Correct |
2 ms |
4944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3087 ms |
7204 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3087 ms |
7204 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
2 ms |
4944 KB |
Output is correct |
4 |
Correct |
2 ms |
4944 KB |
Output is correct |
5 |
Correct |
2 ms |
4944 KB |
Output is correct |
6 |
Correct |
2 ms |
4944 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
2 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
5072 KB |
Output is correct |
10 |
Correct |
3 ms |
5200 KB |
Output is correct |
11 |
Correct |
2 ms |
5200 KB |
Output is correct |
12 |
Correct |
2 ms |
4944 KB |
Output is correct |
13 |
Execution timed out |
3087 ms |
7204 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
2 ms |
4944 KB |
Output is correct |
4 |
Correct |
3 ms |
4944 KB |
Output is correct |
5 |
Correct |
2 ms |
4944 KB |
Output is correct |
6 |
Correct |
2 ms |
4944 KB |
Output is correct |
7 |
Correct |
3 ms |
4944 KB |
Output is correct |
8 |
Correct |
2 ms |
4944 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
2 ms |
4944 KB |
Output is correct |
11 |
Correct |
2 ms |
4944 KB |
Output is correct |
12 |
Correct |
2 ms |
4944 KB |
Output is correct |
13 |
Correct |
2 ms |
4944 KB |
Output is correct |
14 |
Correct |
2 ms |
4944 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
2 ms |
5072 KB |
Output is correct |
17 |
Correct |
3 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5200 KB |
Output is correct |
19 |
Correct |
2 ms |
5200 KB |
Output is correct |
20 |
Correct |
2 ms |
4944 KB |
Output is correct |
21 |
Correct |
2 ms |
4944 KB |
Output is correct |
22 |
Correct |
2 ms |
4944 KB |
Output is correct |
23 |
Correct |
2 ms |
4944 KB |
Output is correct |
24 |
Correct |
3 ms |
5072 KB |
Output is correct |
25 |
Correct |
2 ms |
5072 KB |
Output is correct |
26 |
Correct |
2 ms |
5072 KB |
Output is correct |
27 |
Correct |
3 ms |
5072 KB |
Output is correct |
28 |
Correct |
2 ms |
5072 KB |
Output is correct |
29 |
Correct |
2 ms |
4944 KB |
Output is correct |
30 |
Correct |
2 ms |
4944 KB |
Output is correct |
31 |
Correct |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 KB |
Output is correct |
33 |
Correct |
3 ms |
4944 KB |
Output is correct |
34 |
Correct |
2 ms |
4944 KB |
Output is correct |
35 |
Correct |
2 ms |
4944 KB |
Output is correct |
36 |
Correct |
3 ms |
5200 KB |
Output is correct |
37 |
Correct |
2 ms |
5200 KB |
Output is correct |
38 |
Correct |
3 ms |
5200 KB |
Output is correct |
39 |
Correct |
2 ms |
5072 KB |
Output is correct |
40 |
Correct |
2 ms |
4944 KB |
Output is correct |
41 |
Correct |
2 ms |
4944 KB |
Output is correct |
42 |
Correct |
2 ms |
4944 KB |
Output is correct |
43 |
Correct |
1219 ms |
5200 KB |
Output is correct |
44 |
Correct |
1665 ms |
5200 KB |
Output is correct |
45 |
Correct |
1949 ms |
5200 KB |
Output is correct |
46 |
Correct |
2499 ms |
5328 KB |
Output is correct |
47 |
Correct |
2792 ms |
5328 KB |
Output is correct |
48 |
Correct |
2794 ms |
5328 KB |
Output is correct |
49 |
Correct |
2666 ms |
5328 KB |
Output is correct |
50 |
Correct |
2873 ms |
5328 KB |
Output is correct |
51 |
Correct |
2897 ms |
5220 KB |
Output is correct |
52 |
Correct |
2679 ms |
5224 KB |
Output is correct |
53 |
Correct |
478 ms |
6296 KB |
Output is correct |
54 |
Correct |
2477 ms |
5328 KB |
Output is correct |
55 |
Correct |
2440 ms |
5200 KB |
Output is correct |
56 |
Correct |
2703 ms |
5200 KB |
Output is correct |
57 |
Correct |
2640 ms |
5072 KB |
Output is correct |
58 |
Correct |
2586 ms |
6352 KB |
Output is correct |
59 |
Correct |
2645 ms |
6480 KB |
Output is correct |
60 |
Correct |
2871 ms |
6480 KB |
Output is correct |
61 |
Correct |
1478 ms |
5456 KB |
Output is correct |
62 |
Correct |
2098 ms |
5200 KB |
Output is correct |
63 |
Correct |
2266 ms |
5072 KB |
Output is correct |
64 |
Correct |
2668 ms |
5072 KB |
Output is correct |
65 |
Correct |
700 ms |
5072 KB |
Output is correct |
66 |
Correct |
2387 ms |
5200 KB |
Output is correct |
67 |
Correct |
2338 ms |
5200 KB |
Output is correct |
68 |
Correct |
2524 ms |
5200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
2 ms |
4944 KB |
Output is correct |
4 |
Correct |
3 ms |
4944 KB |
Output is correct |
5 |
Correct |
2 ms |
4944 KB |
Output is correct |
6 |
Correct |
2 ms |
4944 KB |
Output is correct |
7 |
Correct |
3 ms |
4944 KB |
Output is correct |
8 |
Correct |
2 ms |
4944 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
2 ms |
4944 KB |
Output is correct |
11 |
Correct |
2 ms |
4944 KB |
Output is correct |
12 |
Correct |
2 ms |
4944 KB |
Output is correct |
13 |
Correct |
2 ms |
4944 KB |
Output is correct |
14 |
Correct |
2 ms |
4944 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
2 ms |
5072 KB |
Output is correct |
17 |
Correct |
3 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5200 KB |
Output is correct |
19 |
Correct |
2 ms |
5200 KB |
Output is correct |
20 |
Correct |
2 ms |
4944 KB |
Output is correct |
21 |
Correct |
2 ms |
4944 KB |
Output is correct |
22 |
Correct |
2 ms |
4944 KB |
Output is correct |
23 |
Correct |
2 ms |
4944 KB |
Output is correct |
24 |
Correct |
3 ms |
5072 KB |
Output is correct |
25 |
Correct |
2 ms |
5072 KB |
Output is correct |
26 |
Correct |
2 ms |
5072 KB |
Output is correct |
27 |
Correct |
3 ms |
5072 KB |
Output is correct |
28 |
Correct |
2 ms |
5072 KB |
Output is correct |
29 |
Correct |
2 ms |
4944 KB |
Output is correct |
30 |
Correct |
2 ms |
4944 KB |
Output is correct |
31 |
Correct |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 KB |
Output is correct |
33 |
Correct |
3 ms |
4944 KB |
Output is correct |
34 |
Correct |
2 ms |
4944 KB |
Output is correct |
35 |
Correct |
2 ms |
4944 KB |
Output is correct |
36 |
Correct |
3 ms |
5200 KB |
Output is correct |
37 |
Correct |
2 ms |
5200 KB |
Output is correct |
38 |
Correct |
3 ms |
5200 KB |
Output is correct |
39 |
Correct |
2 ms |
5072 KB |
Output is correct |
40 |
Correct |
2 ms |
4944 KB |
Output is correct |
41 |
Correct |
2 ms |
4944 KB |
Output is correct |
42 |
Correct |
2 ms |
4944 KB |
Output is correct |
43 |
Execution timed out |
3087 ms |
7204 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |