#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
pair<int, int> pii;
const int MOD = 1000002022;
const int maxn = 2e5 + 100;
int n, k;
int p[maxn];
vector<int> g[maxn];
int a[maxn];
int t[maxn * 4];
ll sum[maxn];
ll d[2][maxn * 4];
ll dp[maxn];
void pre(int v){
swap(d[0][v], d[1][v]);
t[v] ^= 1;
}
void push(int v, int tl, int tr){
if(tl == tr || !t[v]) return;
t[v] = 0; pre(v<<1); pre(v<<1|1);
}
void build(int v = 1, int tl = k + 1, int tr = n){
if(tl == tr){
d[a[tl]][v] = dp[tl];
} else{
int mid = (tl + tr) >> 1;
build(v<<1, tl, mid);
build(v<<1|1, mid+1, tr);
d[0][v] = (d[0][v<<1] + d[0][v<<1|1]) % MOD;
d[1][v] = (d[1][v<<1] + d[1][v<<1|1]) % MOD;
}
}
void upd(int l, int r, int v = 1, int tl = k + 1, int tr = n){
push(v, tl, tr);
if(tl > r || tr < l) return;
if(l <= tl && tr <= r) pre(v);
else{
int mid = (tl + tr) >> 1;
upd(l, r, v<<1, tl, mid);
upd(l, r, v<<1|1, mid+1, tr);
d[0][v] = (d[0][v<<1] + d[0][v<<1|1]) % MOD;
d[1][v] = (d[1][v<<1] + d[1][v<<1|1]) % MOD;
}
}
void init(int N, int M, std::vector<int> P, std::vector<int> A){
n = N + M; k = N;
for(int i = 1; i <= n; i++){
p[i] = P[i-1]; p[i]++;
g[p[i]].push_back(i);
dp[i] = 1;
}
for(int v = n; v > 0; v--){
sum[v] = 1;
for(int to: g[v]){
sum[v] = sum[v] * sum[to] % MOD;
}
if(v <= k) sum[v] = sum[v] * (ll)g[v].size() % MOD;
}
for(int i = k + 1; i <= n; i++){
a[i] = A[i - k - 1];
}
for(int v = 1; v <= k; v++){
ll d = 1;
for(int to: g[v]){
dp[to] = dp[v];
dp[to] = dp[to] * d % MOD;
d = d * sum[to] % MOD;
}
d = 1;
reverse(g[v].begin(), g[v].end());
for(int to: g[v]){
dp[to] = dp[to] * d % MOD;
d = d * sum[to] % MOD;
}
}
build();
}
int count_ways(int L, int R){
upd(L + 1, R + 1);
return d[1][1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
12632 KB |
Output is correct |
3 |
Correct |
2 ms |
12632 KB |
Output is correct |
4 |
Correct |
2 ms |
10584 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10584 KB |
Output is correct |
7 |
Correct |
2 ms |
10584 KB |
Output is correct |
8 |
Correct |
2 ms |
12632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
12756 KB |
Output is correct |
3 |
Correct |
2 ms |
10584 KB |
Output is correct |
4 |
Correct |
2 ms |
12632 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10672 KB |
Output is correct |
7 |
Correct |
2 ms |
12632 KB |
Output is correct |
8 |
Correct |
2 ms |
12632 KB |
Output is correct |
9 |
Correct |
3 ms |
12632 KB |
Output is correct |
10 |
Correct |
2 ms |
12632 KB |
Output is correct |
11 |
Correct |
2 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
10584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
12632 KB |
Output is correct |
3 |
Correct |
2 ms |
12632 KB |
Output is correct |
4 |
Correct |
2 ms |
10584 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10584 KB |
Output is correct |
7 |
Correct |
2 ms |
10584 KB |
Output is correct |
8 |
Correct |
2 ms |
12632 KB |
Output is correct |
9 |
Correct |
2 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
12756 KB |
Output is correct |
11 |
Correct |
2 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
12632 KB |
Output is correct |
13 |
Correct |
2 ms |
10584 KB |
Output is correct |
14 |
Correct |
2 ms |
10672 KB |
Output is correct |
15 |
Correct |
2 ms |
12632 KB |
Output is correct |
16 |
Correct |
2 ms |
12632 KB |
Output is correct |
17 |
Correct |
3 ms |
12632 KB |
Output is correct |
18 |
Correct |
2 ms |
12632 KB |
Output is correct |
19 |
Correct |
2 ms |
10584 KB |
Output is correct |
20 |
Correct |
2 ms |
10584 KB |
Output is correct |
21 |
Correct |
2 ms |
10584 KB |
Output is correct |
22 |
Correct |
2 ms |
12632 KB |
Output is correct |
23 |
Correct |
2 ms |
12632 KB |
Output is correct |
24 |
Correct |
2 ms |
12632 KB |
Output is correct |
25 |
Correct |
2 ms |
10584 KB |
Output is correct |
26 |
Correct |
2 ms |
10584 KB |
Output is correct |
27 |
Correct |
2 ms |
10584 KB |
Output is correct |
28 |
Correct |
3 ms |
12632 KB |
Output is correct |
29 |
Correct |
2 ms |
12632 KB |
Output is correct |
30 |
Correct |
2 ms |
12632 KB |
Output is correct |
31 |
Correct |
2 ms |
12632 KB |
Output is correct |
32 |
Correct |
2 ms |
10584 KB |
Output is correct |
33 |
Correct |
2 ms |
10584 KB |
Output is correct |
34 |
Correct |
2 ms |
10584 KB |
Output is correct |
35 |
Correct |
2 ms |
10584 KB |
Output is correct |
36 |
Correct |
2 ms |
10584 KB |
Output is correct |
37 |
Correct |
3 ms |
10584 KB |
Output is correct |
38 |
Correct |
2 ms |
12632 KB |
Output is correct |
39 |
Correct |
2 ms |
12632 KB |
Output is correct |
40 |
Correct |
3 ms |
12632 KB |
Output is correct |
41 |
Correct |
2 ms |
12632 KB |
Output is correct |
42 |
Correct |
2 ms |
10660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
465 ms |
15184 KB |
Output is correct |
2 |
Correct |
614 ms |
22616 KB |
Output is correct |
3 |
Correct |
676 ms |
20560 KB |
Output is correct |
4 |
Correct |
655 ms |
19032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
465 ms |
15184 KB |
Output is correct |
2 |
Correct |
614 ms |
22616 KB |
Output is correct |
3 |
Correct |
676 ms |
20560 KB |
Output is correct |
4 |
Correct |
655 ms |
19032 KB |
Output is correct |
5 |
Correct |
552 ms |
16728 KB |
Output is correct |
6 |
Correct |
728 ms |
22616 KB |
Output is correct |
7 |
Correct |
721 ms |
22616 KB |
Output is correct |
8 |
Correct |
649 ms |
21080 KB |
Output is correct |
9 |
Correct |
386 ms |
10840 KB |
Output is correct |
10 |
Correct |
669 ms |
11096 KB |
Output is correct |
11 |
Correct |
615 ms |
12888 KB |
Output is correct |
12 |
Correct |
560 ms |
10840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
12756 KB |
Output is correct |
3 |
Correct |
2 ms |
10584 KB |
Output is correct |
4 |
Correct |
2 ms |
12632 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10672 KB |
Output is correct |
7 |
Correct |
2 ms |
12632 KB |
Output is correct |
8 |
Correct |
2 ms |
12632 KB |
Output is correct |
9 |
Correct |
3 ms |
12632 KB |
Output is correct |
10 |
Correct |
2 ms |
12632 KB |
Output is correct |
11 |
Correct |
2 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
10584 KB |
Output is correct |
13 |
Correct |
465 ms |
15184 KB |
Output is correct |
14 |
Correct |
614 ms |
22616 KB |
Output is correct |
15 |
Correct |
676 ms |
20560 KB |
Output is correct |
16 |
Correct |
655 ms |
19032 KB |
Output is correct |
17 |
Correct |
552 ms |
16728 KB |
Output is correct |
18 |
Correct |
728 ms |
22616 KB |
Output is correct |
19 |
Correct |
721 ms |
22616 KB |
Output is correct |
20 |
Correct |
649 ms |
21080 KB |
Output is correct |
21 |
Correct |
386 ms |
10840 KB |
Output is correct |
22 |
Correct |
669 ms |
11096 KB |
Output is correct |
23 |
Correct |
615 ms |
12888 KB |
Output is correct |
24 |
Correct |
560 ms |
10840 KB |
Output is correct |
25 |
Correct |
674 ms |
25056 KB |
Output is correct |
26 |
Correct |
732 ms |
25036 KB |
Output is correct |
27 |
Correct |
696 ms |
25700 KB |
Output is correct |
28 |
Correct |
497 ms |
25688 KB |
Output is correct |
29 |
Correct |
691 ms |
25148 KB |
Output is correct |
30 |
Correct |
672 ms |
25688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
12632 KB |
Output is correct |
3 |
Correct |
2 ms |
12632 KB |
Output is correct |
4 |
Correct |
2 ms |
10584 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10584 KB |
Output is correct |
7 |
Correct |
2 ms |
10584 KB |
Output is correct |
8 |
Correct |
2 ms |
12632 KB |
Output is correct |
9 |
Correct |
2 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
12756 KB |
Output is correct |
11 |
Correct |
2 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
12632 KB |
Output is correct |
13 |
Correct |
2 ms |
10584 KB |
Output is correct |
14 |
Correct |
2 ms |
10672 KB |
Output is correct |
15 |
Correct |
2 ms |
12632 KB |
Output is correct |
16 |
Correct |
2 ms |
12632 KB |
Output is correct |
17 |
Correct |
3 ms |
12632 KB |
Output is correct |
18 |
Correct |
2 ms |
12632 KB |
Output is correct |
19 |
Correct |
2 ms |
10584 KB |
Output is correct |
20 |
Correct |
2 ms |
10584 KB |
Output is correct |
21 |
Correct |
2 ms |
10584 KB |
Output is correct |
22 |
Correct |
2 ms |
12632 KB |
Output is correct |
23 |
Correct |
2 ms |
12632 KB |
Output is correct |
24 |
Correct |
2 ms |
12632 KB |
Output is correct |
25 |
Correct |
2 ms |
10584 KB |
Output is correct |
26 |
Correct |
2 ms |
10584 KB |
Output is correct |
27 |
Correct |
2 ms |
10584 KB |
Output is correct |
28 |
Correct |
3 ms |
12632 KB |
Output is correct |
29 |
Correct |
2 ms |
12632 KB |
Output is correct |
30 |
Correct |
2 ms |
12632 KB |
Output is correct |
31 |
Correct |
2 ms |
12632 KB |
Output is correct |
32 |
Correct |
2 ms |
10584 KB |
Output is correct |
33 |
Correct |
2 ms |
10584 KB |
Output is correct |
34 |
Correct |
2 ms |
10584 KB |
Output is correct |
35 |
Correct |
2 ms |
10584 KB |
Output is correct |
36 |
Correct |
2 ms |
10584 KB |
Output is correct |
37 |
Correct |
3 ms |
10584 KB |
Output is correct |
38 |
Correct |
2 ms |
12632 KB |
Output is correct |
39 |
Correct |
2 ms |
12632 KB |
Output is correct |
40 |
Correct |
3 ms |
12632 KB |
Output is correct |
41 |
Correct |
2 ms |
12632 KB |
Output is correct |
42 |
Correct |
2 ms |
10660 KB |
Output is correct |
43 |
Correct |
467 ms |
10840 KB |
Output is correct |
44 |
Correct |
699 ms |
12888 KB |
Output is correct |
45 |
Correct |
583 ms |
12888 KB |
Output is correct |
46 |
Correct |
584 ms |
11096 KB |
Output is correct |
47 |
Correct |
621 ms |
11352 KB |
Output is correct |
48 |
Correct |
616 ms |
11096 KB |
Output is correct |
49 |
Correct |
563 ms |
11096 KB |
Output is correct |
50 |
Correct |
644 ms |
12888 KB |
Output is correct |
51 |
Correct |
606 ms |
10840 KB |
Output is correct |
52 |
Correct |
557 ms |
10840 KB |
Output is correct |
53 |
Correct |
490 ms |
13140 KB |
Output is correct |
54 |
Correct |
613 ms |
11096 KB |
Output is correct |
55 |
Correct |
578 ms |
11144 KB |
Output is correct |
56 |
Correct |
586 ms |
12888 KB |
Output is correct |
57 |
Correct |
606 ms |
12888 KB |
Output is correct |
58 |
Correct |
608 ms |
11096 KB |
Output is correct |
59 |
Correct |
615 ms |
13208 KB |
Output is correct |
60 |
Correct |
625 ms |
13164 KB |
Output is correct |
61 |
Correct |
616 ms |
12888 KB |
Output is correct |
62 |
Correct |
554 ms |
12888 KB |
Output is correct |
63 |
Correct |
622 ms |
10840 KB |
Output is correct |
64 |
Correct |
624 ms |
12888 KB |
Output is correct |
65 |
Correct |
290 ms |
10840 KB |
Output is correct |
66 |
Correct |
607 ms |
11096 KB |
Output is correct |
67 |
Correct |
566 ms |
12888 KB |
Output is correct |
68 |
Correct |
558 ms |
10840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
12632 KB |
Output is correct |
3 |
Correct |
2 ms |
12632 KB |
Output is correct |
4 |
Correct |
2 ms |
10584 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10584 KB |
Output is correct |
7 |
Correct |
2 ms |
10584 KB |
Output is correct |
8 |
Correct |
2 ms |
12632 KB |
Output is correct |
9 |
Correct |
2 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
12756 KB |
Output is correct |
11 |
Correct |
2 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
12632 KB |
Output is correct |
13 |
Correct |
2 ms |
10584 KB |
Output is correct |
14 |
Correct |
2 ms |
10672 KB |
Output is correct |
15 |
Correct |
2 ms |
12632 KB |
Output is correct |
16 |
Correct |
2 ms |
12632 KB |
Output is correct |
17 |
Correct |
3 ms |
12632 KB |
Output is correct |
18 |
Correct |
2 ms |
12632 KB |
Output is correct |
19 |
Correct |
2 ms |
10584 KB |
Output is correct |
20 |
Correct |
2 ms |
10584 KB |
Output is correct |
21 |
Correct |
2 ms |
10584 KB |
Output is correct |
22 |
Correct |
2 ms |
12632 KB |
Output is correct |
23 |
Correct |
2 ms |
12632 KB |
Output is correct |
24 |
Correct |
2 ms |
12632 KB |
Output is correct |
25 |
Correct |
2 ms |
10584 KB |
Output is correct |
26 |
Correct |
2 ms |
10584 KB |
Output is correct |
27 |
Correct |
2 ms |
10584 KB |
Output is correct |
28 |
Correct |
3 ms |
12632 KB |
Output is correct |
29 |
Correct |
2 ms |
12632 KB |
Output is correct |
30 |
Correct |
2 ms |
12632 KB |
Output is correct |
31 |
Correct |
2 ms |
12632 KB |
Output is correct |
32 |
Correct |
2 ms |
10584 KB |
Output is correct |
33 |
Correct |
2 ms |
10584 KB |
Output is correct |
34 |
Correct |
2 ms |
10584 KB |
Output is correct |
35 |
Correct |
2 ms |
10584 KB |
Output is correct |
36 |
Correct |
2 ms |
10584 KB |
Output is correct |
37 |
Correct |
3 ms |
10584 KB |
Output is correct |
38 |
Correct |
2 ms |
12632 KB |
Output is correct |
39 |
Correct |
2 ms |
12632 KB |
Output is correct |
40 |
Correct |
3 ms |
12632 KB |
Output is correct |
41 |
Correct |
2 ms |
12632 KB |
Output is correct |
42 |
Correct |
2 ms |
10660 KB |
Output is correct |
43 |
Correct |
465 ms |
15184 KB |
Output is correct |
44 |
Correct |
614 ms |
22616 KB |
Output is correct |
45 |
Correct |
676 ms |
20560 KB |
Output is correct |
46 |
Correct |
655 ms |
19032 KB |
Output is correct |
47 |
Correct |
552 ms |
16728 KB |
Output is correct |
48 |
Correct |
728 ms |
22616 KB |
Output is correct |
49 |
Correct |
721 ms |
22616 KB |
Output is correct |
50 |
Correct |
649 ms |
21080 KB |
Output is correct |
51 |
Correct |
386 ms |
10840 KB |
Output is correct |
52 |
Correct |
669 ms |
11096 KB |
Output is correct |
53 |
Correct |
615 ms |
12888 KB |
Output is correct |
54 |
Correct |
560 ms |
10840 KB |
Output is correct |
55 |
Correct |
674 ms |
25056 KB |
Output is correct |
56 |
Correct |
732 ms |
25036 KB |
Output is correct |
57 |
Correct |
696 ms |
25700 KB |
Output is correct |
58 |
Correct |
497 ms |
25688 KB |
Output is correct |
59 |
Correct |
691 ms |
25148 KB |
Output is correct |
60 |
Correct |
672 ms |
25688 KB |
Output is correct |
61 |
Correct |
467 ms |
10840 KB |
Output is correct |
62 |
Correct |
699 ms |
12888 KB |
Output is correct |
63 |
Correct |
583 ms |
12888 KB |
Output is correct |
64 |
Correct |
584 ms |
11096 KB |
Output is correct |
65 |
Correct |
621 ms |
11352 KB |
Output is correct |
66 |
Correct |
616 ms |
11096 KB |
Output is correct |
67 |
Correct |
563 ms |
11096 KB |
Output is correct |
68 |
Correct |
644 ms |
12888 KB |
Output is correct |
69 |
Correct |
606 ms |
10840 KB |
Output is correct |
70 |
Correct |
557 ms |
10840 KB |
Output is correct |
71 |
Correct |
490 ms |
13140 KB |
Output is correct |
72 |
Correct |
613 ms |
11096 KB |
Output is correct |
73 |
Correct |
578 ms |
11144 KB |
Output is correct |
74 |
Correct |
586 ms |
12888 KB |
Output is correct |
75 |
Correct |
606 ms |
12888 KB |
Output is correct |
76 |
Correct |
608 ms |
11096 KB |
Output is correct |
77 |
Correct |
615 ms |
13208 KB |
Output is correct |
78 |
Correct |
625 ms |
13164 KB |
Output is correct |
79 |
Correct |
616 ms |
12888 KB |
Output is correct |
80 |
Correct |
554 ms |
12888 KB |
Output is correct |
81 |
Correct |
622 ms |
10840 KB |
Output is correct |
82 |
Correct |
624 ms |
12888 KB |
Output is correct |
83 |
Correct |
290 ms |
10840 KB |
Output is correct |
84 |
Correct |
607 ms |
11096 KB |
Output is correct |
85 |
Correct |
566 ms |
12888 KB |
Output is correct |
86 |
Correct |
558 ms |
10840 KB |
Output is correct |
87 |
Correct |
1 ms |
12632 KB |
Output is correct |
88 |
Correct |
387 ms |
25308 KB |
Output is correct |
89 |
Correct |
584 ms |
22872 KB |
Output is correct |
90 |
Correct |
642 ms |
22616 KB |
Output is correct |
91 |
Correct |
633 ms |
25144 KB |
Output is correct |
92 |
Correct |
734 ms |
25152 KB |
Output is correct |
93 |
Correct |
706 ms |
25272 KB |
Output is correct |
94 |
Correct |
616 ms |
25144 KB |
Output is correct |
95 |
Correct |
631 ms |
25804 KB |
Output is correct |
96 |
Correct |
544 ms |
21884 KB |
Output is correct |
97 |
Correct |
692 ms |
21776 KB |
Output is correct |
98 |
Correct |
589 ms |
16728 KB |
Output is correct |
99 |
Correct |
701 ms |
25144 KB |
Output is correct |
100 |
Correct |
673 ms |
23084 KB |
Output is correct |
101 |
Correct |
652 ms |
23296 KB |
Output is correct |
102 |
Correct |
650 ms |
22400 KB |
Output is correct |
103 |
Correct |
738 ms |
25036 KB |
Output is correct |
104 |
Correct |
706 ms |
26212 KB |
Output is correct |
105 |
Correct |
658 ms |
25556 KB |
Output is correct |
106 |
Correct |
704 ms |
22608 KB |
Output is correct |
107 |
Correct |
625 ms |
23132 KB |
Output is correct |
108 |
Correct |
679 ms |
22900 KB |
Output is correct |
109 |
Correct |
671 ms |
22488 KB |
Output is correct |