#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int maxN = 2e5 + 5;
const int mod = 1000002022;
void add(int &a, int b){
a += b;
if (a >= mod) a -= mod;
if (a < 0) a += mod;
}
vector <int> adj[maxN], pre[maxN], suf[maxN];
int val[maxN], sz[maxN], dp[maxN];
void dfs(int u){
sz[u] = adj[u].size();
dp[u] = max(sz[u], 1);
for (int i = 0; i < sz[u]; ++ i){
int v = adj[u][i];
dfs(v);
if (!i) pre[u].pb(dp[v]);
else pre[u].pb(1ll * pre[u].back() * dp[v] % mod);
suf[u].pb(0);
dp[u] = 1ll * dp[u] * dp[v] % mod;
}
for (int i = sz[u] - 1; i >= 0; -- i){
int v = adj[u][i];
if (i == sz[u] - 1) suf[u][i] = dp[v];
else suf[u][i] = 1ll * suf[u][i + 1] * dp[v] % mod;
}
}
void get(int u){
for (int i = 0; i < sz[u]; ++ i){
int v = adj[u][i];
val[v] = val[u];
if (i > 0) val[v] = 1ll * val[v] * pre[u][i - 1] % mod;
if (i < sz[u] - 1) val[v] = 1ll * val[v] * suf[u][i + 1] % mod;
get(v);
}
}
int sum[2][4 * maxN];
bool used[maxN], rev[4 * maxN];
void mer(int s){
sum[0][s] = 0;
sum[1][s] = 0;
for (int i = 0; i <= 1; ++ i){
for (int j = 2 * s; j <= 2 * s + 1; ++ j) add(sum[i][s], sum[i][j]);
}
}
void build(int s, int l, int r, int n){
rev[s] = 0;
if (l == r){
sum[0][s] = used[l] * val[l + n];
sum[1][s] = (1 ^ used[l]) * val[l + n];
return;
}
int mid = (l + r) / 2;
build(2 * s, l, mid, n);
build(2 * s + 1, mid + 1, r, n);
mer(s);
}
int kk, gg;
void init(int N, int M, std::vector<int> P, std::vector<int> A){
kk = N;
gg = M;
for (int i = 1; i < N + M; ++ i) adj[P[i]].pb(i);
dfs(0);
val[0] = 1;
get(0);
for (int i = 0; i < M; ++ i) used[i] = A[i];
build(1, 0, M - 1, N);
}
void down(int s){
if (!rev[s]) return;
for (int i = 2 * s; i <= 2 * s + 1; ++ i) {
rev[i] ^= 1;
swap(sum[0][i], sum[1][i]);
}
rev[s] = 0;
}
void upd(int s, int l, int r, int u, int v){
if (u <= l && r <= v){
rev[s] ^= 1;
swap(sum[0][s], sum[1][s]);
return;
}
int mid = (l + r) / 2;
down(s);
if (mid >= u) upd(2 * s, l, mid, u, v);
if (mid + 1 <= v) upd(2 * s + 1, mid + 1, r, u, v);
mer(s);
}
int count_ways(int L, int R){
upd(1, 0, gg - 1, L - kk, R - kk);
return sum[0][1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
21848 KB |
Output is correct |
2 |
Correct |
5 ms |
21848 KB |
Output is correct |
3 |
Correct |
6 ms |
21848 KB |
Output is correct |
4 |
Correct |
6 ms |
21848 KB |
Output is correct |
5 |
Correct |
6 ms |
21848 KB |
Output is correct |
6 |
Correct |
6 ms |
22260 KB |
Output is correct |
7 |
Correct |
6 ms |
21848 KB |
Output is correct |
8 |
Correct |
6 ms |
21848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
21848 KB |
Output is correct |
2 |
Correct |
6 ms |
21812 KB |
Output is correct |
3 |
Correct |
6 ms |
21844 KB |
Output is correct |
4 |
Correct |
6 ms |
21944 KB |
Output is correct |
5 |
Correct |
7 ms |
21848 KB |
Output is correct |
6 |
Correct |
6 ms |
21848 KB |
Output is correct |
7 |
Correct |
6 ms |
21844 KB |
Output is correct |
8 |
Correct |
6 ms |
21852 KB |
Output is correct |
9 |
Correct |
5 ms |
21848 KB |
Output is correct |
10 |
Correct |
5 ms |
21848 KB |
Output is correct |
11 |
Correct |
5 ms |
21860 KB |
Output is correct |
12 |
Correct |
5 ms |
21848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
21848 KB |
Output is correct |
2 |
Correct |
5 ms |
21848 KB |
Output is correct |
3 |
Correct |
6 ms |
21848 KB |
Output is correct |
4 |
Correct |
6 ms |
21848 KB |
Output is correct |
5 |
Correct |
6 ms |
21848 KB |
Output is correct |
6 |
Correct |
6 ms |
22260 KB |
Output is correct |
7 |
Correct |
6 ms |
21848 KB |
Output is correct |
8 |
Correct |
6 ms |
21848 KB |
Output is correct |
9 |
Correct |
6 ms |
21848 KB |
Output is correct |
10 |
Correct |
6 ms |
21812 KB |
Output is correct |
11 |
Correct |
6 ms |
21844 KB |
Output is correct |
12 |
Correct |
6 ms |
21944 KB |
Output is correct |
13 |
Correct |
7 ms |
21848 KB |
Output is correct |
14 |
Correct |
6 ms |
21848 KB |
Output is correct |
15 |
Correct |
6 ms |
21844 KB |
Output is correct |
16 |
Correct |
6 ms |
21852 KB |
Output is correct |
17 |
Correct |
5 ms |
21848 KB |
Output is correct |
18 |
Correct |
5 ms |
21848 KB |
Output is correct |
19 |
Correct |
5 ms |
21860 KB |
Output is correct |
20 |
Correct |
5 ms |
21848 KB |
Output is correct |
21 |
Correct |
5 ms |
21848 KB |
Output is correct |
22 |
Correct |
5 ms |
21848 KB |
Output is correct |
23 |
Correct |
5 ms |
21848 KB |
Output is correct |
24 |
Correct |
6 ms |
21860 KB |
Output is correct |
25 |
Correct |
7 ms |
21848 KB |
Output is correct |
26 |
Correct |
6 ms |
21848 KB |
Output is correct |
27 |
Correct |
6 ms |
21848 KB |
Output is correct |
28 |
Correct |
5 ms |
21848 KB |
Output is correct |
29 |
Correct |
5 ms |
21848 KB |
Output is correct |
30 |
Correct |
6 ms |
21848 KB |
Output is correct |
31 |
Correct |
6 ms |
21848 KB |
Output is correct |
32 |
Correct |
6 ms |
22024 KB |
Output is correct |
33 |
Correct |
6 ms |
21848 KB |
Output is correct |
34 |
Correct |
6 ms |
21848 KB |
Output is correct |
35 |
Correct |
5 ms |
21848 KB |
Output is correct |
36 |
Correct |
6 ms |
21848 KB |
Output is correct |
37 |
Correct |
6 ms |
22104 KB |
Output is correct |
38 |
Correct |
5 ms |
22104 KB |
Output is correct |
39 |
Correct |
5 ms |
21848 KB |
Output is correct |
40 |
Correct |
6 ms |
22100 KB |
Output is correct |
41 |
Correct |
6 ms |
21848 KB |
Output is correct |
42 |
Correct |
5 ms |
21848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
459 ms |
27736 KB |
Output is correct |
2 |
Correct |
717 ms |
31620 KB |
Output is correct |
3 |
Correct |
696 ms |
31616 KB |
Output is correct |
4 |
Correct |
685 ms |
31612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
459 ms |
27736 KB |
Output is correct |
2 |
Correct |
717 ms |
31620 KB |
Output is correct |
3 |
Correct |
696 ms |
31616 KB |
Output is correct |
4 |
Correct |
685 ms |
31612 KB |
Output is correct |
5 |
Correct |
588 ms |
27664 KB |
Output is correct |
6 |
Correct |
793 ms |
31624 KB |
Output is correct |
7 |
Correct |
754 ms |
29828 KB |
Output is correct |
8 |
Correct |
685 ms |
31616 KB |
Output is correct |
9 |
Correct |
348 ms |
22104 KB |
Output is correct |
10 |
Correct |
691 ms |
22360 KB |
Output is correct |
11 |
Correct |
640 ms |
20312 KB |
Output is correct |
12 |
Correct |
649 ms |
22360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
21848 KB |
Output is correct |
2 |
Correct |
6 ms |
21812 KB |
Output is correct |
3 |
Correct |
6 ms |
21844 KB |
Output is correct |
4 |
Correct |
6 ms |
21944 KB |
Output is correct |
5 |
Correct |
7 ms |
21848 KB |
Output is correct |
6 |
Correct |
6 ms |
21848 KB |
Output is correct |
7 |
Correct |
6 ms |
21844 KB |
Output is correct |
8 |
Correct |
6 ms |
21852 KB |
Output is correct |
9 |
Correct |
5 ms |
21848 KB |
Output is correct |
10 |
Correct |
5 ms |
21848 KB |
Output is correct |
11 |
Correct |
5 ms |
21860 KB |
Output is correct |
12 |
Correct |
5 ms |
21848 KB |
Output is correct |
13 |
Correct |
459 ms |
27736 KB |
Output is correct |
14 |
Correct |
717 ms |
31620 KB |
Output is correct |
15 |
Correct |
696 ms |
31616 KB |
Output is correct |
16 |
Correct |
685 ms |
31612 KB |
Output is correct |
17 |
Correct |
588 ms |
27664 KB |
Output is correct |
18 |
Correct |
793 ms |
31624 KB |
Output is correct |
19 |
Correct |
754 ms |
29828 KB |
Output is correct |
20 |
Correct |
685 ms |
31616 KB |
Output is correct |
21 |
Correct |
348 ms |
22104 KB |
Output is correct |
22 |
Correct |
691 ms |
22360 KB |
Output is correct |
23 |
Correct |
640 ms |
20312 KB |
Output is correct |
24 |
Correct |
649 ms |
22360 KB |
Output is correct |
25 |
Correct |
877 ms |
35488 KB |
Output is correct |
26 |
Correct |
856 ms |
33864 KB |
Output is correct |
27 |
Correct |
873 ms |
33804 KB |
Output is correct |
28 |
Correct |
673 ms |
33800 KB |
Output is correct |
29 |
Correct |
800 ms |
41224 KB |
Output is correct |
30 |
Correct |
729 ms |
45072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
21848 KB |
Output is correct |
2 |
Correct |
5 ms |
21848 KB |
Output is correct |
3 |
Correct |
6 ms |
21848 KB |
Output is correct |
4 |
Correct |
6 ms |
21848 KB |
Output is correct |
5 |
Correct |
6 ms |
21848 KB |
Output is correct |
6 |
Correct |
6 ms |
22260 KB |
Output is correct |
7 |
Correct |
6 ms |
21848 KB |
Output is correct |
8 |
Correct |
6 ms |
21848 KB |
Output is correct |
9 |
Correct |
6 ms |
21848 KB |
Output is correct |
10 |
Correct |
6 ms |
21812 KB |
Output is correct |
11 |
Correct |
6 ms |
21844 KB |
Output is correct |
12 |
Correct |
6 ms |
21944 KB |
Output is correct |
13 |
Correct |
7 ms |
21848 KB |
Output is correct |
14 |
Correct |
6 ms |
21848 KB |
Output is correct |
15 |
Correct |
6 ms |
21844 KB |
Output is correct |
16 |
Correct |
6 ms |
21852 KB |
Output is correct |
17 |
Correct |
5 ms |
21848 KB |
Output is correct |
18 |
Correct |
5 ms |
21848 KB |
Output is correct |
19 |
Correct |
5 ms |
21860 KB |
Output is correct |
20 |
Correct |
5 ms |
21848 KB |
Output is correct |
21 |
Correct |
5 ms |
21848 KB |
Output is correct |
22 |
Correct |
5 ms |
21848 KB |
Output is correct |
23 |
Correct |
5 ms |
21848 KB |
Output is correct |
24 |
Correct |
6 ms |
21860 KB |
Output is correct |
25 |
Correct |
7 ms |
21848 KB |
Output is correct |
26 |
Correct |
6 ms |
21848 KB |
Output is correct |
27 |
Correct |
6 ms |
21848 KB |
Output is correct |
28 |
Correct |
5 ms |
21848 KB |
Output is correct |
29 |
Correct |
5 ms |
21848 KB |
Output is correct |
30 |
Correct |
6 ms |
21848 KB |
Output is correct |
31 |
Correct |
6 ms |
21848 KB |
Output is correct |
32 |
Correct |
6 ms |
22024 KB |
Output is correct |
33 |
Correct |
6 ms |
21848 KB |
Output is correct |
34 |
Correct |
6 ms |
21848 KB |
Output is correct |
35 |
Correct |
5 ms |
21848 KB |
Output is correct |
36 |
Correct |
6 ms |
21848 KB |
Output is correct |
37 |
Correct |
6 ms |
22104 KB |
Output is correct |
38 |
Correct |
5 ms |
22104 KB |
Output is correct |
39 |
Correct |
5 ms |
21848 KB |
Output is correct |
40 |
Correct |
6 ms |
22100 KB |
Output is correct |
41 |
Correct |
6 ms |
21848 KB |
Output is correct |
42 |
Correct |
5 ms |
21848 KB |
Output is correct |
43 |
Correct |
506 ms |
22104 KB |
Output is correct |
44 |
Correct |
661 ms |
22104 KB |
Output is correct |
45 |
Correct |
690 ms |
22104 KB |
Output is correct |
46 |
Correct |
654 ms |
22360 KB |
Output is correct |
47 |
Correct |
690 ms |
22532 KB |
Output is correct |
48 |
Correct |
694 ms |
22360 KB |
Output is correct |
49 |
Correct |
673 ms |
20308 KB |
Output is correct |
50 |
Correct |
656 ms |
22360 KB |
Output is correct |
51 |
Correct |
639 ms |
22072 KB |
Output is correct |
52 |
Correct |
663 ms |
22072 KB |
Output is correct |
53 |
Correct |
563 ms |
22616 KB |
Output is correct |
54 |
Correct |
694 ms |
22360 KB |
Output is correct |
55 |
Correct |
681 ms |
22104 KB |
Output is correct |
56 |
Correct |
658 ms |
22104 KB |
Output is correct |
57 |
Correct |
668 ms |
21848 KB |
Output is correct |
58 |
Correct |
652 ms |
22872 KB |
Output is correct |
59 |
Correct |
656 ms |
22872 KB |
Output is correct |
60 |
Correct |
643 ms |
19032 KB |
Output is correct |
61 |
Correct |
654 ms |
22360 KB |
Output is correct |
62 |
Correct |
690 ms |
22104 KB |
Output is correct |
63 |
Correct |
665 ms |
22104 KB |
Output is correct |
64 |
Correct |
683 ms |
21848 KB |
Output is correct |
65 |
Correct |
346 ms |
22104 KB |
Output is correct |
66 |
Correct |
666 ms |
22356 KB |
Output is correct |
67 |
Correct |
688 ms |
22360 KB |
Output is correct |
68 |
Correct |
624 ms |
22360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
21848 KB |
Output is correct |
2 |
Correct |
5 ms |
21848 KB |
Output is correct |
3 |
Correct |
6 ms |
21848 KB |
Output is correct |
4 |
Correct |
6 ms |
21848 KB |
Output is correct |
5 |
Correct |
6 ms |
21848 KB |
Output is correct |
6 |
Correct |
6 ms |
22260 KB |
Output is correct |
7 |
Correct |
6 ms |
21848 KB |
Output is correct |
8 |
Correct |
6 ms |
21848 KB |
Output is correct |
9 |
Correct |
6 ms |
21848 KB |
Output is correct |
10 |
Correct |
6 ms |
21812 KB |
Output is correct |
11 |
Correct |
6 ms |
21844 KB |
Output is correct |
12 |
Correct |
6 ms |
21944 KB |
Output is correct |
13 |
Correct |
7 ms |
21848 KB |
Output is correct |
14 |
Correct |
6 ms |
21848 KB |
Output is correct |
15 |
Correct |
6 ms |
21844 KB |
Output is correct |
16 |
Correct |
6 ms |
21852 KB |
Output is correct |
17 |
Correct |
5 ms |
21848 KB |
Output is correct |
18 |
Correct |
5 ms |
21848 KB |
Output is correct |
19 |
Correct |
5 ms |
21860 KB |
Output is correct |
20 |
Correct |
5 ms |
21848 KB |
Output is correct |
21 |
Correct |
5 ms |
21848 KB |
Output is correct |
22 |
Correct |
5 ms |
21848 KB |
Output is correct |
23 |
Correct |
5 ms |
21848 KB |
Output is correct |
24 |
Correct |
6 ms |
21860 KB |
Output is correct |
25 |
Correct |
7 ms |
21848 KB |
Output is correct |
26 |
Correct |
6 ms |
21848 KB |
Output is correct |
27 |
Correct |
6 ms |
21848 KB |
Output is correct |
28 |
Correct |
5 ms |
21848 KB |
Output is correct |
29 |
Correct |
5 ms |
21848 KB |
Output is correct |
30 |
Correct |
6 ms |
21848 KB |
Output is correct |
31 |
Correct |
6 ms |
21848 KB |
Output is correct |
32 |
Correct |
6 ms |
22024 KB |
Output is correct |
33 |
Correct |
6 ms |
21848 KB |
Output is correct |
34 |
Correct |
6 ms |
21848 KB |
Output is correct |
35 |
Correct |
5 ms |
21848 KB |
Output is correct |
36 |
Correct |
6 ms |
21848 KB |
Output is correct |
37 |
Correct |
6 ms |
22104 KB |
Output is correct |
38 |
Correct |
5 ms |
22104 KB |
Output is correct |
39 |
Correct |
5 ms |
21848 KB |
Output is correct |
40 |
Correct |
6 ms |
22100 KB |
Output is correct |
41 |
Correct |
6 ms |
21848 KB |
Output is correct |
42 |
Correct |
5 ms |
21848 KB |
Output is correct |
43 |
Correct |
459 ms |
27736 KB |
Output is correct |
44 |
Correct |
717 ms |
31620 KB |
Output is correct |
45 |
Correct |
696 ms |
31616 KB |
Output is correct |
46 |
Correct |
685 ms |
31612 KB |
Output is correct |
47 |
Correct |
588 ms |
27664 KB |
Output is correct |
48 |
Correct |
793 ms |
31624 KB |
Output is correct |
49 |
Correct |
754 ms |
29828 KB |
Output is correct |
50 |
Correct |
685 ms |
31616 KB |
Output is correct |
51 |
Correct |
348 ms |
22104 KB |
Output is correct |
52 |
Correct |
691 ms |
22360 KB |
Output is correct |
53 |
Correct |
640 ms |
20312 KB |
Output is correct |
54 |
Correct |
649 ms |
22360 KB |
Output is correct |
55 |
Correct |
877 ms |
35488 KB |
Output is correct |
56 |
Correct |
856 ms |
33864 KB |
Output is correct |
57 |
Correct |
873 ms |
33804 KB |
Output is correct |
58 |
Correct |
673 ms |
33800 KB |
Output is correct |
59 |
Correct |
800 ms |
41224 KB |
Output is correct |
60 |
Correct |
729 ms |
45072 KB |
Output is correct |
61 |
Correct |
506 ms |
22104 KB |
Output is correct |
62 |
Correct |
661 ms |
22104 KB |
Output is correct |
63 |
Correct |
690 ms |
22104 KB |
Output is correct |
64 |
Correct |
654 ms |
22360 KB |
Output is correct |
65 |
Correct |
690 ms |
22532 KB |
Output is correct |
66 |
Correct |
694 ms |
22360 KB |
Output is correct |
67 |
Correct |
673 ms |
20308 KB |
Output is correct |
68 |
Correct |
656 ms |
22360 KB |
Output is correct |
69 |
Correct |
639 ms |
22072 KB |
Output is correct |
70 |
Correct |
663 ms |
22072 KB |
Output is correct |
71 |
Correct |
563 ms |
22616 KB |
Output is correct |
72 |
Correct |
694 ms |
22360 KB |
Output is correct |
73 |
Correct |
681 ms |
22104 KB |
Output is correct |
74 |
Correct |
658 ms |
22104 KB |
Output is correct |
75 |
Correct |
668 ms |
21848 KB |
Output is correct |
76 |
Correct |
652 ms |
22872 KB |
Output is correct |
77 |
Correct |
656 ms |
22872 KB |
Output is correct |
78 |
Correct |
643 ms |
19032 KB |
Output is correct |
79 |
Correct |
654 ms |
22360 KB |
Output is correct |
80 |
Correct |
690 ms |
22104 KB |
Output is correct |
81 |
Correct |
665 ms |
22104 KB |
Output is correct |
82 |
Correct |
683 ms |
21848 KB |
Output is correct |
83 |
Correct |
346 ms |
22104 KB |
Output is correct |
84 |
Correct |
666 ms |
22356 KB |
Output is correct |
85 |
Correct |
688 ms |
22360 KB |
Output is correct |
86 |
Correct |
624 ms |
22360 KB |
Output is correct |
87 |
Correct |
4 ms |
19836 KB |
Output is correct |
88 |
Correct |
546 ms |
34920 KB |
Output is correct |
89 |
Correct |
819 ms |
27708 KB |
Output is correct |
90 |
Correct |
811 ms |
29932 KB |
Output is correct |
91 |
Correct |
791 ms |
32160 KB |
Output is correct |
92 |
Correct |
879 ms |
32196 KB |
Output is correct |
93 |
Correct |
855 ms |
32052 KB |
Output is correct |
94 |
Correct |
830 ms |
34304 KB |
Output is correct |
95 |
Correct |
783 ms |
32124 KB |
Output is correct |
96 |
Correct |
589 ms |
27144 KB |
Output is correct |
97 |
Correct |
621 ms |
27088 KB |
Output is correct |
98 |
Correct |
569 ms |
41472 KB |
Output is correct |
99 |
Correct |
760 ms |
35832 KB |
Output is correct |
100 |
Correct |
701 ms |
30548 KB |
Output is correct |
101 |
Correct |
677 ms |
29424 KB |
Output is correct |
102 |
Correct |
671 ms |
27004 KB |
Output is correct |
103 |
Correct |
682 ms |
45076 KB |
Output is correct |
104 |
Correct |
633 ms |
46484 KB |
Output is correct |
105 |
Correct |
597 ms |
46468 KB |
Output is correct |
106 |
Correct |
658 ms |
34548 KB |
Output is correct |
107 |
Correct |
674 ms |
29200 KB |
Output is correct |
108 |
Correct |
660 ms |
28536 KB |
Output is correct |
109 |
Correct |
664 ms |
27288 KB |
Output is correct |