Submission #798061

# Submission time Handle Problem Language Result Execution time Memory
798061 2023-07-30T10:33:24 Z jakobrs Digital Circuit (IOI22_circuit) C++17
100 / 100
940 ms 37144 KB
#include <iostream>
#include <vector>

using i64 = int64_t;

const i64 MOD = 1'000'002'022;

struct Node {
    i64 a1, a0;

    Node() : a1(0), a0(0) {}
    Node(i64 a1, i64 a0) : a1(a1), a0(a0) {}

    void toggle() { std::swap(a1, a0); }

    Node operator*(const Node &rhs) const {
        return {(a1 + rhs.a1) % MOD, (a0 + rhs.a0) % MOD};
    }

    // Node operator*(const Node &rhs) const {
    //     return {(a1 * rhs.ax + ax * rhs.a1) % MOD,
    //             (a0 * rhs.ax + ax * rhs.a0) % MOD, ax * rhs.ax * 2 % MOD};
    // }
};

struct SegmentTree {
    std::vector<Node> values;
    std::vector<bool> toggled;
    i64 offset;

    SegmentTree() : values{}, toggled{}, offset{0} {}
    explicit SegmentTree(size_t sz, const std::vector<i64> contribution)
        : values(2 * sz), toggled(2 * sz, false), offset(sz) {
        for (i64 i = 0; i < sz; i++) {
            i64 c = i < contribution.size() ? contribution[i] : 0;
            values[sz + i] = Node(0, c);
        }

        for (i64 i = sz - 1; i > 0; i--) pull(i);
    }

    void toggle(i64 idx) {
        idx += offset;

        values[idx].toggle();

        while (idx /= 2) pull(idx);
    }

    void toggle_range(i64 l, i64 r) { toggle_range(l, r, 1, 0, offset); }
    void toggle_range(i64 l, i64 r, i64 v, i64 s, i64 e) {
        if (e <= l || r <= s) {
            return;
        } else if (l <= s && e <= r) {
            values[v].toggle();
            toggled[v] = !toggled[v];
        } else {
            i64 m = (s + e) / 2;

            push(v);
            toggle_range(l, r, 2 * v, s, m);
            toggle_range(l, r, 2 * v + 1, m, e);
            pull(v);
        }
    }

    void push(i64 v) {
        if (toggled[v]) {
            toggled[v] = false;

            toggled[2 * v] = !toggled[2 * v];
            toggled[2 * v + 1] = !toggled[2 * v + 1];

            values[2 * v].toggle();
            values[2 * v + 1].toggle();
        }
    }

    void pull(i64 v) { values[v] = values[2 * v] * values[2 * v + 1]; }

    const Node &root() const { return values[1]; }
};

int n, m;
std::vector<int> p, a;

std::vector<std::vector<int>> children;
std::vector<i64> possible;
std::vector<i64> contribution;
SegmentTree st;

i64 &dfs(int node) {
    if (node >= n) return possible[node] = 1;

    i64 total = children[node].size();

    for (i64 c : children[node]) {
        total *= dfs(c);
        total %= MOD;
    }

    return possible[node] = total;
}
void dfs1(int node, int contrib) {
    if (node >= n) {
        contribution[node - n] = contrib;
        return;
    }

    // std::cerr << node << ' ' << children.size() << ' ' << n << '\n';
    i64 cs = children[node].size();
    std::vector<i64> xs_left{1}, xs_right{1};
    xs_left.reserve(1 + cs);
    xs_right.reserve(1 + cs);

    for (i64 i = 0; i < cs; i++) {
        xs_left.push_back(xs_left.back() * possible[children[node][i]] % MOD);
        xs_right.push_back(xs_right.back() *
                           possible[children[node][cs - i - 1]] % MOD);
    }

    for (i64 i = 0; i < cs; i++) {
        dfs1(children[node][i],
             contrib * xs_left[i] % MOD * xs_right[cs - i - 1] % MOD);
    }
}

void init(int N, int M, std::vector<int> P, std::vector<int> A) {
    n = N;
    m = M;
    p = P;
    a = A;

    children.resize(N);
    for (i64 i = 1; i < N + M; i++) {
        children[P[i]].push_back(i);
    }

    possible.resize(N + M);
    dfs(0);
    contribution.resize(M);
    dfs1(0, 1);

    i64 o = 1;
    while (o < M) o *= 2;
    st = SegmentTree(o, contribution);

    for (i64 i = 0; i < M; i++) {
        if (A[i]) st.toggle(i);
    }
}

int count_ways(int l, int r) {
    r += 1;

    l -= n;
    r -= n;

    st.toggle_range(l, r);

    return st.root().a1;
}

Compilation message

circuit.cpp: In constructor 'SegmentTree::SegmentTree(size_t, std::vector<long int>)':
circuit.cpp:34:27: warning: comparison of integer expressions of different signedness: 'i64' {aka 'long int'} and 'size_t' {aka 'long unsigned int'} [-Wsign-compare]
   34 |         for (i64 i = 0; i < sz; i++) {
      |                         ~~^~~~
circuit.cpp:35:23: warning: comparison of integer expressions of different signedness: 'i64' {aka 'long int'} and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |             i64 c = i < contribution.size() ? contribution[i] : 0;
      |                     ~~^~~~~~~~~~~~~~~~~~~~~
# 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 0 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 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 464 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 592 KB Output is correct
11 Correct 1 ms 592 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 0 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 208 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 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 592 KB Output is correct
19 Correct 1 ms 592 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 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 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 592 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 0 ms 336 KB Output is correct
36 Correct 1 ms 592 KB Output is correct
37 Correct 1 ms 592 KB Output is correct
38 Correct 1 ms 592 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 504 ms 5308 KB Output is correct
2 Correct 708 ms 10312 KB Output is correct
3 Correct 705 ms 10284 KB Output is correct
4 Correct 718 ms 10288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 504 ms 5308 KB Output is correct
2 Correct 708 ms 10312 KB Output is correct
3 Correct 705 ms 10284 KB Output is correct
4 Correct 718 ms 10288 KB Output is correct
5 Correct 602 ms 5276 KB Output is correct
6 Correct 761 ms 10236 KB Output is correct
7 Correct 734 ms 10324 KB Output is correct
8 Correct 828 ms 10312 KB Output is correct
9 Correct 269 ms 592 KB Output is correct
10 Correct 631 ms 848 KB Output is correct
11 Correct 703 ms 848 KB Output is correct
12 Correct 663 ms 848 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 464 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 592 KB Output is correct
11 Correct 1 ms 592 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 504 ms 5308 KB Output is correct
14 Correct 708 ms 10312 KB Output is correct
15 Correct 705 ms 10284 KB Output is correct
16 Correct 718 ms 10288 KB Output is correct
17 Correct 602 ms 5276 KB Output is correct
18 Correct 761 ms 10236 KB Output is correct
19 Correct 734 ms 10324 KB Output is correct
20 Correct 828 ms 10312 KB Output is correct
21 Correct 269 ms 592 KB Output is correct
22 Correct 631 ms 848 KB Output is correct
23 Correct 703 ms 848 KB Output is correct
24 Correct 663 ms 848 KB Output is correct
25 Correct 820 ms 16348 KB Output is correct
26 Correct 711 ms 16568 KB Output is correct
27 Correct 780 ms 16564 KB Output is correct
28 Correct 687 ms 16528 KB Output is correct
29 Correct 857 ms 35124 KB Output is correct
30 Correct 828 ms 35100 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 0 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 208 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 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 592 KB Output is correct
19 Correct 1 ms 592 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 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 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 592 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 0 ms 336 KB Output is correct
36 Correct 1 ms 592 KB Output is correct
37 Correct 1 ms 592 KB Output is correct
38 Correct 1 ms 592 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 436 ms 720 KB Output is correct
44 Correct 704 ms 848 KB Output is correct
45 Correct 726 ms 720 KB Output is correct
46 Correct 619 ms 1104 KB Output is correct
47 Correct 734 ms 1104 KB Output is correct
48 Correct 666 ms 1056 KB Output is correct
49 Correct 830 ms 1104 KB Output is correct
50 Correct 698 ms 1104 KB Output is correct
51 Correct 681 ms 848 KB Output is correct
52 Correct 764 ms 848 KB Output is correct
53 Correct 627 ms 1744 KB Output is correct
54 Correct 569 ms 1104 KB Output is correct
55 Correct 689 ms 976 KB Output is correct
56 Correct 715 ms 928 KB Output is correct
57 Correct 665 ms 720 KB Output is correct
58 Correct 829 ms 2000 KB Output is correct
59 Correct 623 ms 2384 KB Output is correct
60 Correct 711 ms 2384 KB Output is correct
61 Correct 758 ms 1104 KB Output is correct
62 Correct 631 ms 720 KB Output is correct
63 Correct 703 ms 720 KB Output is correct
64 Correct 806 ms 848 KB Output is correct
65 Correct 341 ms 592 KB Output is correct
66 Correct 733 ms 848 KB Output is correct
67 Correct 668 ms 848 KB Output is correct
68 Correct 654 ms 848 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 0 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 208 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 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 592 KB Output is correct
19 Correct 1 ms 592 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 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 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 592 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 0 ms 336 KB Output is correct
36 Correct 1 ms 592 KB Output is correct
37 Correct 1 ms 592 KB Output is correct
38 Correct 1 ms 592 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 504 ms 5308 KB Output is correct
44 Correct 708 ms 10312 KB Output is correct
45 Correct 705 ms 10284 KB Output is correct
46 Correct 718 ms 10288 KB Output is correct
47 Correct 602 ms 5276 KB Output is correct
48 Correct 761 ms 10236 KB Output is correct
49 Correct 734 ms 10324 KB Output is correct
50 Correct 828 ms 10312 KB Output is correct
51 Correct 269 ms 592 KB Output is correct
52 Correct 631 ms 848 KB Output is correct
53 Correct 703 ms 848 KB Output is correct
54 Correct 663 ms 848 KB Output is correct
55 Correct 820 ms 16348 KB Output is correct
56 Correct 711 ms 16568 KB Output is correct
57 Correct 780 ms 16564 KB Output is correct
58 Correct 687 ms 16528 KB Output is correct
59 Correct 857 ms 35124 KB Output is correct
60 Correct 828 ms 35100 KB Output is correct
61 Correct 436 ms 720 KB Output is correct
62 Correct 704 ms 848 KB Output is correct
63 Correct 726 ms 720 KB Output is correct
64 Correct 619 ms 1104 KB Output is correct
65 Correct 734 ms 1104 KB Output is correct
66 Correct 666 ms 1056 KB Output is correct
67 Correct 830 ms 1104 KB Output is correct
68 Correct 698 ms 1104 KB Output is correct
69 Correct 681 ms 848 KB Output is correct
70 Correct 764 ms 848 KB Output is correct
71 Correct 627 ms 1744 KB Output is correct
72 Correct 569 ms 1104 KB Output is correct
73 Correct 689 ms 976 KB Output is correct
74 Correct 715 ms 928 KB Output is correct
75 Correct 665 ms 720 KB Output is correct
76 Correct 829 ms 2000 KB Output is correct
77 Correct 623 ms 2384 KB Output is correct
78 Correct 711 ms 2384 KB Output is correct
79 Correct 758 ms 1104 KB Output is correct
80 Correct 631 ms 720 KB Output is correct
81 Correct 703 ms 720 KB Output is correct
82 Correct 806 ms 848 KB Output is correct
83 Correct 341 ms 592 KB Output is correct
84 Correct 733 ms 848 KB Output is correct
85 Correct 668 ms 848 KB Output is correct
86 Correct 654 ms 848 KB Output is correct
87 Correct 0 ms 208 KB Output is correct
88 Correct 499 ms 15260 KB Output is correct
89 Correct 788 ms 10524 KB Output is correct
90 Correct 769 ms 10008 KB Output is correct
91 Correct 774 ms 16584 KB Output is correct
92 Correct 775 ms 16668 KB Output is correct
93 Correct 839 ms 16584 KB Output is correct
94 Correct 782 ms 16564 KB Output is correct
95 Correct 627 ms 16616 KB Output is correct
96 Correct 695 ms 9472 KB Output is correct
97 Correct 768 ms 9480 KB Output is correct
98 Correct 702 ms 31124 KB Output is correct
99 Correct 923 ms 16456 KB Output is correct
100 Correct 868 ms 12744 KB Output is correct
101 Correct 803 ms 11444 KB Output is correct
102 Correct 683 ms 9640 KB Output is correct
103 Correct 889 ms 35016 KB Output is correct
104 Correct 940 ms 37144 KB Output is correct
105 Correct 873 ms 37064 KB Output is correct
106 Correct 808 ms 14872 KB Output is correct
107 Correct 756 ms 10576 KB Output is correct
108 Correct 673 ms 10440 KB Output is correct
109 Correct 814 ms 9760 KB Output is correct