#include "circuit.h"
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = (int)1e9 + 2022;
const int MAX = 1e6;
int par[MAX];
vector<int> childs[MAX];
int cntParams[MAX];
int nbInternes, nbFeuilles;
int coeff[MAX];
int iDeb[MAX], iFin[MAX];
int sum[MAX], sumCoeffs[MAX], lazy[MAX];
void pull(int node) {
sum[node] = sum[2 * node] + sum[2 * node + 1];
if (sum[node] >= MOD)
sum[node] -= MOD;
}
void push(int node) {
if (!lazy[node])
return;
sum[node] = sumCoeffs[node] - sum[node];
if (sum[node] < 0)
sum[node] += MOD;
if (iDeb[node] < iFin[node])
lazy[2 * node] ^= 1, lazy[2 * node + 1] ^= 1;
lazy[node] = 0;
}
void initSeg(int node, int l, int r, vector<signed> &valInit) {
iDeb[node] = l, iFin[node] = r;
if (l == r) {
sumCoeffs[node] = coeff[l];
sum[node] = valInit[l] * coeff[l];
return;
}
initSeg(2 * node, l, (l + r) / 2, valInit);
initSeg(2 * node + 1, (l + r) / 2 + 1, r, valInit);
pull(node);
sumCoeffs[node] = sumCoeffs[2 * node] + sumCoeffs[2 * node + 1];
if (sumCoeffs[node] >= MOD)
sumCoeffs[node] -= MOD;
}
void upd(int node, int l, int r) {
push(node);
if (l > iFin[node] or r < iDeb[node])
return;
if (l <= iDeb[node] and r >= iFin[node]) {
lazy[node] ^= 1;
push(node);
return;
}
upd(2 * node, l, r);
upd(2 * node + 1, l, r);
pull(node);
}
void setParams(int u) {
if (childs[u].empty()) {
cntParams[u] = 1;
return;
}
cntParams[u] = childs[u].size();
for (int v : childs[u]) {
setParams(v);
cntParams[u] = cntParams[u] * cntParams[v] % MOD;
}
}
void setCoeffs(int u, int bef) {
if (u >= nbInternes) {
coeff[u - nbInternes] = bef;
return;
}
int deg = childs[u].size();
vector<int> prefProd(deg + 1, 1), suffProd(deg + 1, 1);
for (int i = 0; i < deg; ++i)
prefProd[i + 1] = prefProd[i] * cntParams[childs[u][i]] % MOD;
for (int i = deg - 1; i >= 0; --i)
suffProd[i] = suffProd[i + 1] * cntParams[childs[u][i]] % MOD;
for (int i = 0; i < deg; ++i)
setCoeffs(childs[u][i], bef * prefProd[i] % MOD * suffProd[i + 1] % MOD);
}
void init(signed N, signed M, vector<signed> P, std::vector<signed> A) {
nbInternes = N, nbFeuilles = M;
int nbSommets = N + M;
for (int i = 1; i < nbSommets; ++i)
childs[P[i]].push_back(i);
setParams(0);
setCoeffs(0, 1);
/*for (int i = 0; i < nbFeuilles; ++i)
cout << coeff[i] << ' ';
cout << endl;*/
initSeg(1, 0, nbFeuilles - 1, A);
// cout << sum[1] << endl;
}
signed count_ways(signed L, signed R) {
L -= nbInternes, R -= nbInternes;
upd(1, L, R);
return sum[1];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23760 KB |
Output is correct |
2 |
Correct |
11 ms |
23760 KB |
Output is correct |
3 |
Correct |
12 ms |
23888 KB |
Output is correct |
4 |
Correct |
12 ms |
23888 KB |
Output is correct |
5 |
Correct |
12 ms |
23888 KB |
Output is correct |
6 |
Correct |
12 ms |
23888 KB |
Output is correct |
7 |
Correct |
14 ms |
23892 KB |
Output is correct |
8 |
Correct |
15 ms |
23888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23760 KB |
Output is correct |
2 |
Correct |
12 ms |
23892 KB |
Output is correct |
3 |
Correct |
12 ms |
23888 KB |
Output is correct |
4 |
Correct |
14 ms |
24016 KB |
Output is correct |
5 |
Correct |
12 ms |
23876 KB |
Output is correct |
6 |
Correct |
12 ms |
23888 KB |
Output is correct |
7 |
Correct |
12 ms |
23888 KB |
Output is correct |
8 |
Correct |
12 ms |
23940 KB |
Output is correct |
9 |
Correct |
13 ms |
23888 KB |
Output is correct |
10 |
Correct |
13 ms |
24188 KB |
Output is correct |
11 |
Correct |
12 ms |
24144 KB |
Output is correct |
12 |
Correct |
13 ms |
23888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23760 KB |
Output is correct |
2 |
Correct |
11 ms |
23760 KB |
Output is correct |
3 |
Correct |
12 ms |
23888 KB |
Output is correct |
4 |
Correct |
12 ms |
23888 KB |
Output is correct |
5 |
Correct |
12 ms |
23888 KB |
Output is correct |
6 |
Correct |
12 ms |
23888 KB |
Output is correct |
7 |
Correct |
14 ms |
23892 KB |
Output is correct |
8 |
Correct |
15 ms |
23888 KB |
Output is correct |
9 |
Correct |
12 ms |
23760 KB |
Output is correct |
10 |
Correct |
12 ms |
23892 KB |
Output is correct |
11 |
Correct |
12 ms |
23888 KB |
Output is correct |
12 |
Correct |
14 ms |
24016 KB |
Output is correct |
13 |
Correct |
12 ms |
23876 KB |
Output is correct |
14 |
Correct |
12 ms |
23888 KB |
Output is correct |
15 |
Correct |
12 ms |
23888 KB |
Output is correct |
16 |
Correct |
12 ms |
23940 KB |
Output is correct |
17 |
Correct |
13 ms |
23888 KB |
Output is correct |
18 |
Correct |
13 ms |
24188 KB |
Output is correct |
19 |
Correct |
12 ms |
24144 KB |
Output is correct |
20 |
Correct |
13 ms |
23888 KB |
Output is correct |
21 |
Correct |
12 ms |
23880 KB |
Output is correct |
22 |
Correct |
14 ms |
23856 KB |
Output is correct |
23 |
Correct |
14 ms |
23900 KB |
Output is correct |
24 |
Correct |
14 ms |
23888 KB |
Output is correct |
25 |
Correct |
13 ms |
24004 KB |
Output is correct |
26 |
Correct |
13 ms |
23896 KB |
Output is correct |
27 |
Correct |
12 ms |
24004 KB |
Output is correct |
28 |
Correct |
13 ms |
23936 KB |
Output is correct |
29 |
Correct |
12 ms |
23888 KB |
Output is correct |
30 |
Correct |
12 ms |
23888 KB |
Output is correct |
31 |
Correct |
13 ms |
24016 KB |
Output is correct |
32 |
Correct |
12 ms |
24000 KB |
Output is correct |
33 |
Correct |
14 ms |
23952 KB |
Output is correct |
34 |
Correct |
13 ms |
23888 KB |
Output is correct |
35 |
Correct |
12 ms |
23888 KB |
Output is correct |
36 |
Correct |
13 ms |
24144 KB |
Output is correct |
37 |
Correct |
13 ms |
24144 KB |
Output is correct |
38 |
Correct |
13 ms |
24144 KB |
Output is correct |
39 |
Correct |
12 ms |
23960 KB |
Output is correct |
40 |
Correct |
13 ms |
23888 KB |
Output is correct |
41 |
Correct |
13 ms |
23888 KB |
Output is correct |
42 |
Correct |
15 ms |
23980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
658 ms |
28368 KB |
Output is correct |
2 |
Correct |
943 ms |
33044 KB |
Output is correct |
3 |
Correct |
926 ms |
33032 KB |
Output is correct |
4 |
Correct |
988 ms |
33080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
658 ms |
28368 KB |
Output is correct |
2 |
Correct |
943 ms |
33044 KB |
Output is correct |
3 |
Correct |
926 ms |
33032 KB |
Output is correct |
4 |
Correct |
988 ms |
33080 KB |
Output is correct |
5 |
Correct |
684 ms |
28640 KB |
Output is correct |
6 |
Correct |
973 ms |
33352 KB |
Output is correct |
7 |
Correct |
905 ms |
33232 KB |
Output is correct |
8 |
Correct |
870 ms |
32968 KB |
Output is correct |
9 |
Correct |
360 ms |
24164 KB |
Output is correct |
10 |
Correct |
933 ms |
24488 KB |
Output is correct |
11 |
Correct |
686 ms |
24428 KB |
Output is correct |
12 |
Correct |
863 ms |
24300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23760 KB |
Output is correct |
2 |
Correct |
12 ms |
23892 KB |
Output is correct |
3 |
Correct |
12 ms |
23888 KB |
Output is correct |
4 |
Correct |
14 ms |
24016 KB |
Output is correct |
5 |
Correct |
12 ms |
23876 KB |
Output is correct |
6 |
Correct |
12 ms |
23888 KB |
Output is correct |
7 |
Correct |
12 ms |
23888 KB |
Output is correct |
8 |
Correct |
12 ms |
23940 KB |
Output is correct |
9 |
Correct |
13 ms |
23888 KB |
Output is correct |
10 |
Correct |
13 ms |
24188 KB |
Output is correct |
11 |
Correct |
12 ms |
24144 KB |
Output is correct |
12 |
Correct |
13 ms |
23888 KB |
Output is correct |
13 |
Correct |
658 ms |
28368 KB |
Output is correct |
14 |
Correct |
943 ms |
33044 KB |
Output is correct |
15 |
Correct |
926 ms |
33032 KB |
Output is correct |
16 |
Correct |
988 ms |
33080 KB |
Output is correct |
17 |
Correct |
684 ms |
28640 KB |
Output is correct |
18 |
Correct |
973 ms |
33352 KB |
Output is correct |
19 |
Correct |
905 ms |
33232 KB |
Output is correct |
20 |
Correct |
870 ms |
32968 KB |
Output is correct |
21 |
Correct |
360 ms |
24164 KB |
Output is correct |
22 |
Correct |
933 ms |
24488 KB |
Output is correct |
23 |
Correct |
686 ms |
24428 KB |
Output is correct |
24 |
Correct |
863 ms |
24300 KB |
Output is correct |
25 |
Correct |
1008 ms |
40644 KB |
Output is correct |
26 |
Correct |
920 ms |
40772 KB |
Output is correct |
27 |
Correct |
991 ms |
40772 KB |
Output is correct |
28 |
Correct |
738 ms |
39916 KB |
Output is correct |
29 |
Correct |
968 ms |
59544 KB |
Output is correct |
30 |
Correct |
907 ms |
58724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23760 KB |
Output is correct |
2 |
Correct |
11 ms |
23760 KB |
Output is correct |
3 |
Correct |
12 ms |
23888 KB |
Output is correct |
4 |
Correct |
12 ms |
23888 KB |
Output is correct |
5 |
Correct |
12 ms |
23888 KB |
Output is correct |
6 |
Correct |
12 ms |
23888 KB |
Output is correct |
7 |
Correct |
14 ms |
23892 KB |
Output is correct |
8 |
Correct |
15 ms |
23888 KB |
Output is correct |
9 |
Correct |
12 ms |
23760 KB |
Output is correct |
10 |
Correct |
12 ms |
23892 KB |
Output is correct |
11 |
Correct |
12 ms |
23888 KB |
Output is correct |
12 |
Correct |
14 ms |
24016 KB |
Output is correct |
13 |
Correct |
12 ms |
23876 KB |
Output is correct |
14 |
Correct |
12 ms |
23888 KB |
Output is correct |
15 |
Correct |
12 ms |
23888 KB |
Output is correct |
16 |
Correct |
12 ms |
23940 KB |
Output is correct |
17 |
Correct |
13 ms |
23888 KB |
Output is correct |
18 |
Correct |
13 ms |
24188 KB |
Output is correct |
19 |
Correct |
12 ms |
24144 KB |
Output is correct |
20 |
Correct |
13 ms |
23888 KB |
Output is correct |
21 |
Correct |
12 ms |
23880 KB |
Output is correct |
22 |
Correct |
14 ms |
23856 KB |
Output is correct |
23 |
Correct |
14 ms |
23900 KB |
Output is correct |
24 |
Correct |
14 ms |
23888 KB |
Output is correct |
25 |
Correct |
13 ms |
24004 KB |
Output is correct |
26 |
Correct |
13 ms |
23896 KB |
Output is correct |
27 |
Correct |
12 ms |
24004 KB |
Output is correct |
28 |
Correct |
13 ms |
23936 KB |
Output is correct |
29 |
Correct |
12 ms |
23888 KB |
Output is correct |
30 |
Correct |
12 ms |
23888 KB |
Output is correct |
31 |
Correct |
13 ms |
24016 KB |
Output is correct |
32 |
Correct |
12 ms |
24000 KB |
Output is correct |
33 |
Correct |
14 ms |
23952 KB |
Output is correct |
34 |
Correct |
13 ms |
23888 KB |
Output is correct |
35 |
Correct |
12 ms |
23888 KB |
Output is correct |
36 |
Correct |
13 ms |
24144 KB |
Output is correct |
37 |
Correct |
13 ms |
24144 KB |
Output is correct |
38 |
Correct |
13 ms |
24144 KB |
Output is correct |
39 |
Correct |
12 ms |
23960 KB |
Output is correct |
40 |
Correct |
13 ms |
23888 KB |
Output is correct |
41 |
Correct |
13 ms |
23888 KB |
Output is correct |
42 |
Correct |
15 ms |
23980 KB |
Output is correct |
43 |
Correct |
577 ms |
24400 KB |
Output is correct |
44 |
Correct |
772 ms |
24436 KB |
Output is correct |
45 |
Correct |
911 ms |
24400 KB |
Output is correct |
46 |
Correct |
652 ms |
24784 KB |
Output is correct |
47 |
Correct |
937 ms |
24792 KB |
Output is correct |
48 |
Correct |
848 ms |
24884 KB |
Output is correct |
49 |
Correct |
833 ms |
24912 KB |
Output is correct |
50 |
Correct |
679 ms |
24788 KB |
Output is correct |
51 |
Correct |
772 ms |
24776 KB |
Output is correct |
52 |
Correct |
937 ms |
24764 KB |
Output is correct |
53 |
Correct |
582 ms |
24948 KB |
Output is correct |
54 |
Correct |
1002 ms |
24824 KB |
Output is correct |
55 |
Correct |
917 ms |
24784 KB |
Output is correct |
56 |
Correct |
926 ms |
24656 KB |
Output is correct |
57 |
Correct |
933 ms |
24688 KB |
Output is correct |
58 |
Correct |
1000 ms |
25808 KB |
Output is correct |
59 |
Correct |
963 ms |
25808 KB |
Output is correct |
60 |
Correct |
802 ms |
25800 KB |
Output is correct |
61 |
Correct |
663 ms |
24528 KB |
Output is correct |
62 |
Correct |
916 ms |
24272 KB |
Output is correct |
63 |
Correct |
770 ms |
24400 KB |
Output is correct |
64 |
Correct |
830 ms |
24656 KB |
Output is correct |
65 |
Correct |
484 ms |
24072 KB |
Output is correct |
66 |
Correct |
818 ms |
24452 KB |
Output is correct |
67 |
Correct |
901 ms |
24460 KB |
Output is correct |
68 |
Correct |
926 ms |
24400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23760 KB |
Output is correct |
2 |
Correct |
11 ms |
23760 KB |
Output is correct |
3 |
Correct |
12 ms |
23888 KB |
Output is correct |
4 |
Correct |
12 ms |
23888 KB |
Output is correct |
5 |
Correct |
12 ms |
23888 KB |
Output is correct |
6 |
Correct |
12 ms |
23888 KB |
Output is correct |
7 |
Correct |
14 ms |
23892 KB |
Output is correct |
8 |
Correct |
15 ms |
23888 KB |
Output is correct |
9 |
Correct |
12 ms |
23760 KB |
Output is correct |
10 |
Correct |
12 ms |
23892 KB |
Output is correct |
11 |
Correct |
12 ms |
23888 KB |
Output is correct |
12 |
Correct |
14 ms |
24016 KB |
Output is correct |
13 |
Correct |
12 ms |
23876 KB |
Output is correct |
14 |
Correct |
12 ms |
23888 KB |
Output is correct |
15 |
Correct |
12 ms |
23888 KB |
Output is correct |
16 |
Correct |
12 ms |
23940 KB |
Output is correct |
17 |
Correct |
13 ms |
23888 KB |
Output is correct |
18 |
Correct |
13 ms |
24188 KB |
Output is correct |
19 |
Correct |
12 ms |
24144 KB |
Output is correct |
20 |
Correct |
13 ms |
23888 KB |
Output is correct |
21 |
Correct |
12 ms |
23880 KB |
Output is correct |
22 |
Correct |
14 ms |
23856 KB |
Output is correct |
23 |
Correct |
14 ms |
23900 KB |
Output is correct |
24 |
Correct |
14 ms |
23888 KB |
Output is correct |
25 |
Correct |
13 ms |
24004 KB |
Output is correct |
26 |
Correct |
13 ms |
23896 KB |
Output is correct |
27 |
Correct |
12 ms |
24004 KB |
Output is correct |
28 |
Correct |
13 ms |
23936 KB |
Output is correct |
29 |
Correct |
12 ms |
23888 KB |
Output is correct |
30 |
Correct |
12 ms |
23888 KB |
Output is correct |
31 |
Correct |
13 ms |
24016 KB |
Output is correct |
32 |
Correct |
12 ms |
24000 KB |
Output is correct |
33 |
Correct |
14 ms |
23952 KB |
Output is correct |
34 |
Correct |
13 ms |
23888 KB |
Output is correct |
35 |
Correct |
12 ms |
23888 KB |
Output is correct |
36 |
Correct |
13 ms |
24144 KB |
Output is correct |
37 |
Correct |
13 ms |
24144 KB |
Output is correct |
38 |
Correct |
13 ms |
24144 KB |
Output is correct |
39 |
Correct |
12 ms |
23960 KB |
Output is correct |
40 |
Correct |
13 ms |
23888 KB |
Output is correct |
41 |
Correct |
13 ms |
23888 KB |
Output is correct |
42 |
Correct |
15 ms |
23980 KB |
Output is correct |
43 |
Correct |
658 ms |
28368 KB |
Output is correct |
44 |
Correct |
943 ms |
33044 KB |
Output is correct |
45 |
Correct |
926 ms |
33032 KB |
Output is correct |
46 |
Correct |
988 ms |
33080 KB |
Output is correct |
47 |
Correct |
684 ms |
28640 KB |
Output is correct |
48 |
Correct |
973 ms |
33352 KB |
Output is correct |
49 |
Correct |
905 ms |
33232 KB |
Output is correct |
50 |
Correct |
870 ms |
32968 KB |
Output is correct |
51 |
Correct |
360 ms |
24164 KB |
Output is correct |
52 |
Correct |
933 ms |
24488 KB |
Output is correct |
53 |
Correct |
686 ms |
24428 KB |
Output is correct |
54 |
Correct |
863 ms |
24300 KB |
Output is correct |
55 |
Correct |
1008 ms |
40644 KB |
Output is correct |
56 |
Correct |
920 ms |
40772 KB |
Output is correct |
57 |
Correct |
991 ms |
40772 KB |
Output is correct |
58 |
Correct |
738 ms |
39916 KB |
Output is correct |
59 |
Correct |
968 ms |
59544 KB |
Output is correct |
60 |
Correct |
907 ms |
58724 KB |
Output is correct |
61 |
Correct |
577 ms |
24400 KB |
Output is correct |
62 |
Correct |
772 ms |
24436 KB |
Output is correct |
63 |
Correct |
911 ms |
24400 KB |
Output is correct |
64 |
Correct |
652 ms |
24784 KB |
Output is correct |
65 |
Correct |
937 ms |
24792 KB |
Output is correct |
66 |
Correct |
848 ms |
24884 KB |
Output is correct |
67 |
Correct |
833 ms |
24912 KB |
Output is correct |
68 |
Correct |
679 ms |
24788 KB |
Output is correct |
69 |
Correct |
772 ms |
24776 KB |
Output is correct |
70 |
Correct |
937 ms |
24764 KB |
Output is correct |
71 |
Correct |
582 ms |
24948 KB |
Output is correct |
72 |
Correct |
1002 ms |
24824 KB |
Output is correct |
73 |
Correct |
917 ms |
24784 KB |
Output is correct |
74 |
Correct |
926 ms |
24656 KB |
Output is correct |
75 |
Correct |
933 ms |
24688 KB |
Output is correct |
76 |
Correct |
1000 ms |
25808 KB |
Output is correct |
77 |
Correct |
963 ms |
25808 KB |
Output is correct |
78 |
Correct |
802 ms |
25800 KB |
Output is correct |
79 |
Correct |
663 ms |
24528 KB |
Output is correct |
80 |
Correct |
916 ms |
24272 KB |
Output is correct |
81 |
Correct |
770 ms |
24400 KB |
Output is correct |
82 |
Correct |
830 ms |
24656 KB |
Output is correct |
83 |
Correct |
484 ms |
24072 KB |
Output is correct |
84 |
Correct |
818 ms |
24452 KB |
Output is correct |
85 |
Correct |
901 ms |
24460 KB |
Output is correct |
86 |
Correct |
926 ms |
24400 KB |
Output is correct |
87 |
Correct |
12 ms |
23760 KB |
Output is correct |
88 |
Correct |
639 ms |
40556 KB |
Output is correct |
89 |
Correct |
1100 ms |
33528 KB |
Output is correct |
90 |
Correct |
989 ms |
33564 KB |
Output is correct |
91 |
Correct |
1074 ms |
40872 KB |
Output is correct |
92 |
Correct |
1271 ms |
41396 KB |
Output is correct |
93 |
Correct |
1027 ms |
41524 KB |
Output is correct |
94 |
Correct |
993 ms |
41380 KB |
Output is correct |
95 |
Correct |
1062 ms |
40420 KB |
Output is correct |
96 |
Correct |
1026 ms |
35996 KB |
Output is correct |
97 |
Correct |
958 ms |
37444 KB |
Output is correct |
98 |
Correct |
866 ms |
47208 KB |
Output is correct |
99 |
Correct |
1081 ms |
40772 KB |
Output is correct |
100 |
Correct |
1050 ms |
39700 KB |
Output is correct |
101 |
Correct |
957 ms |
38904 KB |
Output is correct |
102 |
Correct |
959 ms |
37520 KB |
Output is correct |
103 |
Correct |
1194 ms |
59544 KB |
Output is correct |
104 |
Correct |
980 ms |
60328 KB |
Output is correct |
105 |
Correct |
1141 ms |
59408 KB |
Output is correct |
106 |
Correct |
1102 ms |
37628 KB |
Output is correct |
107 |
Correct |
1004 ms |
37832 KB |
Output is correct |
108 |
Correct |
868 ms |
38060 KB |
Output is correct |
109 |
Correct |
1010 ms |
37708 KB |
Output is correct |