#include "circuit.h"
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define pii pair<int, int>
#define piii pair<pii, int>
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define f first
#define s second
#define oo 1000000001
#define eb emplace_back
#define pb push_back
#define mpr make_pair
#define size(v) (int)v.size()
#define ln '\n'
#define ull unsigned long long
#define ll long long
#define all(v) v.begin(), v.end()
using namespace std;
const int mod = 1e9 + 2022;
const int up = 1e5 + 5;
vvi tree;
vi reNum, realNode, suff;
vi cont, psum;
int n, m;
int tme = 0;
void dfs1(int v) {
if (v < n) {
reNum[v] = tme;
realNode[tme] = v;
tme++;
}
for (int to : tree[v]) {
dfs1(to);
}
}
int pre = 1, tmr = 0;
void dfs2(int v) {
if (v >= n) {
cont[v - n] = (1ll * pre * suff[tmr]) % mod;
} else {
tmr++;
}
for (int to : tree[v]) {
dfs2(to);
}
if (v < n) {
pre = (1ll * pre * size(tree[v])) % mod;
}
}
int segTree[up << 2];
int lazy[up << 2];
void build(int v, int l, int r, vi &A) {
if (l == r) {
segTree[v] = A[l] * cont[l];
} else {
int mid = (l + r) >> 1;
build(v << 1, l, mid , A);
build((v << 1) | 1, mid + 1, r, A);
segTree[v] = (segTree[v << 1] + segTree[(v << 1) | 1]) % mod;
}
}
void relax(int v, int l, int r) {
lazy[v] = 0;
int total = psum[r + 1] - psum[l];
if (total < 0) total += mod;
segTree[v] = total - segTree[v];
if (segTree[v] < 0) segTree[v] += mod;
if (l != r) {
lazy[v << 1] ^= 1;
lazy[(v << 1) | 1] ^= 1;
}
}
void update(int v, int l, int r, int ul, int ur) {
if (lazy[v]) relax(v, l, r);
if (r < ul || ur < l) return;
if (ul <= l && r <= ur) {
relax(v, l, r);
} else {
int mid = (l + r) >> 1;
update(v << 1, l, mid, ul, ur);
update((v << 1) | 1, mid + 1, r, ul, ur);
segTree[v] = (segTree[v << 1] + segTree[(v << 1) | 1]) % mod;
}
}
void init(int N, int M, vi P, vi A) {
n = N; m = M;
tree.resize(N + M);
for (int i = 1; i < N + M; ++i) {
tree[P[i]].eb(i);
}
reNum.resize(N);
realNode.resize(N);
dfs1(0);
suff.resize(N + 1);
suff[N] = 1;
for (int i = N - 1; i >= 0; --i) {
suff[i] = (1ll * suff[i + 1] * size(tree[realNode[i]])) % mod;
}
cont.resize(M);
dfs2(0);
psum.resize(M + 1);
psum[0] = 0;
for (int i = 0; i < M; ++i) {
psum[i + 1] = psum[i] + cont[i];
if (psum[i + 1] >= mod) psum[i + 1] -= mod;
}
build(1, 0, M - 1, A);
}
int count_ways(int L, int R) {
update(1, 0, m - 1, L - n, R - n);
return segTree[1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 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 |
368 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 |
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 |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
372 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
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 |
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 |
368 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
208 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 |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
372 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
2 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 |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
336 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 |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 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 |
464 KB |
Output is correct |
37 |
Correct |
1 ms |
464 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 |
684 ms |
4536 KB |
Output is correct |
2 |
Correct |
893 ms |
8752 KB |
Output is correct |
3 |
Correct |
773 ms |
8748 KB |
Output is correct |
4 |
Correct |
852 ms |
8748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
684 ms |
4536 KB |
Output is correct |
2 |
Correct |
893 ms |
8752 KB |
Output is correct |
3 |
Correct |
773 ms |
8748 KB |
Output is correct |
4 |
Correct |
852 ms |
8748 KB |
Output is correct |
5 |
Correct |
740 ms |
4540 KB |
Output is correct |
6 |
Correct |
863 ms |
8748 KB |
Output is correct |
7 |
Correct |
916 ms |
8776 KB |
Output is correct |
8 |
Correct |
814 ms |
8752 KB |
Output is correct |
9 |
Correct |
413 ms |
592 KB |
Output is correct |
10 |
Correct |
772 ms |
848 KB |
Output is correct |
11 |
Correct |
779 ms |
808 KB |
Output is correct |
12 |
Correct |
835 ms |
848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 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 |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
372 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
684 ms |
4536 KB |
Output is correct |
14 |
Correct |
893 ms |
8752 KB |
Output is correct |
15 |
Correct |
773 ms |
8748 KB |
Output is correct |
16 |
Correct |
852 ms |
8748 KB |
Output is correct |
17 |
Correct |
740 ms |
4540 KB |
Output is correct |
18 |
Correct |
863 ms |
8748 KB |
Output is correct |
19 |
Correct |
916 ms |
8776 KB |
Output is correct |
20 |
Correct |
814 ms |
8752 KB |
Output is correct |
21 |
Correct |
413 ms |
592 KB |
Output is correct |
22 |
Correct |
772 ms |
848 KB |
Output is correct |
23 |
Correct |
779 ms |
808 KB |
Output is correct |
24 |
Correct |
835 ms |
848 KB |
Output is correct |
25 |
Correct |
839 ms |
13160 KB |
Output is correct |
26 |
Correct |
1101 ms |
13492 KB |
Output is correct |
27 |
Correct |
829 ms |
13372 KB |
Output is correct |
28 |
Correct |
803 ms |
13472 KB |
Output is correct |
29 |
Correct |
1033 ms |
18116 KB |
Output is correct |
30 |
Correct |
792 ms |
18088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 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 |
368 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
208 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 |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
372 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
2 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 |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
336 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 |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 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 |
464 KB |
Output is correct |
37 |
Correct |
1 ms |
464 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 |
678 ms |
736 KB |
Output is correct |
44 |
Correct |
975 ms |
796 KB |
Output is correct |
45 |
Correct |
864 ms |
756 KB |
Output is correct |
46 |
Correct |
858 ms |
1028 KB |
Output is correct |
47 |
Correct |
807 ms |
1044 KB |
Output is correct |
48 |
Correct |
881 ms |
1048 KB |
Output is correct |
49 |
Correct |
757 ms |
1044 KB |
Output is correct |
50 |
Correct |
807 ms |
1024 KB |
Output is correct |
51 |
Correct |
762 ms |
560 KB |
Output is correct |
52 |
Correct |
736 ms |
684 KB |
Output is correct |
53 |
Correct |
647 ms |
848 KB |
Output is correct |
54 |
Correct |
895 ms |
1036 KB |
Output is correct |
55 |
Correct |
1014 ms |
868 KB |
Output is correct |
56 |
Correct |
856 ms |
800 KB |
Output is correct |
57 |
Correct |
930 ms |
680 KB |
Output is correct |
58 |
Correct |
1046 ms |
1268 KB |
Output is correct |
59 |
Correct |
969 ms |
1304 KB |
Output is correct |
60 |
Correct |
829 ms |
1260 KB |
Output is correct |
61 |
Correct |
847 ms |
848 KB |
Output is correct |
62 |
Correct |
881 ms |
592 KB |
Output is correct |
63 |
Correct |
818 ms |
560 KB |
Output is correct |
64 |
Correct |
882 ms |
592 KB |
Output is correct |
65 |
Correct |
436 ms |
560 KB |
Output is correct |
66 |
Correct |
742 ms |
812 KB |
Output is correct |
67 |
Correct |
954 ms |
848 KB |
Output is correct |
68 |
Correct |
753 ms |
820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 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 |
368 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
208 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 |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
372 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
2 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 |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
336 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 |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 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 |
464 KB |
Output is correct |
37 |
Correct |
1 ms |
464 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 |
684 ms |
4536 KB |
Output is correct |
44 |
Correct |
893 ms |
8752 KB |
Output is correct |
45 |
Correct |
773 ms |
8748 KB |
Output is correct |
46 |
Correct |
852 ms |
8748 KB |
Output is correct |
47 |
Correct |
740 ms |
4540 KB |
Output is correct |
48 |
Correct |
863 ms |
8748 KB |
Output is correct |
49 |
Correct |
916 ms |
8776 KB |
Output is correct |
50 |
Correct |
814 ms |
8752 KB |
Output is correct |
51 |
Correct |
413 ms |
592 KB |
Output is correct |
52 |
Correct |
772 ms |
848 KB |
Output is correct |
53 |
Correct |
779 ms |
808 KB |
Output is correct |
54 |
Correct |
835 ms |
848 KB |
Output is correct |
55 |
Correct |
839 ms |
13160 KB |
Output is correct |
56 |
Correct |
1101 ms |
13492 KB |
Output is correct |
57 |
Correct |
829 ms |
13372 KB |
Output is correct |
58 |
Correct |
803 ms |
13472 KB |
Output is correct |
59 |
Correct |
1033 ms |
18116 KB |
Output is correct |
60 |
Correct |
792 ms |
18088 KB |
Output is correct |
61 |
Correct |
678 ms |
736 KB |
Output is correct |
62 |
Correct |
975 ms |
796 KB |
Output is correct |
63 |
Correct |
864 ms |
756 KB |
Output is correct |
64 |
Correct |
858 ms |
1028 KB |
Output is correct |
65 |
Correct |
807 ms |
1044 KB |
Output is correct |
66 |
Correct |
881 ms |
1048 KB |
Output is correct |
67 |
Correct |
757 ms |
1044 KB |
Output is correct |
68 |
Correct |
807 ms |
1024 KB |
Output is correct |
69 |
Correct |
762 ms |
560 KB |
Output is correct |
70 |
Correct |
736 ms |
684 KB |
Output is correct |
71 |
Correct |
647 ms |
848 KB |
Output is correct |
72 |
Correct |
895 ms |
1036 KB |
Output is correct |
73 |
Correct |
1014 ms |
868 KB |
Output is correct |
74 |
Correct |
856 ms |
800 KB |
Output is correct |
75 |
Correct |
930 ms |
680 KB |
Output is correct |
76 |
Correct |
1046 ms |
1268 KB |
Output is correct |
77 |
Correct |
969 ms |
1304 KB |
Output is correct |
78 |
Correct |
829 ms |
1260 KB |
Output is correct |
79 |
Correct |
847 ms |
848 KB |
Output is correct |
80 |
Correct |
881 ms |
592 KB |
Output is correct |
81 |
Correct |
818 ms |
560 KB |
Output is correct |
82 |
Correct |
882 ms |
592 KB |
Output is correct |
83 |
Correct |
436 ms |
560 KB |
Output is correct |
84 |
Correct |
742 ms |
812 KB |
Output is correct |
85 |
Correct |
954 ms |
848 KB |
Output is correct |
86 |
Correct |
753 ms |
820 KB |
Output is correct |
87 |
Correct |
0 ms |
208 KB |
Output is correct |
88 |
Correct |
575 ms |
12244 KB |
Output is correct |
89 |
Correct |
1095 ms |
8924 KB |
Output is correct |
90 |
Correct |
847 ms |
8432 KB |
Output is correct |
91 |
Correct |
880 ms |
13588 KB |
Output is correct |
92 |
Correct |
1075 ms |
13480 KB |
Output is correct |
93 |
Correct |
974 ms |
13504 KB |
Output is correct |
94 |
Correct |
1156 ms |
13512 KB |
Output is correct |
95 |
Correct |
1082 ms |
13504 KB |
Output is correct |
96 |
Correct |
597 ms |
6472 KB |
Output is correct |
97 |
Correct |
870 ms |
6800 KB |
Output is correct |
98 |
Correct |
687 ms |
12324 KB |
Output is correct |
99 |
Correct |
992 ms |
13388 KB |
Output is correct |
100 |
Correct |
936 ms |
9804 KB |
Output is correct |
101 |
Correct |
1161 ms |
8548 KB |
Output is correct |
102 |
Correct |
856 ms |
6808 KB |
Output is correct |
103 |
Correct |
1044 ms |
18084 KB |
Output is correct |
104 |
Correct |
1044 ms |
18588 KB |
Output is correct |
105 |
Correct |
959 ms |
18584 KB |
Output is correct |
106 |
Correct |
631 ms |
9516 KB |
Output is correct |
107 |
Correct |
802 ms |
7984 KB |
Output is correct |
108 |
Correct |
849 ms |
7664 KB |
Output is correct |
109 |
Correct |
1082 ms |
6860 KB |
Output is correct |