#include "circuit.h"
#include <algorithm>
#include <array>
#include <cassert>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using std::array;
using std::pair;
using std::string;
using std::tuple;
using std::vector;
using ll = long long;
constexpr int inf = (1 << 30) - 1;
constexpr ll infll = (1ll << 62) - 1;
template <class F> struct make_fixed : private F {
explicit make_fixed(F&& f) : F(std::forward<F>(f)) {}
template <class... Args> decltype(auto) operator()(Args&&... args) const {
return F::operator()(*this, std::forward<Args>(args)...);
}
};
template <class T, class... Args> void DBG(const T& x, const Args&... args) {
std::cerr << x;
if constexpr (sizeof...(args)) {
std::cerr << ' ';
DBG(args...);
} else {
std::cerr << std::endl;
}
}
template <class T> void DBG(const std::vector<T>& v) {
std::cerr << '[';
bool f = false;
for (const T& x : v) {
if (f) std::cerr << ", ";
f = true;
std::cerr << x;
}
std::cerr << ']' << std::endl;
}
constexpr int mod = 1000002022;
struct Fp {
Fp() : x(0) {}
Fp(int x) : x(x) {}
Fp& operator+=(const Fp& t) {
if ((x += t.x) >= mod) x -= mod;
return *this;
}
Fp& operator-=(const Fp& t) {
if ((x -= t.x) < 0) x += mod;
return *this;
}
Fp& operator*=(const Fp& t) {
x = (ll)x * t.x % mod;
return *this;
}
Fp operator+(const Fp& t) const { return Fp(*this) += t; }
Fp operator-(const Fp& t) const { return Fp(*this) -= t; }
Fp operator*(const Fp& t) const { return Fp(*this) *= t; }
int get() const { return x; }
friend std::ostream& operator<<(std::ostream& os, const Fp& t) { return os << t.x; }
private:
int x;
};
struct segment_tree {
segment_tree() = default;
explicit segment_tree(vector<pair<Fp, Fp>> init) {
logn = 0;
while ((1 << logn) < (int)init.size()) logn += 1;
size = 1 << logn;
data.resize(2 * size);
sub.resize(2 * size);
flip.resize(size);
for (int i = 0; i < (int)init.size(); ++i) std::tie(data[i + size], sub[i + size]) = init[i];
for (int i = size - 1; i > 0; --i) fetch(i);
}
void range_flip(int l, int r) {
l += size;
r += size;
push(l);
push(r);
for (int s = l, t = r; s < t; s >>= 1, t >>= 1) {
if (s & 1) apply(s++);
if (t & 1) apply(--t);
}
pull(l);
pull(r);
}
Fp get() const { return data[1]; }
private:
int size, logn;
vector<Fp> data, sub;
vector<char> flip;
void fetch(int i) {
data[i] = data[2 * i] + data[2 * i + 1];
sub[i] = sub[2 * i] + sub[2 * i + 1];
}
void apply(int i) {
std::swap(data[i], sub[i]);
if (i < size) flip[i] ^= 1;
}
void flush(int i) {
if (flip[i]) {
apply(2 * i);
apply(2 * i + 1);
flip[i] = false;
}
}
void push(int i) {
int rz = __builtin_ctz(i);
for (int d = logn; d > rz; --d) flush(i >> d);
}
void pull(int i) {
i >>= __builtin_ctz(i);
while (i >>= 1) fetch(i);
}
};
int N_memo;
segment_tree seg;
void init(int N, int M, vector<int> P, vector<int> A) {
N_memo = N;
vector<vector<int>> child(N + M);
for (int i = 1; i < N + M; ++i) child[P[i]].push_back(i);
vector<Fp> ways(N + M, 1);
for (int i = N - 1; i >= 0; --i) {
ways[i] = (int)child[i].size();
for (const int j : child[i]) ways[i] *= ways[j];
}
vector<Fp> coeff(N + M, 1);
for (int i = 0; i < N; ++i) {
const int n = (int)child[i].size();
vector<Fp> suff(n);
suff[n - 1] = 1;
for (int k = n - 1; k > 0; --k) suff[k - 1] = suff[k] * ways[child[i][k]];
Fp pref = coeff[i];
for (int k = 0; k < n; ++k) {
coeff[child[i][k]] = pref * suff[k];
pref *= ways[child[i][k]];
}
}
vector<pair<Fp, Fp>> data(M);
for (int i = 0; i < M; ++i) {
if (A[i]) data[i] = {coeff[i + N], 0};
else data[i] = {0, coeff[i + N]};
}
seg = segment_tree(std::move(data));
}
int count_ways(int L, int R) {
seg.range_flip(L - N_memo, R - N_memo + 1);
return seg.get().get();
}
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
336 KB |
Output is correct |
6 |
Correct |
0 ms |
376 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
240 KB |
Output is correct |
2 |
Correct |
1 ms |
384 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 |
1 ms |
336 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
336 KB |
Output is correct |
6 |
Correct |
0 ms |
376 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
240 KB |
Output is correct |
10 |
Correct |
1 ms |
384 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 |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
384 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 |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 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 |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
2 ms |
336 KB |
Output is correct |
39 |
Correct |
0 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
490 ms |
4964 KB |
Output is correct |
2 |
Correct |
671 ms |
9552 KB |
Output is correct |
3 |
Correct |
790 ms |
9500 KB |
Output is correct |
4 |
Correct |
849 ms |
9528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
490 ms |
4964 KB |
Output is correct |
2 |
Correct |
671 ms |
9552 KB |
Output is correct |
3 |
Correct |
790 ms |
9500 KB |
Output is correct |
4 |
Correct |
849 ms |
9528 KB |
Output is correct |
5 |
Correct |
549 ms |
4904 KB |
Output is correct |
6 |
Correct |
710 ms |
9496 KB |
Output is correct |
7 |
Correct |
890 ms |
9604 KB |
Output is correct |
8 |
Correct |
685 ms |
9516 KB |
Output is correct |
9 |
Correct |
311 ms |
572 KB |
Output is correct |
10 |
Correct |
592 ms |
848 KB |
Output is correct |
11 |
Correct |
745 ms |
848 KB |
Output is correct |
12 |
Correct |
721 ms |
848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
240 KB |
Output is correct |
2 |
Correct |
1 ms |
384 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 |
1 ms |
336 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 |
490 ms |
4964 KB |
Output is correct |
14 |
Correct |
671 ms |
9552 KB |
Output is correct |
15 |
Correct |
790 ms |
9500 KB |
Output is correct |
16 |
Correct |
849 ms |
9528 KB |
Output is correct |
17 |
Correct |
549 ms |
4904 KB |
Output is correct |
18 |
Correct |
710 ms |
9496 KB |
Output is correct |
19 |
Correct |
890 ms |
9604 KB |
Output is correct |
20 |
Correct |
685 ms |
9516 KB |
Output is correct |
21 |
Correct |
311 ms |
572 KB |
Output is correct |
22 |
Correct |
592 ms |
848 KB |
Output is correct |
23 |
Correct |
745 ms |
848 KB |
Output is correct |
24 |
Correct |
721 ms |
848 KB |
Output is correct |
25 |
Correct |
882 ms |
14740 KB |
Output is correct |
26 |
Correct |
869 ms |
15000 KB |
Output is correct |
27 |
Correct |
706 ms |
15000 KB |
Output is correct |
28 |
Correct |
617 ms |
14900 KB |
Output is correct |
29 |
Correct |
816 ms |
14904 KB |
Output is correct |
30 |
Correct |
794 ms |
14892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
336 KB |
Output is correct |
6 |
Correct |
0 ms |
376 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
240 KB |
Output is correct |
10 |
Correct |
1 ms |
384 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 |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
384 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 |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 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 |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
2 ms |
336 KB |
Output is correct |
39 |
Correct |
0 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 |
479 ms |
720 KB |
Output is correct |
44 |
Correct |
585 ms |
712 KB |
Output is correct |
45 |
Correct |
657 ms |
672 KB |
Output is correct |
46 |
Correct |
695 ms |
976 KB |
Output is correct |
47 |
Correct |
603 ms |
976 KB |
Output is correct |
48 |
Correct |
685 ms |
968 KB |
Output is correct |
49 |
Correct |
725 ms |
976 KB |
Output is correct |
50 |
Correct |
679 ms |
976 KB |
Output is correct |
51 |
Correct |
784 ms |
720 KB |
Output is correct |
52 |
Correct |
729 ms |
716 KB |
Output is correct |
53 |
Correct |
592 ms |
592 KB |
Output is correct |
54 |
Correct |
553 ms |
976 KB |
Output is correct |
55 |
Correct |
609 ms |
848 KB |
Output is correct |
56 |
Correct |
785 ms |
796 KB |
Output is correct |
57 |
Correct |
843 ms |
840 KB |
Output is correct |
58 |
Correct |
554 ms |
988 KB |
Output is correct |
59 |
Correct |
475 ms |
1104 KB |
Output is correct |
60 |
Correct |
728 ms |
1104 KB |
Output is correct |
61 |
Correct |
579 ms |
676 KB |
Output is correct |
62 |
Correct |
687 ms |
592 KB |
Output is correct |
63 |
Correct |
620 ms |
712 KB |
Output is correct |
64 |
Correct |
484 ms |
728 KB |
Output is correct |
65 |
Correct |
425 ms |
576 KB |
Output is correct |
66 |
Correct |
598 ms |
844 KB |
Output is correct |
67 |
Correct |
649 ms |
800 KB |
Output is correct |
68 |
Correct |
453 ms |
840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
336 KB |
Output is correct |
6 |
Correct |
0 ms |
376 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
240 KB |
Output is correct |
10 |
Correct |
1 ms |
384 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 |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
384 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 |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 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 |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
2 ms |
336 KB |
Output is correct |
39 |
Correct |
0 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 |
490 ms |
4964 KB |
Output is correct |
44 |
Correct |
671 ms |
9552 KB |
Output is correct |
45 |
Correct |
790 ms |
9500 KB |
Output is correct |
46 |
Correct |
849 ms |
9528 KB |
Output is correct |
47 |
Correct |
549 ms |
4904 KB |
Output is correct |
48 |
Correct |
710 ms |
9496 KB |
Output is correct |
49 |
Correct |
890 ms |
9604 KB |
Output is correct |
50 |
Correct |
685 ms |
9516 KB |
Output is correct |
51 |
Correct |
311 ms |
572 KB |
Output is correct |
52 |
Correct |
592 ms |
848 KB |
Output is correct |
53 |
Correct |
745 ms |
848 KB |
Output is correct |
54 |
Correct |
721 ms |
848 KB |
Output is correct |
55 |
Correct |
882 ms |
14740 KB |
Output is correct |
56 |
Correct |
869 ms |
15000 KB |
Output is correct |
57 |
Correct |
706 ms |
15000 KB |
Output is correct |
58 |
Correct |
617 ms |
14900 KB |
Output is correct |
59 |
Correct |
816 ms |
14904 KB |
Output is correct |
60 |
Correct |
794 ms |
14892 KB |
Output is correct |
61 |
Correct |
479 ms |
720 KB |
Output is correct |
62 |
Correct |
585 ms |
712 KB |
Output is correct |
63 |
Correct |
657 ms |
672 KB |
Output is correct |
64 |
Correct |
695 ms |
976 KB |
Output is correct |
65 |
Correct |
603 ms |
976 KB |
Output is correct |
66 |
Correct |
685 ms |
968 KB |
Output is correct |
67 |
Correct |
725 ms |
976 KB |
Output is correct |
68 |
Correct |
679 ms |
976 KB |
Output is correct |
69 |
Correct |
784 ms |
720 KB |
Output is correct |
70 |
Correct |
729 ms |
716 KB |
Output is correct |
71 |
Correct |
592 ms |
592 KB |
Output is correct |
72 |
Correct |
553 ms |
976 KB |
Output is correct |
73 |
Correct |
609 ms |
848 KB |
Output is correct |
74 |
Correct |
785 ms |
796 KB |
Output is correct |
75 |
Correct |
843 ms |
840 KB |
Output is correct |
76 |
Correct |
554 ms |
988 KB |
Output is correct |
77 |
Correct |
475 ms |
1104 KB |
Output is correct |
78 |
Correct |
728 ms |
1104 KB |
Output is correct |
79 |
Correct |
579 ms |
676 KB |
Output is correct |
80 |
Correct |
687 ms |
592 KB |
Output is correct |
81 |
Correct |
620 ms |
712 KB |
Output is correct |
82 |
Correct |
484 ms |
728 KB |
Output is correct |
83 |
Correct |
425 ms |
576 KB |
Output is correct |
84 |
Correct |
598 ms |
844 KB |
Output is correct |
85 |
Correct |
649 ms |
800 KB |
Output is correct |
86 |
Correct |
453 ms |
840 KB |
Output is correct |
87 |
Correct |
0 ms |
208 KB |
Output is correct |
88 |
Correct |
301 ms |
13584 KB |
Output is correct |
89 |
Correct |
706 ms |
9556 KB |
Output is correct |
90 |
Correct |
755 ms |
9192 KB |
Output is correct |
91 |
Correct |
826 ms |
15128 KB |
Output is correct |
92 |
Correct |
698 ms |
15128 KB |
Output is correct |
93 |
Correct |
626 ms |
15136 KB |
Output is correct |
94 |
Correct |
684 ms |
15048 KB |
Output is correct |
95 |
Correct |
773 ms |
15064 KB |
Output is correct |
96 |
Correct |
807 ms |
8332 KB |
Output is correct |
97 |
Correct |
851 ms |
8352 KB |
Output is correct |
98 |
Correct |
624 ms |
7240 KB |
Output is correct |
99 |
Correct |
575 ms |
14992 KB |
Output is correct |
100 |
Correct |
687 ms |
11408 KB |
Output is correct |
101 |
Correct |
785 ms |
10136 KB |
Output is correct |
102 |
Correct |
676 ms |
8468 KB |
Output is correct |
103 |
Correct |
755 ms |
15012 KB |
Output is correct |
104 |
Correct |
687 ms |
15376 KB |
Output is correct |
105 |
Correct |
744 ms |
15404 KB |
Output is correct |
106 |
Correct |
594 ms |
9116 KB |
Output is correct |
107 |
Correct |
979 ms |
8984 KB |
Output is correct |
108 |
Correct |
652 ms |
8972 KB |
Output is correct |
109 |
Correct |
802 ms |
8472 KB |
Output is correct |