#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
const long long mod = 1e9 + 2022;
int n, m;
vector<int> g[maxn];
long long contribution[maxn];
long long subtree[maxn];
void dfs(int node, int dep)
{
if (g[node].empty())
{
subtree[node] = 1;
return;
}
subtree[node] = 1LL * (g[node].size());
for (int i = 0; i < g[node].size(); ++i)
{
dfs(g[node][i], dep + 1);
subtree[node] *= subtree[g[node][i]];
subtree[node] %= mod;
}
}
void dfs2(int node, long long from_parent)
{
if (g[node].empty())
{
contribution[node] = from_parent;
return;;
}
for (int i = 0; i < g[node].size(); ++i)
{
long long product = 1;
for (int j = 0; j < g[node].size(); ++j)
{
if (i != j)
{
product *= subtree[g[node][j]];
product %= mod;
}
}
dfs2(g[node][i], (from_parent * product) % mod);
}
}
long long preffix[maxn];
long long get_sum(int l, int r)
{
if (l == 0)
{
return preffix[r];
}
return (preffix[r] - preffix[l - 1] + mod) % mod;
}
struct segment_tree
{
struct vertex
{
long long sum;
bool lazy;
vertex()
{
sum = 0;
lazy = false;
}
vertex(long long _sum, bool _lazy)
{
sum = _sum;
lazy = _lazy;
}
};
vertex tree[4 * maxn];
void push_lazy(int node, int l, int r)
{
if (tree[node].lazy == true)
{
tree[node].sum = (get_sum(l, r) - tree[node].sum + mod) % mod;
if (l != r)
{
tree[2 * node].lazy = 1 - tree[2 * node].lazy;
tree[2 * node + 1].lazy = 1 - tree[2 * node + 1].lazy;
}
tree[node].lazy = false;
}
}
void update(int node, int l, int r, int ql, int qr)
{
push_lazy(node, l, r);
if (l > qr || r < ql)
{
return;
}
if (ql <= l && r <= qr)
{
tree[node].lazy = true;
push_lazy(node, l, r);
return;
}
int mid = (l + r) / 2;
update(2 * node, l, mid, ql, qr);
update(2 * node + 1, mid + 1, r, ql, qr);
tree[node].sum = (tree[2 * node].sum + tree[2 * node + 1].sum) % mod;
}
void update(int l, int r)
{
update(1, 0, m - 1, l, r);
}
};
segment_tree tree;
void init(int N, int M, vector<int> P, vector<int> A)
{
n = N;
m = M;
for (int i = 1; i < P.size(); ++i)
{
g[P[i]].push_back(i);
}
dfs(0, 0);
dfs2(0, 1);
for (int i = 0; i < m; ++i)
{
preffix[i] = contribution[i + n];
if (i > 0)
{
preffix[i] += preffix[i - 1];
}
preffix[i] %= mod;
}
for (int i = 0; i < m; ++i)
{
if (A[i] == 1)
{
tree.update(i, i);
}
}
}
int count_ways(int L, int R)
{
int l = L - n;
int r = R - n;
tree.update(l, r);
return tree.tree[1].sum;
}
Compilation message
circuit.cpp: In function 'void dfs(int, int)':
circuit.cpp:27:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for (int i = 0; i < g[node].size(); ++i)
| ~~^~~~~~~~~~~~~~~~
circuit.cpp: In function 'void dfs2(int, long long int)':
circuit.cpp:43:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (int i = 0; i < g[node].size(); ++i)
| ~~^~~~~~~~~~~~~~~~
circuit.cpp:47:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int j = 0; j < g[node].size(); ++j)
| ~~^~~~~~~~~~~~~~~~
circuit.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>)':
circuit.cpp:147:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
147 | for (int i = 1; i < P.size(); ++i)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
17488 KB |
Output is correct |
2 |
Correct |
8 ms |
17488 KB |
Output is correct |
3 |
Correct |
12 ms |
17576 KB |
Output is correct |
4 |
Correct |
13 ms |
17460 KB |
Output is correct |
5 |
Correct |
13 ms |
17488 KB |
Output is correct |
6 |
Correct |
14 ms |
17488 KB |
Output is correct |
7 |
Correct |
13 ms |
17596 KB |
Output is correct |
8 |
Correct |
12 ms |
17488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
17488 KB |
Output is correct |
2 |
Correct |
7 ms |
17440 KB |
Output is correct |
3 |
Correct |
8 ms |
17504 KB |
Output is correct |
4 |
Correct |
8 ms |
17488 KB |
Output is correct |
5 |
Correct |
8 ms |
17488 KB |
Output is correct |
6 |
Correct |
9 ms |
17488 KB |
Output is correct |
7 |
Correct |
8 ms |
17616 KB |
Output is correct |
8 |
Correct |
8 ms |
17588 KB |
Output is correct |
9 |
Correct |
8 ms |
17616 KB |
Output is correct |
10 |
Correct |
8 ms |
17616 KB |
Output is correct |
11 |
Correct |
8 ms |
17616 KB |
Output is correct |
12 |
Correct |
8 ms |
17488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
17488 KB |
Output is correct |
2 |
Correct |
8 ms |
17488 KB |
Output is correct |
3 |
Correct |
12 ms |
17576 KB |
Output is correct |
4 |
Correct |
13 ms |
17460 KB |
Output is correct |
5 |
Correct |
13 ms |
17488 KB |
Output is correct |
6 |
Correct |
14 ms |
17488 KB |
Output is correct |
7 |
Correct |
13 ms |
17596 KB |
Output is correct |
8 |
Correct |
12 ms |
17488 KB |
Output is correct |
9 |
Correct |
7 ms |
17488 KB |
Output is correct |
10 |
Correct |
7 ms |
17440 KB |
Output is correct |
11 |
Correct |
8 ms |
17504 KB |
Output is correct |
12 |
Correct |
8 ms |
17488 KB |
Output is correct |
13 |
Correct |
8 ms |
17488 KB |
Output is correct |
14 |
Correct |
9 ms |
17488 KB |
Output is correct |
15 |
Correct |
8 ms |
17616 KB |
Output is correct |
16 |
Correct |
8 ms |
17588 KB |
Output is correct |
17 |
Correct |
8 ms |
17616 KB |
Output is correct |
18 |
Correct |
8 ms |
17616 KB |
Output is correct |
19 |
Correct |
8 ms |
17616 KB |
Output is correct |
20 |
Correct |
8 ms |
17488 KB |
Output is correct |
21 |
Correct |
8 ms |
17488 KB |
Output is correct |
22 |
Correct |
8 ms |
17488 KB |
Output is correct |
23 |
Correct |
8 ms |
17512 KB |
Output is correct |
24 |
Correct |
8 ms |
17616 KB |
Output is correct |
25 |
Correct |
8 ms |
17616 KB |
Output is correct |
26 |
Correct |
9 ms |
17616 KB |
Output is correct |
27 |
Correct |
9 ms |
17616 KB |
Output is correct |
28 |
Correct |
8 ms |
17616 KB |
Output is correct |
29 |
Correct |
12 ms |
17488 KB |
Output is correct |
30 |
Correct |
12 ms |
17488 KB |
Output is correct |
31 |
Correct |
8 ms |
17620 KB |
Output is correct |
32 |
Correct |
8 ms |
17616 KB |
Output is correct |
33 |
Correct |
7 ms |
17488 KB |
Output is correct |
34 |
Correct |
9 ms |
17544 KB |
Output is correct |
35 |
Correct |
9 ms |
17488 KB |
Output is correct |
36 |
Correct |
8 ms |
17616 KB |
Output is correct |
37 |
Correct |
12 ms |
17668 KB |
Output is correct |
38 |
Correct |
12 ms |
17692 KB |
Output is correct |
39 |
Correct |
8 ms |
17472 KB |
Output is correct |
40 |
Correct |
8 ms |
17488 KB |
Output is correct |
41 |
Correct |
8 ms |
17484 KB |
Output is correct |
42 |
Correct |
9 ms |
17488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
495 ms |
20336 KB |
Output is correct |
2 |
Correct |
728 ms |
23112 KB |
Output is correct |
3 |
Correct |
724 ms |
23180 KB |
Output is correct |
4 |
Correct |
828 ms |
23148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
495 ms |
20336 KB |
Output is correct |
2 |
Correct |
728 ms |
23112 KB |
Output is correct |
3 |
Correct |
724 ms |
23180 KB |
Output is correct |
4 |
Correct |
828 ms |
23148 KB |
Output is correct |
5 |
Correct |
616 ms |
20304 KB |
Output is correct |
6 |
Correct |
777 ms |
23268 KB |
Output is correct |
7 |
Correct |
663 ms |
23120 KB |
Output is correct |
8 |
Correct |
675 ms |
23164 KB |
Output is correct |
9 |
Correct |
450 ms |
17588 KB |
Output is correct |
10 |
Correct |
766 ms |
17796 KB |
Output is correct |
11 |
Correct |
671 ms |
17872 KB |
Output is correct |
12 |
Correct |
870 ms |
17828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
17488 KB |
Output is correct |
2 |
Correct |
7 ms |
17440 KB |
Output is correct |
3 |
Correct |
8 ms |
17504 KB |
Output is correct |
4 |
Correct |
8 ms |
17488 KB |
Output is correct |
5 |
Correct |
8 ms |
17488 KB |
Output is correct |
6 |
Correct |
9 ms |
17488 KB |
Output is correct |
7 |
Correct |
8 ms |
17616 KB |
Output is correct |
8 |
Correct |
8 ms |
17588 KB |
Output is correct |
9 |
Correct |
8 ms |
17616 KB |
Output is correct |
10 |
Correct |
8 ms |
17616 KB |
Output is correct |
11 |
Correct |
8 ms |
17616 KB |
Output is correct |
12 |
Correct |
8 ms |
17488 KB |
Output is correct |
13 |
Correct |
495 ms |
20336 KB |
Output is correct |
14 |
Correct |
728 ms |
23112 KB |
Output is correct |
15 |
Correct |
724 ms |
23180 KB |
Output is correct |
16 |
Correct |
828 ms |
23148 KB |
Output is correct |
17 |
Correct |
616 ms |
20304 KB |
Output is correct |
18 |
Correct |
777 ms |
23268 KB |
Output is correct |
19 |
Correct |
663 ms |
23120 KB |
Output is correct |
20 |
Correct |
675 ms |
23164 KB |
Output is correct |
21 |
Correct |
450 ms |
17588 KB |
Output is correct |
22 |
Correct |
766 ms |
17796 KB |
Output is correct |
23 |
Correct |
671 ms |
17872 KB |
Output is correct |
24 |
Correct |
870 ms |
17828 KB |
Output is correct |
25 |
Correct |
819 ms |
26056 KB |
Output is correct |
26 |
Correct |
679 ms |
26212 KB |
Output is correct |
27 |
Correct |
1045 ms |
26056 KB |
Output is correct |
28 |
Correct |
556 ms |
26124 KB |
Output is correct |
29 |
Correct |
836 ms |
32368 KB |
Output is correct |
30 |
Correct |
641 ms |
32364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
17488 KB |
Output is correct |
2 |
Correct |
8 ms |
17488 KB |
Output is correct |
3 |
Correct |
12 ms |
17576 KB |
Output is correct |
4 |
Correct |
13 ms |
17460 KB |
Output is correct |
5 |
Correct |
13 ms |
17488 KB |
Output is correct |
6 |
Correct |
14 ms |
17488 KB |
Output is correct |
7 |
Correct |
13 ms |
17596 KB |
Output is correct |
8 |
Correct |
12 ms |
17488 KB |
Output is correct |
9 |
Correct |
7 ms |
17488 KB |
Output is correct |
10 |
Correct |
7 ms |
17440 KB |
Output is correct |
11 |
Correct |
8 ms |
17504 KB |
Output is correct |
12 |
Correct |
8 ms |
17488 KB |
Output is correct |
13 |
Correct |
8 ms |
17488 KB |
Output is correct |
14 |
Correct |
9 ms |
17488 KB |
Output is correct |
15 |
Correct |
8 ms |
17616 KB |
Output is correct |
16 |
Correct |
8 ms |
17588 KB |
Output is correct |
17 |
Correct |
8 ms |
17616 KB |
Output is correct |
18 |
Correct |
8 ms |
17616 KB |
Output is correct |
19 |
Correct |
8 ms |
17616 KB |
Output is correct |
20 |
Correct |
8 ms |
17488 KB |
Output is correct |
21 |
Correct |
8 ms |
17488 KB |
Output is correct |
22 |
Correct |
8 ms |
17488 KB |
Output is correct |
23 |
Correct |
8 ms |
17512 KB |
Output is correct |
24 |
Correct |
8 ms |
17616 KB |
Output is correct |
25 |
Correct |
8 ms |
17616 KB |
Output is correct |
26 |
Correct |
9 ms |
17616 KB |
Output is correct |
27 |
Correct |
9 ms |
17616 KB |
Output is correct |
28 |
Correct |
8 ms |
17616 KB |
Output is correct |
29 |
Correct |
12 ms |
17488 KB |
Output is correct |
30 |
Correct |
12 ms |
17488 KB |
Output is correct |
31 |
Correct |
8 ms |
17620 KB |
Output is correct |
32 |
Correct |
8 ms |
17616 KB |
Output is correct |
33 |
Correct |
7 ms |
17488 KB |
Output is correct |
34 |
Correct |
9 ms |
17544 KB |
Output is correct |
35 |
Correct |
9 ms |
17488 KB |
Output is correct |
36 |
Correct |
8 ms |
17616 KB |
Output is correct |
37 |
Correct |
12 ms |
17668 KB |
Output is correct |
38 |
Correct |
12 ms |
17692 KB |
Output is correct |
39 |
Correct |
8 ms |
17472 KB |
Output is correct |
40 |
Correct |
8 ms |
17488 KB |
Output is correct |
41 |
Correct |
8 ms |
17484 KB |
Output is correct |
42 |
Correct |
9 ms |
17488 KB |
Output is correct |
43 |
Correct |
624 ms |
17744 KB |
Output is correct |
44 |
Correct |
745 ms |
17744 KB |
Output is correct |
45 |
Correct |
785 ms |
17744 KB |
Output is correct |
46 |
Correct |
542 ms |
17872 KB |
Output is correct |
47 |
Correct |
736 ms |
17892 KB |
Output is correct |
48 |
Correct |
752 ms |
17892 KB |
Output is correct |
49 |
Correct |
654 ms |
17872 KB |
Output is correct |
50 |
Correct |
748 ms |
17872 KB |
Output is correct |
51 |
Correct |
814 ms |
17704 KB |
Output is correct |
52 |
Correct |
885 ms |
17824 KB |
Output is correct |
53 |
Correct |
513 ms |
18000 KB |
Output is correct |
54 |
Correct |
754 ms |
17872 KB |
Output is correct |
55 |
Correct |
888 ms |
17744 KB |
Output is correct |
56 |
Correct |
741 ms |
17760 KB |
Output is correct |
57 |
Correct |
747 ms |
17744 KB |
Output is correct |
58 |
Correct |
657 ms |
18256 KB |
Output is correct |
59 |
Correct |
740 ms |
18312 KB |
Output is correct |
60 |
Correct |
763 ms |
18308 KB |
Output is correct |
61 |
Correct |
811 ms |
17892 KB |
Output is correct |
62 |
Correct |
732 ms |
17744 KB |
Output is correct |
63 |
Correct |
673 ms |
17744 KB |
Output is correct |
64 |
Correct |
816 ms |
17760 KB |
Output is correct |
65 |
Correct |
375 ms |
17616 KB |
Output is correct |
66 |
Correct |
656 ms |
17872 KB |
Output is correct |
67 |
Correct |
673 ms |
17872 KB |
Output is correct |
68 |
Correct |
775 ms |
17872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
17488 KB |
Output is correct |
2 |
Correct |
8 ms |
17488 KB |
Output is correct |
3 |
Correct |
12 ms |
17576 KB |
Output is correct |
4 |
Correct |
13 ms |
17460 KB |
Output is correct |
5 |
Correct |
13 ms |
17488 KB |
Output is correct |
6 |
Correct |
14 ms |
17488 KB |
Output is correct |
7 |
Correct |
13 ms |
17596 KB |
Output is correct |
8 |
Correct |
12 ms |
17488 KB |
Output is correct |
9 |
Correct |
7 ms |
17488 KB |
Output is correct |
10 |
Correct |
7 ms |
17440 KB |
Output is correct |
11 |
Correct |
8 ms |
17504 KB |
Output is correct |
12 |
Correct |
8 ms |
17488 KB |
Output is correct |
13 |
Correct |
8 ms |
17488 KB |
Output is correct |
14 |
Correct |
9 ms |
17488 KB |
Output is correct |
15 |
Correct |
8 ms |
17616 KB |
Output is correct |
16 |
Correct |
8 ms |
17588 KB |
Output is correct |
17 |
Correct |
8 ms |
17616 KB |
Output is correct |
18 |
Correct |
8 ms |
17616 KB |
Output is correct |
19 |
Correct |
8 ms |
17616 KB |
Output is correct |
20 |
Correct |
8 ms |
17488 KB |
Output is correct |
21 |
Correct |
8 ms |
17488 KB |
Output is correct |
22 |
Correct |
8 ms |
17488 KB |
Output is correct |
23 |
Correct |
8 ms |
17512 KB |
Output is correct |
24 |
Correct |
8 ms |
17616 KB |
Output is correct |
25 |
Correct |
8 ms |
17616 KB |
Output is correct |
26 |
Correct |
9 ms |
17616 KB |
Output is correct |
27 |
Correct |
9 ms |
17616 KB |
Output is correct |
28 |
Correct |
8 ms |
17616 KB |
Output is correct |
29 |
Correct |
12 ms |
17488 KB |
Output is correct |
30 |
Correct |
12 ms |
17488 KB |
Output is correct |
31 |
Correct |
8 ms |
17620 KB |
Output is correct |
32 |
Correct |
8 ms |
17616 KB |
Output is correct |
33 |
Correct |
7 ms |
17488 KB |
Output is correct |
34 |
Correct |
9 ms |
17544 KB |
Output is correct |
35 |
Correct |
9 ms |
17488 KB |
Output is correct |
36 |
Correct |
8 ms |
17616 KB |
Output is correct |
37 |
Correct |
12 ms |
17668 KB |
Output is correct |
38 |
Correct |
12 ms |
17692 KB |
Output is correct |
39 |
Correct |
8 ms |
17472 KB |
Output is correct |
40 |
Correct |
8 ms |
17488 KB |
Output is correct |
41 |
Correct |
8 ms |
17484 KB |
Output is correct |
42 |
Correct |
9 ms |
17488 KB |
Output is correct |
43 |
Correct |
495 ms |
20336 KB |
Output is correct |
44 |
Correct |
728 ms |
23112 KB |
Output is correct |
45 |
Correct |
724 ms |
23180 KB |
Output is correct |
46 |
Correct |
828 ms |
23148 KB |
Output is correct |
47 |
Correct |
616 ms |
20304 KB |
Output is correct |
48 |
Correct |
777 ms |
23268 KB |
Output is correct |
49 |
Correct |
663 ms |
23120 KB |
Output is correct |
50 |
Correct |
675 ms |
23164 KB |
Output is correct |
51 |
Correct |
450 ms |
17588 KB |
Output is correct |
52 |
Correct |
766 ms |
17796 KB |
Output is correct |
53 |
Correct |
671 ms |
17872 KB |
Output is correct |
54 |
Correct |
870 ms |
17828 KB |
Output is correct |
55 |
Correct |
819 ms |
26056 KB |
Output is correct |
56 |
Correct |
679 ms |
26212 KB |
Output is correct |
57 |
Correct |
1045 ms |
26056 KB |
Output is correct |
58 |
Correct |
556 ms |
26124 KB |
Output is correct |
59 |
Correct |
836 ms |
32368 KB |
Output is correct |
60 |
Correct |
641 ms |
32364 KB |
Output is correct |
61 |
Correct |
624 ms |
17744 KB |
Output is correct |
62 |
Correct |
745 ms |
17744 KB |
Output is correct |
63 |
Correct |
785 ms |
17744 KB |
Output is correct |
64 |
Correct |
542 ms |
17872 KB |
Output is correct |
65 |
Correct |
736 ms |
17892 KB |
Output is correct |
66 |
Correct |
752 ms |
17892 KB |
Output is correct |
67 |
Correct |
654 ms |
17872 KB |
Output is correct |
68 |
Correct |
748 ms |
17872 KB |
Output is correct |
69 |
Correct |
814 ms |
17704 KB |
Output is correct |
70 |
Correct |
885 ms |
17824 KB |
Output is correct |
71 |
Correct |
513 ms |
18000 KB |
Output is correct |
72 |
Correct |
754 ms |
17872 KB |
Output is correct |
73 |
Correct |
888 ms |
17744 KB |
Output is correct |
74 |
Correct |
741 ms |
17760 KB |
Output is correct |
75 |
Correct |
747 ms |
17744 KB |
Output is correct |
76 |
Correct |
657 ms |
18256 KB |
Output is correct |
77 |
Correct |
740 ms |
18312 KB |
Output is correct |
78 |
Correct |
763 ms |
18308 KB |
Output is correct |
79 |
Correct |
811 ms |
17892 KB |
Output is correct |
80 |
Correct |
732 ms |
17744 KB |
Output is correct |
81 |
Correct |
673 ms |
17744 KB |
Output is correct |
82 |
Correct |
816 ms |
17760 KB |
Output is correct |
83 |
Correct |
375 ms |
17616 KB |
Output is correct |
84 |
Correct |
656 ms |
17872 KB |
Output is correct |
85 |
Correct |
673 ms |
17872 KB |
Output is correct |
86 |
Correct |
775 ms |
17872 KB |
Output is correct |
87 |
Correct |
8 ms |
17488 KB |
Output is correct |
88 |
Correct |
506 ms |
25192 KB |
Output is correct |
89 |
Correct |
871 ms |
23020 KB |
Output is correct |
90 |
Correct |
699 ms |
22840 KB |
Output is correct |
91 |
Correct |
697 ms |
26232 KB |
Output is correct |
92 |
Correct |
912 ms |
26220 KB |
Output is correct |
93 |
Correct |
667 ms |
26220 KB |
Output is correct |
94 |
Correct |
859 ms |
26180 KB |
Output is correct |
95 |
Correct |
752 ms |
26240 KB |
Output is correct |
96 |
Execution timed out |
3039 ms |
20568 KB |
Time limit exceeded |
97 |
Halted |
0 ms |
0 KB |
- |