#include <bits/stdc++.h>
#include "circuit.h"
using namespace std;
const int BASE = 1e9 + 2022;
const int N = 200200;
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++)
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
5 ms |
5072 KB |
Output is correct |
4 |
Correct |
2 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
4 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5200 KB |
Output is correct |
8 |
Correct |
3 ms |
5200 KB |
Output is correct |
9 |
Correct |
3 ms |
5200 KB |
Output is correct |
10 |
Correct |
3 ms |
5200 KB |
Output is correct |
11 |
Correct |
3 ms |
5200 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
5 ms |
5072 KB |
Output is correct |
12 |
Correct |
2 ms |
5072 KB |
Output is correct |
13 |
Correct |
3 ms |
5072 KB |
Output is correct |
14 |
Correct |
4 ms |
5072 KB |
Output is correct |
15 |
Correct |
3 ms |
5200 KB |
Output is correct |
16 |
Correct |
3 ms |
5200 KB |
Output is correct |
17 |
Correct |
3 ms |
5200 KB |
Output is correct |
18 |
Correct |
3 ms |
5200 KB |
Output is correct |
19 |
Correct |
3 ms |
5200 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
5072 KB |
Output is correct |
24 |
Correct |
3 ms |
5200 KB |
Output is correct |
25 |
Correct |
4 ms |
5200 KB |
Output is correct |
26 |
Correct |
4 ms |
5200 KB |
Output is correct |
27 |
Correct |
4 ms |
5200 KB |
Output is correct |
28 |
Correct |
4 ms |
5112 KB |
Output is correct |
29 |
Correct |
5 ms |
5072 KB |
Output is correct |
30 |
Correct |
4 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5072 KB |
Output is correct |
32 |
Correct |
3 ms |
5200 KB |
Output is correct |
33 |
Correct |
3 ms |
5072 KB |
Output is correct |
34 |
Correct |
4 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5072 KB |
Output is correct |
36 |
Correct |
3 ms |
5200 KB |
Output is correct |
37 |
Correct |
3 ms |
5200 KB |
Output is correct |
38 |
Correct |
3 ms |
5200 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
639 ms |
11936 KB |
Output is correct |
2 |
Correct |
837 ms |
18760 KB |
Output is correct |
3 |
Correct |
863 ms |
18812 KB |
Output is correct |
4 |
Correct |
665 ms |
18760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
639 ms |
11936 KB |
Output is correct |
2 |
Correct |
837 ms |
18760 KB |
Output is correct |
3 |
Correct |
863 ms |
18812 KB |
Output is correct |
4 |
Correct |
665 ms |
18760 KB |
Output is correct |
5 |
Correct |
747 ms |
11832 KB |
Output is correct |
6 |
Correct |
952 ms |
18856 KB |
Output is correct |
7 |
Correct |
1010 ms |
18776 KB |
Output is correct |
8 |
Correct |
977 ms |
18812 KB |
Output is correct |
9 |
Correct |
475 ms |
5328 KB |
Output is correct |
10 |
Correct |
743 ms |
5840 KB |
Output is correct |
11 |
Correct |
847 ms |
5840 KB |
Output is correct |
12 |
Correct |
688 ms |
5840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
5 ms |
5072 KB |
Output is correct |
4 |
Correct |
2 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
4 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5200 KB |
Output is correct |
8 |
Correct |
3 ms |
5200 KB |
Output is correct |
9 |
Correct |
3 ms |
5200 KB |
Output is correct |
10 |
Correct |
3 ms |
5200 KB |
Output is correct |
11 |
Correct |
3 ms |
5200 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
639 ms |
11936 KB |
Output is correct |
14 |
Correct |
837 ms |
18760 KB |
Output is correct |
15 |
Correct |
863 ms |
18812 KB |
Output is correct |
16 |
Correct |
665 ms |
18760 KB |
Output is correct |
17 |
Correct |
747 ms |
11832 KB |
Output is correct |
18 |
Correct |
952 ms |
18856 KB |
Output is correct |
19 |
Correct |
1010 ms |
18776 KB |
Output is correct |
20 |
Correct |
977 ms |
18812 KB |
Output is correct |
21 |
Correct |
475 ms |
5328 KB |
Output is correct |
22 |
Correct |
743 ms |
5840 KB |
Output is correct |
23 |
Correct |
847 ms |
5840 KB |
Output is correct |
24 |
Correct |
688 ms |
5840 KB |
Output is correct |
25 |
Correct |
888 ms |
25744 KB |
Output is correct |
26 |
Correct |
1158 ms |
26120 KB |
Output is correct |
27 |
Correct |
984 ms |
26108 KB |
Output is correct |
28 |
Correct |
632 ms |
26104 KB |
Output is correct |
29 |
Correct |
874 ms |
33900 KB |
Output is correct |
30 |
Correct |
864 ms |
33916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
5 ms |
5072 KB |
Output is correct |
12 |
Correct |
2 ms |
5072 KB |
Output is correct |
13 |
Correct |
3 ms |
5072 KB |
Output is correct |
14 |
Correct |
4 ms |
5072 KB |
Output is correct |
15 |
Correct |
3 ms |
5200 KB |
Output is correct |
16 |
Correct |
3 ms |
5200 KB |
Output is correct |
17 |
Correct |
3 ms |
5200 KB |
Output is correct |
18 |
Correct |
3 ms |
5200 KB |
Output is correct |
19 |
Correct |
3 ms |
5200 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
5072 KB |
Output is correct |
24 |
Correct |
3 ms |
5200 KB |
Output is correct |
25 |
Correct |
4 ms |
5200 KB |
Output is correct |
26 |
Correct |
4 ms |
5200 KB |
Output is correct |
27 |
Correct |
4 ms |
5200 KB |
Output is correct |
28 |
Correct |
4 ms |
5112 KB |
Output is correct |
29 |
Correct |
5 ms |
5072 KB |
Output is correct |
30 |
Correct |
4 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5072 KB |
Output is correct |
32 |
Correct |
3 ms |
5200 KB |
Output is correct |
33 |
Correct |
3 ms |
5072 KB |
Output is correct |
34 |
Correct |
4 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5072 KB |
Output is correct |
36 |
Correct |
3 ms |
5200 KB |
Output is correct |
37 |
Correct |
3 ms |
5200 KB |
Output is correct |
38 |
Correct |
3 ms |
5200 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
3 ms |
5072 KB |
Output is correct |
43 |
Correct |
529 ms |
5584 KB |
Output is correct |
44 |
Correct |
799 ms |
5584 KB |
Output is correct |
45 |
Correct |
834 ms |
5584 KB |
Output is correct |
46 |
Correct |
498 ms |
5968 KB |
Output is correct |
47 |
Correct |
609 ms |
6048 KB |
Output is correct |
48 |
Correct |
835 ms |
5968 KB |
Output is correct |
49 |
Correct |
929 ms |
5968 KB |
Output is correct |
50 |
Correct |
952 ms |
5968 KB |
Output is correct |
51 |
Correct |
757 ms |
5712 KB |
Output is correct |
52 |
Correct |
941 ms |
5712 KB |
Output is correct |
53 |
Correct |
800 ms |
5712 KB |
Output is correct |
54 |
Correct |
815 ms |
5968 KB |
Output is correct |
55 |
Correct |
698 ms |
5840 KB |
Output is correct |
56 |
Correct |
892 ms |
5840 KB |
Output is correct |
57 |
Correct |
924 ms |
5712 KB |
Output is correct |
58 |
Correct |
789 ms |
6388 KB |
Output is correct |
59 |
Correct |
841 ms |
6352 KB |
Output is correct |
60 |
Correct |
825 ms |
6352 KB |
Output is correct |
61 |
Correct |
801 ms |
5584 KB |
Output is correct |
62 |
Correct |
938 ms |
5584 KB |
Output is correct |
63 |
Correct |
718 ms |
5584 KB |
Output is correct |
64 |
Correct |
721 ms |
5712 KB |
Output is correct |
65 |
Correct |
411 ms |
5412 KB |
Output is correct |
66 |
Correct |
790 ms |
5840 KB |
Output is correct |
67 |
Correct |
807 ms |
5840 KB |
Output is correct |
68 |
Correct |
957 ms |
5840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
5 ms |
5072 KB |
Output is correct |
12 |
Correct |
2 ms |
5072 KB |
Output is correct |
13 |
Correct |
3 ms |
5072 KB |
Output is correct |
14 |
Correct |
4 ms |
5072 KB |
Output is correct |
15 |
Correct |
3 ms |
5200 KB |
Output is correct |
16 |
Correct |
3 ms |
5200 KB |
Output is correct |
17 |
Correct |
3 ms |
5200 KB |
Output is correct |
18 |
Correct |
3 ms |
5200 KB |
Output is correct |
19 |
Correct |
3 ms |
5200 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
5072 KB |
Output is correct |
24 |
Correct |
3 ms |
5200 KB |
Output is correct |
25 |
Correct |
4 ms |
5200 KB |
Output is correct |
26 |
Correct |
4 ms |
5200 KB |
Output is correct |
27 |
Correct |
4 ms |
5200 KB |
Output is correct |
28 |
Correct |
4 ms |
5112 KB |
Output is correct |
29 |
Correct |
5 ms |
5072 KB |
Output is correct |
30 |
Correct |
4 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5072 KB |
Output is correct |
32 |
Correct |
3 ms |
5200 KB |
Output is correct |
33 |
Correct |
3 ms |
5072 KB |
Output is correct |
34 |
Correct |
4 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5072 KB |
Output is correct |
36 |
Correct |
3 ms |
5200 KB |
Output is correct |
37 |
Correct |
3 ms |
5200 KB |
Output is correct |
38 |
Correct |
3 ms |
5200 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
3 ms |
5072 KB |
Output is correct |
43 |
Correct |
639 ms |
11936 KB |
Output is correct |
44 |
Correct |
837 ms |
18760 KB |
Output is correct |
45 |
Correct |
863 ms |
18812 KB |
Output is correct |
46 |
Correct |
665 ms |
18760 KB |
Output is correct |
47 |
Correct |
747 ms |
11832 KB |
Output is correct |
48 |
Correct |
952 ms |
18856 KB |
Output is correct |
49 |
Correct |
1010 ms |
18776 KB |
Output is correct |
50 |
Correct |
977 ms |
18812 KB |
Output is correct |
51 |
Correct |
475 ms |
5328 KB |
Output is correct |
52 |
Correct |
743 ms |
5840 KB |
Output is correct |
53 |
Correct |
847 ms |
5840 KB |
Output is correct |
54 |
Correct |
688 ms |
5840 KB |
Output is correct |
55 |
Correct |
888 ms |
25744 KB |
Output is correct |
56 |
Correct |
1158 ms |
26120 KB |
Output is correct |
57 |
Correct |
984 ms |
26108 KB |
Output is correct |
58 |
Correct |
632 ms |
26104 KB |
Output is correct |
59 |
Correct |
874 ms |
33900 KB |
Output is correct |
60 |
Correct |
864 ms |
33916 KB |
Output is correct |
61 |
Correct |
529 ms |
5584 KB |
Output is correct |
62 |
Correct |
799 ms |
5584 KB |
Output is correct |
63 |
Correct |
834 ms |
5584 KB |
Output is correct |
64 |
Correct |
498 ms |
5968 KB |
Output is correct |
65 |
Correct |
609 ms |
6048 KB |
Output is correct |
66 |
Correct |
835 ms |
5968 KB |
Output is correct |
67 |
Correct |
929 ms |
5968 KB |
Output is correct |
68 |
Correct |
952 ms |
5968 KB |
Output is correct |
69 |
Correct |
757 ms |
5712 KB |
Output is correct |
70 |
Correct |
941 ms |
5712 KB |
Output is correct |
71 |
Correct |
800 ms |
5712 KB |
Output is correct |
72 |
Correct |
815 ms |
5968 KB |
Output is correct |
73 |
Correct |
698 ms |
5840 KB |
Output is correct |
74 |
Correct |
892 ms |
5840 KB |
Output is correct |
75 |
Correct |
924 ms |
5712 KB |
Output is correct |
76 |
Correct |
789 ms |
6388 KB |
Output is correct |
77 |
Correct |
841 ms |
6352 KB |
Output is correct |
78 |
Correct |
825 ms |
6352 KB |
Output is correct |
79 |
Correct |
801 ms |
5584 KB |
Output is correct |
80 |
Correct |
938 ms |
5584 KB |
Output is correct |
81 |
Correct |
718 ms |
5584 KB |
Output is correct |
82 |
Correct |
721 ms |
5712 KB |
Output is correct |
83 |
Correct |
411 ms |
5412 KB |
Output is correct |
84 |
Correct |
790 ms |
5840 KB |
Output is correct |
85 |
Correct |
807 ms |
5840 KB |
Output is correct |
86 |
Correct |
957 ms |
5840 KB |
Output is correct |
87 |
Correct |
3 ms |
4944 KB |
Output is correct |
88 |
Correct |
442 ms |
23068 KB |
Output is correct |
89 |
Correct |
1061 ms |
16856 KB |
Output is correct |
90 |
Correct |
1012 ms |
17608 KB |
Output is correct |
91 |
Correct |
920 ms |
26136 KB |
Output is correct |
92 |
Correct |
1022 ms |
26144 KB |
Output is correct |
93 |
Correct |
1136 ms |
26236 KB |
Output is correct |
94 |
Correct |
1014 ms |
26132 KB |
Output is correct |
95 |
Correct |
988 ms |
26236 KB |
Output is correct |
96 |
Correct |
845 ms |
20976 KB |
Output is correct |
97 |
Correct |
997 ms |
20960 KB |
Output is correct |
98 |
Correct |
923 ms |
21400 KB |
Output is correct |
99 |
Correct |
991 ms |
26136 KB |
Output is correct |
100 |
Correct |
892 ms |
23368 KB |
Output is correct |
101 |
Correct |
1031 ms |
22468 KB |
Output is correct |
102 |
Correct |
1073 ms |
21112 KB |
Output is correct |
103 |
Correct |
990 ms |
33896 KB |
Output is correct |
104 |
Correct |
928 ms |
34232 KB |
Output is correct |
105 |
Correct |
686 ms |
34328 KB |
Output is correct |
106 |
Correct |
891 ms |
17352 KB |
Output is correct |
107 |
Correct |
1024 ms |
17568 KB |
Output is correct |
108 |
Correct |
929 ms |
18812 KB |
Output is correct |
109 |
Correct |
952 ms |
21132 KB |
Output is correct |