Submission #1067079

# Submission time Handle Problem Language Result Execution time Memory
1067079 2024-08-20T10:45:49 Z j_vdd16 Digital Circuit (IOI22_circuit) C++17
100 / 100
783 ms 28336 KB
#include "circuit.h"

#include <algorithm>
#include <bitset>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define int long long
#define loop(X, N) for(int X = 0; X < (N); X++)
#define all(V) V.begin(), V.end()
#define rall(V) V.rbegin(), V.rend()

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;

constexpr int mod = 1'000'002'022;

struct SegTree {
    int n, N;

    vi prefix;
    int prefSum(int l, int r) {
        return (prefix[r] - prefix[l] + mod) % mod;
    }

    vi tree;
    vi lazy;
    SegTree() = default;
    SegTree(vi values, vector<signed> assignment) {
        n = values.size();
        N = 1;
        while (N < n) N *= 2;

        prefix = vi(n + 1);
        loop(i, n) {
            prefix[i + 1] = (prefix[i] + values[i]) % mod;
        }

        tree = vi(2 * N);
        lazy = vi(2 * N);
        loop(i, n) {
            tree[N + i] = assignment[i] ? values[i] : 0;
        }
        for (int i = N - 1; i >= 1; i--) {
            tree[i] = (tree[2 * i] + tree[2 * i + 1]) % mod;
        }
    }

    int get(int l, int r, int i = 1, int tl = 0, int tr = -1) {
        if (tr == -1) {
            tr = N;
        }
        
        if (lazy[i]) {
            tree[i] = (prefSum(tl, tr) - tree[i] + mod) % mod;
            if (tr - tl > 1) {
                lazy[2 * i] ^= true;
                lazy[2 * i + 1] ^= true;
            }
            lazy[i] = false;
        }

        if (tr <= l || tl >= r) {
            return tree[i];
        }

        if (tl >= l && tr <= r) {
            tree[i] = (prefSum(tl, tr) - tree[i] + mod) % mod;
            if (tr - tl > 1) {
                lazy[2 * i] ^= true;
                lazy[2 * i + 1] ^= true;
            }

            return tree[i];
        }

        int tm = (tl + tr) / 2;
        tree[i] = (get(l, r, 2 * i, tl, tm) + get(l, r, 2 * i + 1, tm, tr)) % mod;
        return tree[i];
    }
};

int n, m;
vvi children;
vector<signed> assignment;
vi c;

vi factors;
void cDfs(int node) {
    if (node >= n) {
        c[node] = 1;
        return;
    }

    vii out;
    c[node] = children[node].size();
    for (int child : children[node]) {
        cDfs(child);

        c[node] = (c[node] * c[child]) % mod;
    }
}
void factorDfs(int node, int factor) {
    if (node >= n) {
        factors[node - n] = factor;
        return;
    }

    int deg = children[node].size();
    vi factorPref(deg + 1, 1);
    for (int i = deg - 1; i >= 0; i--) {
        factorPref[i] = (c[children[node][i]] * factorPref[i + 1]) % mod;
    }

    int factorSoFar = factor;
    loop(i, deg) {
        factorDfs(children[node][i], (factorSoFar * factorPref[i + 1]) % mod);

        factorSoFar = (factorSoFar * c[children[node][i]]) % mod;
    }

    //factorDfs(children[node][0], (factor * c[children[node][1]]) % mod);
    //factorDfs(children[node][1], (factor * c[children[node][0]]) % mod);
}

SegTree segTree;
void init(signed N, signed M, std::vector<signed> P, std::vector<signed> A) {
    n = N;
    m = M;
    children = vvi(n);
    assignment = A;
    c = vi(n + m);
    factors = vi(m);

    for (int i = 1; i < n + m; i++) {
        children[P[i]].push_back(i);
    }

    cDfs(0);
    factorDfs(0, 1);

    segTree = SegTree(factors, assignment);
}

signed count_ways(signed L, signed R) {
    int res = segTree.get(L - n, R - n + 1);
    
    return res;
    /*
    3 children:
    a = 
        1 * (a[0] * b[1] * b[2] + b[0] * a[1] * b[2] + b[0] * b[1] * a[2]) + 
        2 * (a[0] * a[1] * b[2] + a[0] * b[1] * a[2] + b[0] * a[1] * a[2]) +
        3 * (a[0] * a[1] * a[2])
      =
        1 * (a[0] * (c[1] - a[1]) * (c[2] - a[2]))
        2 * (a[0] * a[1] * (c[2] - a[2]))
        3 * (a[0] * a[1] * a[2])
      =
        (1 + 2 - 3) * (a[0] * a[1] * a[2])
        a[0] * c[1] * c[2]
        2 * (a[0] * a[1] * c[2]) - 2 * (a[0] * a[1] * c[2])
      = 
        a[0] * c[1] * c[2]
    

    
    C = c[0] * c[1] * c[2] * 3
    b = C - a
      = 

    2 children:
    a = 
        1 * (a[0] * b[1] + b[0] * a[1]) +
        2 * (a[0] * a[1])
      =
        1 * (a[0] * (c[1] - a[1]) + (c[0] - a[0]) * a[1]) +
        2 * (a[0] * a[1])
      = a[0] * c[1] + c[0] * a[1]
    
    C = c[0] * c[1] * 2
    b = C - a
      = 
    */

}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 0 ms 600 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 1 ms 600 KB Output is correct
38 Correct 1 ms 600 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 430 ms 5620 KB Output is correct
2 Correct 675 ms 10584 KB Output is correct
3 Correct 675 ms 10584 KB Output is correct
4 Correct 680 ms 10836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 430 ms 5620 KB Output is correct
2 Correct 675 ms 10584 KB Output is correct
3 Correct 675 ms 10584 KB Output is correct
4 Correct 680 ms 10836 KB Output is correct
5 Correct 556 ms 5464 KB Output is correct
6 Correct 704 ms 10580 KB Output is correct
7 Correct 701 ms 10584 KB Output is correct
8 Correct 685 ms 10584 KB Output is correct
9 Correct 300 ms 724 KB Output is correct
10 Correct 643 ms 1048 KB Output is correct
11 Correct 608 ms 1048 KB Output is correct
12 Correct 647 ms 1056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 430 ms 5620 KB Output is correct
14 Correct 675 ms 10584 KB Output is correct
15 Correct 675 ms 10584 KB Output is correct
16 Correct 680 ms 10836 KB Output is correct
17 Correct 556 ms 5464 KB Output is correct
18 Correct 704 ms 10580 KB Output is correct
19 Correct 701 ms 10584 KB Output is correct
20 Correct 685 ms 10584 KB Output is correct
21 Correct 300 ms 724 KB Output is correct
22 Correct 643 ms 1048 KB Output is correct
23 Correct 608 ms 1048 KB Output is correct
24 Correct 647 ms 1056 KB Output is correct
25 Correct 715 ms 16720 KB Output is correct
26 Correct 766 ms 16976 KB Output is correct
27 Correct 734 ms 16968 KB Output is correct
28 Correct 615 ms 16980 KB Output is correct
29 Correct 703 ms 27512 KB Output is correct
30 Correct 681 ms 27388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 0 ms 600 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 1 ms 600 KB Output is correct
38 Correct 1 ms 600 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Correct 434 ms 912 KB Output is correct
44 Correct 655 ms 968 KB Output is correct
45 Correct 698 ms 944 KB Output is correct
46 Correct 577 ms 1112 KB Output is correct
47 Correct 612 ms 1112 KB Output is correct
48 Correct 631 ms 1112 KB Output is correct
49 Correct 656 ms 1112 KB Output is correct
50 Correct 644 ms 1112 KB Output is correct
51 Correct 586 ms 856 KB Output is correct
52 Correct 609 ms 856 KB Output is correct
53 Correct 638 ms 1368 KB Output is correct
54 Correct 639 ms 1312 KB Output is correct
55 Correct 627 ms 1112 KB Output is correct
56 Correct 644 ms 1132 KB Output is correct
57 Correct 671 ms 1016 KB Output is correct
58 Correct 639 ms 1836 KB Output is correct
59 Correct 628 ms 1880 KB Output is correct
60 Correct 592 ms 1880 KB Output is correct
61 Correct 681 ms 1364 KB Output is correct
62 Correct 626 ms 896 KB Output is correct
63 Correct 646 ms 900 KB Output is correct
64 Correct 625 ms 1044 KB Output is correct
65 Correct 315 ms 728 KB Output is correct
66 Correct 708 ms 1048 KB Output is correct
67 Correct 639 ms 1048 KB Output is correct
68 Correct 658 ms 1048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 0 ms 600 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 1 ms 600 KB Output is correct
38 Correct 1 ms 600 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Correct 430 ms 5620 KB Output is correct
44 Correct 675 ms 10584 KB Output is correct
45 Correct 675 ms 10584 KB Output is correct
46 Correct 680 ms 10836 KB Output is correct
47 Correct 556 ms 5464 KB Output is correct
48 Correct 704 ms 10580 KB Output is correct
49 Correct 701 ms 10584 KB Output is correct
50 Correct 685 ms 10584 KB Output is correct
51 Correct 300 ms 724 KB Output is correct
52 Correct 643 ms 1048 KB Output is correct
53 Correct 608 ms 1048 KB Output is correct
54 Correct 647 ms 1056 KB Output is correct
55 Correct 715 ms 16720 KB Output is correct
56 Correct 766 ms 16976 KB Output is correct
57 Correct 734 ms 16968 KB Output is correct
58 Correct 615 ms 16980 KB Output is correct
59 Correct 703 ms 27512 KB Output is correct
60 Correct 681 ms 27388 KB Output is correct
61 Correct 434 ms 912 KB Output is correct
62 Correct 655 ms 968 KB Output is correct
63 Correct 698 ms 944 KB Output is correct
64 Correct 577 ms 1112 KB Output is correct
65 Correct 612 ms 1112 KB Output is correct
66 Correct 631 ms 1112 KB Output is correct
67 Correct 656 ms 1112 KB Output is correct
68 Correct 644 ms 1112 KB Output is correct
69 Correct 586 ms 856 KB Output is correct
70 Correct 609 ms 856 KB Output is correct
71 Correct 638 ms 1368 KB Output is correct
72 Correct 639 ms 1312 KB Output is correct
73 Correct 627 ms 1112 KB Output is correct
74 Correct 644 ms 1132 KB Output is correct
75 Correct 671 ms 1016 KB Output is correct
76 Correct 639 ms 1836 KB Output is correct
77 Correct 628 ms 1880 KB Output is correct
78 Correct 592 ms 1880 KB Output is correct
79 Correct 681 ms 1364 KB Output is correct
80 Correct 626 ms 896 KB Output is correct
81 Correct 646 ms 900 KB Output is correct
82 Correct 625 ms 1044 KB Output is correct
83 Correct 315 ms 728 KB Output is correct
84 Correct 708 ms 1048 KB Output is correct
85 Correct 639 ms 1048 KB Output is correct
86 Correct 658 ms 1048 KB Output is correct
87 Correct 0 ms 344 KB Output is correct
88 Correct 420 ms 16040 KB Output is correct
89 Correct 695 ms 10568 KB Output is correct
90 Correct 646 ms 10576 KB Output is correct
91 Correct 736 ms 17492 KB Output is correct
92 Correct 783 ms 17492 KB Output is correct
93 Correct 753 ms 17488 KB Output is correct
94 Correct 748 ms 17488 KB Output is correct
95 Correct 763 ms 17564 KB Output is correct
96 Correct 701 ms 10684 KB Output is correct
97 Correct 776 ms 10676 KB Output is correct
98 Correct 622 ms 19800 KB Output is correct
99 Correct 753 ms 16976 KB Output is correct
100 Correct 743 ms 14424 KB Output is correct
101 Correct 666 ms 12880 KB Output is correct
102 Correct 681 ms 10840 KB Output is correct
103 Correct 740 ms 27472 KB Output is correct
104 Correct 733 ms 28172 KB Output is correct
105 Correct 711 ms 28336 KB Output is correct
106 Correct 705 ms 12584 KB Output is correct
107 Correct 740 ms 11172 KB Output is correct
108 Correct 716 ms 11660 KB Output is correct
109 Correct 731 ms 11096 KB Output is correct