Submission #634605

# Submission time Handle Problem Language Result Execution time Memory
634605 2022-08-24T15:39:50 Z JeanBombeur Digital Circuit (IOI22_circuit) C++17
100 / 100
1088 ms 42504 KB
#include "circuit.h"
#include <vector>
#include <cstdio>
using namespace std;

//  <|°_°|>

//  Jean Bombeur & M. Broccoli

//  The hardest choices require the strongest wills

//  What is better - to be born good, or to overcome your evil nature with great effort ?

const int MOD = (1000002022);
const int MAX_NODES = (2e5);
const int LOG = (17);
const int SIZE = (1 << LOG);

struct node {
    int val = 0, inv = 0, flag = 0;
};

template <class T = int, class F = int>
struct segment_tree {
    vector <T> Tree;
    vector <F> Flags;
    int size = 0, log_max = 0;
    
    // =================== TO FILL ====================
    
    // put here your favorite operation on nodes like min, max, gcd ...
    inline T combine(T left_value, T right_value) {
        return {(left_value.val + right_value.val) % MOD, (left_value.inv + right_value.inv) % MOD, 0};
    }
    
    bool using_lazy = true;  // set to true if you want to enable lazy flags
    
    // put here your favorite operation on flags
    inline F combine_flags(F old_flag, F new_flag) {
        return old_flag ^ new_flag;
    }
    
    bool flags_commute = true;  // set to true if combine_flags is commutative (speeds up a little)
    
    // put here how values and flags interact
    inline T combine_with_flag(T value, F flag) {
        return flag ? (T){value.inv, value.val, 0} : value;
    }
    
    T start_value = {0, 0, 0};  // put here the default value of a node
    T null_value = {0, 0, 0};  // put here the identity value of combine
    F null_flag = 0;  // put here the default value of a flag
    
    // make sure that operator != is defined for flags !
    
    // ================= FOR DEBUG ====================
    
    // put here what you'd like to print about the nodes
    void print_value(T value) {
        printf("%d", value);
        return;
    }
    
    // put here what you'd like to print about the flags
    void print_flag(F flag) {
        printf("%d", flag);
        return;
    }
    
    // ======== DO NOT TOUCH AFTER THIS LINE ==========
    
    // remember to call this at the start !
    // n doesn't need to be a power of two
    void set_to_size(int n) {
        Tree.resize(2 * n, start_value);
        if (using_lazy)
            Flags.resize(n, null_flag);
        size = n;
        log_max = 32 - __builtin_clz(n);
        return;
    }
    
    // destroys everything : ready to be used again !
    void clear() {
        Tree.clear();
        Flags.clear();
        size = log_max = 0;
        return;
    }
    
    // turns an array of exactly size elements into a segtree : equivalent to updating each element but faster
    // void build(vector <T> &Array) {  // you may use this instead
    void build(T* Array) {
        for (int i = 0; i < size; i ++)
            Tree[i + size] = Array[i];
        for (int i = size - 1; i; i --)
            Tree[i] = combine(Tree[i << 1], Tree[(i << 1) ^ 1]);
        return;
    }
    
    // combines flag to node index
    void put_flag(int index, F flag) {
        Tree[index] = combine_with_flag(Tree[index], flag);
        if (index < size)
            Flags[index] = combine_flags(Flags[index], flag);
        return;
    }
    
    // pushes all flags above node index
    void push(int index) {
        for (int h = log_max; h; h --)
        {
            int p = index >> h;
            if (Flags[p] != null_flag)
            {
                put_flag(p << 1, Flags[p]);
                put_flag((p << 1) ^ 1, Flags[p]);
                Flags[p] = null_flag;
            }
        }
        return;
    }
    
    // updates all values above node index
    void pull(int index) {
        for (index >>= 1; index; index >>= 1)
            Tree[index] = combine_with_flag(combine(Tree[index << 1], Tree[(index << 1) ^ 1]), Flags[index]);
        return;
    }
    
    // sets the index-th element to value (zero-based)
    void set(int index, T value) {
        index += size;
        if (using_lazy)
        {
            push(index);
            Tree[index] = value;
            pull(index);
            return;
        }
        Tree[index] = value;
        for (index >>= 1; index; index >>= 1)
            Tree[index] = combine(Tree[index << 1], Tree[(index << 1) ^ 1]);
        return;
    }
    
    // returns the value of the index-th element (zero-based)
    T get(int index) {
        index += size;
        if (using_lazy)
            push(index);
        return Tree[index];
    }
    
    // combines lazily a flag to the range [left, right[  (zero-based)
    void range_add(int left, int right, F flag) {
        left += size, right += size;
        if (!flags_commute)
            push(left), push(right - 1);
        for (int l = left, r = right; l < r; l >>= 1, r >>= 1)
        {
            if (l & 1)
                put_flag(l ++, flag);
            if (r & 1)
                put_flag(-- r, flag);
        }
        pull(left), pull(right - 1);
        return;
    }
    
    // returns combine of all elements in range [left, right[  (zero-based)
    T range_op(int left, int right) {
        left += size, right += size;
        if (using_lazy)
            push(left), push(right - 1);
        T left_ans = null_value, right_ans = null_value;
        for (; left < right; left >>= 1, right >>= 1)
        {
            if (left & 1)
                left_ans = combine(left_ans, Tree[left ++]);
            if (right & 1)
                right_ans = combine(Tree[-- right], right_ans);
        }
        return combine(left_ans, right_ans);
    }
    
    // prints the tree rotated 90 degrees left
    void print(int a = 1, int p = 0) {
        if (a >= 2 * size)
            return;
        if (a == 1)
        {
            for (int i = 0; i < 60; i ++)
                printf("=");
            printf("\n");
        }
        print((a << 1) ^ 1, p + 1);
        for (int i = 0; i < p; i ++)
            printf("\t");
        print_value(Tree[a]);
        if (using_lazy)
        {
            printf(" / ");
            if (a < size)
                print_flag(Flags[a]);
        }
        printf("\n");
        print(a << 1, p + 1);
        if (a == 1)
        {
            for (int i = 0; i < 60; i ++)
                printf("=");
            printf("\n");
        }
        return;
    }
};

vector <int> Adj[MAX_NODES];

long long Sz[MAX_NODES];

segment_tree <node> Segt;
node Sources[MAX_NODES];

int nbThresholds;

int DfsSz(int node) {
    Sz[node] = Adj[node].size() + Adj[node].empty();
    for (int dest : Adj[node])
        Sz[node] *= DfsSz(dest), Sz[node] %= MOD;
    return Sz[node];
}

void DfsSet(int node, long long value) {
    if (Adj[node].empty())
        Sources[node - nbThresholds] = {0, (int)value, 0};
    vector <long long> ProdPref (1, 1);
    vector <long long> ProdSuff (1, 1);
    int sz = Adj[node].size();
    for (int i = 0; i < sz; i ++)
    {
        ProdPref.push_back((ProdPref.back() * Sz[Adj[node][i]]) % MOD);
    }
    for (int i = sz - 1; i >= 0; i --)
    {
        ProdSuff.push_back((ProdSuff.back() * Sz[Adj[node][i]]) % MOD);
    }
    for (int i = 0; i < sz; i ++)
    {
        DfsSet(Adj[node][i], (((value * ProdPref[i]) % MOD) * ProdSuff[sz - i - 1]) % MOD);
    }
    return;
}

void init(int nbNodes, int nbSources, vector <int> Parent, vector <int> States) {
    nbThresholds = nbNodes;
    Segt.set_to_size(nbSources);
    for (int i = 1; i < nbThresholds + nbSources; i ++)
    {
        Adj[Parent[i]].push_back(i);
    }
    DfsSz(0);
    DfsSet(0, 1);
    for (int i = 0; i < nbSources; i ++)
    {
        if (States[i])
            swap(Sources[i].val, Sources[i].inv);
    }
    Segt.build(Sources);
    return;
}

int count_ways(int left, int right) {
    Segt.range_add(left - nbThresholds, (++ right) - nbThresholds, 1);
    return Segt.Tree[1].val;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 2 ms 4944 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 3 ms 5072 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 2 ms 5072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4912 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 3 ms 4944 KB Output is correct
4 Correct 3 ms 4944 KB Output is correct
5 Correct 3 ms 4944 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 5072 KB Output is correct
9 Correct 3 ms 5072 KB Output is correct
10 Correct 4 ms 5272 KB Output is correct
11 Correct 3 ms 5328 KB Output is correct
12 Correct 4 ms 5072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 2 ms 4944 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 3 ms 5072 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 2 ms 5072 KB Output is correct
9 Correct 3 ms 4912 KB Output is correct
10 Correct 3 ms 4944 KB Output is correct
11 Correct 3 ms 4944 KB Output is correct
12 Correct 3 ms 4944 KB Output is correct
13 Correct 3 ms 4944 KB Output is correct
14 Correct 3 ms 5072 KB Output is correct
15 Correct 3 ms 5072 KB Output is correct
16 Correct 3 ms 5072 KB Output is correct
17 Correct 3 ms 5072 KB Output is correct
18 Correct 4 ms 5272 KB Output is correct
19 Correct 3 ms 5328 KB Output is correct
20 Correct 4 ms 5072 KB Output is correct
21 Correct 3 ms 5072 KB Output is correct
22 Correct 3 ms 4944 KB Output is correct
23 Correct 3 ms 4944 KB Output is correct
24 Correct 3 ms 5072 KB Output is correct
25 Correct 4 ms 5096 KB Output is correct
26 Correct 4 ms 5072 KB Output is correct
27 Correct 4 ms 5072 KB Output is correct
28 Correct 3 ms 5072 KB Output is correct
29 Correct 3 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5200 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 5328 KB Output is correct
37 Correct 3 ms 5328 KB Output is correct
38 Correct 3 ms 5328 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 4 ms 5072 KB Output is correct
42 Correct 3 ms 5072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 622 ms 8548 KB Output is correct
2 Correct 802 ms 12104 KB Output is correct
3 Correct 628 ms 12104 KB Output is correct
4 Correct 872 ms 12104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 622 ms 8548 KB Output is correct
2 Correct 802 ms 12104 KB Output is correct
3 Correct 628 ms 12104 KB Output is correct
4 Correct 872 ms 12104 KB Output is correct
5 Correct 738 ms 8592 KB Output is correct
6 Correct 961 ms 12104 KB Output is correct
7 Correct 901 ms 12100 KB Output is correct
8 Correct 752 ms 12104 KB Output is correct
9 Correct 271 ms 5200 KB Output is correct
10 Correct 708 ms 5328 KB Output is correct
11 Correct 736 ms 5328 KB Output is correct
12 Correct 759 ms 5328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4912 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 3 ms 4944 KB Output is correct
4 Correct 3 ms 4944 KB Output is correct
5 Correct 3 ms 4944 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 5072 KB Output is correct
9 Correct 3 ms 5072 KB Output is correct
10 Correct 4 ms 5272 KB Output is correct
11 Correct 3 ms 5328 KB Output is correct
12 Correct 4 ms 5072 KB Output is correct
13 Correct 622 ms 8548 KB Output is correct
14 Correct 802 ms 12104 KB Output is correct
15 Correct 628 ms 12104 KB Output is correct
16 Correct 872 ms 12104 KB Output is correct
17 Correct 738 ms 8592 KB Output is correct
18 Correct 961 ms 12104 KB Output is correct
19 Correct 901 ms 12100 KB Output is correct
20 Correct 752 ms 12104 KB Output is correct
21 Correct 271 ms 5200 KB Output is correct
22 Correct 708 ms 5328 KB Output is correct
23 Correct 736 ms 5328 KB Output is correct
24 Correct 759 ms 5328 KB Output is correct
25 Correct 849 ms 15740 KB Output is correct
26 Correct 964 ms 16000 KB Output is correct
27 Correct 966 ms 15864 KB Output is correct
28 Correct 894 ms 15944 KB Output is correct
29 Correct 1080 ms 42484 KB Output is correct
30 Correct 975 ms 42492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 2 ms 4944 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 3 ms 5072 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 2 ms 5072 KB Output is correct
9 Correct 3 ms 4912 KB Output is correct
10 Correct 3 ms 4944 KB Output is correct
11 Correct 3 ms 4944 KB Output is correct
12 Correct 3 ms 4944 KB Output is correct
13 Correct 3 ms 4944 KB Output is correct
14 Correct 3 ms 5072 KB Output is correct
15 Correct 3 ms 5072 KB Output is correct
16 Correct 3 ms 5072 KB Output is correct
17 Correct 3 ms 5072 KB Output is correct
18 Correct 4 ms 5272 KB Output is correct
19 Correct 3 ms 5328 KB Output is correct
20 Correct 4 ms 5072 KB Output is correct
21 Correct 3 ms 5072 KB Output is correct
22 Correct 3 ms 4944 KB Output is correct
23 Correct 3 ms 4944 KB Output is correct
24 Correct 3 ms 5072 KB Output is correct
25 Correct 4 ms 5096 KB Output is correct
26 Correct 4 ms 5072 KB Output is correct
27 Correct 4 ms 5072 KB Output is correct
28 Correct 3 ms 5072 KB Output is correct
29 Correct 3 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5200 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 5328 KB Output is correct
37 Correct 3 ms 5328 KB Output is correct
38 Correct 3 ms 5328 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 4 ms 5072 KB Output is correct
42 Correct 3 ms 5072 KB Output is correct
43 Correct 604 ms 5200 KB Output is correct
44 Correct 737 ms 5328 KB Output is correct
45 Correct 744 ms 5328 KB Output is correct
46 Correct 920 ms 5456 KB Output is correct
47 Correct 857 ms 5456 KB Output is correct
48 Correct 410 ms 5456 KB Output is correct
49 Correct 987 ms 5456 KB Output is correct
50 Correct 866 ms 5456 KB Output is correct
51 Correct 889 ms 5456 KB Output is correct
52 Correct 859 ms 5456 KB Output is correct
53 Correct 757 ms 6352 KB Output is correct
54 Correct 902 ms 5456 KB Output is correct
55 Correct 719 ms 5328 KB Output is correct
56 Correct 779 ms 5372 KB Output is correct
57 Correct 577 ms 5272 KB Output is correct
58 Correct 796 ms 6864 KB Output is correct
59 Correct 787 ms 6864 KB Output is correct
60 Correct 873 ms 6864 KB Output is correct
61 Correct 898 ms 5584 KB Output is correct
62 Correct 754 ms 5200 KB Output is correct
63 Correct 803 ms 5200 KB Output is correct
64 Correct 915 ms 5328 KB Output is correct
65 Correct 468 ms 5200 KB Output is correct
66 Correct 943 ms 5428 KB Output is correct
67 Correct 1008 ms 5328 KB Output is correct
68 Correct 765 ms 5356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 2 ms 4944 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 3 ms 5072 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 2 ms 5072 KB Output is correct
9 Correct 3 ms 4912 KB Output is correct
10 Correct 3 ms 4944 KB Output is correct
11 Correct 3 ms 4944 KB Output is correct
12 Correct 3 ms 4944 KB Output is correct
13 Correct 3 ms 4944 KB Output is correct
14 Correct 3 ms 5072 KB Output is correct
15 Correct 3 ms 5072 KB Output is correct
16 Correct 3 ms 5072 KB Output is correct
17 Correct 3 ms 5072 KB Output is correct
18 Correct 4 ms 5272 KB Output is correct
19 Correct 3 ms 5328 KB Output is correct
20 Correct 4 ms 5072 KB Output is correct
21 Correct 3 ms 5072 KB Output is correct
22 Correct 3 ms 4944 KB Output is correct
23 Correct 3 ms 4944 KB Output is correct
24 Correct 3 ms 5072 KB Output is correct
25 Correct 4 ms 5096 KB Output is correct
26 Correct 4 ms 5072 KB Output is correct
27 Correct 4 ms 5072 KB Output is correct
28 Correct 3 ms 5072 KB Output is correct
29 Correct 3 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5200 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 5328 KB Output is correct
37 Correct 3 ms 5328 KB Output is correct
38 Correct 3 ms 5328 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 4 ms 5072 KB Output is correct
42 Correct 3 ms 5072 KB Output is correct
43 Correct 622 ms 8548 KB Output is correct
44 Correct 802 ms 12104 KB Output is correct
45 Correct 628 ms 12104 KB Output is correct
46 Correct 872 ms 12104 KB Output is correct
47 Correct 738 ms 8592 KB Output is correct
48 Correct 961 ms 12104 KB Output is correct
49 Correct 901 ms 12100 KB Output is correct
50 Correct 752 ms 12104 KB Output is correct
51 Correct 271 ms 5200 KB Output is correct
52 Correct 708 ms 5328 KB Output is correct
53 Correct 736 ms 5328 KB Output is correct
54 Correct 759 ms 5328 KB Output is correct
55 Correct 849 ms 15740 KB Output is correct
56 Correct 964 ms 16000 KB Output is correct
57 Correct 966 ms 15864 KB Output is correct
58 Correct 894 ms 15944 KB Output is correct
59 Correct 1080 ms 42484 KB Output is correct
60 Correct 975 ms 42492 KB Output is correct
61 Correct 604 ms 5200 KB Output is correct
62 Correct 737 ms 5328 KB Output is correct
63 Correct 744 ms 5328 KB Output is correct
64 Correct 920 ms 5456 KB Output is correct
65 Correct 857 ms 5456 KB Output is correct
66 Correct 410 ms 5456 KB Output is correct
67 Correct 987 ms 5456 KB Output is correct
68 Correct 866 ms 5456 KB Output is correct
69 Correct 889 ms 5456 KB Output is correct
70 Correct 859 ms 5456 KB Output is correct
71 Correct 757 ms 6352 KB Output is correct
72 Correct 902 ms 5456 KB Output is correct
73 Correct 719 ms 5328 KB Output is correct
74 Correct 779 ms 5372 KB Output is correct
75 Correct 577 ms 5272 KB Output is correct
76 Correct 796 ms 6864 KB Output is correct
77 Correct 787 ms 6864 KB Output is correct
78 Correct 873 ms 6864 KB Output is correct
79 Correct 898 ms 5584 KB Output is correct
80 Correct 754 ms 5200 KB Output is correct
81 Correct 803 ms 5200 KB Output is correct
82 Correct 915 ms 5328 KB Output is correct
83 Correct 468 ms 5200 KB Output is correct
84 Correct 943 ms 5428 KB Output is correct
85 Correct 1008 ms 5328 KB Output is correct
86 Correct 765 ms 5356 KB Output is correct
87 Correct 2 ms 4944 KB Output is correct
88 Correct 589 ms 14584 KB Output is correct
89 Correct 983 ms 11640 KB Output is correct
90 Correct 1088 ms 11728 KB Output is correct
91 Correct 990 ms 15992 KB Output is correct
92 Correct 995 ms 16032 KB Output is correct
93 Correct 924 ms 16020 KB Output is correct
94 Correct 882 ms 15944 KB Output is correct
95 Correct 930 ms 15944 KB Output is correct
96 Correct 866 ms 13288 KB Output is correct
97 Correct 674 ms 13336 KB Output is correct
98 Correct 888 ms 33164 KB Output is correct
99 Correct 881 ms 15852 KB Output is correct
100 Correct 925 ms 13532 KB Output is correct
101 Correct 956 ms 12916 KB Output is correct
102 Correct 959 ms 11768 KB Output is correct
103 Correct 848 ms 42504 KB Output is correct
104 Correct 967 ms 41468 KB Output is correct
105 Correct 880 ms 41504 KB Output is correct
106 Correct 706 ms 16768 KB Output is correct
107 Correct 778 ms 11112 KB Output is correct
108 Correct 878 ms 11336 KB Output is correct
109 Correct 770 ms 11848 KB Output is correct