#include "circuit.h"
#include <cmath>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
typedef long long ll;
int NN, MM;
const ll N = 2e5 + 10, mod = 1000002022;
int p[N], d[N];
int lazy[4 * N];
int a[N], cnt[N];
ll pref[N];
ll c[N];
ll tree2[4 * N];
void build_tree2(int v, int tl, int tr) {
if (tl == tr) {
if (a[tl]) {
tree2[v] = pref[tl];
if (tl) tree2[v] -= pref[tl - 1];
tree2[v] = (tree2[v] + mod) % mod;
}
else tree2[v] = 0;
}
else {
int tm = (tl + tr) / 2;
build_tree2(2 * v, tl, tm);
build_tree2(2 * v + 1, tm + 1, tr);
tree2[v] = tree2[2 * v] + tree2[2 * v + 1];
tree2[v] %= mod;
}
}
void pushh2(int v, int tl, int tr) {
if (lazy[v]) {
lazy[v] = 0;
if (tl != tr) {
lazy[2 * v] = (lazy[2 * v] + 1) % 2;
lazy[2 * v + 1] = (lazy[2 * v + 1] + 1) % 2;
int tm = (tl + tr) / 2;
ll u = pref[tm];
if (tl) u -= pref[tl - 1];
u += mod;
u %= mod;
tree2[2 * v] = (u + mod - tree2[2 * v]) % mod;
u = pref[tr] - pref[tm] + mod;
u %= mod;
tree2[2 * v + 1] = (u + mod - tree2[2 * v + 1]) % mod;
}
}
}
void update2(int v, int tl, int tr, int l, int r) {
if (tl > r || tr < l) return;
if (tl >= l && tr <= r) {
lazy[v] = (lazy[v] + 1) % 2;
ll u = pref[tr];
if (tl) u -= pref[tl - 1];
u += mod;
u %= mod;
tree2[v] = (u - tree2[v] + mod) % mod;
return;
}
pushh2(v, tl, tr);
int tm = (tl + tr) / 2;
update2(2 * v, tl, tm, l, r);
update2(2 * v + 1, tm + 1, tr, l, r);
tree2[v] = tree2[2 * v] + tree2[2 * v + 1];
tree2[v] %= mod;
}
ll dp[N];
vector <vector<int>> G;
void dfs(int v, int p) {
ll u = 1;
if (v >= NN) {
dp[v] = 1;
return;
}
bool ch = false;
for (auto it : G[v]) {
if (it == p) continue;
dfs(it, v);
u *= dp[it];
u %= mod;
ch = true;
}
if (!ch) {
dp[v] = 1;
return;
}
u *= (ll)G[v].size();
u %= mod;
dp[v] = u;
}
void dfss(int v, ll way) {
if (v >= NN) {
pref[v - NN] = way;
return;
}
vector <int> children;
for (auto it : G[v]) {
children.push_back(it);
}
vector <ll> pref((int)children.size()), suff((int)children.size());
for (int i = 0; i < (int)children.size(); i++) {
pref[i] = dp[children[i]];
if (i) pref[i] = (pref[i - 1] * pref[i]) % mod;
}
for (int i = (int)children.size() - 1; i >= 0; i--) {
suff[i] = dp[children[i]];
if (i != (int)children.size() - 1) {
suff[i] = (suff[i] * suff[i + 1]) % mod;
}
}
for (int i = 0; i < (int)children.size(); i++) {
ll u = way;
if (i) u *= pref[i - 1];
u %= mod;
if (i != (int)children.size() - 1) u *= suff[i + 1];
u %= mod;
dfss(children[i], u);
}
}
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
NN = N, MM = M;
G.resize(N + M);
for (int i = 0; i < N + M; i++) {
if (i) {
G[P[i]].push_back(i);
}
}
for (int i = 0; i < M; i++) {
a[i] = A[i];
}
dfs(0, -1);
dfss(0, 1);
for (int i = 0; i < M; i++) {
if (i) pref[i] += pref[i - 1];
pref[i] %= mod;
}
build_tree2(1, 0, MM - 1);
}
int count_ways(int L, int R) {
update2(1, 0, MM - 1, L - NN, R - NN);
return tree2[1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
464 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
720 KB |
Output is correct |
11 |
Correct |
1 ms |
720 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
720 KB |
Output is correct |
19 |
Correct |
1 ms |
720 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
732 KB |
Output is correct |
37 |
Correct |
2 ms |
720 KB |
Output is correct |
38 |
Correct |
1 ms |
720 KB |
Output is correct |
39 |
Correct |
1 ms |
464 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
615 ms |
5056 KB |
Output is correct |
2 |
Correct |
640 ms |
9776 KB |
Output is correct |
3 |
Correct |
747 ms |
9772 KB |
Output is correct |
4 |
Correct |
693 ms |
9800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
615 ms |
5056 KB |
Output is correct |
2 |
Correct |
640 ms |
9776 KB |
Output is correct |
3 |
Correct |
747 ms |
9772 KB |
Output is correct |
4 |
Correct |
693 ms |
9800 KB |
Output is correct |
5 |
Correct |
634 ms |
5072 KB |
Output is correct |
6 |
Correct |
772 ms |
9800 KB |
Output is correct |
7 |
Correct |
890 ms |
9800 KB |
Output is correct |
8 |
Correct |
675 ms |
9776 KB |
Output is correct |
9 |
Correct |
403 ms |
592 KB |
Output is correct |
10 |
Correct |
707 ms |
848 KB |
Output is correct |
11 |
Correct |
660 ms |
976 KB |
Output is correct |
12 |
Correct |
656 ms |
848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
464 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
720 KB |
Output is correct |
11 |
Correct |
1 ms |
720 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
615 ms |
5056 KB |
Output is correct |
14 |
Correct |
640 ms |
9776 KB |
Output is correct |
15 |
Correct |
747 ms |
9772 KB |
Output is correct |
16 |
Correct |
693 ms |
9800 KB |
Output is correct |
17 |
Correct |
634 ms |
5072 KB |
Output is correct |
18 |
Correct |
772 ms |
9800 KB |
Output is correct |
19 |
Correct |
890 ms |
9800 KB |
Output is correct |
20 |
Correct |
675 ms |
9776 KB |
Output is correct |
21 |
Correct |
403 ms |
592 KB |
Output is correct |
22 |
Correct |
707 ms |
848 KB |
Output is correct |
23 |
Correct |
660 ms |
976 KB |
Output is correct |
24 |
Correct |
656 ms |
848 KB |
Output is correct |
25 |
Correct |
821 ms |
15056 KB |
Output is correct |
26 |
Correct |
865 ms |
15264 KB |
Output is correct |
27 |
Correct |
819 ms |
15260 KB |
Output is correct |
28 |
Correct |
665 ms |
15272 KB |
Output is correct |
29 |
Correct |
839 ms |
40224 KB |
Output is correct |
30 |
Correct |
700 ms |
40356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
720 KB |
Output is correct |
19 |
Correct |
1 ms |
720 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
732 KB |
Output is correct |
37 |
Correct |
2 ms |
720 KB |
Output is correct |
38 |
Correct |
1 ms |
720 KB |
Output is correct |
39 |
Correct |
1 ms |
464 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
428 KB |
Output is correct |
43 |
Correct |
497 ms |
768 KB |
Output is correct |
44 |
Correct |
652 ms |
848 KB |
Output is correct |
45 |
Correct |
760 ms |
848 KB |
Output is correct |
46 |
Correct |
742 ms |
1104 KB |
Output is correct |
47 |
Correct |
742 ms |
1104 KB |
Output is correct |
48 |
Correct |
627 ms |
1104 KB |
Output is correct |
49 |
Correct |
563 ms |
1104 KB |
Output is correct |
50 |
Correct |
708 ms |
1104 KB |
Output is correct |
51 |
Correct |
551 ms |
912 KB |
Output is correct |
52 |
Correct |
567 ms |
976 KB |
Output is correct |
53 |
Correct |
541 ms |
1872 KB |
Output is correct |
54 |
Correct |
764 ms |
1104 KB |
Output is correct |
55 |
Correct |
772 ms |
940 KB |
Output is correct |
56 |
Correct |
851 ms |
848 KB |
Output is correct |
57 |
Correct |
663 ms |
808 KB |
Output is correct |
58 |
Correct |
645 ms |
2384 KB |
Output is correct |
59 |
Correct |
845 ms |
2500 KB |
Output is correct |
60 |
Correct |
716 ms |
2460 KB |
Output is correct |
61 |
Correct |
784 ms |
1072 KB |
Output is correct |
62 |
Correct |
816 ms |
720 KB |
Output is correct |
63 |
Correct |
764 ms |
720 KB |
Output is correct |
64 |
Correct |
721 ms |
848 KB |
Output is correct |
65 |
Correct |
377 ms |
592 KB |
Output is correct |
66 |
Correct |
829 ms |
848 KB |
Output is correct |
67 |
Correct |
754 ms |
944 KB |
Output is correct |
68 |
Correct |
751 ms |
848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
720 KB |
Output is correct |
19 |
Correct |
1 ms |
720 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
732 KB |
Output is correct |
37 |
Correct |
2 ms |
720 KB |
Output is correct |
38 |
Correct |
1 ms |
720 KB |
Output is correct |
39 |
Correct |
1 ms |
464 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
428 KB |
Output is correct |
43 |
Correct |
615 ms |
5056 KB |
Output is correct |
44 |
Correct |
640 ms |
9776 KB |
Output is correct |
45 |
Correct |
747 ms |
9772 KB |
Output is correct |
46 |
Correct |
693 ms |
9800 KB |
Output is correct |
47 |
Correct |
634 ms |
5072 KB |
Output is correct |
48 |
Correct |
772 ms |
9800 KB |
Output is correct |
49 |
Correct |
890 ms |
9800 KB |
Output is correct |
50 |
Correct |
675 ms |
9776 KB |
Output is correct |
51 |
Correct |
403 ms |
592 KB |
Output is correct |
52 |
Correct |
707 ms |
848 KB |
Output is correct |
53 |
Correct |
660 ms |
976 KB |
Output is correct |
54 |
Correct |
656 ms |
848 KB |
Output is correct |
55 |
Correct |
821 ms |
15056 KB |
Output is correct |
56 |
Correct |
865 ms |
15264 KB |
Output is correct |
57 |
Correct |
819 ms |
15260 KB |
Output is correct |
58 |
Correct |
665 ms |
15272 KB |
Output is correct |
59 |
Correct |
839 ms |
40224 KB |
Output is correct |
60 |
Correct |
700 ms |
40356 KB |
Output is correct |
61 |
Correct |
497 ms |
768 KB |
Output is correct |
62 |
Correct |
652 ms |
848 KB |
Output is correct |
63 |
Correct |
760 ms |
848 KB |
Output is correct |
64 |
Correct |
742 ms |
1104 KB |
Output is correct |
65 |
Correct |
742 ms |
1104 KB |
Output is correct |
66 |
Correct |
627 ms |
1104 KB |
Output is correct |
67 |
Correct |
563 ms |
1104 KB |
Output is correct |
68 |
Correct |
708 ms |
1104 KB |
Output is correct |
69 |
Correct |
551 ms |
912 KB |
Output is correct |
70 |
Correct |
567 ms |
976 KB |
Output is correct |
71 |
Correct |
541 ms |
1872 KB |
Output is correct |
72 |
Correct |
764 ms |
1104 KB |
Output is correct |
73 |
Correct |
772 ms |
940 KB |
Output is correct |
74 |
Correct |
851 ms |
848 KB |
Output is correct |
75 |
Correct |
663 ms |
808 KB |
Output is correct |
76 |
Correct |
645 ms |
2384 KB |
Output is correct |
77 |
Correct |
845 ms |
2500 KB |
Output is correct |
78 |
Correct |
716 ms |
2460 KB |
Output is correct |
79 |
Correct |
784 ms |
1072 KB |
Output is correct |
80 |
Correct |
816 ms |
720 KB |
Output is correct |
81 |
Correct |
764 ms |
720 KB |
Output is correct |
82 |
Correct |
721 ms |
848 KB |
Output is correct |
83 |
Correct |
377 ms |
592 KB |
Output is correct |
84 |
Correct |
829 ms |
848 KB |
Output is correct |
85 |
Correct |
754 ms |
944 KB |
Output is correct |
86 |
Correct |
751 ms |
848 KB |
Output is correct |
87 |
Correct |
0 ms |
208 KB |
Output is correct |
88 |
Correct |
530 ms |
13892 KB |
Output is correct |
89 |
Correct |
833 ms |
9760 KB |
Output is correct |
90 |
Correct |
938 ms |
9452 KB |
Output is correct |
91 |
Correct |
800 ms |
15356 KB |
Output is correct |
92 |
Correct |
816 ms |
15400 KB |
Output is correct |
93 |
Correct |
939 ms |
15376 KB |
Output is correct |
94 |
Correct |
930 ms |
15396 KB |
Output is correct |
95 |
Correct |
854 ms |
15384 KB |
Output is correct |
96 |
Correct |
856 ms |
8720 KB |
Output is correct |
97 |
Correct |
781 ms |
9016 KB |
Output is correct |
98 |
Correct |
685 ms |
32292 KB |
Output is correct |
99 |
Correct |
848 ms |
15268 KB |
Output is correct |
100 |
Correct |
753 ms |
11824 KB |
Output is correct |
101 |
Correct |
889 ms |
10664 KB |
Output is correct |
102 |
Correct |
838 ms |
9040 KB |
Output is correct |
103 |
Correct |
917 ms |
40252 KB |
Output is correct |
104 |
Correct |
840 ms |
40788 KB |
Output is correct |
105 |
Correct |
786 ms |
40720 KB |
Output is correct |
106 |
Correct |
824 ms |
14624 KB |
Output is correct |
107 |
Correct |
878 ms |
9556 KB |
Output is correct |
108 |
Correct |
770 ms |
9496 KB |
Output is correct |
109 |
Correct |
754 ms |
9204 KB |
Output is correct |