Submission #627720

# Submission time Handle Problem Language Result Execution time Memory
627720 2022-08-12T20:11:20 Z dqhungdl Digital Circuit (IOI22_circuit) C++17
100 / 100
1156 ms 52320 KB
#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;

const int MAX = 2e5 + 5, MOD = 1e9 + 2022;
int N;
long long C[MAX], f[MAX];
vector<int> g[MAX];

struct SegmentTree {
    int n;
    vector<vector<long long>> tree;
    vector<int> lazy;

    SegmentTree() {}

    SegmentTree(vector<vector<long long>> &V) {
        n = V.size();
        tree.resize(4 * n + 5, {0, 0});
        lazy.resize(4 * n + 5);
        init(1, 0, n - 1, V);
    }

    void init(int k, int l, int r, vector<vector<long long>> &V) {
        if (l == r) {
            tree[k] = V[l];
            return;
        }
        int mid = (l + r) / 2;
        init(2 * k, l, mid, V);
        init(2 * k + 1, mid + 1, r, V);
        for (int i = 0; i <= 1; i++)
            tree[k][i] = (tree[2 * k][i] + tree[2 * k + 1][i]) % MOD;
    }

    void down(int k) {
        if (lazy[k]) {
            lazy[2 * k] ^= lazy[k], lazy[2 * k + 1] ^= lazy[k];
            swap(tree[2 * k][0], tree[2 * k][1]);
            swap(tree[2 * k + 1][0], tree[2 * k + 1][1]);
            lazy[k] = 0;
        }
    }

    void update(int k, int l, int r, int L, int R) {
        if (l > R || L > r)
            return;
        if (L <= l && r <= R) {
            lazy[k] ^= 1;
            swap(tree[k][0], tree[k][1]);
            return;
        }
        down(k);
        int mid = (l + r) / 2;
        update(2 * k, l, mid, L, R);
        update(2 * k + 1, mid + 1, r, L, R);
        for (int i = 0; i <= 1; i++)
            tree[k][i] = (tree[2 * k][i] + tree[2 * k + 1][i]) % MOD;
    }

    void update(int L, int R) {
        update(1, 0, n - 1, L, R);
    }
} tree;

void dfs(int u) {
    if (g[u].empty()) {
        C[u] = 1;
        return;
    }
    C[u] = g[u].size();
    for (int v: g[u]) {
        dfs(v);
        C[u] = C[u] * C[v] % MOD;
    }
    vector<long long> L(g[u].size(), 1), R(g[u].size(), 1);
    for (int i = 1; i < g[u].size(); i++)
        L[i] = L[i - 1] * C[g[u][i - 1]] % MOD;
    for (int i = (int) g[u].size() - 2; i >= 0; i--)
        R[i] = R[i + 1] * C[g[u][i + 1]] % MOD;
    for (int i = 0; i < g[u].size(); i++)
        f[g[u][i]] = L[i] * R[i] % MOD;
}

void init(int _N, int M, vector<int> P, vector<int> A) {
    N = _N;
    for (int i = 1; i < N + M; i++)
        g[P[i]].push_back(i);
    dfs(0);
    f[0] = 1;
    for (int i = 1; i < N + M; i++)
        f[i] = f[i] * f[P[i]] % MOD;
    vector<vector<long long>> V(M);
    for (int i = 0; i < M; i++)
        if (A[i])
            V[i] = {0, f[i + N]};
        else
            V[i] = {f[i + N], 0};
    tree = SegmentTree(V);
}

int count_ways(int L, int R) {
    tree.update(L - N, R - N);
    return tree.tree[1][1];
}

Compilation message

circuit.cpp: In function 'void dfs(int)':
circuit.cpp:77:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |     for (int i = 1; i < g[u].size(); i++)
      |                     ~~^~~~~~~~~~~~~
circuit.cpp:81:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |     for (int i = 0; i < g[u].size(); i++)
      |                     ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 4 ms 5328 KB Output is correct
4 Correct 3 ms 5328 KB Output is correct
5 Correct 3 ms 5328 KB Output is correct
6 Correct 3 ms 5328 KB Output is correct
7 Correct 3 ms 5328 KB Output is correct
8 Correct 3 ms 5328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4968 KB Output is correct
2 Correct 3 ms 5072 KB Output is correct
3 Correct 3 ms 5072 KB Output is correct
4 Correct 3 ms 5072 KB Output is correct
5 Correct 3 ms 5072 KB Output is correct
6 Correct 5 ms 5220 KB Output is correct
7 Correct 4 ms 5328 KB Output is correct
8 Correct 3 ms 5328 KB Output is correct
9 Correct 3 ms 5328 KB Output is correct
10 Correct 4 ms 5456 KB Output is correct
11 Correct 3 ms 5456 KB Output is correct
12 Correct 3 ms 5328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 4 ms 5328 KB Output is correct
4 Correct 3 ms 5328 KB Output is correct
5 Correct 3 ms 5328 KB Output is correct
6 Correct 3 ms 5328 KB Output is correct
7 Correct 3 ms 5328 KB Output is correct
8 Correct 3 ms 5328 KB Output is correct
9 Correct 3 ms 4968 KB Output is correct
10 Correct 3 ms 5072 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 3 ms 5072 KB Output is correct
14 Correct 5 ms 5220 KB Output is correct
15 Correct 4 ms 5328 KB Output is correct
16 Correct 3 ms 5328 KB Output is correct
17 Correct 3 ms 5328 KB Output is correct
18 Correct 4 ms 5456 KB Output is correct
19 Correct 3 ms 5456 KB Output is correct
20 Correct 3 ms 5328 KB Output is correct
21 Correct 4 ms 5328 KB Output is correct
22 Correct 4 ms 5200 KB Output is correct
23 Correct 3 ms 5200 KB Output is correct
24 Correct 3 ms 5328 KB Output is correct
25 Correct 4 ms 5384 KB Output is correct
26 Correct 3 ms 5328 KB Output is correct
27 Correct 3 ms 5328 KB Output is correct
28 Correct 4 ms 5328 KB Output is correct
29 Correct 3 ms 5328 KB Output is correct
30 Correct 3 ms 5328 KB Output is correct
31 Correct 3 ms 5072 KB Output is correct
32 Correct 4 ms 5328 KB Output is correct
33 Correct 3 ms 5328 KB Output is correct
34 Correct 3 ms 5328 KB Output is correct
35 Correct 3 ms 5200 KB Output is correct
36 Correct 3 ms 5456 KB Output is correct
37 Correct 3 ms 5456 KB Output is correct
38 Correct 3 ms 5456 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 3 ms 5200 KB Output is correct
41 Correct 3 ms 5272 KB Output is correct
42 Correct 3 ms 5200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 746 ms 17280 KB Output is correct
2 Correct 851 ms 29644 KB Output is correct
3 Correct 971 ms 29552 KB Output is correct
4 Correct 906 ms 29640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 746 ms 17280 KB Output is correct
2 Correct 851 ms 29644 KB Output is correct
3 Correct 971 ms 29552 KB Output is correct
4 Correct 906 ms 29640 KB Output is correct
5 Correct 847 ms 17232 KB Output is correct
6 Correct 964 ms 29580 KB Output is correct
7 Correct 1002 ms 29616 KB Output is correct
8 Correct 954 ms 29640 KB Output is correct
9 Correct 441 ms 5712 KB Output is correct
10 Correct 758 ms 6600 KB Output is correct
11 Correct 713 ms 6540 KB Output is correct
12 Correct 859 ms 6480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4968 KB Output is correct
2 Correct 3 ms 5072 KB Output is correct
3 Correct 3 ms 5072 KB Output is correct
4 Correct 3 ms 5072 KB Output is correct
5 Correct 3 ms 5072 KB Output is correct
6 Correct 5 ms 5220 KB Output is correct
7 Correct 4 ms 5328 KB Output is correct
8 Correct 3 ms 5328 KB Output is correct
9 Correct 3 ms 5328 KB Output is correct
10 Correct 4 ms 5456 KB Output is correct
11 Correct 3 ms 5456 KB Output is correct
12 Correct 3 ms 5328 KB Output is correct
13 Correct 746 ms 17280 KB Output is correct
14 Correct 851 ms 29644 KB Output is correct
15 Correct 971 ms 29552 KB Output is correct
16 Correct 906 ms 29640 KB Output is correct
17 Correct 847 ms 17232 KB Output is correct
18 Correct 964 ms 29580 KB Output is correct
19 Correct 1002 ms 29616 KB Output is correct
20 Correct 954 ms 29640 KB Output is correct
21 Correct 441 ms 5712 KB Output is correct
22 Correct 758 ms 6600 KB Output is correct
23 Correct 713 ms 6540 KB Output is correct
24 Correct 859 ms 6480 KB Output is correct
25 Correct 1039 ms 41920 KB Output is correct
26 Correct 948 ms 42500 KB Output is correct
27 Correct 1150 ms 42528 KB Output is correct
28 Correct 861 ms 42524 KB Output is correct
29 Correct 1012 ms 51840 KB Output is correct
30 Correct 985 ms 51848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 4 ms 5328 KB Output is correct
4 Correct 3 ms 5328 KB Output is correct
5 Correct 3 ms 5328 KB Output is correct
6 Correct 3 ms 5328 KB Output is correct
7 Correct 3 ms 5328 KB Output is correct
8 Correct 3 ms 5328 KB Output is correct
9 Correct 3 ms 4968 KB Output is correct
10 Correct 3 ms 5072 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 3 ms 5072 KB Output is correct
14 Correct 5 ms 5220 KB Output is correct
15 Correct 4 ms 5328 KB Output is correct
16 Correct 3 ms 5328 KB Output is correct
17 Correct 3 ms 5328 KB Output is correct
18 Correct 4 ms 5456 KB Output is correct
19 Correct 3 ms 5456 KB Output is correct
20 Correct 3 ms 5328 KB Output is correct
21 Correct 4 ms 5328 KB Output is correct
22 Correct 4 ms 5200 KB Output is correct
23 Correct 3 ms 5200 KB Output is correct
24 Correct 3 ms 5328 KB Output is correct
25 Correct 4 ms 5384 KB Output is correct
26 Correct 3 ms 5328 KB Output is correct
27 Correct 3 ms 5328 KB Output is correct
28 Correct 4 ms 5328 KB Output is correct
29 Correct 3 ms 5328 KB Output is correct
30 Correct 3 ms 5328 KB Output is correct
31 Correct 3 ms 5072 KB Output is correct
32 Correct 4 ms 5328 KB Output is correct
33 Correct 3 ms 5328 KB Output is correct
34 Correct 3 ms 5328 KB Output is correct
35 Correct 3 ms 5200 KB Output is correct
36 Correct 3 ms 5456 KB Output is correct
37 Correct 3 ms 5456 KB Output is correct
38 Correct 3 ms 5456 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 3 ms 5200 KB Output is correct
41 Correct 3 ms 5272 KB Output is correct
42 Correct 3 ms 5200 KB Output is correct
43 Correct 614 ms 5968 KB Output is correct
44 Correct 556 ms 6032 KB Output is correct
45 Correct 801 ms 6096 KB Output is correct
46 Correct 774 ms 6896 KB Output is correct
47 Correct 813 ms 6884 KB Output is correct
48 Correct 828 ms 6896 KB Output is correct
49 Correct 784 ms 6888 KB Output is correct
50 Correct 837 ms 6884 KB Output is correct
51 Correct 865 ms 6632 KB Output is correct
52 Correct 946 ms 6628 KB Output is correct
53 Correct 717 ms 5712 KB Output is correct
54 Correct 844 ms 6884 KB Output is correct
55 Correct 861 ms 6760 KB Output is correct
56 Correct 886 ms 6704 KB Output is correct
57 Correct 913 ms 6608 KB Output is correct
58 Correct 531 ms 7352 KB Output is correct
59 Correct 888 ms 7376 KB Output is correct
60 Correct 863 ms 7376 KB Output is correct
61 Correct 729 ms 5968 KB Output is correct
62 Correct 781 ms 6224 KB Output is correct
63 Correct 995 ms 6224 KB Output is correct
64 Correct 859 ms 6728 KB Output is correct
65 Correct 489 ms 5712 KB Output is correct
66 Correct 839 ms 6540 KB Output is correct
67 Correct 820 ms 6540 KB Output is correct
68 Correct 925 ms 6544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 4 ms 5328 KB Output is correct
4 Correct 3 ms 5328 KB Output is correct
5 Correct 3 ms 5328 KB Output is correct
6 Correct 3 ms 5328 KB Output is correct
7 Correct 3 ms 5328 KB Output is correct
8 Correct 3 ms 5328 KB Output is correct
9 Correct 3 ms 4968 KB Output is correct
10 Correct 3 ms 5072 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 3 ms 5072 KB Output is correct
14 Correct 5 ms 5220 KB Output is correct
15 Correct 4 ms 5328 KB Output is correct
16 Correct 3 ms 5328 KB Output is correct
17 Correct 3 ms 5328 KB Output is correct
18 Correct 4 ms 5456 KB Output is correct
19 Correct 3 ms 5456 KB Output is correct
20 Correct 3 ms 5328 KB Output is correct
21 Correct 4 ms 5328 KB Output is correct
22 Correct 4 ms 5200 KB Output is correct
23 Correct 3 ms 5200 KB Output is correct
24 Correct 3 ms 5328 KB Output is correct
25 Correct 4 ms 5384 KB Output is correct
26 Correct 3 ms 5328 KB Output is correct
27 Correct 3 ms 5328 KB Output is correct
28 Correct 4 ms 5328 KB Output is correct
29 Correct 3 ms 5328 KB Output is correct
30 Correct 3 ms 5328 KB Output is correct
31 Correct 3 ms 5072 KB Output is correct
32 Correct 4 ms 5328 KB Output is correct
33 Correct 3 ms 5328 KB Output is correct
34 Correct 3 ms 5328 KB Output is correct
35 Correct 3 ms 5200 KB Output is correct
36 Correct 3 ms 5456 KB Output is correct
37 Correct 3 ms 5456 KB Output is correct
38 Correct 3 ms 5456 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 3 ms 5200 KB Output is correct
41 Correct 3 ms 5272 KB Output is correct
42 Correct 3 ms 5200 KB Output is correct
43 Correct 746 ms 17280 KB Output is correct
44 Correct 851 ms 29644 KB Output is correct
45 Correct 971 ms 29552 KB Output is correct
46 Correct 906 ms 29640 KB Output is correct
47 Correct 847 ms 17232 KB Output is correct
48 Correct 964 ms 29580 KB Output is correct
49 Correct 1002 ms 29616 KB Output is correct
50 Correct 954 ms 29640 KB Output is correct
51 Correct 441 ms 5712 KB Output is correct
52 Correct 758 ms 6600 KB Output is correct
53 Correct 713 ms 6540 KB Output is correct
54 Correct 859 ms 6480 KB Output is correct
55 Correct 1039 ms 41920 KB Output is correct
56 Correct 948 ms 42500 KB Output is correct
57 Correct 1150 ms 42528 KB Output is correct
58 Correct 861 ms 42524 KB Output is correct
59 Correct 1012 ms 51840 KB Output is correct
60 Correct 985 ms 51848 KB Output is correct
61 Correct 614 ms 5968 KB Output is correct
62 Correct 556 ms 6032 KB Output is correct
63 Correct 801 ms 6096 KB Output is correct
64 Correct 774 ms 6896 KB Output is correct
65 Correct 813 ms 6884 KB Output is correct
66 Correct 828 ms 6896 KB Output is correct
67 Correct 784 ms 6888 KB Output is correct
68 Correct 837 ms 6884 KB Output is correct
69 Correct 865 ms 6632 KB Output is correct
70 Correct 946 ms 6628 KB Output is correct
71 Correct 717 ms 5712 KB Output is correct
72 Correct 844 ms 6884 KB Output is correct
73 Correct 861 ms 6760 KB Output is correct
74 Correct 886 ms 6704 KB Output is correct
75 Correct 913 ms 6608 KB Output is correct
76 Correct 531 ms 7352 KB Output is correct
77 Correct 888 ms 7376 KB Output is correct
78 Correct 863 ms 7376 KB Output is correct
79 Correct 729 ms 5968 KB Output is correct
80 Correct 781 ms 6224 KB Output is correct
81 Correct 995 ms 6224 KB Output is correct
82 Correct 859 ms 6728 KB Output is correct
83 Correct 489 ms 5712 KB Output is correct
84 Correct 839 ms 6540 KB Output is correct
85 Correct 820 ms 6540 KB Output is correct
86 Correct 925 ms 6544 KB Output is correct
87 Correct 3 ms 4944 KB Output is correct
88 Correct 673 ms 36556 KB Output is correct
89 Correct 1063 ms 25032 KB Output is correct
90 Correct 927 ms 26932 KB Output is correct
91 Correct 1046 ms 42648 KB Output is correct
92 Correct 1156 ms 42652 KB Output is correct
93 Correct 1004 ms 42624 KB Output is correct
94 Correct 1121 ms 42676 KB Output is correct
95 Correct 984 ms 42648 KB Output is correct
96 Correct 838 ms 37488 KB Output is correct
97 Correct 954 ms 37484 KB Output is correct
98 Correct 737 ms 19784 KB Output is correct
99 Correct 1038 ms 42492 KB Output is correct
100 Correct 959 ms 39752 KB Output is correct
101 Correct 938 ms 38856 KB Output is correct
102 Correct 1071 ms 37544 KB Output is correct
103 Correct 1054 ms 51912 KB Output is correct
104 Correct 1010 ms 52316 KB Output is correct
105 Correct 949 ms 52320 KB Output is correct
106 Correct 986 ms 24576 KB Output is correct
107 Correct 877 ms 28544 KB Output is correct
108 Correct 1064 ms 31484 KB Output is correct
109 Correct 1041 ms 37536 KB Output is correct