#include "bits/extc++.h"
using namespace std;
template <typename T>
void dbgh(const T& t) {
cerr << t << endl;
}
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t << " | ";
dbgh(u...);
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
<< ": "; \
dbgh(__VA_ARGS__)
#else
#define cerr \
if (false) \
cerr
#define dbg(...)
#endif
#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())
const int maxn = 2e5 + 5;
const long mod = 1e9 + 2022;
pair<long, long> operator+(pair<long, long> a, pair<long, long> b) {
return {a.first + b.first, a.second + b.second};
}
struct DS {
int n;
vector<int> lazy;
vector<pair<long, long>> arr, v;
DS() {}
DS(vector<int> v, vector<int> on)
: n(sz(v)), lazy(4 * n), arr(n), v(4 * n) {
for (int i = 0; i < n; i++) {
arr[i] = {0, v[i]};
if (on[i]) {
swap(arr[i].first, arr[i].second);
}
}
build(1, 0, n - 1);
}
void build(int o, int l, int r) {
if (l == r) {
v[o] = arr[l];
return;
}
int mid = (l + r) / 2, lc = o * 2, rc = lc + 1;
build(lc, l, mid);
build(rc, mid + 1, r);
v[o] = v[lc] + v[rc];
}
void update(int o, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) {
swap(v[o].first, v[o].second);
lazy[o] ^= 1;
return;
}
int mid = (l + r) / 2, lc = o * 2, rc = lc + 1;
if (ql <= mid) {
update(lc, l, mid, ql, qr);
}
if (mid < qr) {
update(rc, mid + 1, r, ql, qr);
}
v[o] = v[lc] + v[rc];
if (lazy[o]) {
swap(v[o].first, v[o].second);
}
}
void update(int l, int r) {
update(1, 0, n - 1, l, r);
}
int query() const {
return int(v[1].first % mod);
}
} ds;
int n;
vector<int> arr, graph[maxn];
long ways[maxn], contrib[maxn];
void pdfs(int u) {
ways[u] = 1;
for (auto& v : graph[u]) {
pdfs(v);
ways[u] = (ways[u] * ways[v]) % mod;
}
if (sz(graph[u])) {
ways[u] = (ways[u] * sz(graph[u])) % mod;
}
dbg(u, ways[u]);
}
void dfs(int u, long x = 1) {
int m = sz(graph[u]);
if (!m) {
dbg(u, x);
contrib[u] = x;
return;
}
long psum[m + 1], ssum[m + 1];
psum[0] = ssum[m] = 1;
for (int i = 0; i < m; i++) {
psum[i + 1] = (psum[i] * ways[graph[u][i]]) % mod;
}
for (int i = m - 1; i >= 0; i--) {
ssum[i] = (ssum[i + 1] * ways[graph[u][i]]) % mod;
}
for (int i = 0; i < m; i++) {
dfs(graph[u][i], x * psum[i] % mod * ssum[i + 1] % mod);
}
}
void init(int _n, int m, vector<int> p, vector<int> _arr) {
n = _n;
arr = _arr;
for (int i = 1; i < n + m; i++) {
graph[p[i]].push_back(i);
}
pdfs(0);
dfs(0);
ds = DS(vector<int>(contrib + n, contrib + n + m), arr);
}
int count_ways(int l, int r) {
l -= n;
r -= n;
ds.update(l, r);
return ds.query();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
2 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5092 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5032 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5140 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
5100 KB |
Output is correct |
10 |
Correct |
5 ms |
5328 KB |
Output is correct |
11 |
Correct |
3 ms |
5328 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
2 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5092 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
5032 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
3 ms |
5072 KB |
Output is correct |
14 |
Correct |
3 ms |
5140 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5072 KB |
Output is correct |
17 |
Correct |
3 ms |
5100 KB |
Output is correct |
18 |
Correct |
5 ms |
5328 KB |
Output is correct |
19 |
Correct |
3 ms |
5328 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
5116 KB |
Output is correct |
24 |
Correct |
3 ms |
5168 KB |
Output is correct |
25 |
Correct |
3 ms |
5072 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
3 ms |
5072 KB |
Output is correct |
28 |
Correct |
3 ms |
5072 KB |
Output is correct |
29 |
Correct |
3 ms |
5096 KB |
Output is correct |
30 |
Correct |
3 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5072 KB |
Output is correct |
32 |
Correct |
4 ms |
5072 KB |
Output is correct |
33 |
Correct |
3 ms |
5072 KB |
Output is correct |
34 |
Correct |
3 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5072 KB |
Output is correct |
36 |
Correct |
4 ms |
5328 KB |
Output is correct |
37 |
Correct |
3 ms |
5328 KB |
Output is correct |
38 |
Correct |
3 ms |
5328 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5140 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
516 ms |
10960 KB |
Output is correct |
2 |
Correct |
853 ms |
17028 KB |
Output is correct |
3 |
Correct |
788 ms |
17020 KB |
Output is correct |
4 |
Correct |
906 ms |
17144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
516 ms |
10960 KB |
Output is correct |
2 |
Correct |
853 ms |
17028 KB |
Output is correct |
3 |
Correct |
788 ms |
17020 KB |
Output is correct |
4 |
Correct |
906 ms |
17144 KB |
Output is correct |
5 |
Correct |
752 ms |
11012 KB |
Output is correct |
6 |
Correct |
976 ms |
16968 KB |
Output is correct |
7 |
Correct |
954 ms |
17020 KB |
Output is correct |
8 |
Correct |
793 ms |
17024 KB |
Output is correct |
9 |
Correct |
249 ms |
5364 KB |
Output is correct |
10 |
Correct |
912 ms |
5712 KB |
Output is correct |
11 |
Correct |
886 ms |
5712 KB |
Output is correct |
12 |
Correct |
730 ms |
5712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5032 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5140 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
5100 KB |
Output is correct |
10 |
Correct |
5 ms |
5328 KB |
Output is correct |
11 |
Correct |
3 ms |
5328 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
516 ms |
10960 KB |
Output is correct |
14 |
Correct |
853 ms |
17028 KB |
Output is correct |
15 |
Correct |
788 ms |
17020 KB |
Output is correct |
16 |
Correct |
906 ms |
17144 KB |
Output is correct |
17 |
Correct |
752 ms |
11012 KB |
Output is correct |
18 |
Correct |
976 ms |
16968 KB |
Output is correct |
19 |
Correct |
954 ms |
17020 KB |
Output is correct |
20 |
Correct |
793 ms |
17024 KB |
Output is correct |
21 |
Correct |
249 ms |
5364 KB |
Output is correct |
22 |
Correct |
912 ms |
5712 KB |
Output is correct |
23 |
Correct |
886 ms |
5712 KB |
Output is correct |
24 |
Correct |
730 ms |
5712 KB |
Output is correct |
25 |
Correct |
797 ms |
23044 KB |
Output is correct |
26 |
Correct |
989 ms |
23368 KB |
Output is correct |
27 |
Correct |
838 ms |
23292 KB |
Output is correct |
28 |
Correct |
835 ms |
23312 KB |
Output is correct |
29 |
Correct |
1078 ms |
39028 KB |
Output is correct |
30 |
Correct |
1010 ms |
39024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
2 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5092 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
5032 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
3 ms |
5072 KB |
Output is correct |
14 |
Correct |
3 ms |
5140 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5072 KB |
Output is correct |
17 |
Correct |
3 ms |
5100 KB |
Output is correct |
18 |
Correct |
5 ms |
5328 KB |
Output is correct |
19 |
Correct |
3 ms |
5328 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
5116 KB |
Output is correct |
24 |
Correct |
3 ms |
5168 KB |
Output is correct |
25 |
Correct |
3 ms |
5072 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
3 ms |
5072 KB |
Output is correct |
28 |
Correct |
3 ms |
5072 KB |
Output is correct |
29 |
Correct |
3 ms |
5096 KB |
Output is correct |
30 |
Correct |
3 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5072 KB |
Output is correct |
32 |
Correct |
4 ms |
5072 KB |
Output is correct |
33 |
Correct |
3 ms |
5072 KB |
Output is correct |
34 |
Correct |
3 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5072 KB |
Output is correct |
36 |
Correct |
4 ms |
5328 KB |
Output is correct |
37 |
Correct |
3 ms |
5328 KB |
Output is correct |
38 |
Correct |
3 ms |
5328 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5140 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
3 ms |
5072 KB |
Output is correct |
43 |
Correct |
651 ms |
5456 KB |
Output is correct |
44 |
Correct |
657 ms |
5456 KB |
Output is correct |
45 |
Correct |
900 ms |
5456 KB |
Output is correct |
46 |
Correct |
858 ms |
5840 KB |
Output is correct |
47 |
Correct |
825 ms |
5840 KB |
Output is correct |
48 |
Correct |
853 ms |
5840 KB |
Output is correct |
49 |
Correct |
916 ms |
5892 KB |
Output is correct |
50 |
Correct |
856 ms |
5840 KB |
Output is correct |
51 |
Correct |
792 ms |
5712 KB |
Output is correct |
52 |
Correct |
841 ms |
5712 KB |
Output is correct |
53 |
Correct |
839 ms |
5840 KB |
Output is correct |
54 |
Correct |
559 ms |
5840 KB |
Output is correct |
55 |
Correct |
563 ms |
5712 KB |
Output is correct |
56 |
Correct |
743 ms |
5712 KB |
Output is correct |
57 |
Correct |
692 ms |
5680 KB |
Output is correct |
58 |
Correct |
765 ms |
6608 KB |
Output is correct |
59 |
Correct |
989 ms |
6584 KB |
Output is correct |
60 |
Correct |
659 ms |
6608 KB |
Output is correct |
61 |
Correct |
829 ms |
5584 KB |
Output is correct |
62 |
Correct |
924 ms |
5584 KB |
Output is correct |
63 |
Correct |
587 ms |
5584 KB |
Output is correct |
64 |
Correct |
873 ms |
5712 KB |
Output is correct |
65 |
Correct |
259 ms |
5364 KB |
Output is correct |
66 |
Correct |
793 ms |
5712 KB |
Output is correct |
67 |
Correct |
897 ms |
5712 KB |
Output is correct |
68 |
Correct |
884 ms |
5712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
2 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5092 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
5032 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
3 ms |
5072 KB |
Output is correct |
14 |
Correct |
3 ms |
5140 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5072 KB |
Output is correct |
17 |
Correct |
3 ms |
5100 KB |
Output is correct |
18 |
Correct |
5 ms |
5328 KB |
Output is correct |
19 |
Correct |
3 ms |
5328 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
5116 KB |
Output is correct |
24 |
Correct |
3 ms |
5168 KB |
Output is correct |
25 |
Correct |
3 ms |
5072 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
3 ms |
5072 KB |
Output is correct |
28 |
Correct |
3 ms |
5072 KB |
Output is correct |
29 |
Correct |
3 ms |
5096 KB |
Output is correct |
30 |
Correct |
3 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5072 KB |
Output is correct |
32 |
Correct |
4 ms |
5072 KB |
Output is correct |
33 |
Correct |
3 ms |
5072 KB |
Output is correct |
34 |
Correct |
3 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5072 KB |
Output is correct |
36 |
Correct |
4 ms |
5328 KB |
Output is correct |
37 |
Correct |
3 ms |
5328 KB |
Output is correct |
38 |
Correct |
3 ms |
5328 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5140 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
3 ms |
5072 KB |
Output is correct |
43 |
Correct |
516 ms |
10960 KB |
Output is correct |
44 |
Correct |
853 ms |
17028 KB |
Output is correct |
45 |
Correct |
788 ms |
17020 KB |
Output is correct |
46 |
Correct |
906 ms |
17144 KB |
Output is correct |
47 |
Correct |
752 ms |
11012 KB |
Output is correct |
48 |
Correct |
976 ms |
16968 KB |
Output is correct |
49 |
Correct |
954 ms |
17020 KB |
Output is correct |
50 |
Correct |
793 ms |
17024 KB |
Output is correct |
51 |
Correct |
249 ms |
5364 KB |
Output is correct |
52 |
Correct |
912 ms |
5712 KB |
Output is correct |
53 |
Correct |
886 ms |
5712 KB |
Output is correct |
54 |
Correct |
730 ms |
5712 KB |
Output is correct |
55 |
Correct |
797 ms |
23044 KB |
Output is correct |
56 |
Correct |
989 ms |
23368 KB |
Output is correct |
57 |
Correct |
838 ms |
23292 KB |
Output is correct |
58 |
Correct |
835 ms |
23312 KB |
Output is correct |
59 |
Correct |
1078 ms |
39028 KB |
Output is correct |
60 |
Correct |
1010 ms |
39024 KB |
Output is correct |
61 |
Correct |
651 ms |
5456 KB |
Output is correct |
62 |
Correct |
657 ms |
5456 KB |
Output is correct |
63 |
Correct |
900 ms |
5456 KB |
Output is correct |
64 |
Correct |
858 ms |
5840 KB |
Output is correct |
65 |
Correct |
825 ms |
5840 KB |
Output is correct |
66 |
Correct |
853 ms |
5840 KB |
Output is correct |
67 |
Correct |
916 ms |
5892 KB |
Output is correct |
68 |
Correct |
856 ms |
5840 KB |
Output is correct |
69 |
Correct |
792 ms |
5712 KB |
Output is correct |
70 |
Correct |
841 ms |
5712 KB |
Output is correct |
71 |
Correct |
839 ms |
5840 KB |
Output is correct |
72 |
Correct |
559 ms |
5840 KB |
Output is correct |
73 |
Correct |
563 ms |
5712 KB |
Output is correct |
74 |
Correct |
743 ms |
5712 KB |
Output is correct |
75 |
Correct |
692 ms |
5680 KB |
Output is correct |
76 |
Correct |
765 ms |
6608 KB |
Output is correct |
77 |
Correct |
989 ms |
6584 KB |
Output is correct |
78 |
Correct |
659 ms |
6608 KB |
Output is correct |
79 |
Correct |
829 ms |
5584 KB |
Output is correct |
80 |
Correct |
924 ms |
5584 KB |
Output is correct |
81 |
Correct |
587 ms |
5584 KB |
Output is correct |
82 |
Correct |
873 ms |
5712 KB |
Output is correct |
83 |
Correct |
259 ms |
5364 KB |
Output is correct |
84 |
Correct |
793 ms |
5712 KB |
Output is correct |
85 |
Correct |
897 ms |
5712 KB |
Output is correct |
86 |
Correct |
884 ms |
5712 KB |
Output is correct |
87 |
Correct |
3 ms |
4944 KB |
Output is correct |
88 |
Correct |
546 ms |
20752 KB |
Output is correct |
89 |
Correct |
813 ms |
15304 KB |
Output is correct |
90 |
Correct |
838 ms |
15944 KB |
Output is correct |
91 |
Correct |
871 ms |
23440 KB |
Output is correct |
92 |
Correct |
891 ms |
23420 KB |
Output is correct |
93 |
Correct |
969 ms |
23400 KB |
Output is correct |
94 |
Correct |
1053 ms |
23428 KB |
Output is correct |
95 |
Correct |
926 ms |
23420 KB |
Output is correct |
96 |
Correct |
826 ms |
20744 KB |
Output is correct |
97 |
Correct |
884 ms |
20732 KB |
Output is correct |
98 |
Correct |
830 ms |
22088 KB |
Output is correct |
99 |
Correct |
982 ms |
23356 KB |
Output is correct |
100 |
Correct |
997 ms |
20936 KB |
Output is correct |
101 |
Correct |
854 ms |
20296 KB |
Output is correct |
102 |
Correct |
793 ms |
19192 KB |
Output is correct |
103 |
Correct |
997 ms |
39024 KB |
Output is correct |
104 |
Correct |
869 ms |
38008 KB |
Output is correct |
105 |
Correct |
989 ms |
37908 KB |
Output is correct |
106 |
Correct |
817 ms |
17396 KB |
Output is correct |
107 |
Correct |
748 ms |
16048 KB |
Output is correct |
108 |
Correct |
875 ms |
17096 KB |
Output is correct |
109 |
Correct |
1004 ms |
19188 KB |
Output is correct |