Submission #630222

# Submission time Handle Problem Language Result Execution time Memory
630222 2022-08-16T05:59:11 Z talant117408 Digital Circuit (IOI22_circuit) C++17
100 / 100
1136 ms 42972 KB
#include "circuit.h"
#include <bits/stdc++.h>

#ifndef EVAL
#include "stub.cpp"
#endif

using namespace std;
 
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

#define long                unsigned long 
#define pb                  push_back
#define mp                  make_pair
#define all(v)              (v).begin(),(v).end()
#define rall(v)             (v).rbegin(),(v).rend()
#define lb                  lower_bound
#define ub                  upper_bound
#define sz(v)               int((v).size())
#define do_not_disturb      ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl                '\n'
#define PI                  2*acos(0.0)

const ll mod = 1000002022;

ll add(ll a, ll b) {
    return ((a + b) % mod + mod) % mod;
}

ll mult(ll a, ll b) {
    return ((a * b) % mod + mod) % mod;
}

const int N = 2e5 + 7;
ll tree[2][N * 4], lz[N * 4]; // 0 - all configurations; 1 - valid configurations;
vector <int> graph[N];
vector <ll> pref[N], suff[N];
int n, m;
vector <int> parent, state;
ll valid_for[N], ans_for[N];

ll dfs(int v) {
    valid_for[v] = max(1, sz(graph[v]));
    for (auto to : graph[v]) {
        valid_for[v] = mult(valid_for[v], dfs(to));
    }
    return valid_for[v];
}

void calc(int v, ll val) {
    ans_for[v] = val;
    for (int i = 0; i < sz(graph[v]); i++) {
        auto to = graph[v][i];
        calc(to, mult(val, mult((i == 0 ? 1ll : pref[v][i - 1]), (i == sz(graph[v]) - 1ll ? 1 : suff[v][i + 1]))));
    }
}

void build(int v, int l, int r) {
    if (l == r) {
        tree[0][v] = ans_for[l + n];
        if (state[l]) {
            tree[1][v] = ans_for[l + n];
        }
        return;
    }
    int mid = (l + r) >> 1;
    build(v * 2, l, mid);
    build(v * 2 + 1, mid + 1, r);
    tree[0][v] = add(tree[0][v * 2], tree[0][v * 2 + 1]);
    tree[1][v] = add(tree[1][v * 2], tree[1][v * 2 + 1]);
}

void push(int v, int l, int r) {
    if (lz[v]) {
        tree[1][v] = add(tree[0][v], -tree[1][v]);
        if (l != r) {
            lz[v * 2] ^= lz[v];
            lz[v * 2 + 1] ^= lz[v];
        }
        lz[v] = 0;
    }
}

void update(int v, int l, int r, int ql, int qr) {
    push(v, l, r);
    if (ql > r || l > qr) return;
    if (ql <= l && r <= qr) {
        lz[v] ^= 1;
        push(v, l, r);
        return;
    }
    int mid = (l + r) >> 1;
    update(v * 2, l, mid, ql, qr);
    update(v * 2 + 1, mid + 1, r, ql, qr);
    tree[1][v] = add(tree[1][v * 2], tree[1][v * 2 + 1]);
}

void init(int N, int M, std::vector<int> P, std::vector<int> A) {
    n = N, m = M, parent = P, state = A;
    for (int i = 1; i < n + m; i++) {
        graph[parent[i]].pb(i);
    }
    dfs(0);
    for (int i = 0; i < n + m; i++) {
        for (auto to : graph[i]) {
            pref[i].pb(valid_for[to]);
            suff[i].pb(valid_for[to]);
        }
        for (int j = 1; j < sz(pref[i]); j++) {
            pref[i][j] = mult(pref[i][j], pref[i][j - 1]);
        }
        for (int j = sz(suff[i]) - 2; j >= 0; j--) {
            suff[i][j] = mult(suff[i][j], suff[i][j + 1]);
        }
    }
    calc(0, 1);
    build(1, 0, m - 1);
}

int count_ways(int L, int R) {
    update(1, 0, m - 1, L - n, R - n);
    return tree[1][1];
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14416 KB Output is correct
2 Correct 8 ms 14416 KB Output is correct
3 Correct 7 ms 14416 KB Output is correct
4 Correct 8 ms 14416 KB Output is correct
5 Correct 7 ms 14416 KB Output is correct
6 Correct 7 ms 14416 KB Output is correct
7 Correct 8 ms 14416 KB Output is correct
8 Correct 7 ms 14532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14416 KB Output is correct
2 Correct 8 ms 14416 KB Output is correct
3 Correct 8 ms 14516 KB Output is correct
4 Correct 7 ms 14416 KB Output is correct
5 Correct 8 ms 14416 KB Output is correct
6 Correct 8 ms 14544 KB Output is correct
7 Correct 9 ms 14504 KB Output is correct
8 Correct 8 ms 14604 KB Output is correct
9 Correct 8 ms 14544 KB Output is correct
10 Correct 8 ms 14544 KB Output is correct
11 Correct 8 ms 14544 KB Output is correct
12 Correct 8 ms 14532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14416 KB Output is correct
2 Correct 8 ms 14416 KB Output is correct
3 Correct 7 ms 14416 KB Output is correct
4 Correct 8 ms 14416 KB Output is correct
5 Correct 7 ms 14416 KB Output is correct
6 Correct 7 ms 14416 KB Output is correct
7 Correct 8 ms 14416 KB Output is correct
8 Correct 7 ms 14532 KB Output is correct
9 Correct 7 ms 14416 KB Output is correct
10 Correct 8 ms 14416 KB Output is correct
11 Correct 8 ms 14516 KB Output is correct
12 Correct 7 ms 14416 KB Output is correct
13 Correct 8 ms 14416 KB Output is correct
14 Correct 8 ms 14544 KB Output is correct
15 Correct 9 ms 14504 KB Output is correct
16 Correct 8 ms 14604 KB Output is correct
17 Correct 8 ms 14544 KB Output is correct
18 Correct 8 ms 14544 KB Output is correct
19 Correct 8 ms 14544 KB Output is correct
20 Correct 8 ms 14532 KB Output is correct
21 Correct 8 ms 14604 KB Output is correct
22 Correct 7 ms 14544 KB Output is correct
23 Correct 8 ms 14568 KB Output is correct
24 Correct 8 ms 14544 KB Output is correct
25 Correct 8 ms 14640 KB Output is correct
26 Correct 8 ms 14544 KB Output is correct
27 Correct 8 ms 14544 KB Output is correct
28 Correct 8 ms 14544 KB Output is correct
29 Correct 8 ms 14416 KB Output is correct
30 Correct 8 ms 14416 KB Output is correct
31 Correct 8 ms 14544 KB Output is correct
32 Correct 8 ms 14544 KB Output is correct
33 Correct 8 ms 14576 KB Output is correct
34 Correct 8 ms 14544 KB Output is correct
35 Correct 8 ms 14416 KB Output is correct
36 Correct 8 ms 14672 KB Output is correct
37 Correct 10 ms 14672 KB Output is correct
38 Correct 8 ms 14672 KB Output is correct
39 Correct 7 ms 14452 KB Output is correct
40 Correct 8 ms 14544 KB Output is correct
41 Correct 11 ms 14560 KB Output is correct
42 Correct 8 ms 14544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 618 ms 20680 KB Output is correct
2 Correct 942 ms 26944 KB Output is correct
3 Correct 947 ms 26972 KB Output is correct
4 Correct 770 ms 26744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 618 ms 20680 KB Output is correct
2 Correct 942 ms 26944 KB Output is correct
3 Correct 947 ms 26972 KB Output is correct
4 Correct 770 ms 26744 KB Output is correct
5 Correct 778 ms 20800 KB Output is correct
6 Correct 851 ms 27340 KB Output is correct
7 Correct 1101 ms 27188 KB Output is correct
8 Correct 803 ms 26948 KB Output is correct
9 Correct 433 ms 14800 KB Output is correct
10 Correct 953 ms 15232 KB Output is correct
11 Correct 833 ms 15184 KB Output is correct
12 Correct 792 ms 15184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14416 KB Output is correct
2 Correct 8 ms 14416 KB Output is correct
3 Correct 8 ms 14516 KB Output is correct
4 Correct 7 ms 14416 KB Output is correct
5 Correct 8 ms 14416 KB Output is correct
6 Correct 8 ms 14544 KB Output is correct
7 Correct 9 ms 14504 KB Output is correct
8 Correct 8 ms 14604 KB Output is correct
9 Correct 8 ms 14544 KB Output is correct
10 Correct 8 ms 14544 KB Output is correct
11 Correct 8 ms 14544 KB Output is correct
12 Correct 8 ms 14532 KB Output is correct
13 Correct 618 ms 20680 KB Output is correct
14 Correct 942 ms 26944 KB Output is correct
15 Correct 947 ms 26972 KB Output is correct
16 Correct 770 ms 26744 KB Output is correct
17 Correct 778 ms 20800 KB Output is correct
18 Correct 851 ms 27340 KB Output is correct
19 Correct 1101 ms 27188 KB Output is correct
20 Correct 803 ms 26948 KB Output is correct
21 Correct 433 ms 14800 KB Output is correct
22 Correct 953 ms 15232 KB Output is correct
23 Correct 833 ms 15184 KB Output is correct
24 Correct 792 ms 15184 KB Output is correct
25 Correct 1079 ms 35192 KB Output is correct
26 Correct 1116 ms 35452 KB Output is correct
27 Correct 1121 ms 35456 KB Output is correct
28 Correct 878 ms 34632 KB Output is correct
29 Correct 1051 ms 40136 KB Output is correct
30 Correct 891 ms 39220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14416 KB Output is correct
2 Correct 8 ms 14416 KB Output is correct
3 Correct 7 ms 14416 KB Output is correct
4 Correct 8 ms 14416 KB Output is correct
5 Correct 7 ms 14416 KB Output is correct
6 Correct 7 ms 14416 KB Output is correct
7 Correct 8 ms 14416 KB Output is correct
8 Correct 7 ms 14532 KB Output is correct
9 Correct 7 ms 14416 KB Output is correct
10 Correct 8 ms 14416 KB Output is correct
11 Correct 8 ms 14516 KB Output is correct
12 Correct 7 ms 14416 KB Output is correct
13 Correct 8 ms 14416 KB Output is correct
14 Correct 8 ms 14544 KB Output is correct
15 Correct 9 ms 14504 KB Output is correct
16 Correct 8 ms 14604 KB Output is correct
17 Correct 8 ms 14544 KB Output is correct
18 Correct 8 ms 14544 KB Output is correct
19 Correct 8 ms 14544 KB Output is correct
20 Correct 8 ms 14532 KB Output is correct
21 Correct 8 ms 14604 KB Output is correct
22 Correct 7 ms 14544 KB Output is correct
23 Correct 8 ms 14568 KB Output is correct
24 Correct 8 ms 14544 KB Output is correct
25 Correct 8 ms 14640 KB Output is correct
26 Correct 8 ms 14544 KB Output is correct
27 Correct 8 ms 14544 KB Output is correct
28 Correct 8 ms 14544 KB Output is correct
29 Correct 8 ms 14416 KB Output is correct
30 Correct 8 ms 14416 KB Output is correct
31 Correct 8 ms 14544 KB Output is correct
32 Correct 8 ms 14544 KB Output is correct
33 Correct 8 ms 14576 KB Output is correct
34 Correct 8 ms 14544 KB Output is correct
35 Correct 8 ms 14416 KB Output is correct
36 Correct 8 ms 14672 KB Output is correct
37 Correct 10 ms 14672 KB Output is correct
38 Correct 8 ms 14672 KB Output is correct
39 Correct 7 ms 14452 KB Output is correct
40 Correct 8 ms 14544 KB Output is correct
41 Correct 11 ms 14560 KB Output is correct
42 Correct 8 ms 14544 KB Output is correct
43 Correct 534 ms 15056 KB Output is correct
44 Correct 725 ms 15184 KB Output is correct
45 Correct 853 ms 15056 KB Output is correct
46 Correct 888 ms 15568 KB Output is correct
47 Correct 850 ms 15568 KB Output is correct
48 Correct 686 ms 15568 KB Output is correct
49 Correct 598 ms 15568 KB Output is correct
50 Correct 773 ms 15440 KB Output is correct
51 Correct 937 ms 15100 KB Output is correct
52 Correct 981 ms 15208 KB Output is correct
53 Correct 745 ms 15252 KB Output is correct
54 Correct 867 ms 15496 KB Output is correct
55 Correct 875 ms 15380 KB Output is correct
56 Correct 893 ms 15184 KB Output is correct
57 Correct 830 ms 15100 KB Output is correct
58 Correct 661 ms 15720 KB Output is correct
59 Correct 902 ms 16072 KB Output is correct
60 Correct 721 ms 15916 KB Output is correct
61 Correct 865 ms 15216 KB Output is correct
62 Correct 975 ms 14928 KB Output is correct
63 Correct 800 ms 14928 KB Output is correct
64 Correct 853 ms 15056 KB Output is correct
65 Correct 390 ms 14832 KB Output is correct
66 Correct 607 ms 15184 KB Output is correct
67 Correct 816 ms 15228 KB Output is correct
68 Correct 874 ms 15184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14416 KB Output is correct
2 Correct 8 ms 14416 KB Output is correct
3 Correct 7 ms 14416 KB Output is correct
4 Correct 8 ms 14416 KB Output is correct
5 Correct 7 ms 14416 KB Output is correct
6 Correct 7 ms 14416 KB Output is correct
7 Correct 8 ms 14416 KB Output is correct
8 Correct 7 ms 14532 KB Output is correct
9 Correct 7 ms 14416 KB Output is correct
10 Correct 8 ms 14416 KB Output is correct
11 Correct 8 ms 14516 KB Output is correct
12 Correct 7 ms 14416 KB Output is correct
13 Correct 8 ms 14416 KB Output is correct
14 Correct 8 ms 14544 KB Output is correct
15 Correct 9 ms 14504 KB Output is correct
16 Correct 8 ms 14604 KB Output is correct
17 Correct 8 ms 14544 KB Output is correct
18 Correct 8 ms 14544 KB Output is correct
19 Correct 8 ms 14544 KB Output is correct
20 Correct 8 ms 14532 KB Output is correct
21 Correct 8 ms 14604 KB Output is correct
22 Correct 7 ms 14544 KB Output is correct
23 Correct 8 ms 14568 KB Output is correct
24 Correct 8 ms 14544 KB Output is correct
25 Correct 8 ms 14640 KB Output is correct
26 Correct 8 ms 14544 KB Output is correct
27 Correct 8 ms 14544 KB Output is correct
28 Correct 8 ms 14544 KB Output is correct
29 Correct 8 ms 14416 KB Output is correct
30 Correct 8 ms 14416 KB Output is correct
31 Correct 8 ms 14544 KB Output is correct
32 Correct 8 ms 14544 KB Output is correct
33 Correct 8 ms 14576 KB Output is correct
34 Correct 8 ms 14544 KB Output is correct
35 Correct 8 ms 14416 KB Output is correct
36 Correct 8 ms 14672 KB Output is correct
37 Correct 10 ms 14672 KB Output is correct
38 Correct 8 ms 14672 KB Output is correct
39 Correct 7 ms 14452 KB Output is correct
40 Correct 8 ms 14544 KB Output is correct
41 Correct 11 ms 14560 KB Output is correct
42 Correct 8 ms 14544 KB Output is correct
43 Correct 618 ms 20680 KB Output is correct
44 Correct 942 ms 26944 KB Output is correct
45 Correct 947 ms 26972 KB Output is correct
46 Correct 770 ms 26744 KB Output is correct
47 Correct 778 ms 20800 KB Output is correct
48 Correct 851 ms 27340 KB Output is correct
49 Correct 1101 ms 27188 KB Output is correct
50 Correct 803 ms 26948 KB Output is correct
51 Correct 433 ms 14800 KB Output is correct
52 Correct 953 ms 15232 KB Output is correct
53 Correct 833 ms 15184 KB Output is correct
54 Correct 792 ms 15184 KB Output is correct
55 Correct 1079 ms 35192 KB Output is correct
56 Correct 1116 ms 35452 KB Output is correct
57 Correct 1121 ms 35456 KB Output is correct
58 Correct 878 ms 34632 KB Output is correct
59 Correct 1051 ms 40136 KB Output is correct
60 Correct 891 ms 39220 KB Output is correct
61 Correct 534 ms 15056 KB Output is correct
62 Correct 725 ms 15184 KB Output is correct
63 Correct 853 ms 15056 KB Output is correct
64 Correct 888 ms 15568 KB Output is correct
65 Correct 850 ms 15568 KB Output is correct
66 Correct 686 ms 15568 KB Output is correct
67 Correct 598 ms 15568 KB Output is correct
68 Correct 773 ms 15440 KB Output is correct
69 Correct 937 ms 15100 KB Output is correct
70 Correct 981 ms 15208 KB Output is correct
71 Correct 745 ms 15252 KB Output is correct
72 Correct 867 ms 15496 KB Output is correct
73 Correct 875 ms 15380 KB Output is correct
74 Correct 893 ms 15184 KB Output is correct
75 Correct 830 ms 15100 KB Output is correct
76 Correct 661 ms 15720 KB Output is correct
77 Correct 902 ms 16072 KB Output is correct
78 Correct 721 ms 15916 KB Output is correct
79 Correct 865 ms 15216 KB Output is correct
80 Correct 975 ms 14928 KB Output is correct
81 Correct 800 ms 14928 KB Output is correct
82 Correct 853 ms 15056 KB Output is correct
83 Correct 390 ms 14832 KB Output is correct
84 Correct 607 ms 15184 KB Output is correct
85 Correct 816 ms 15228 KB Output is correct
86 Correct 874 ms 15184 KB Output is correct
87 Correct 8 ms 14416 KB Output is correct
88 Correct 682 ms 35180 KB Output is correct
89 Correct 1136 ms 28224 KB Output is correct
90 Correct 1117 ms 27960 KB Output is correct
91 Correct 1092 ms 36280 KB Output is correct
92 Correct 1058 ms 36884 KB Output is correct
93 Correct 1111 ms 36800 KB Output is correct
94 Correct 1102 ms 36856 KB Output is correct
95 Correct 1069 ms 35888 KB Output is correct
96 Correct 933 ms 25196 KB Output is correct
97 Correct 1075 ms 26584 KB Output is correct
98 Correct 900 ms 31204 KB Output is correct
99 Correct 1034 ms 35512 KB Output is correct
100 Correct 957 ms 31144 KB Output is correct
101 Correct 1065 ms 29564 KB Output is correct
102 Correct 1025 ms 26024 KB Output is correct
103 Correct 1014 ms 40164 KB Output is correct
104 Correct 1102 ms 42972 KB Output is correct
105 Correct 989 ms 42008 KB Output is correct
106 Correct 1044 ms 29252 KB Output is correct
107 Correct 928 ms 28272 KB Output is correct
108 Correct 957 ms 27948 KB Output is correct
109 Correct 781 ms 26560 KB Output is correct