#include "circuit.h"
#include "bits/stdc++.h"
using namespace std;
const int MAX_N = 2e5 + 5;
const int md = 1000002022;
struct node {
int tot_sum = 0, sum = 0;
bool lazy = 0;
};
int add(int x, int y) {
x += y;
if (x >= md) x -= md;
return x;
}
int sub(int x, int y) {
x -= y;
if (x < 0) x += md;
return x;
}
int mul(int x, int y) {
return (int) ((long long) x * y % md);
}
int n, m;
vector<int> a;
int dp[MAX_N];
int contrib[MAX_N];
node seg[MAX_N * 2];
vector<int> g[MAX_N];
void dfs(int v) {
if (v >= n) {
dp[v] = 1;
return;
}
dp[v] = g[v].size();
for (int u : g[v]) {
dfs(u);
dp[v] = mul(dp[v], dp[u]);
}
//cerr << "V, dp[V]: " << v << ' ' << dp[v] << '\n';
}
void dfs2(int v, int val = 1) {
//cerr << "V, Val: " << v << ' ' << val << '\n';
if (v >= n) {
contrib[v - n] = val;
return;
}
int sz = (int) g[v].size();
vector<int> pref(sz), suf(sz);
pref[0] = suf[sz - 1] = 1;
for (int i = 1; i < sz; ++i) {
int u = g[v][i - 1];
pref[i] = mul(pref[i - 1], dp[u]);
}
for (int i = sz - 2; i >= 0; --i) {
int u = g[v][i + 1];
suf[i] = mul(suf[i + 1], dp[u]);
}
for (int i = 0; i < sz; ++i) {
int u = g[v][i];
int nval = mul(val, pref[i]);
nval = mul(nval, suf[i]);
dfs2(u, nval);
}
}
node combine(node lx, node rx) {
node ret;
ret.sum = add(lx.sum, rx.sum);
ret.tot_sum = add(lx.tot_sum, rx.tot_sum);
return ret;
}
void build(int nd, int l, int r) {
if (l == r) {
seg[nd].tot_sum = contrib[l];
if (a[l]) {
seg[nd].sum = contrib[l];
}
return;
}
int mid = (l + r) >> 1;
int rs = nd + ((mid - l + 1) << 1);
build(nd + 1, l, mid);
build(rs, mid + 1, r);
seg[nd] = combine(seg[nd + 1], seg[rs]);
}
void push(int nd, int l, int r) {
if (!seg[nd].lazy) {
return;
}
seg[nd].sum = sub(seg[nd].tot_sum, seg[nd].sum);
if (l != r) {
int mid = (l + r) >> 1;
int rs = nd + ((mid - l + 1) << 1);
seg[nd + 1].lazy ^= 1;
seg[rs].lazy ^= 1;
}
seg[nd].lazy = false;
}
void upd(int nd, int l, int r, int s, int e) {
//cerr << "l, r, s, e: " << l << ' ' << r << ' ' << s << ' ' << e << '\n';
push(nd, l, r);
if (l >= s && r <= e) {
seg[nd].lazy = 1;
push(nd, l, r);
return;
}
int mid = (l + r) >> 1;
int rs = nd + ((mid - l + 1) << 1);
if (mid >= e) {
push(rs, mid + 1, r);
upd(nd + 1, l, mid, s, e);
} else {
if (mid < s) {
push(nd + 1, l, mid);
upd(rs, mid + 1, r, s, e);
} else {
upd(nd + 1, l, mid, s, e);
upd(rs, mid + 1, r, s, e);
}
}
seg[nd] = combine(seg[nd + 1], seg[rs]);
return;
}
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
n = N;
m = M;
a = A;
for (int i = 1; i < n + m; ++i) {
g[P[i]].push_back(i);
}
dfs(0);
dfs2(0);
build(0, 0, m - 1);
}
int count_ways(int l, int r) {
upd(0, 0, m - 1, l - n, r - n);
return seg[0].sum;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
2 ms |
5208 KB |
Output is correct |
4 |
Correct |
2 ms |
5208 KB |
Output is correct |
5 |
Correct |
2 ms |
5208 KB |
Output is correct |
6 |
Correct |
2 ms |
5208 KB |
Output is correct |
7 |
Correct |
2 ms |
5144 KB |
Output is correct |
8 |
Correct |
2 ms |
5208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
3 ms |
4952 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
4 ms |
4952 KB |
Output is correct |
6 |
Correct |
2 ms |
5208 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
3 ms |
5208 KB |
Output is correct |
9 |
Correct |
4 ms |
5208 KB |
Output is correct |
10 |
Correct |
4 ms |
5208 KB |
Output is correct |
11 |
Correct |
3 ms |
5208 KB |
Output is correct |
12 |
Correct |
4 ms |
5208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
2 ms |
5208 KB |
Output is correct |
4 |
Correct |
2 ms |
5208 KB |
Output is correct |
5 |
Correct |
2 ms |
5208 KB |
Output is correct |
6 |
Correct |
2 ms |
5208 KB |
Output is correct |
7 |
Correct |
2 ms |
5144 KB |
Output is correct |
8 |
Correct |
2 ms |
5208 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
4952 KB |
Output is correct |
11 |
Correct |
3 ms |
4952 KB |
Output is correct |
12 |
Correct |
3 ms |
4956 KB |
Output is correct |
13 |
Correct |
4 ms |
4952 KB |
Output is correct |
14 |
Correct |
2 ms |
5208 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
3 ms |
5208 KB |
Output is correct |
17 |
Correct |
4 ms |
5208 KB |
Output is correct |
18 |
Correct |
4 ms |
5208 KB |
Output is correct |
19 |
Correct |
3 ms |
5208 KB |
Output is correct |
20 |
Correct |
4 ms |
5208 KB |
Output is correct |
21 |
Correct |
2 ms |
5208 KB |
Output is correct |
22 |
Correct |
3 ms |
5208 KB |
Output is correct |
23 |
Correct |
2 ms |
4952 KB |
Output is correct |
24 |
Correct |
3 ms |
5208 KB |
Output is correct |
25 |
Correct |
2 ms |
5208 KB |
Output is correct |
26 |
Correct |
2 ms |
5208 KB |
Output is correct |
27 |
Correct |
4 ms |
5208 KB |
Output is correct |
28 |
Correct |
4 ms |
5192 KB |
Output is correct |
29 |
Correct |
3 ms |
5208 KB |
Output is correct |
30 |
Correct |
2 ms |
5208 KB |
Output is correct |
31 |
Correct |
4 ms |
5208 KB |
Output is correct |
32 |
Correct |
3 ms |
5208 KB |
Output is correct |
33 |
Correct |
4 ms |
5208 KB |
Output is correct |
34 |
Correct |
4 ms |
5208 KB |
Output is correct |
35 |
Correct |
2 ms |
4952 KB |
Output is correct |
36 |
Correct |
2 ms |
5224 KB |
Output is correct |
37 |
Correct |
2 ms |
5208 KB |
Output is correct |
38 |
Correct |
2 ms |
5208 KB |
Output is correct |
39 |
Correct |
2 ms |
5208 KB |
Output is correct |
40 |
Correct |
2 ms |
5208 KB |
Output is correct |
41 |
Correct |
4 ms |
5208 KB |
Output is correct |
42 |
Correct |
2 ms |
5208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
459 ms |
8104 KB |
Output is correct |
2 |
Correct |
668 ms |
11088 KB |
Output is correct |
3 |
Correct |
582 ms |
11300 KB |
Output is correct |
4 |
Correct |
645 ms |
11096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
459 ms |
8104 KB |
Output is correct |
2 |
Correct |
668 ms |
11088 KB |
Output is correct |
3 |
Correct |
582 ms |
11300 KB |
Output is correct |
4 |
Correct |
645 ms |
11096 KB |
Output is correct |
5 |
Correct |
497 ms |
8024 KB |
Output is correct |
6 |
Correct |
726 ms |
11300 KB |
Output is correct |
7 |
Correct |
670 ms |
11308 KB |
Output is correct |
8 |
Correct |
675 ms |
11224 KB |
Output is correct |
9 |
Correct |
313 ms |
5208 KB |
Output is correct |
10 |
Correct |
680 ms |
5464 KB |
Output is correct |
11 |
Correct |
552 ms |
5464 KB |
Output is correct |
12 |
Correct |
608 ms |
5464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
3 ms |
4952 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
4 ms |
4952 KB |
Output is correct |
6 |
Correct |
2 ms |
5208 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
3 ms |
5208 KB |
Output is correct |
9 |
Correct |
4 ms |
5208 KB |
Output is correct |
10 |
Correct |
4 ms |
5208 KB |
Output is correct |
11 |
Correct |
3 ms |
5208 KB |
Output is correct |
12 |
Correct |
4 ms |
5208 KB |
Output is correct |
13 |
Correct |
459 ms |
8104 KB |
Output is correct |
14 |
Correct |
668 ms |
11088 KB |
Output is correct |
15 |
Correct |
582 ms |
11300 KB |
Output is correct |
16 |
Correct |
645 ms |
11096 KB |
Output is correct |
17 |
Correct |
497 ms |
8024 KB |
Output is correct |
18 |
Correct |
726 ms |
11300 KB |
Output is correct |
19 |
Correct |
670 ms |
11308 KB |
Output is correct |
20 |
Correct |
675 ms |
11224 KB |
Output is correct |
21 |
Correct |
313 ms |
5208 KB |
Output is correct |
22 |
Correct |
680 ms |
5464 KB |
Output is correct |
23 |
Correct |
552 ms |
5464 KB |
Output is correct |
24 |
Correct |
608 ms |
5464 KB |
Output is correct |
25 |
Correct |
672 ms |
14160 KB |
Output is correct |
26 |
Correct |
737 ms |
14292 KB |
Output is correct |
27 |
Correct |
729 ms |
14416 KB |
Output is correct |
28 |
Correct |
564 ms |
14416 KB |
Output is correct |
29 |
Correct |
721 ms |
33104 KB |
Output is correct |
30 |
Correct |
697 ms |
33104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
2 ms |
5208 KB |
Output is correct |
4 |
Correct |
2 ms |
5208 KB |
Output is correct |
5 |
Correct |
2 ms |
5208 KB |
Output is correct |
6 |
Correct |
2 ms |
5208 KB |
Output is correct |
7 |
Correct |
2 ms |
5144 KB |
Output is correct |
8 |
Correct |
2 ms |
5208 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
4952 KB |
Output is correct |
11 |
Correct |
3 ms |
4952 KB |
Output is correct |
12 |
Correct |
3 ms |
4956 KB |
Output is correct |
13 |
Correct |
4 ms |
4952 KB |
Output is correct |
14 |
Correct |
2 ms |
5208 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
3 ms |
5208 KB |
Output is correct |
17 |
Correct |
4 ms |
5208 KB |
Output is correct |
18 |
Correct |
4 ms |
5208 KB |
Output is correct |
19 |
Correct |
3 ms |
5208 KB |
Output is correct |
20 |
Correct |
4 ms |
5208 KB |
Output is correct |
21 |
Correct |
2 ms |
5208 KB |
Output is correct |
22 |
Correct |
3 ms |
5208 KB |
Output is correct |
23 |
Correct |
2 ms |
4952 KB |
Output is correct |
24 |
Correct |
3 ms |
5208 KB |
Output is correct |
25 |
Correct |
2 ms |
5208 KB |
Output is correct |
26 |
Correct |
2 ms |
5208 KB |
Output is correct |
27 |
Correct |
4 ms |
5208 KB |
Output is correct |
28 |
Correct |
4 ms |
5192 KB |
Output is correct |
29 |
Correct |
3 ms |
5208 KB |
Output is correct |
30 |
Correct |
2 ms |
5208 KB |
Output is correct |
31 |
Correct |
4 ms |
5208 KB |
Output is correct |
32 |
Correct |
3 ms |
5208 KB |
Output is correct |
33 |
Correct |
4 ms |
5208 KB |
Output is correct |
34 |
Correct |
4 ms |
5208 KB |
Output is correct |
35 |
Correct |
2 ms |
4952 KB |
Output is correct |
36 |
Correct |
2 ms |
5224 KB |
Output is correct |
37 |
Correct |
2 ms |
5208 KB |
Output is correct |
38 |
Correct |
2 ms |
5208 KB |
Output is correct |
39 |
Correct |
2 ms |
5208 KB |
Output is correct |
40 |
Correct |
2 ms |
5208 KB |
Output is correct |
41 |
Correct |
4 ms |
5208 KB |
Output is correct |
42 |
Correct |
2 ms |
5208 KB |
Output is correct |
43 |
Correct |
406 ms |
5208 KB |
Output is correct |
44 |
Correct |
661 ms |
5208 KB |
Output is correct |
45 |
Correct |
630 ms |
5208 KB |
Output is correct |
46 |
Correct |
606 ms |
5464 KB |
Output is correct |
47 |
Correct |
651 ms |
5464 KB |
Output is correct |
48 |
Correct |
583 ms |
5464 KB |
Output is correct |
49 |
Correct |
655 ms |
5464 KB |
Output is correct |
50 |
Correct |
635 ms |
5464 KB |
Output is correct |
51 |
Correct |
593 ms |
5464 KB |
Output is correct |
52 |
Correct |
673 ms |
5464 KB |
Output is correct |
53 |
Correct |
574 ms |
6232 KB |
Output is correct |
54 |
Correct |
615 ms |
5464 KB |
Output is correct |
55 |
Correct |
571 ms |
5464 KB |
Output is correct |
56 |
Correct |
678 ms |
5464 KB |
Output is correct |
57 |
Correct |
661 ms |
5208 KB |
Output is correct |
58 |
Correct |
623 ms |
6488 KB |
Output is correct |
59 |
Correct |
667 ms |
6488 KB |
Output is correct |
60 |
Correct |
554 ms |
6488 KB |
Output is correct |
61 |
Correct |
645 ms |
5464 KB |
Output is correct |
62 |
Correct |
620 ms |
5208 KB |
Output is correct |
63 |
Correct |
658 ms |
5208 KB |
Output is correct |
64 |
Correct |
614 ms |
5208 KB |
Output is correct |
65 |
Correct |
319 ms |
5208 KB |
Output is correct |
66 |
Correct |
709 ms |
5464 KB |
Output is correct |
67 |
Correct |
578 ms |
5464 KB |
Output is correct |
68 |
Correct |
630 ms |
5464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
2 ms |
5208 KB |
Output is correct |
4 |
Correct |
2 ms |
5208 KB |
Output is correct |
5 |
Correct |
2 ms |
5208 KB |
Output is correct |
6 |
Correct |
2 ms |
5208 KB |
Output is correct |
7 |
Correct |
2 ms |
5144 KB |
Output is correct |
8 |
Correct |
2 ms |
5208 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
4952 KB |
Output is correct |
11 |
Correct |
3 ms |
4952 KB |
Output is correct |
12 |
Correct |
3 ms |
4956 KB |
Output is correct |
13 |
Correct |
4 ms |
4952 KB |
Output is correct |
14 |
Correct |
2 ms |
5208 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
3 ms |
5208 KB |
Output is correct |
17 |
Correct |
4 ms |
5208 KB |
Output is correct |
18 |
Correct |
4 ms |
5208 KB |
Output is correct |
19 |
Correct |
3 ms |
5208 KB |
Output is correct |
20 |
Correct |
4 ms |
5208 KB |
Output is correct |
21 |
Correct |
2 ms |
5208 KB |
Output is correct |
22 |
Correct |
3 ms |
5208 KB |
Output is correct |
23 |
Correct |
2 ms |
4952 KB |
Output is correct |
24 |
Correct |
3 ms |
5208 KB |
Output is correct |
25 |
Correct |
2 ms |
5208 KB |
Output is correct |
26 |
Correct |
2 ms |
5208 KB |
Output is correct |
27 |
Correct |
4 ms |
5208 KB |
Output is correct |
28 |
Correct |
4 ms |
5192 KB |
Output is correct |
29 |
Correct |
3 ms |
5208 KB |
Output is correct |
30 |
Correct |
2 ms |
5208 KB |
Output is correct |
31 |
Correct |
4 ms |
5208 KB |
Output is correct |
32 |
Correct |
3 ms |
5208 KB |
Output is correct |
33 |
Correct |
4 ms |
5208 KB |
Output is correct |
34 |
Correct |
4 ms |
5208 KB |
Output is correct |
35 |
Correct |
2 ms |
4952 KB |
Output is correct |
36 |
Correct |
2 ms |
5224 KB |
Output is correct |
37 |
Correct |
2 ms |
5208 KB |
Output is correct |
38 |
Correct |
2 ms |
5208 KB |
Output is correct |
39 |
Correct |
2 ms |
5208 KB |
Output is correct |
40 |
Correct |
2 ms |
5208 KB |
Output is correct |
41 |
Correct |
4 ms |
5208 KB |
Output is correct |
42 |
Correct |
2 ms |
5208 KB |
Output is correct |
43 |
Correct |
459 ms |
8104 KB |
Output is correct |
44 |
Correct |
668 ms |
11088 KB |
Output is correct |
45 |
Correct |
582 ms |
11300 KB |
Output is correct |
46 |
Correct |
645 ms |
11096 KB |
Output is correct |
47 |
Correct |
497 ms |
8024 KB |
Output is correct |
48 |
Correct |
726 ms |
11300 KB |
Output is correct |
49 |
Correct |
670 ms |
11308 KB |
Output is correct |
50 |
Correct |
675 ms |
11224 KB |
Output is correct |
51 |
Correct |
313 ms |
5208 KB |
Output is correct |
52 |
Correct |
680 ms |
5464 KB |
Output is correct |
53 |
Correct |
552 ms |
5464 KB |
Output is correct |
54 |
Correct |
608 ms |
5464 KB |
Output is correct |
55 |
Correct |
672 ms |
14160 KB |
Output is correct |
56 |
Correct |
737 ms |
14292 KB |
Output is correct |
57 |
Correct |
729 ms |
14416 KB |
Output is correct |
58 |
Correct |
564 ms |
14416 KB |
Output is correct |
59 |
Correct |
721 ms |
33104 KB |
Output is correct |
60 |
Correct |
697 ms |
33104 KB |
Output is correct |
61 |
Correct |
406 ms |
5208 KB |
Output is correct |
62 |
Correct |
661 ms |
5208 KB |
Output is correct |
63 |
Correct |
630 ms |
5208 KB |
Output is correct |
64 |
Correct |
606 ms |
5464 KB |
Output is correct |
65 |
Correct |
651 ms |
5464 KB |
Output is correct |
66 |
Correct |
583 ms |
5464 KB |
Output is correct |
67 |
Correct |
655 ms |
5464 KB |
Output is correct |
68 |
Correct |
635 ms |
5464 KB |
Output is correct |
69 |
Correct |
593 ms |
5464 KB |
Output is correct |
70 |
Correct |
673 ms |
5464 KB |
Output is correct |
71 |
Correct |
574 ms |
6232 KB |
Output is correct |
72 |
Correct |
615 ms |
5464 KB |
Output is correct |
73 |
Correct |
571 ms |
5464 KB |
Output is correct |
74 |
Correct |
678 ms |
5464 KB |
Output is correct |
75 |
Correct |
661 ms |
5208 KB |
Output is correct |
76 |
Correct |
623 ms |
6488 KB |
Output is correct |
77 |
Correct |
667 ms |
6488 KB |
Output is correct |
78 |
Correct |
554 ms |
6488 KB |
Output is correct |
79 |
Correct |
645 ms |
5464 KB |
Output is correct |
80 |
Correct |
620 ms |
5208 KB |
Output is correct |
81 |
Correct |
658 ms |
5208 KB |
Output is correct |
82 |
Correct |
614 ms |
5208 KB |
Output is correct |
83 |
Correct |
319 ms |
5208 KB |
Output is correct |
84 |
Correct |
709 ms |
5464 KB |
Output is correct |
85 |
Correct |
578 ms |
5464 KB |
Output is correct |
86 |
Correct |
630 ms |
5464 KB |
Output is correct |
87 |
Correct |
2 ms |
4952 KB |
Output is correct |
88 |
Correct |
446 ms |
13304 KB |
Output is correct |
89 |
Correct |
678 ms |
10888 KB |
Output is correct |
90 |
Correct |
721 ms |
10840 KB |
Output is correct |
91 |
Correct |
714 ms |
14420 KB |
Output is correct |
92 |
Correct |
691 ms |
14416 KB |
Output is correct |
93 |
Correct |
723 ms |
14416 KB |
Output is correct |
94 |
Correct |
741 ms |
14520 KB |
Output is correct |
95 |
Correct |
678 ms |
14416 KB |
Output is correct |
96 |
Correct |
610 ms |
10688 KB |
Output is correct |
97 |
Correct |
668 ms |
10688 KB |
Output is correct |
98 |
Correct |
575 ms |
27984 KB |
Output is correct |
99 |
Correct |
766 ms |
14516 KB |
Output is correct |
100 |
Correct |
753 ms |
12368 KB |
Output is correct |
101 |
Correct |
707 ms |
11600 KB |
Output is correct |
102 |
Correct |
701 ms |
10584 KB |
Output is correct |
103 |
Correct |
698 ms |
33104 KB |
Output is correct |
104 |
Correct |
614 ms |
33576 KB |
Output is correct |
105 |
Correct |
630 ms |
33476 KB |
Output is correct |
106 |
Correct |
676 ms |
14416 KB |
Output is correct |
107 |
Correct |
650 ms |
10328 KB |
Output is correct |
108 |
Correct |
731 ms |
10476 KB |
Output is correct |
109 |
Correct |
705 ms |
10840 KB |
Output is correct |