Submission #820089

# Submission time Handle Problem Language Result Execution time Memory
820089 2023-08-10T19:06:58 Z finn__ Digital Circuit (IOI22_circuit) C++17
100 / 100
941 ms 39276 KB
#include <bits/stdc++.h>

#include "circuit.h"

using namespace std;

constexpr int64_t MOD = 1'000'002'022;

template <size_t L>
struct segment_tree
{
    int64_t t[L << 1], sum[L << 1];
    bitset<L << 1> lazy;

    void propagate(size_t k)
    {
        if (lazy[k])
        {
            t[k << 1] = (sum[k << 1] - t[k << 1] + MOD) % MOD;
            t[k << 1 | 1] = (sum[k << 1 | 1] - t[k << 1 | 1] + MOD) % MOD;
            lazy[k << 1] = !lazy[k << 1];
            lazy[k << 1 | 1] = !lazy[k << 1 | 1];
            lazy[k] = 0;
        }
    }

    void flip(size_t i, size_t j, size_t k = 1, size_t a = 0, size_t b = L - 1)
    {
        if (b < i || a > j)
            return;
        if (i <= a && b <= j)
        {
            lazy[k] = !lazy[k];
            t[k] = (sum[k] - t[k] + MOD) % MOD;
        }
        else
        {
            propagate(k);
            flip(i, j, k << 1, a, (a + b) >> 1);
            flip(i, j, k << 1 | 1, ((a + b) >> 1) + 1, b);
            t[k] = (t[k << 1] + t[k << 1 | 1]) % MOD;
        }
    }

    void build_sum()
    {
        for (size_t i = L - 1; i; --i)
            sum[i] = (sum[i << 1] + sum[i << 1 | 1]) % MOD;
    }
};

constexpr size_t N = 200000, L = 1 << 18;

size_t n, m;
segment_tree<L> tree;
bitset<N> state;
vector<size_t> g[N];
int64_t num_children_prod[N];

void find_num_children_prod(size_t u)
{
    if (g[u].empty())
    {
        num_children_prod[u] = 1;
        return;
    }

    num_children_prod[u] = g[u].size();
    for (auto const &v : g[u])
    {
        find_num_children_prod(v);
        num_children_prod[u] = (num_children_prod[u] * num_children_prod[v]) % MOD;
    }
}

void dfs(size_t u, int64_t prod = 1)
{
    if (g[u].empty())
    {
        tree.sum[L + u] = prod;
        return;
    }

    vector<int64_t> pre(g[u].size()), suf(g[u].size());

    pre[0] = 1;
    for (size_t i = 1; i < pre.size(); ++i)
        pre[i] = (pre[i - 1] * num_children_prod[g[u][i - 1]]) % MOD;

    suf[suf.size() - 1] = 1;
    for (size_t i = suf.size() - 2; i < suf.size(); --i)
        suf[i] = (suf[i + 1] * num_children_prod[g[u][i + 1]]) % MOD;

    for (size_t i = 0; i < g[u].size(); ++i)
    {
        auto const &v = g[u][i];
        dfs(v, (((prod * pre[i]) % MOD) * suf[i]) % MOD);
    }
}

void init(int n_, int m_, vector<int> p, vector<int> a)
{
    n = n_, m = m_;
    for (size_t i = 0; i < p.size(); ++i)
        if (p[i] != -1)
            g[p[i]].push_back(i);

    find_num_children_prod(0);
    dfs(0);
    tree.build_sum();
    for (size_t i = 0; i < a.size(); ++i)
        if (state[n + i] = a[i])
            tree.flip(n + i, n + i);
}

int count_ways(int l, int r)
{
    tree.flip(l, r);
    return tree.t[1];
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7120 KB Output is correct
2 Correct 3 ms 7120 KB Output is correct
3 Correct 4 ms 7248 KB Output is correct
4 Correct 4 ms 7248 KB Output is correct
5 Correct 4 ms 7248 KB Output is correct
6 Correct 4 ms 7248 KB Output is correct
7 Correct 4 ms 7248 KB Output is correct
8 Correct 4 ms 7248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7120 KB Output is correct
2 Correct 3 ms 7120 KB Output is correct
3 Correct 4 ms 7120 KB Output is correct
4 Correct 4 ms 7108 KB Output is correct
5 Correct 4 ms 7120 KB Output is correct
6 Correct 4 ms 7144 KB Output is correct
7 Correct 4 ms 7248 KB Output is correct
8 Correct 5 ms 7248 KB Output is correct
9 Correct 4 ms 7160 KB Output is correct
10 Correct 4 ms 7376 KB Output is correct
11 Correct 4 ms 7376 KB Output is correct
12 Correct 4 ms 7248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7120 KB Output is correct
2 Correct 3 ms 7120 KB Output is correct
3 Correct 4 ms 7248 KB Output is correct
4 Correct 4 ms 7248 KB Output is correct
5 Correct 4 ms 7248 KB Output is correct
6 Correct 4 ms 7248 KB Output is correct
7 Correct 4 ms 7248 KB Output is correct
8 Correct 4 ms 7248 KB Output is correct
9 Correct 3 ms 7120 KB Output is correct
10 Correct 3 ms 7120 KB Output is correct
11 Correct 4 ms 7120 KB Output is correct
12 Correct 4 ms 7108 KB Output is correct
13 Correct 4 ms 7120 KB Output is correct
14 Correct 4 ms 7144 KB Output is correct
15 Correct 4 ms 7248 KB Output is correct
16 Correct 5 ms 7248 KB Output is correct
17 Correct 4 ms 7160 KB Output is correct
18 Correct 4 ms 7376 KB Output is correct
19 Correct 4 ms 7376 KB Output is correct
20 Correct 4 ms 7248 KB Output is correct
21 Correct 4 ms 7248 KB Output is correct
22 Correct 4 ms 7248 KB Output is correct
23 Correct 4 ms 7248 KB Output is correct
24 Correct 4 ms 7248 KB Output is correct
25 Correct 5 ms 7248 KB Output is correct
26 Correct 5 ms 7248 KB Output is correct
27 Correct 4 ms 7248 KB Output is correct
28 Correct 4 ms 7248 KB Output is correct
29 Correct 4 ms 7248 KB Output is correct
30 Correct 4 ms 7248 KB Output is correct
31 Correct 4 ms 7376 KB Output is correct
32 Correct 4 ms 7248 KB Output is correct
33 Correct 4 ms 7224 KB Output is correct
34 Correct 4 ms 7248 KB Output is correct
35 Correct 4 ms 7248 KB Output is correct
36 Correct 4 ms 7376 KB Output is correct
37 Correct 4 ms 7504 KB Output is correct
38 Correct 4 ms 7440 KB Output is correct
39 Correct 4 ms 7248 KB Output is correct
40 Correct 5 ms 7248 KB Output is correct
41 Correct 4 ms 7248 KB Output is correct
42 Correct 4 ms 7248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 491 ms 10196 KB Output is correct
2 Correct 745 ms 13260 KB Output is correct
3 Correct 770 ms 13260 KB Output is correct
4 Correct 767 ms 12588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 491 ms 10196 KB Output is correct
2 Correct 745 ms 13260 KB Output is correct
3 Correct 770 ms 13260 KB Output is correct
4 Correct 767 ms 12588 KB Output is correct
5 Correct 515 ms 10188 KB Output is correct
6 Correct 826 ms 13280 KB Output is correct
7 Correct 821 ms 13264 KB Output is correct
8 Correct 663 ms 13240 KB Output is correct
9 Correct 444 ms 7376 KB Output is correct
10 Correct 574 ms 7568 KB Output is correct
11 Correct 784 ms 7504 KB Output is correct
12 Correct 505 ms 7504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7120 KB Output is correct
2 Correct 3 ms 7120 KB Output is correct
3 Correct 4 ms 7120 KB Output is correct
4 Correct 4 ms 7108 KB Output is correct
5 Correct 4 ms 7120 KB Output is correct
6 Correct 4 ms 7144 KB Output is correct
7 Correct 4 ms 7248 KB Output is correct
8 Correct 5 ms 7248 KB Output is correct
9 Correct 4 ms 7160 KB Output is correct
10 Correct 4 ms 7376 KB Output is correct
11 Correct 4 ms 7376 KB Output is correct
12 Correct 4 ms 7248 KB Output is correct
13 Correct 491 ms 10196 KB Output is correct
14 Correct 745 ms 13260 KB Output is correct
15 Correct 770 ms 13260 KB Output is correct
16 Correct 767 ms 12588 KB Output is correct
17 Correct 515 ms 10188 KB Output is correct
18 Correct 826 ms 13280 KB Output is correct
19 Correct 821 ms 13264 KB Output is correct
20 Correct 663 ms 13240 KB Output is correct
21 Correct 444 ms 7376 KB Output is correct
22 Correct 574 ms 7568 KB Output is correct
23 Correct 784 ms 7504 KB Output is correct
24 Correct 505 ms 7504 KB Output is correct
25 Correct 743 ms 16316 KB Output is correct
26 Correct 941 ms 16484 KB Output is correct
27 Correct 925 ms 16456 KB Output is correct
28 Correct 668 ms 16492 KB Output is correct
29 Correct 757 ms 38372 KB Output is correct
30 Correct 753 ms 38468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7120 KB Output is correct
2 Correct 3 ms 7120 KB Output is correct
3 Correct 4 ms 7248 KB Output is correct
4 Correct 4 ms 7248 KB Output is correct
5 Correct 4 ms 7248 KB Output is correct
6 Correct 4 ms 7248 KB Output is correct
7 Correct 4 ms 7248 KB Output is correct
8 Correct 4 ms 7248 KB Output is correct
9 Correct 3 ms 7120 KB Output is correct
10 Correct 3 ms 7120 KB Output is correct
11 Correct 4 ms 7120 KB Output is correct
12 Correct 4 ms 7108 KB Output is correct
13 Correct 4 ms 7120 KB Output is correct
14 Correct 4 ms 7144 KB Output is correct
15 Correct 4 ms 7248 KB Output is correct
16 Correct 5 ms 7248 KB Output is correct
17 Correct 4 ms 7160 KB Output is correct
18 Correct 4 ms 7376 KB Output is correct
19 Correct 4 ms 7376 KB Output is correct
20 Correct 4 ms 7248 KB Output is correct
21 Correct 4 ms 7248 KB Output is correct
22 Correct 4 ms 7248 KB Output is correct
23 Correct 4 ms 7248 KB Output is correct
24 Correct 4 ms 7248 KB Output is correct
25 Correct 5 ms 7248 KB Output is correct
26 Correct 5 ms 7248 KB Output is correct
27 Correct 4 ms 7248 KB Output is correct
28 Correct 4 ms 7248 KB Output is correct
29 Correct 4 ms 7248 KB Output is correct
30 Correct 4 ms 7248 KB Output is correct
31 Correct 4 ms 7376 KB Output is correct
32 Correct 4 ms 7248 KB Output is correct
33 Correct 4 ms 7224 KB Output is correct
34 Correct 4 ms 7248 KB Output is correct
35 Correct 4 ms 7248 KB Output is correct
36 Correct 4 ms 7376 KB Output is correct
37 Correct 4 ms 7504 KB Output is correct
38 Correct 4 ms 7440 KB Output is correct
39 Correct 4 ms 7248 KB Output is correct
40 Correct 5 ms 7248 KB Output is correct
41 Correct 4 ms 7248 KB Output is correct
42 Correct 4 ms 7248 KB Output is correct
43 Correct 503 ms 7376 KB Output is correct
44 Correct 780 ms 7504 KB Output is correct
45 Correct 819 ms 7504 KB Output is correct
46 Correct 709 ms 7632 KB Output is correct
47 Correct 687 ms 7632 KB Output is correct
48 Correct 664 ms 7632 KB Output is correct
49 Correct 681 ms 7632 KB Output is correct
50 Correct 593 ms 7632 KB Output is correct
51 Correct 673 ms 7568 KB Output is correct
52 Correct 799 ms 7568 KB Output is correct
53 Correct 677 ms 8404 KB Output is correct
54 Correct 807 ms 7632 KB Output is correct
55 Correct 545 ms 7504 KB Output is correct
56 Correct 611 ms 7512 KB Output is correct
57 Correct 647 ms 7504 KB Output is correct
58 Correct 771 ms 8664 KB Output is correct
59 Correct 648 ms 8784 KB Output is correct
60 Correct 541 ms 8784 KB Output is correct
61 Correct 599 ms 7632 KB Output is correct
62 Correct 734 ms 7376 KB Output is correct
63 Correct 726 ms 7376 KB Output is correct
64 Correct 630 ms 7504 KB Output is correct
65 Correct 296 ms 7376 KB Output is correct
66 Correct 658 ms 7552 KB Output is correct
67 Correct 739 ms 7504 KB Output is correct
68 Correct 702 ms 7504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7120 KB Output is correct
2 Correct 3 ms 7120 KB Output is correct
3 Correct 4 ms 7248 KB Output is correct
4 Correct 4 ms 7248 KB Output is correct
5 Correct 4 ms 7248 KB Output is correct
6 Correct 4 ms 7248 KB Output is correct
7 Correct 4 ms 7248 KB Output is correct
8 Correct 4 ms 7248 KB Output is correct
9 Correct 3 ms 7120 KB Output is correct
10 Correct 3 ms 7120 KB Output is correct
11 Correct 4 ms 7120 KB Output is correct
12 Correct 4 ms 7108 KB Output is correct
13 Correct 4 ms 7120 KB Output is correct
14 Correct 4 ms 7144 KB Output is correct
15 Correct 4 ms 7248 KB Output is correct
16 Correct 5 ms 7248 KB Output is correct
17 Correct 4 ms 7160 KB Output is correct
18 Correct 4 ms 7376 KB Output is correct
19 Correct 4 ms 7376 KB Output is correct
20 Correct 4 ms 7248 KB Output is correct
21 Correct 4 ms 7248 KB Output is correct
22 Correct 4 ms 7248 KB Output is correct
23 Correct 4 ms 7248 KB Output is correct
24 Correct 4 ms 7248 KB Output is correct
25 Correct 5 ms 7248 KB Output is correct
26 Correct 5 ms 7248 KB Output is correct
27 Correct 4 ms 7248 KB Output is correct
28 Correct 4 ms 7248 KB Output is correct
29 Correct 4 ms 7248 KB Output is correct
30 Correct 4 ms 7248 KB Output is correct
31 Correct 4 ms 7376 KB Output is correct
32 Correct 4 ms 7248 KB Output is correct
33 Correct 4 ms 7224 KB Output is correct
34 Correct 4 ms 7248 KB Output is correct
35 Correct 4 ms 7248 KB Output is correct
36 Correct 4 ms 7376 KB Output is correct
37 Correct 4 ms 7504 KB Output is correct
38 Correct 4 ms 7440 KB Output is correct
39 Correct 4 ms 7248 KB Output is correct
40 Correct 5 ms 7248 KB Output is correct
41 Correct 4 ms 7248 KB Output is correct
42 Correct 4 ms 7248 KB Output is correct
43 Correct 491 ms 10196 KB Output is correct
44 Correct 745 ms 13260 KB Output is correct
45 Correct 770 ms 13260 KB Output is correct
46 Correct 767 ms 12588 KB Output is correct
47 Correct 515 ms 10188 KB Output is correct
48 Correct 826 ms 13280 KB Output is correct
49 Correct 821 ms 13264 KB Output is correct
50 Correct 663 ms 13240 KB Output is correct
51 Correct 444 ms 7376 KB Output is correct
52 Correct 574 ms 7568 KB Output is correct
53 Correct 784 ms 7504 KB Output is correct
54 Correct 505 ms 7504 KB Output is correct
55 Correct 743 ms 16316 KB Output is correct
56 Correct 941 ms 16484 KB Output is correct
57 Correct 925 ms 16456 KB Output is correct
58 Correct 668 ms 16492 KB Output is correct
59 Correct 757 ms 38372 KB Output is correct
60 Correct 753 ms 38468 KB Output is correct
61 Correct 503 ms 7376 KB Output is correct
62 Correct 780 ms 7504 KB Output is correct
63 Correct 819 ms 7504 KB Output is correct
64 Correct 709 ms 7632 KB Output is correct
65 Correct 687 ms 7632 KB Output is correct
66 Correct 664 ms 7632 KB Output is correct
67 Correct 681 ms 7632 KB Output is correct
68 Correct 593 ms 7632 KB Output is correct
69 Correct 673 ms 7568 KB Output is correct
70 Correct 799 ms 7568 KB Output is correct
71 Correct 677 ms 8404 KB Output is correct
72 Correct 807 ms 7632 KB Output is correct
73 Correct 545 ms 7504 KB Output is correct
74 Correct 611 ms 7512 KB Output is correct
75 Correct 647 ms 7504 KB Output is correct
76 Correct 771 ms 8664 KB Output is correct
77 Correct 648 ms 8784 KB Output is correct
78 Correct 541 ms 8784 KB Output is correct
79 Correct 599 ms 7632 KB Output is correct
80 Correct 734 ms 7376 KB Output is correct
81 Correct 726 ms 7376 KB Output is correct
82 Correct 630 ms 7504 KB Output is correct
83 Correct 296 ms 7376 KB Output is correct
84 Correct 658 ms 7552 KB Output is correct
85 Correct 739 ms 7504 KB Output is correct
86 Correct 702 ms 7504 KB Output is correct
87 Correct 4 ms 7120 KB Output is correct
88 Correct 460 ms 15852 KB Output is correct
89 Correct 710 ms 13072 KB Output is correct
90 Correct 788 ms 13260 KB Output is correct
91 Correct 680 ms 17100 KB Output is correct
92 Correct 830 ms 17184 KB Output is correct
93 Correct 870 ms 16036 KB Output is correct
94 Correct 851 ms 17084 KB Output is correct
95 Correct 780 ms 17164 KB Output is correct
96 Correct 774 ms 12700 KB Output is correct
97 Correct 733 ms 12700 KB Output is correct
98 Correct 631 ms 33736 KB Output is correct
99 Correct 738 ms 16460 KB Output is correct
100 Correct 701 ms 15168 KB Output is correct
101 Correct 721 ms 14276 KB Output is correct
102 Correct 691 ms 12872 KB Output is correct
103 Correct 806 ms 38468 KB Output is correct
104 Correct 756 ms 39276 KB Output is correct
105 Correct 762 ms 39204 KB Output is correct
106 Correct 686 ms 17612 KB Output is correct
107 Correct 797 ms 12552 KB Output is correct
108 Correct 628 ms 12980 KB Output is correct
109 Correct 770 ms 13028 KB Output is correct