Submission #790525

# Submission time Handle Problem Language Result Execution time Memory
790525 2023-07-22T19:29:21 Z borisAngelov Digital Circuit (IOI22_circuit) C++17
100 / 100
981 ms 52348 KB
#include "circuit.h"

#include <bits/stdc++.h>

using namespace std;

const int maxn = 200005;
const long long mod = 1e9 + 2022;

int n, m;

vector<int> g[maxn];

long long contribution[maxn];
long long subtree[maxn];

void dfs(int node, int dep)
{
    if (g[node].empty())
    {
        subtree[node] = 1;
        return;
    }

    subtree[node] = 1LL * (g[node].size());

    for (int i = 0; i < g[node].size(); ++i)
    {
        dfs(g[node][i], dep + 1);
        subtree[node] *= subtree[g[node][i]];
        subtree[node] %= mod;
    }
}

void dfs2(int node, long long from_parent)
{
    if (g[node].empty())
    {
        contribution[node] = from_parent;
        return;;
    }

    vector<long long> preffix(g[node].size() + 5, 1);
    vector<long long> suffix(g[node].size() + 5, 1);

    for (int i = 0; i < g[node].size(); ++i)
    {
        if (i == 0)
        {
            preffix[i] = 1;
        }
        else
        {
            preffix[i] = preffix[i - 1] * subtree[g[node][i - 1]];
            preffix[i] %= mod;
        }
    }

    suffix[g[node].size()] = 1LL;

    for (int i = g[node].size() - 1; i >= 0; --i)
    {
        suffix[i] = (suffix[i + 1] * subtree[g[node][i]]) % mod;
    }

    for (int i = 0; i < g[node].size(); ++i)
    {
        long long product = (preffix[i] * suffix[i + 1]) % mod;

        dfs2(g[node][i], (from_parent * product) % mod);
    }
}

long long preffix[maxn];

long long get_sum(int l, int r)
{
    if (l == 0)
    {
        return preffix[r];
    }

    return (preffix[r] - preffix[l - 1] + mod) % mod;
}

struct segment_tree
{
    struct vertex
    {
        long long sum;
        bool lazy;

        vertex()
        {
            sum = 0;
            lazy = false;
        }

        vertex(long long _sum, bool _lazy)
        {
            sum = _sum;
            lazy = _lazy;
        }
    };

    vertex tree[4 * maxn];

    void push_lazy(int node, int l, int r)
    {
        if (tree[node].lazy == true)
        {
            tree[node].sum = (get_sum(l, r) - tree[node].sum + mod) % mod;

            if (l != r)
            {
                tree[2 * node].lazy = 1 - tree[2 * node].lazy;
                tree[2 * node + 1].lazy = 1 - tree[2 * node + 1].lazy;
            }

            tree[node].lazy = false;
        }
    }

    void update(int node, int l, int r, int ql, int qr)
    {
        push_lazy(node, l, r);

        if (l > qr || r < ql)
        {
            return;
        }

        if (ql <= l && r <= qr)
        {
            tree[node].lazy = true;
            push_lazy(node, l, r);
            return;
        }

        int mid = (l + r) / 2;

        update(2 * node, l, mid, ql, qr);
        update(2 * node + 1, mid + 1, r, ql, qr);

        tree[node].sum = (tree[2 * node].sum + tree[2 * node + 1].sum) % mod;
    }

    void update(int l, int r)
    {
        update(1, 0, m - 1, l, r);
    }
};

segment_tree tree;

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

    for (int i = 1; i < P.size(); ++i)
    {
        g[P[i]].push_back(i);
    }

    dfs(0, 0);
    dfs2(0, 1);

    for (int i = 0; i < m; ++i)
    {
        preffix[i] = contribution[i + n];

        if (i > 0)
        {
            preffix[i] += preffix[i - 1];
        }

        preffix[i] %= mod;
    }

    for (int i = 0; i < m; ++i)
    {
        if (A[i] == 1)
        {
            tree.update(i, i);
        }
    }
}

int count_ways(int L, int R)
{
    int l = L - n;
    int r = R - n;

    tree.update(l, r);

    return tree.tree[1].sum;
}

Compilation message

circuit.cpp: In function 'void dfs(int, int)':
circuit.cpp:27:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |     for (int i = 0; i < g[node].size(); ++i)
      |                     ~~^~~~~~~~~~~~~~~~
circuit.cpp: In function 'void dfs2(int, long long int)':
circuit.cpp:46:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |     for (int i = 0; i < g[node].size(); ++i)
      |                     ~~^~~~~~~~~~~~~~~~
circuit.cpp:66:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |     for (int i = 0; i < g[node].size(); ++i)
      |                     ~~^~~~~~~~~~~~~~~~
circuit.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>)':
circuit.cpp:161:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  161 |     for (int i = 1; i < P.size(); ++i)
      |                     ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 17488 KB Output is correct
2 Correct 8 ms 17488 KB Output is correct
3 Correct 8 ms 17488 KB Output is correct
4 Correct 9 ms 17528 KB Output is correct
5 Correct 8 ms 17488 KB Output is correct
6 Correct 8 ms 17488 KB Output is correct
7 Correct 8 ms 17588 KB Output is correct
8 Correct 9 ms 17488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 17520 KB Output is correct
2 Correct 8 ms 17488 KB Output is correct
3 Correct 7 ms 17524 KB Output is correct
4 Correct 7 ms 17488 KB Output is correct
5 Correct 8 ms 17532 KB Output is correct
6 Correct 8 ms 17508 KB Output is correct
7 Correct 8 ms 17616 KB Output is correct
8 Correct 9 ms 17616 KB Output is correct
9 Correct 8 ms 17552 KB Output is correct
10 Correct 9 ms 17872 KB Output is correct
11 Correct 8 ms 17756 KB Output is correct
12 Correct 8 ms 17592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 17488 KB Output is correct
2 Correct 8 ms 17488 KB Output is correct
3 Correct 8 ms 17488 KB Output is correct
4 Correct 9 ms 17528 KB Output is correct
5 Correct 8 ms 17488 KB Output is correct
6 Correct 8 ms 17488 KB Output is correct
7 Correct 8 ms 17588 KB Output is correct
8 Correct 9 ms 17488 KB Output is correct
9 Correct 8 ms 17520 KB Output is correct
10 Correct 8 ms 17488 KB Output is correct
11 Correct 7 ms 17524 KB Output is correct
12 Correct 7 ms 17488 KB Output is correct
13 Correct 8 ms 17532 KB Output is correct
14 Correct 8 ms 17508 KB Output is correct
15 Correct 8 ms 17616 KB Output is correct
16 Correct 9 ms 17616 KB Output is correct
17 Correct 8 ms 17552 KB Output is correct
18 Correct 9 ms 17872 KB Output is correct
19 Correct 8 ms 17756 KB Output is correct
20 Correct 8 ms 17592 KB Output is correct
21 Correct 8 ms 17488 KB Output is correct
22 Correct 7 ms 17488 KB Output is correct
23 Correct 8 ms 17488 KB Output is correct
24 Correct 8 ms 17616 KB Output is correct
25 Correct 8 ms 17616 KB Output is correct
26 Correct 8 ms 17616 KB Output is correct
27 Correct 8 ms 17616 KB Output is correct
28 Correct 8 ms 17604 KB Output is correct
29 Correct 8 ms 17488 KB Output is correct
30 Correct 8 ms 17488 KB Output is correct
31 Correct 8 ms 17744 KB Output is correct
32 Correct 10 ms 17616 KB Output is correct
33 Correct 10 ms 17524 KB Output is correct
34 Correct 9 ms 17616 KB Output is correct
35 Correct 8 ms 17488 KB Output is correct
36 Correct 9 ms 17744 KB Output is correct
37 Correct 9 ms 17872 KB Output is correct
38 Correct 8 ms 17872 KB Output is correct
39 Correct 9 ms 17548 KB Output is correct
40 Correct 8 ms 17588 KB Output is correct
41 Correct 8 ms 17536 KB Output is correct
42 Correct 8 ms 17576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 495 ms 20336 KB Output is correct
2 Correct 679 ms 23144 KB Output is correct
3 Correct 737 ms 23072 KB Output is correct
4 Correct 778 ms 23144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 495 ms 20336 KB Output is correct
2 Correct 679 ms 23144 KB Output is correct
3 Correct 737 ms 23072 KB Output is correct
4 Correct 778 ms 23144 KB Output is correct
5 Correct 756 ms 20324 KB Output is correct
6 Correct 836 ms 23112 KB Output is correct
7 Correct 879 ms 23192 KB Output is correct
8 Correct 868 ms 23168 KB Output is correct
9 Correct 380 ms 17616 KB Output is correct
10 Correct 757 ms 17872 KB Output is correct
11 Correct 793 ms 17872 KB Output is correct
12 Correct 856 ms 17872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 17520 KB Output is correct
2 Correct 8 ms 17488 KB Output is correct
3 Correct 7 ms 17524 KB Output is correct
4 Correct 7 ms 17488 KB Output is correct
5 Correct 8 ms 17532 KB Output is correct
6 Correct 8 ms 17508 KB Output is correct
7 Correct 8 ms 17616 KB Output is correct
8 Correct 9 ms 17616 KB Output is correct
9 Correct 8 ms 17552 KB Output is correct
10 Correct 9 ms 17872 KB Output is correct
11 Correct 8 ms 17756 KB Output is correct
12 Correct 8 ms 17592 KB Output is correct
13 Correct 495 ms 20336 KB Output is correct
14 Correct 679 ms 23144 KB Output is correct
15 Correct 737 ms 23072 KB Output is correct
16 Correct 778 ms 23144 KB Output is correct
17 Correct 756 ms 20324 KB Output is correct
18 Correct 836 ms 23112 KB Output is correct
19 Correct 879 ms 23192 KB Output is correct
20 Correct 868 ms 23168 KB Output is correct
21 Correct 380 ms 17616 KB Output is correct
22 Correct 757 ms 17872 KB Output is correct
23 Correct 793 ms 17872 KB Output is correct
24 Correct 856 ms 17872 KB Output is correct
25 Correct 930 ms 25984 KB Output is correct
26 Correct 927 ms 26092 KB Output is correct
27 Correct 808 ms 26124 KB Output is correct
28 Correct 599 ms 26116 KB Output is correct
29 Correct 843 ms 51120 KB Output is correct
30 Correct 870 ms 51176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 17488 KB Output is correct
2 Correct 8 ms 17488 KB Output is correct
3 Correct 8 ms 17488 KB Output is correct
4 Correct 9 ms 17528 KB Output is correct
5 Correct 8 ms 17488 KB Output is correct
6 Correct 8 ms 17488 KB Output is correct
7 Correct 8 ms 17588 KB Output is correct
8 Correct 9 ms 17488 KB Output is correct
9 Correct 8 ms 17520 KB Output is correct
10 Correct 8 ms 17488 KB Output is correct
11 Correct 7 ms 17524 KB Output is correct
12 Correct 7 ms 17488 KB Output is correct
13 Correct 8 ms 17532 KB Output is correct
14 Correct 8 ms 17508 KB Output is correct
15 Correct 8 ms 17616 KB Output is correct
16 Correct 9 ms 17616 KB Output is correct
17 Correct 8 ms 17552 KB Output is correct
18 Correct 9 ms 17872 KB Output is correct
19 Correct 8 ms 17756 KB Output is correct
20 Correct 8 ms 17592 KB Output is correct
21 Correct 8 ms 17488 KB Output is correct
22 Correct 7 ms 17488 KB Output is correct
23 Correct 8 ms 17488 KB Output is correct
24 Correct 8 ms 17616 KB Output is correct
25 Correct 8 ms 17616 KB Output is correct
26 Correct 8 ms 17616 KB Output is correct
27 Correct 8 ms 17616 KB Output is correct
28 Correct 8 ms 17604 KB Output is correct
29 Correct 8 ms 17488 KB Output is correct
30 Correct 8 ms 17488 KB Output is correct
31 Correct 8 ms 17744 KB Output is correct
32 Correct 10 ms 17616 KB Output is correct
33 Correct 10 ms 17524 KB Output is correct
34 Correct 9 ms 17616 KB Output is correct
35 Correct 8 ms 17488 KB Output is correct
36 Correct 9 ms 17744 KB Output is correct
37 Correct 9 ms 17872 KB Output is correct
38 Correct 8 ms 17872 KB Output is correct
39 Correct 9 ms 17548 KB Output is correct
40 Correct 8 ms 17588 KB Output is correct
41 Correct 8 ms 17536 KB Output is correct
42 Correct 8 ms 17576 KB Output is correct
43 Correct 504 ms 17772 KB Output is correct
44 Correct 749 ms 17708 KB Output is correct
45 Correct 716 ms 17744 KB Output is correct
46 Correct 672 ms 17872 KB Output is correct
47 Correct 866 ms 17872 KB Output is correct
48 Correct 830 ms 17872 KB Output is correct
49 Correct 622 ms 17872 KB Output is correct
50 Correct 654 ms 17872 KB Output is correct
51 Correct 733 ms 17844 KB Output is correct
52 Correct 793 ms 17916 KB Output is correct
53 Correct 637 ms 18896 KB Output is correct
54 Correct 868 ms 17872 KB Output is correct
55 Correct 771 ms 17812 KB Output is correct
56 Correct 703 ms 17736 KB Output is correct
57 Correct 728 ms 17744 KB Output is correct
58 Correct 687 ms 19152 KB Output is correct
59 Correct 868 ms 19304 KB Output is correct
60 Correct 733 ms 19248 KB Output is correct
61 Correct 846 ms 18000 KB Output is correct
62 Correct 821 ms 17744 KB Output is correct
63 Correct 795 ms 17744 KB Output is correct
64 Correct 847 ms 17744 KB Output is correct
65 Correct 373 ms 17616 KB Output is correct
66 Correct 688 ms 17864 KB Output is correct
67 Correct 615 ms 17872 KB Output is correct
68 Correct 562 ms 17872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 17488 KB Output is correct
2 Correct 8 ms 17488 KB Output is correct
3 Correct 8 ms 17488 KB Output is correct
4 Correct 9 ms 17528 KB Output is correct
5 Correct 8 ms 17488 KB Output is correct
6 Correct 8 ms 17488 KB Output is correct
7 Correct 8 ms 17588 KB Output is correct
8 Correct 9 ms 17488 KB Output is correct
9 Correct 8 ms 17520 KB Output is correct
10 Correct 8 ms 17488 KB Output is correct
11 Correct 7 ms 17524 KB Output is correct
12 Correct 7 ms 17488 KB Output is correct
13 Correct 8 ms 17532 KB Output is correct
14 Correct 8 ms 17508 KB Output is correct
15 Correct 8 ms 17616 KB Output is correct
16 Correct 9 ms 17616 KB Output is correct
17 Correct 8 ms 17552 KB Output is correct
18 Correct 9 ms 17872 KB Output is correct
19 Correct 8 ms 17756 KB Output is correct
20 Correct 8 ms 17592 KB Output is correct
21 Correct 8 ms 17488 KB Output is correct
22 Correct 7 ms 17488 KB Output is correct
23 Correct 8 ms 17488 KB Output is correct
24 Correct 8 ms 17616 KB Output is correct
25 Correct 8 ms 17616 KB Output is correct
26 Correct 8 ms 17616 KB Output is correct
27 Correct 8 ms 17616 KB Output is correct
28 Correct 8 ms 17604 KB Output is correct
29 Correct 8 ms 17488 KB Output is correct
30 Correct 8 ms 17488 KB Output is correct
31 Correct 8 ms 17744 KB Output is correct
32 Correct 10 ms 17616 KB Output is correct
33 Correct 10 ms 17524 KB Output is correct
34 Correct 9 ms 17616 KB Output is correct
35 Correct 8 ms 17488 KB Output is correct
36 Correct 9 ms 17744 KB Output is correct
37 Correct 9 ms 17872 KB Output is correct
38 Correct 8 ms 17872 KB Output is correct
39 Correct 9 ms 17548 KB Output is correct
40 Correct 8 ms 17588 KB Output is correct
41 Correct 8 ms 17536 KB Output is correct
42 Correct 8 ms 17576 KB Output is correct
43 Correct 495 ms 20336 KB Output is correct
44 Correct 679 ms 23144 KB Output is correct
45 Correct 737 ms 23072 KB Output is correct
46 Correct 778 ms 23144 KB Output is correct
47 Correct 756 ms 20324 KB Output is correct
48 Correct 836 ms 23112 KB Output is correct
49 Correct 879 ms 23192 KB Output is correct
50 Correct 868 ms 23168 KB Output is correct
51 Correct 380 ms 17616 KB Output is correct
52 Correct 757 ms 17872 KB Output is correct
53 Correct 793 ms 17872 KB Output is correct
54 Correct 856 ms 17872 KB Output is correct
55 Correct 930 ms 25984 KB Output is correct
56 Correct 927 ms 26092 KB Output is correct
57 Correct 808 ms 26124 KB Output is correct
58 Correct 599 ms 26116 KB Output is correct
59 Correct 843 ms 51120 KB Output is correct
60 Correct 870 ms 51176 KB Output is correct
61 Correct 504 ms 17772 KB Output is correct
62 Correct 749 ms 17708 KB Output is correct
63 Correct 716 ms 17744 KB Output is correct
64 Correct 672 ms 17872 KB Output is correct
65 Correct 866 ms 17872 KB Output is correct
66 Correct 830 ms 17872 KB Output is correct
67 Correct 622 ms 17872 KB Output is correct
68 Correct 654 ms 17872 KB Output is correct
69 Correct 733 ms 17844 KB Output is correct
70 Correct 793 ms 17916 KB Output is correct
71 Correct 637 ms 18896 KB Output is correct
72 Correct 868 ms 17872 KB Output is correct
73 Correct 771 ms 17812 KB Output is correct
74 Correct 703 ms 17736 KB Output is correct
75 Correct 728 ms 17744 KB Output is correct
76 Correct 687 ms 19152 KB Output is correct
77 Correct 868 ms 19304 KB Output is correct
78 Correct 733 ms 19248 KB Output is correct
79 Correct 846 ms 18000 KB Output is correct
80 Correct 821 ms 17744 KB Output is correct
81 Correct 795 ms 17744 KB Output is correct
82 Correct 847 ms 17744 KB Output is correct
83 Correct 373 ms 17616 KB Output is correct
84 Correct 688 ms 17864 KB Output is correct
85 Correct 615 ms 17872 KB Output is correct
86 Correct 562 ms 17872 KB Output is correct
87 Correct 8 ms 17488 KB Output is correct
88 Correct 565 ms 25196 KB Output is correct
89 Correct 791 ms 23108 KB Output is correct
90 Correct 882 ms 22888 KB Output is correct
91 Correct 911 ms 26184 KB Output is correct
92 Correct 900 ms 26184 KB Output is correct
93 Correct 845 ms 26128 KB Output is correct
94 Correct 880 ms 26208 KB Output is correct
95 Correct 800 ms 26244 KB Output is correct
96 Correct 685 ms 22720 KB Output is correct
97 Correct 773 ms 22724 KB Output is correct
98 Correct 752 ms 47232 KB Output is correct
99 Correct 777 ms 26080 KB Output is correct
100 Correct 757 ms 23784 KB Output is correct
101 Correct 868 ms 23112 KB Output is correct
102 Correct 788 ms 21912 KB Output is correct
103 Correct 933 ms 51060 KB Output is correct
104 Correct 981 ms 52348 KB Output is correct
105 Correct 839 ms 52292 KB Output is correct
106 Correct 802 ms 28328 KB Output is correct
107 Correct 858 ms 22044 KB Output is correct
108 Correct 765 ms 22044 KB Output is correct
109 Correct 837 ms 22216 KB Output is correct