#include <bits/stdc++.h>
#include "circuit.h"
using namespace std;
const int BASE = 1e9 + 2022;
const int N = 100100;
struct SegmentTree
{
int low, mid, high;
long long value, sum;
int flip;
SegmentTree *l, *r;
SegmentTree(int low, int high, vector<long long> &coef, vector<int> &state): low(low), high(high)
{
mid = (low + high) / 2;
value = sum = flip = 0;
if (low == high)
{
l = r = NULL;
sum = coef[low];
value = state[low] * sum;
}
else
{
l = new SegmentTree(low, mid, coef, state);
r = new SegmentTree(mid + 1, high, coef, state);
sum = l->sum + r->sum;
value = l->value + r->value;
}
}
void update(int x, int y)
{
if (low == x && high == y)
{
value = sum - value;
flip ^= 1;
return;
}
if (x <= mid)
l->update(x, min(y, mid));
if (mid < y)
r->update(max(x, mid + 1), y);
value = l->value + r->value;
if (flip)
value = sum - value;
}
};
int n;
vector<int> a[N];
vector<long long> coef, ways;
SegmentTree *tree;
void dfsWay(int x)
{
if (empty(a[x]))
return;
for (int y : a[x])
{
dfsWay(y);
ways[x] *= ways[y];
ways[x] %= BASE;
}
ways[x] *= size(a[x]);
ways[x] %= BASE;
}
void dfsCoef(int x)
{
vector<long long> prefix(size(a[x]));
for (int i = 0; i < size(a[x]); i++)
{
prefix[i] = ways[a[x][i]];
if (i)
prefix[i] = prefix[i] * prefix[i - 1] % BASE;
}
long long suffix = 1;
for (int i = int(size(a[x])) - 1; i >= 0; i--)
{
int y = a[x][i];
coef[y] = suffix;
if (i)
coef[y] = coef[y] * prefix[i - 1] % BASE;
suffix *= ways[y];
suffix %= BASE;
}
for (int y : a[x])
{
coef[y] = coef[y] * coef[x] % BASE;
dfsCoef(y);
}
}
void init(int N, int m, vector<int> P, vector<int> A)
{
n = N;
for (int i = 1; i < n + m; i++)
a[P[i]].push_back(i);
ways.assign(n + m, 1);
dfsWay(0);
coef.assign(n + m, 1);
dfsCoef(0);
for (int i = 0; i < m; i++)
coef[i] = coef[i + n];
coef.resize(m);
tree = new SegmentTree(0, m - 1, coef, A);
}
int count_ways(int L, int R)
{
tree->update(L - n, R - n);
return int(tree->value % BASE);
}
Compilation message
circuit.cpp: In function 'void dfsCoef(int)':
circuit.cpp:73:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for (int i = 0; i < size(a[x]); i++)
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2768 KB |
Output is correct |
4 |
Correct |
2 ms |
2696 KB |
Output is correct |
5 |
Correct |
2 ms |
2768 KB |
Output is correct |
6 |
Correct |
2 ms |
2768 KB |
Output is correct |
7 |
Correct |
2 ms |
2768 KB |
Output is correct |
8 |
Correct |
2 ms |
2768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
1 ms |
2640 KB |
Output is correct |
4 |
Correct |
2 ms |
2640 KB |
Output is correct |
5 |
Correct |
2 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2768 KB |
Output is correct |
7 |
Correct |
2 ms |
2768 KB |
Output is correct |
8 |
Correct |
2 ms |
2768 KB |
Output is correct |
9 |
Correct |
2 ms |
2768 KB |
Output is correct |
10 |
Correct |
2 ms |
2896 KB |
Output is correct |
11 |
Correct |
2 ms |
2896 KB |
Output is correct |
12 |
Correct |
2 ms |
2768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2768 KB |
Output is correct |
4 |
Correct |
2 ms |
2696 KB |
Output is correct |
5 |
Correct |
2 ms |
2768 KB |
Output is correct |
6 |
Correct |
2 ms |
2768 KB |
Output is correct |
7 |
Correct |
2 ms |
2768 KB |
Output is correct |
8 |
Correct |
2 ms |
2768 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2640 KB |
Output is correct |
11 |
Correct |
1 ms |
2640 KB |
Output is correct |
12 |
Correct |
2 ms |
2640 KB |
Output is correct |
13 |
Correct |
2 ms |
2640 KB |
Output is correct |
14 |
Correct |
2 ms |
2768 KB |
Output is correct |
15 |
Correct |
2 ms |
2768 KB |
Output is correct |
16 |
Correct |
2 ms |
2768 KB |
Output is correct |
17 |
Correct |
2 ms |
2768 KB |
Output is correct |
18 |
Correct |
2 ms |
2896 KB |
Output is correct |
19 |
Correct |
2 ms |
2896 KB |
Output is correct |
20 |
Correct |
2 ms |
2768 KB |
Output is correct |
21 |
Correct |
3 ms |
2768 KB |
Output is correct |
22 |
Correct |
2 ms |
2768 KB |
Output is correct |
23 |
Correct |
2 ms |
2768 KB |
Output is correct |
24 |
Correct |
2 ms |
2768 KB |
Output is correct |
25 |
Correct |
2 ms |
2768 KB |
Output is correct |
26 |
Correct |
2 ms |
2768 KB |
Output is correct |
27 |
Correct |
2 ms |
2768 KB |
Output is correct |
28 |
Correct |
2 ms |
2768 KB |
Output is correct |
29 |
Correct |
3 ms |
2720 KB |
Output is correct |
30 |
Correct |
2 ms |
2768 KB |
Output is correct |
31 |
Correct |
2 ms |
2768 KB |
Output is correct |
32 |
Correct |
2 ms |
2768 KB |
Output is correct |
33 |
Correct |
2 ms |
2768 KB |
Output is correct |
34 |
Correct |
2 ms |
2768 KB |
Output is correct |
35 |
Correct |
2 ms |
2768 KB |
Output is correct |
36 |
Correct |
2 ms |
2896 KB |
Output is correct |
37 |
Correct |
2 ms |
2896 KB |
Output is correct |
38 |
Correct |
2 ms |
2896 KB |
Output is correct |
39 |
Correct |
3 ms |
2768 KB |
Output is correct |
40 |
Correct |
3 ms |
2768 KB |
Output is correct |
41 |
Correct |
2 ms |
2768 KB |
Output is correct |
42 |
Correct |
2 ms |
2816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
642 ms |
9544 KB |
Output is correct |
2 |
Runtime error |
28 ms |
14560 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
642 ms |
9544 KB |
Output is correct |
2 |
Runtime error |
28 ms |
14560 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
1 ms |
2640 KB |
Output is correct |
4 |
Correct |
2 ms |
2640 KB |
Output is correct |
5 |
Correct |
2 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2768 KB |
Output is correct |
7 |
Correct |
2 ms |
2768 KB |
Output is correct |
8 |
Correct |
2 ms |
2768 KB |
Output is correct |
9 |
Correct |
2 ms |
2768 KB |
Output is correct |
10 |
Correct |
2 ms |
2896 KB |
Output is correct |
11 |
Correct |
2 ms |
2896 KB |
Output is correct |
12 |
Correct |
2 ms |
2768 KB |
Output is correct |
13 |
Correct |
642 ms |
9544 KB |
Output is correct |
14 |
Runtime error |
28 ms |
14560 KB |
Execution killed with signal 11 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2768 KB |
Output is correct |
4 |
Correct |
2 ms |
2696 KB |
Output is correct |
5 |
Correct |
2 ms |
2768 KB |
Output is correct |
6 |
Correct |
2 ms |
2768 KB |
Output is correct |
7 |
Correct |
2 ms |
2768 KB |
Output is correct |
8 |
Correct |
2 ms |
2768 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2640 KB |
Output is correct |
11 |
Correct |
1 ms |
2640 KB |
Output is correct |
12 |
Correct |
2 ms |
2640 KB |
Output is correct |
13 |
Correct |
2 ms |
2640 KB |
Output is correct |
14 |
Correct |
2 ms |
2768 KB |
Output is correct |
15 |
Correct |
2 ms |
2768 KB |
Output is correct |
16 |
Correct |
2 ms |
2768 KB |
Output is correct |
17 |
Correct |
2 ms |
2768 KB |
Output is correct |
18 |
Correct |
2 ms |
2896 KB |
Output is correct |
19 |
Correct |
2 ms |
2896 KB |
Output is correct |
20 |
Correct |
2 ms |
2768 KB |
Output is correct |
21 |
Correct |
3 ms |
2768 KB |
Output is correct |
22 |
Correct |
2 ms |
2768 KB |
Output is correct |
23 |
Correct |
2 ms |
2768 KB |
Output is correct |
24 |
Correct |
2 ms |
2768 KB |
Output is correct |
25 |
Correct |
2 ms |
2768 KB |
Output is correct |
26 |
Correct |
2 ms |
2768 KB |
Output is correct |
27 |
Correct |
2 ms |
2768 KB |
Output is correct |
28 |
Correct |
2 ms |
2768 KB |
Output is correct |
29 |
Correct |
3 ms |
2720 KB |
Output is correct |
30 |
Correct |
2 ms |
2768 KB |
Output is correct |
31 |
Correct |
2 ms |
2768 KB |
Output is correct |
32 |
Correct |
2 ms |
2768 KB |
Output is correct |
33 |
Correct |
2 ms |
2768 KB |
Output is correct |
34 |
Correct |
2 ms |
2768 KB |
Output is correct |
35 |
Correct |
2 ms |
2768 KB |
Output is correct |
36 |
Correct |
2 ms |
2896 KB |
Output is correct |
37 |
Correct |
2 ms |
2896 KB |
Output is correct |
38 |
Correct |
2 ms |
2896 KB |
Output is correct |
39 |
Correct |
3 ms |
2768 KB |
Output is correct |
40 |
Correct |
3 ms |
2768 KB |
Output is correct |
41 |
Correct |
2 ms |
2768 KB |
Output is correct |
42 |
Correct |
2 ms |
2816 KB |
Output is correct |
43 |
Correct |
656 ms |
3152 KB |
Output is correct |
44 |
Correct |
873 ms |
3152 KB |
Output is correct |
45 |
Correct |
878 ms |
3280 KB |
Output is correct |
46 |
Correct |
774 ms |
3664 KB |
Output is correct |
47 |
Correct |
939 ms |
3692 KB |
Output is correct |
48 |
Correct |
748 ms |
3664 KB |
Output is correct |
49 |
Correct |
762 ms |
3664 KB |
Output is correct |
50 |
Correct |
779 ms |
3664 KB |
Output is correct |
51 |
Correct |
795 ms |
3408 KB |
Output is correct |
52 |
Correct |
697 ms |
3408 KB |
Output is correct |
53 |
Correct |
589 ms |
3408 KB |
Output is correct |
54 |
Correct |
764 ms |
3664 KB |
Output is correct |
55 |
Correct |
903 ms |
3536 KB |
Output is correct |
56 |
Correct |
785 ms |
3408 KB |
Output is correct |
57 |
Correct |
902 ms |
3408 KB |
Output is correct |
58 |
Correct |
861 ms |
3976 KB |
Output is correct |
59 |
Correct |
901 ms |
4116 KB |
Output is correct |
60 |
Correct |
711 ms |
4048 KB |
Output is correct |
61 |
Correct |
908 ms |
3280 KB |
Output is correct |
62 |
Correct |
797 ms |
3280 KB |
Output is correct |
63 |
Correct |
717 ms |
3280 KB |
Output is correct |
64 |
Correct |
924 ms |
3408 KB |
Output is correct |
65 |
Correct |
377 ms |
3024 KB |
Output is correct |
66 |
Correct |
772 ms |
3412 KB |
Output is correct |
67 |
Correct |
870 ms |
3408 KB |
Output is correct |
68 |
Correct |
783 ms |
3408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2768 KB |
Output is correct |
4 |
Correct |
2 ms |
2696 KB |
Output is correct |
5 |
Correct |
2 ms |
2768 KB |
Output is correct |
6 |
Correct |
2 ms |
2768 KB |
Output is correct |
7 |
Correct |
2 ms |
2768 KB |
Output is correct |
8 |
Correct |
2 ms |
2768 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2640 KB |
Output is correct |
11 |
Correct |
1 ms |
2640 KB |
Output is correct |
12 |
Correct |
2 ms |
2640 KB |
Output is correct |
13 |
Correct |
2 ms |
2640 KB |
Output is correct |
14 |
Correct |
2 ms |
2768 KB |
Output is correct |
15 |
Correct |
2 ms |
2768 KB |
Output is correct |
16 |
Correct |
2 ms |
2768 KB |
Output is correct |
17 |
Correct |
2 ms |
2768 KB |
Output is correct |
18 |
Correct |
2 ms |
2896 KB |
Output is correct |
19 |
Correct |
2 ms |
2896 KB |
Output is correct |
20 |
Correct |
2 ms |
2768 KB |
Output is correct |
21 |
Correct |
3 ms |
2768 KB |
Output is correct |
22 |
Correct |
2 ms |
2768 KB |
Output is correct |
23 |
Correct |
2 ms |
2768 KB |
Output is correct |
24 |
Correct |
2 ms |
2768 KB |
Output is correct |
25 |
Correct |
2 ms |
2768 KB |
Output is correct |
26 |
Correct |
2 ms |
2768 KB |
Output is correct |
27 |
Correct |
2 ms |
2768 KB |
Output is correct |
28 |
Correct |
2 ms |
2768 KB |
Output is correct |
29 |
Correct |
3 ms |
2720 KB |
Output is correct |
30 |
Correct |
2 ms |
2768 KB |
Output is correct |
31 |
Correct |
2 ms |
2768 KB |
Output is correct |
32 |
Correct |
2 ms |
2768 KB |
Output is correct |
33 |
Correct |
2 ms |
2768 KB |
Output is correct |
34 |
Correct |
2 ms |
2768 KB |
Output is correct |
35 |
Correct |
2 ms |
2768 KB |
Output is correct |
36 |
Correct |
2 ms |
2896 KB |
Output is correct |
37 |
Correct |
2 ms |
2896 KB |
Output is correct |
38 |
Correct |
2 ms |
2896 KB |
Output is correct |
39 |
Correct |
3 ms |
2768 KB |
Output is correct |
40 |
Correct |
3 ms |
2768 KB |
Output is correct |
41 |
Correct |
2 ms |
2768 KB |
Output is correct |
42 |
Correct |
2 ms |
2816 KB |
Output is correct |
43 |
Correct |
642 ms |
9544 KB |
Output is correct |
44 |
Runtime error |
28 ms |
14560 KB |
Execution killed with signal 11 |
45 |
Halted |
0 ms |
0 KB |
- |