#include <bits/stdc++.h>
#include "circuit.h"
using namespace std;
const int MOD = 1e9 + 2022;
inline int add(int x, int y) {
x += y;
if (x >= MOD)
return x - MOD;
if (x < 0)
return x + MOD;
return x;
}
inline int mul(int x, int y) {
return (long long)x * y % MOD;
}
struct Node {
int tot, sum, lazy;
Node(int tot, int sum, int lazy) : tot(tot), sum(sum), lazy(lazy) {}
Node() : tot(0), sum(0), lazy(0) {}
};
class Tournament {
int m, offset;
vector <Node> tour;
public:
Node merge(Node lhs, Node rhs) {
return Node(add(lhs.tot, rhs.tot), add(lhs.sum, rhs.sum), 0);
}
void refresh(int x) {
tour[x] = merge(tour[2 * x], tour[2 * x + 1]);
}
Tournament(int m, int n, vector <int> v, vector <int> s) : m(m) {
for (offset = 1; offset < n; offset *= 2);
tour.resize(2 * offset);
for (int i = 0; i < n; i++)
tour[i + offset] = Node(v[i], s[i] * v[i], 0);
for (int i = offset - 1; i; i--)
refresh(i);
}
Tournament() {}
void prop(int x) {
if (!tour[x].lazy)
return;
tour[x].sum = add(tour[x].tot, -tour[x].sum);
if (x < offset) {
tour[2 * x].lazy ^= 1;
tour[2 * x + 1].lazy ^= 1;
}
tour[x].lazy = 0;
}
void update(int x, int lo, int hi, int from, int to) {
prop(x);
if (lo >= to || hi <= from)
return;
if (lo >= from && hi <= to) {
tour[x].lazy ^= 1;
prop(x);
return;
}
int mid = (lo + hi) / 2;
update(2 * x, lo, mid, from, to);
update(2 * x + 1, mid, hi, from, to);
refresh(x);
}
void update(int from, int to) {
update(1, 0, offset, from - m, to - m);
}
int query() {
prop(1);
return tour[1].sum;
}
};
Tournament solver;
void init(int N, int M, vector <int> p, vector <int> a) {
vector <int> pos(N + M);
vector <vector <int>> ch(N);
for (int i = 1; i < N + M; i++) {
pos[i] = ch[p[i]].size();
ch[p[i]].push_back(i);
}
vector <int> sz(N), sub(N + M, 1);
for (int i = 0; i < N; i++)
sz[i] = sub[i] = ch[i].size();
for (int i = N - 1; i; i--)
sub[p[i]] = mul(sub[p[i]], sub[i]);
vector <vector <int>> pref(N), suff(N);
for (int i = 0; i < N; i++) {
pref[i].resize(sz[i]);
suff[i].resize(sz[i]);
for (int j = 0; j < sz[i]; j++)
pref[i][j] = mul(j ? pref[i][j - 1] : 1, sub[ch[i][j]]);
for (int j = sz[i] - 1; j >= 0; j--)
suff[i][j] = mul(j < sz[i] - 1 ? suff[i][j + 1] : 1, sub[ch[i][j]]);
}
vector <int> prod(N + M);
prod[0] = 1;
for (int i = 1; i < N + M; i++)
prod[i] = mul(prod[p[i]], mul(pos[i] ? pref[p[i]][pos[i] - 1] : 1, pos[i] < sz[p[i]] - 1 ? suff[p[i]][pos[i] + 1] : 1));
solver = Tournament(N, M, vector <int> (prod.begin() + N, prod.end()), a);
}
int count_ways(int l, int r) {
solver.update(l, r + 1);
return solver.query();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 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 |
208 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 |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
464 KB |
Output is correct |
8 |
Correct |
1 ms |
544 KB |
Output is correct |
9 |
Correct |
1 ms |
548 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
1 ms |
548 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 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 |
208 KB |
Output is correct |
10 |
Correct |
0 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 |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
544 KB |
Output is correct |
17 |
Correct |
1 ms |
548 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
548 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
464 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 |
544 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 |
512 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 |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
548 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
0 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
464 KB |
Output is correct |
37 |
Correct |
1 ms |
548 KB |
Output is correct |
38 |
Correct |
1 ms |
464 KB |
Output is correct |
39 |
Correct |
1 ms |
336 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 |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
583 ms |
8348 KB |
Output is correct |
2 |
Correct |
885 ms |
16456 KB |
Output is correct |
3 |
Correct |
1006 ms |
16436 KB |
Output is correct |
4 |
Correct |
723 ms |
16408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
583 ms |
8348 KB |
Output is correct |
2 |
Correct |
885 ms |
16456 KB |
Output is correct |
3 |
Correct |
1006 ms |
16436 KB |
Output is correct |
4 |
Correct |
723 ms |
16408 KB |
Output is correct |
5 |
Correct |
733 ms |
8360 KB |
Output is correct |
6 |
Correct |
981 ms |
16436 KB |
Output is correct |
7 |
Correct |
1053 ms |
16416 KB |
Output is correct |
8 |
Correct |
797 ms |
16456 KB |
Output is correct |
9 |
Correct |
518 ms |
776 KB |
Output is correct |
10 |
Correct |
987 ms |
1280 KB |
Output is correct |
11 |
Correct |
891 ms |
1284 KB |
Output is correct |
12 |
Correct |
720 ms |
1284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 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 |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
464 KB |
Output is correct |
8 |
Correct |
1 ms |
544 KB |
Output is correct |
9 |
Correct |
1 ms |
548 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
1 ms |
548 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
583 ms |
8348 KB |
Output is correct |
14 |
Correct |
885 ms |
16456 KB |
Output is correct |
15 |
Correct |
1006 ms |
16436 KB |
Output is correct |
16 |
Correct |
723 ms |
16408 KB |
Output is correct |
17 |
Correct |
733 ms |
8360 KB |
Output is correct |
18 |
Correct |
981 ms |
16436 KB |
Output is correct |
19 |
Correct |
1053 ms |
16416 KB |
Output is correct |
20 |
Correct |
797 ms |
16456 KB |
Output is correct |
21 |
Correct |
518 ms |
776 KB |
Output is correct |
22 |
Correct |
987 ms |
1280 KB |
Output is correct |
23 |
Correct |
891 ms |
1284 KB |
Output is correct |
24 |
Correct |
720 ms |
1284 KB |
Output is correct |
25 |
Correct |
947 ms |
25180 KB |
Output is correct |
26 |
Correct |
1051 ms |
25688 KB |
Output is correct |
27 |
Correct |
1139 ms |
25620 KB |
Output is correct |
28 |
Correct |
768 ms |
25620 KB |
Output is correct |
29 |
Correct |
1177 ms |
25604 KB |
Output is correct |
30 |
Correct |
1072 ms |
25636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 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 |
208 KB |
Output is correct |
10 |
Correct |
0 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 |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
544 KB |
Output is correct |
17 |
Correct |
1 ms |
548 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
548 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
464 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 |
544 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 |
512 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 |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
548 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
0 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
464 KB |
Output is correct |
37 |
Correct |
1 ms |
548 KB |
Output is correct |
38 |
Correct |
1 ms |
464 KB |
Output is correct |
39 |
Correct |
1 ms |
336 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 |
336 KB |
Output is correct |
43 |
Correct |
569 ms |
1032 KB |
Output is correct |
44 |
Correct |
1066 ms |
1236 KB |
Output is correct |
45 |
Correct |
821 ms |
1116 KB |
Output is correct |
46 |
Correct |
778 ms |
1616 KB |
Output is correct |
47 |
Correct |
868 ms |
1616 KB |
Output is correct |
48 |
Correct |
947 ms |
1616 KB |
Output is correct |
49 |
Correct |
982 ms |
1616 KB |
Output is correct |
50 |
Correct |
897 ms |
1616 KB |
Output is correct |
51 |
Correct |
730 ms |
720 KB |
Output is correct |
52 |
Correct |
987 ms |
720 KB |
Output is correct |
53 |
Correct |
463 ms |
1104 KB |
Output is correct |
54 |
Correct |
1100 ms |
1616 KB |
Output is correct |
55 |
Correct |
936 ms |
1104 KB |
Output is correct |
56 |
Correct |
844 ms |
848 KB |
Output is correct |
57 |
Correct |
934 ms |
720 KB |
Output is correct |
58 |
Correct |
683 ms |
1488 KB |
Output is correct |
59 |
Correct |
881 ms |
1616 KB |
Output is correct |
60 |
Correct |
804 ms |
1616 KB |
Output is correct |
61 |
Correct |
876 ms |
1236 KB |
Output is correct |
62 |
Correct |
850 ms |
968 KB |
Output is correct |
63 |
Correct |
871 ms |
792 KB |
Output is correct |
64 |
Correct |
991 ms |
720 KB |
Output is correct |
65 |
Correct |
476 ms |
780 KB |
Output is correct |
66 |
Correct |
983 ms |
1292 KB |
Output is correct |
67 |
Correct |
972 ms |
1288 KB |
Output is correct |
68 |
Correct |
916 ms |
1288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 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 |
208 KB |
Output is correct |
10 |
Correct |
0 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 |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
544 KB |
Output is correct |
17 |
Correct |
1 ms |
548 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
548 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
464 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 |
544 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 |
512 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 |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
548 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
0 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
464 KB |
Output is correct |
37 |
Correct |
1 ms |
548 KB |
Output is correct |
38 |
Correct |
1 ms |
464 KB |
Output is correct |
39 |
Correct |
1 ms |
336 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 |
336 KB |
Output is correct |
43 |
Correct |
583 ms |
8348 KB |
Output is correct |
44 |
Correct |
885 ms |
16456 KB |
Output is correct |
45 |
Correct |
1006 ms |
16436 KB |
Output is correct |
46 |
Correct |
723 ms |
16408 KB |
Output is correct |
47 |
Correct |
733 ms |
8360 KB |
Output is correct |
48 |
Correct |
981 ms |
16436 KB |
Output is correct |
49 |
Correct |
1053 ms |
16416 KB |
Output is correct |
50 |
Correct |
797 ms |
16456 KB |
Output is correct |
51 |
Correct |
518 ms |
776 KB |
Output is correct |
52 |
Correct |
987 ms |
1280 KB |
Output is correct |
53 |
Correct |
891 ms |
1284 KB |
Output is correct |
54 |
Correct |
720 ms |
1284 KB |
Output is correct |
55 |
Correct |
947 ms |
25180 KB |
Output is correct |
56 |
Correct |
1051 ms |
25688 KB |
Output is correct |
57 |
Correct |
1139 ms |
25620 KB |
Output is correct |
58 |
Correct |
768 ms |
25620 KB |
Output is correct |
59 |
Correct |
1177 ms |
25604 KB |
Output is correct |
60 |
Correct |
1072 ms |
25636 KB |
Output is correct |
61 |
Correct |
569 ms |
1032 KB |
Output is correct |
62 |
Correct |
1066 ms |
1236 KB |
Output is correct |
63 |
Correct |
821 ms |
1116 KB |
Output is correct |
64 |
Correct |
778 ms |
1616 KB |
Output is correct |
65 |
Correct |
868 ms |
1616 KB |
Output is correct |
66 |
Correct |
947 ms |
1616 KB |
Output is correct |
67 |
Correct |
982 ms |
1616 KB |
Output is correct |
68 |
Correct |
897 ms |
1616 KB |
Output is correct |
69 |
Correct |
730 ms |
720 KB |
Output is correct |
70 |
Correct |
987 ms |
720 KB |
Output is correct |
71 |
Correct |
463 ms |
1104 KB |
Output is correct |
72 |
Correct |
1100 ms |
1616 KB |
Output is correct |
73 |
Correct |
936 ms |
1104 KB |
Output is correct |
74 |
Correct |
844 ms |
848 KB |
Output is correct |
75 |
Correct |
934 ms |
720 KB |
Output is correct |
76 |
Correct |
683 ms |
1488 KB |
Output is correct |
77 |
Correct |
881 ms |
1616 KB |
Output is correct |
78 |
Correct |
804 ms |
1616 KB |
Output is correct |
79 |
Correct |
876 ms |
1236 KB |
Output is correct |
80 |
Correct |
850 ms |
968 KB |
Output is correct |
81 |
Correct |
871 ms |
792 KB |
Output is correct |
82 |
Correct |
991 ms |
720 KB |
Output is correct |
83 |
Correct |
476 ms |
780 KB |
Output is correct |
84 |
Correct |
983 ms |
1292 KB |
Output is correct |
85 |
Correct |
972 ms |
1288 KB |
Output is correct |
86 |
Correct |
916 ms |
1288 KB |
Output is correct |
87 |
Correct |
1 ms |
208 KB |
Output is correct |
88 |
Correct |
536 ms |
23796 KB |
Output is correct |
89 |
Correct |
1097 ms |
18364 KB |
Output is correct |
90 |
Correct |
952 ms |
16496 KB |
Output is correct |
91 |
Correct |
856 ms |
25780 KB |
Output is correct |
92 |
Correct |
1029 ms |
25840 KB |
Output is correct |
93 |
Correct |
942 ms |
25776 KB |
Output is correct |
94 |
Correct |
911 ms |
25828 KB |
Output is correct |
95 |
Correct |
868 ms |
25760 KB |
Output is correct |
96 |
Correct |
896 ms |
8112 KB |
Output is correct |
97 |
Correct |
861 ms |
8128 KB |
Output is correct |
98 |
Correct |
740 ms |
19024 KB |
Output is correct |
99 |
Correct |
910 ms |
25700 KB |
Output is correct |
100 |
Correct |
924 ms |
16240 KB |
Output is correct |
101 |
Correct |
1024 ms |
11940 KB |
Output is correct |
102 |
Correct |
1016 ms |
8136 KB |
Output is correct |
103 |
Correct |
931 ms |
25672 KB |
Output is correct |
104 |
Correct |
777 ms |
26912 KB |
Output is correct |
105 |
Correct |
837 ms |
26812 KB |
Output is correct |
106 |
Correct |
808 ms |
17772 KB |
Output is correct |
107 |
Correct |
927 ms |
13388 KB |
Output is correct |
108 |
Correct |
1779 ms |
12032 KB |
Output is correct |
109 |
Correct |
1340 ms |
8220 KB |
Output is correct |