Submission #629136

# Submission time Handle Problem Language Result Execution time Memory
629136 2022-08-14T08:09:29 Z two_sides Digital Circuit (IOI22_circuit) C++17
100 / 100
1173 ms 25420 KB
#include "circuit.h"
#include <bits/stdc++.h>

namespace {
    #define il i * 2
    #define ir i * 2 + 1

    using namespace std;

    const int N = 200'000;
    const int MOD = 1'000'002'022;

    vector<int> adj[N];
    int prd[N], ctb[N], sum[N * 4][2], n, m;
    bool flip[N * 4];

    int add(int x, int y) {
        return (x += y) >= MOD ? x - MOD : x;
    }

    int mul(int x, int y) {
        return (long long)x * y % MOD;
    }

    void dfs_prd(int u) {
        prd[u] = max(int(adj[u].size()), 1);
        for (int v : adj[u]) {
            dfs_prd(v);
            prd[u] = mul(prd[u], prd[v]);
        }
    }

    void dfs_ctb(int u) {
        vector<int> pre(adj[u].size() + 1);
        pre[0] = ctb[u]; int suf = 1;
        for (int i = 0; i < adj[u].size(); i++)
            pre[i + 1] = mul(pre[i], prd[adj[u][i]]);
        for (int i = adj[u].size() - 1; i >= 0; i--) {
            ctb[adj[u][i]] = mul(pre[i], suf);
            suf = mul(suf, prd[adj[u][i]]);
            dfs_ctb(adj[u][i]);
        }
    }

    void build(int i, int l, int r, vector<int> &a) {
        if (l == r) {
            if (a[l]) sum[i][0] = ctb[l + n];
            else sum[i][1] = ctb[l + n];
        } else {
        int m = (l + r) / 2;
            build(il, l, m, a);
            build(ir, m + 1, r, a);
            sum[i][0] = add(sum[il][0], sum[ir][0]);
            sum[i][1] = add(sum[il][1], sum[ir][1]);
        }
    }

    void apply(int i) {
        swap(sum[i][0], sum[i][1]); flip[i] ^= 1;
    }

    void push(int i) {
        if (flip[i]) {
            apply(il); apply(ir); flip[i] = 0;
        }
    }

    void update(int i, int l, int r, int x, int y) {
        if (l >= x && r <= y) return apply(i);
        int m = (l + r) / 2; push(i);
        if (m >= x) update(il, l, m, x, y);
        if (m < y) update(ir, m + 1, r, x, y);
        sum[i][0] = add(sum[il][0], sum[ir][0]);
        sum[i][1] = add(sum[il][1], sum[ir][1]);
    }
}

void init(int n, int m, vector<int> p, vector<int> a) {
    for (int i = 1; i < n + m; i++)
        adj[p[i]].push_back(i);
    dfs_prd(0); ctb[0] = 1; dfs_ctb(0);
    ::n = n; ::m = m; build(1, 0, m - 1, a);
}

int count_ways(int l, int r) {
    update(1, 0, m - 1, l - n, r - n);
    return sum[1][0];
}

Compilation message

circuit.cpp: In function 'void {anonymous}::dfs_ctb(int)':
circuit.cpp:36:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |         for (int i = 0; i < adj[u].size(); i++)
      |                         ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 4 ms 4944 KB Output is correct
3 Correct 3 ms 4944 KB Output is correct
4 Correct 3 ms 5072 KB Output is correct
5 Correct 3 ms 4996 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 4944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 4 ms 4944 KB Output is correct
3 Correct 3 ms 4964 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 3 ms 4944 KB Output is correct
6 Correct 4 ms 5052 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 4 ms 5032 KB Output is correct
9 Correct 3 ms 5072 KB Output is correct
10 Correct 4 ms 5200 KB Output is correct
11 Correct 3 ms 5200 KB Output is correct
12 Correct 4 ms 4996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 4 ms 4944 KB Output is correct
3 Correct 3 ms 4944 KB Output is correct
4 Correct 3 ms 5072 KB Output is correct
5 Correct 3 ms 4996 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 4944 KB Output is correct
9 Correct 3 ms 4944 KB Output is correct
10 Correct 4 ms 4944 KB Output is correct
11 Correct 3 ms 4964 KB Output is correct
12 Correct 3 ms 4956 KB Output is correct
13 Correct 3 ms 4944 KB Output is correct
14 Correct 4 ms 5052 KB Output is correct
15 Correct 3 ms 5072 KB Output is correct
16 Correct 4 ms 5032 KB Output is correct
17 Correct 3 ms 5072 KB Output is correct
18 Correct 4 ms 5200 KB Output is correct
19 Correct 3 ms 5200 KB Output is correct
20 Correct 4 ms 4996 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 5072 KB Output is correct
24 Correct 4 ms 5072 KB Output is correct
25 Correct 4 ms 5072 KB Output is correct
26 Correct 3 ms 5072 KB Output is correct
27 Correct 4 ms 5072 KB Output is correct
28 Correct 5 ms 5072 KB Output is correct
29 Correct 4 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5072 KB Output is correct
32 Correct 3 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 4944 KB Output is correct
36 Correct 3 ms 5200 KB Output is correct
37 Correct 3 ms 5200 KB Output is correct
38 Correct 3 ms 5204 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 3 ms 4944 KB Output is correct
42 Correct 3 ms 5072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 594 ms 7820 KB Output is correct
2 Correct 869 ms 10636 KB Output is correct
3 Correct 949 ms 10624 KB Output is correct
4 Correct 862 ms 10568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 594 ms 7820 KB Output is correct
2 Correct 869 ms 10636 KB Output is correct
3 Correct 949 ms 10624 KB Output is correct
4 Correct 862 ms 10568 KB Output is correct
5 Correct 705 ms 7752 KB Output is correct
6 Correct 972 ms 10544 KB Output is correct
7 Correct 941 ms 10676 KB Output is correct
8 Correct 960 ms 10652 KB Output is correct
9 Correct 337 ms 5200 KB Output is correct
10 Correct 874 ms 5372 KB Output is correct
11 Correct 938 ms 5328 KB Output is correct
12 Correct 832 ms 5328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 4 ms 4944 KB Output is correct
3 Correct 3 ms 4964 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 3 ms 4944 KB Output is correct
6 Correct 4 ms 5052 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 4 ms 5032 KB Output is correct
9 Correct 3 ms 5072 KB Output is correct
10 Correct 4 ms 5200 KB Output is correct
11 Correct 3 ms 5200 KB Output is correct
12 Correct 4 ms 4996 KB Output is correct
13 Correct 594 ms 7820 KB Output is correct
14 Correct 869 ms 10636 KB Output is correct
15 Correct 949 ms 10624 KB Output is correct
16 Correct 862 ms 10568 KB Output is correct
17 Correct 705 ms 7752 KB Output is correct
18 Correct 972 ms 10544 KB Output is correct
19 Correct 941 ms 10676 KB Output is correct
20 Correct 960 ms 10652 KB Output is correct
21 Correct 337 ms 5200 KB Output is correct
22 Correct 874 ms 5372 KB Output is correct
23 Correct 938 ms 5328 KB Output is correct
24 Correct 832 ms 5328 KB Output is correct
25 Correct 799 ms 13956 KB Output is correct
26 Correct 928 ms 14084 KB Output is correct
27 Correct 952 ms 14096 KB Output is correct
28 Correct 796 ms 14152 KB Output is correct
29 Correct 1022 ms 24956 KB Output is correct
30 Correct 780 ms 25044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 4 ms 4944 KB Output is correct
3 Correct 3 ms 4944 KB Output is correct
4 Correct 3 ms 5072 KB Output is correct
5 Correct 3 ms 4996 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 4944 KB Output is correct
9 Correct 3 ms 4944 KB Output is correct
10 Correct 4 ms 4944 KB Output is correct
11 Correct 3 ms 4964 KB Output is correct
12 Correct 3 ms 4956 KB Output is correct
13 Correct 3 ms 4944 KB Output is correct
14 Correct 4 ms 5052 KB Output is correct
15 Correct 3 ms 5072 KB Output is correct
16 Correct 4 ms 5032 KB Output is correct
17 Correct 3 ms 5072 KB Output is correct
18 Correct 4 ms 5200 KB Output is correct
19 Correct 3 ms 5200 KB Output is correct
20 Correct 4 ms 4996 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 5072 KB Output is correct
24 Correct 4 ms 5072 KB Output is correct
25 Correct 4 ms 5072 KB Output is correct
26 Correct 3 ms 5072 KB Output is correct
27 Correct 4 ms 5072 KB Output is correct
28 Correct 5 ms 5072 KB Output is correct
29 Correct 4 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5072 KB Output is correct
32 Correct 3 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 4944 KB Output is correct
36 Correct 3 ms 5200 KB Output is correct
37 Correct 3 ms 5200 KB Output is correct
38 Correct 3 ms 5204 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 3 ms 4944 KB Output is correct
42 Correct 3 ms 5072 KB Output is correct
43 Correct 658 ms 5200 KB Output is correct
44 Correct 892 ms 5316 KB Output is correct
45 Correct 740 ms 5200 KB Output is correct
46 Correct 643 ms 5456 KB Output is correct
47 Correct 720 ms 5508 KB Output is correct
48 Correct 692 ms 5456 KB Output is correct
49 Correct 755 ms 5456 KB Output is correct
50 Correct 795 ms 5456 KB Output is correct
51 Correct 907 ms 5328 KB Output is correct
52 Correct 771 ms 5328 KB Output is correct
53 Correct 554 ms 5712 KB Output is correct
54 Correct 886 ms 5456 KB Output is correct
55 Correct 863 ms 5384 KB Output is correct
56 Correct 1059 ms 5328 KB Output is correct
57 Correct 1173 ms 5200 KB Output is correct
58 Correct 1018 ms 5976 KB Output is correct
59 Correct 1049 ms 5988 KB Output is correct
60 Correct 809 ms 6028 KB Output is correct
61 Correct 886 ms 5456 KB Output is correct
62 Correct 1058 ms 5200 KB Output is correct
63 Correct 930 ms 5252 KB Output is correct
64 Correct 925 ms 5316 KB Output is correct
65 Correct 451 ms 5072 KB Output is correct
66 Correct 801 ms 5360 KB Output is correct
67 Correct 883 ms 5328 KB Output is correct
68 Correct 720 ms 5336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 4 ms 4944 KB Output is correct
3 Correct 3 ms 4944 KB Output is correct
4 Correct 3 ms 5072 KB Output is correct
5 Correct 3 ms 4996 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 4944 KB Output is correct
9 Correct 3 ms 4944 KB Output is correct
10 Correct 4 ms 4944 KB Output is correct
11 Correct 3 ms 4964 KB Output is correct
12 Correct 3 ms 4956 KB Output is correct
13 Correct 3 ms 4944 KB Output is correct
14 Correct 4 ms 5052 KB Output is correct
15 Correct 3 ms 5072 KB Output is correct
16 Correct 4 ms 5032 KB Output is correct
17 Correct 3 ms 5072 KB Output is correct
18 Correct 4 ms 5200 KB Output is correct
19 Correct 3 ms 5200 KB Output is correct
20 Correct 4 ms 4996 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 5072 KB Output is correct
24 Correct 4 ms 5072 KB Output is correct
25 Correct 4 ms 5072 KB Output is correct
26 Correct 3 ms 5072 KB Output is correct
27 Correct 4 ms 5072 KB Output is correct
28 Correct 5 ms 5072 KB Output is correct
29 Correct 4 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5072 KB Output is correct
32 Correct 3 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 4944 KB Output is correct
36 Correct 3 ms 5200 KB Output is correct
37 Correct 3 ms 5200 KB Output is correct
38 Correct 3 ms 5204 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 3 ms 4944 KB Output is correct
42 Correct 3 ms 5072 KB Output is correct
43 Correct 594 ms 7820 KB Output is correct
44 Correct 869 ms 10636 KB Output is correct
45 Correct 949 ms 10624 KB Output is correct
46 Correct 862 ms 10568 KB Output is correct
47 Correct 705 ms 7752 KB Output is correct
48 Correct 972 ms 10544 KB Output is correct
49 Correct 941 ms 10676 KB Output is correct
50 Correct 960 ms 10652 KB Output is correct
51 Correct 337 ms 5200 KB Output is correct
52 Correct 874 ms 5372 KB Output is correct
53 Correct 938 ms 5328 KB Output is correct
54 Correct 832 ms 5328 KB Output is correct
55 Correct 799 ms 13956 KB Output is correct
56 Correct 928 ms 14084 KB Output is correct
57 Correct 952 ms 14096 KB Output is correct
58 Correct 796 ms 14152 KB Output is correct
59 Correct 1022 ms 24956 KB Output is correct
60 Correct 780 ms 25044 KB Output is correct
61 Correct 658 ms 5200 KB Output is correct
62 Correct 892 ms 5316 KB Output is correct
63 Correct 740 ms 5200 KB Output is correct
64 Correct 643 ms 5456 KB Output is correct
65 Correct 720 ms 5508 KB Output is correct
66 Correct 692 ms 5456 KB Output is correct
67 Correct 755 ms 5456 KB Output is correct
68 Correct 795 ms 5456 KB Output is correct
69 Correct 907 ms 5328 KB Output is correct
70 Correct 771 ms 5328 KB Output is correct
71 Correct 554 ms 5712 KB Output is correct
72 Correct 886 ms 5456 KB Output is correct
73 Correct 863 ms 5384 KB Output is correct
74 Correct 1059 ms 5328 KB Output is correct
75 Correct 1173 ms 5200 KB Output is correct
76 Correct 1018 ms 5976 KB Output is correct
77 Correct 1049 ms 5988 KB Output is correct
78 Correct 809 ms 6028 KB Output is correct
79 Correct 886 ms 5456 KB Output is correct
80 Correct 1058 ms 5200 KB Output is correct
81 Correct 930 ms 5252 KB Output is correct
82 Correct 925 ms 5316 KB Output is correct
83 Correct 451 ms 5072 KB Output is correct
84 Correct 801 ms 5360 KB Output is correct
85 Correct 883 ms 5328 KB Output is correct
86 Correct 720 ms 5336 KB Output is correct
87 Correct 3 ms 4944 KB Output is correct
88 Correct 636 ms 13448 KB Output is correct
89 Correct 1061 ms 10856 KB Output is correct
90 Correct 941 ms 10496 KB Output is correct
91 Correct 937 ms 14112 KB Output is correct
92 Correct 988 ms 14188 KB Output is correct
93 Correct 1047 ms 14204 KB Output is correct
94 Correct 926 ms 14104 KB Output is correct
95 Correct 1122 ms 14264 KB Output is correct
96 Correct 923 ms 9912 KB Output is correct
97 Correct 1016 ms 9840 KB Output is correct
98 Correct 804 ms 20612 KB Output is correct
99 Correct 1018 ms 14080 KB Output is correct
100 Correct 1120 ms 11728 KB Output is correct
101 Correct 922 ms 11100 KB Output is correct
102 Correct 1016 ms 9860 KB Output is correct
103 Correct 1107 ms 24988 KB Output is correct
104 Correct 1033 ms 25420 KB Output is correct
105 Correct 980 ms 25372 KB Output is correct
106 Correct 945 ms 13000 KB Output is correct
107 Correct 907 ms 10572 KB Output is correct
108 Correct 998 ms 10380 KB Output is correct
109 Correct 917 ms 9928 KB Output is correct