Submission #668690

# Submission time Handle Problem Language Result Execution time Memory
668690 2022-12-04T13:30:06 Z hoanghq2004 Digital Circuit (IOI22_circuit) C++17
100 / 100
1092 ms 33056 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include "circuit.h"

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;

const int N = 6e5 + 10;
const int mod = 1e9 + 2022;

int n, m;
vector <int> g[N];
int sz[N];

void dfs(int u) {
    sz[u] = (u >= n ? 1 : g[u].size());
    for (auto v: g[u]) {
        dfs(v);
        sz[u] = 1LL * sz[u] * sz[v] % mod;
    }
}

int c[N], prf[N], suf[N];
int mul[N];

void fix(int u) {
    int num = 0;
    for (auto v: g[u]) c[++num] = v;
    prf[0] = suf[num + 1] = 1;
    for (int i = 1; i <= num; ++i) prf[i] = 1LL * prf[i - 1] * sz[c[i]] % mod;
    for (int i = num; i >= 1; --i) suf[i] = 1LL * suf[i + 1] * sz[c[i]] % mod;
    for (int i = 1; i <= num; ++i)
        mul[c[i]] = 1LL * prf[i - 1] * suf[i + 1] % mod;
    for (auto v: g[u]) fix(v);
}

int ans = 0;

void add(int& a, int b) {
    a += b;
    if (a >= mod) a -= mod;
    if (a < 0) a += mod;
}

int st[N * 4];
int lz[N * 4];

void build(int id, int L, int R) {
    if (R - L == 1) {
        st[id] = mul[L + n];
        return;
    }
    int mid = L + R >> 1;
    build(id * 2, L, mid);
    build(id * 2 + 1, mid, R);
    st[id] = (st[id * 2] + st[id * 2 + 1]) % mod;
}

void push(int id, int val) {
    if (!val) return;
    st[id] = (mod - st[id]) % mod;
    lz[id] ^= 1;
}

void update(int id, int L, int R, int u, int v) {
    if (u >= R || L >= v) return;
    if (u <= L && R <= v) {
        push(id, 1);
        return;
    }
    push(id * 2, lz[id]);
    push(id * 2 + 1, lz[id]);
    lz[id] = 0;
    int mid = L + R >> 1;
    update(id * 2, L, mid, u, v);
    update(id * 2 + 1, mid, R, u, v);
    st[id] = (st[id * 2] + st[id * 2 + 1]) % mod;
}

int get(int id, int L, int R, int u, int v) {
    if (u >= R || L >= v) return 0;
    if (u <= L && R <= v) return st[id];
    push(id * 2, lz[id]);
    push(id * 2 + 1, lz[id]);
    lz[id] = 0;
    int mid = L + R >> 1;
    return (get(id * 2, L, mid, u, v) + get(id * 2 + 1, mid, R, u, v)) % mod;
}

void init(int N, int M, vector<int> P, vector<int> A) {
    n = N, m = M;
    for (int i = 1; i < n + m; ++i) g[P[i]].push_back(i);
    mul[0] = 1;
    dfs(0);
    fix(0);
    for (int i = 1; i < n + m; ++i) mul[i] = 1LL * mul[i] * mul[P[i]] % mod;
    build(1, 0, m);
    for (int i = 0; i < m; ++i) if (A[i]) add(ans, get(1, 0, m, i, i + 1)), update(1, 0, m, i, i + 1);
}


int count_ways(int L, int R) {
    ++R;
    add(ans, get(1, 0, m, L - n, R - n));
    update(1, 0, m, L - n, R - n);
    return ans;
}

//
//int main() {
//  int N, M, Q;
//  assert(3 == scanf("%d %d %d", &N, &M, &Q));
//  std::vector<int> P(N + M), A(M);
//  for (int i = 0; i < N + M; ++i) {
//    assert(1 == scanf("%d", &P[i]));
//  }
//  for (int j = 0; j < M; ++j) {
//    assert(1 == scanf("%d", &A[j]));
//  }
//  init(N, M, P, A);
//
//  for (int i = 0; i < Q; ++i) {
//    int L, R;
//    assert(2 == scanf("%d %d", &L, &R));
//    printf("%d\n", count_ways(L, R));
//  }
//  return 0;
//}

Compilation message

circuit.cpp: In function 'void build(int, int, int)':
circuit.cpp:59:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   59 |     int mid = L + R >> 1;
      |               ~~^~~
circuit.cpp: In function 'void update(int, int, int, int, int)':
circuit.cpp:80:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   80 |     int mid = L + R >> 1;
      |               ~~^~~
circuit.cpp: In function 'int get(int, int, int, int, int)':
circuit.cpp:92:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   92 |     int mid = L + R >> 1;
      |               ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14416 KB Output is correct
2 Correct 8 ms 14368 KB Output is correct
3 Correct 8 ms 14416 KB Output is correct
4 Correct 9 ms 14384 KB Output is correct
5 Correct 9 ms 14388 KB Output is correct
6 Correct 9 ms 14492 KB Output is correct
7 Correct 8 ms 14416 KB Output is correct
8 Correct 10 ms 14416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14416 KB Output is correct
2 Correct 8 ms 14416 KB Output is correct
3 Correct 8 ms 14416 KB Output is correct
4 Correct 8 ms 14416 KB Output is correct
5 Correct 9 ms 14456 KB Output is correct
6 Correct 9 ms 14416 KB Output is correct
7 Correct 8 ms 14416 KB Output is correct
8 Correct 8 ms 14492 KB Output is correct
9 Correct 9 ms 14416 KB Output is correct
10 Correct 11 ms 14544 KB Output is correct
11 Correct 8 ms 14544 KB Output is correct
12 Correct 10 ms 14416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14416 KB Output is correct
2 Correct 8 ms 14368 KB Output is correct
3 Correct 8 ms 14416 KB Output is correct
4 Correct 9 ms 14384 KB Output is correct
5 Correct 9 ms 14388 KB Output is correct
6 Correct 9 ms 14492 KB Output is correct
7 Correct 8 ms 14416 KB Output is correct
8 Correct 10 ms 14416 KB Output is correct
9 Correct 8 ms 14416 KB Output is correct
10 Correct 8 ms 14416 KB Output is correct
11 Correct 8 ms 14416 KB Output is correct
12 Correct 8 ms 14416 KB Output is correct
13 Correct 9 ms 14456 KB Output is correct
14 Correct 9 ms 14416 KB Output is correct
15 Correct 8 ms 14416 KB Output is correct
16 Correct 8 ms 14492 KB Output is correct
17 Correct 9 ms 14416 KB Output is correct
18 Correct 11 ms 14544 KB Output is correct
19 Correct 8 ms 14544 KB Output is correct
20 Correct 10 ms 14416 KB Output is correct
21 Correct 8 ms 14436 KB Output is correct
22 Correct 9 ms 14408 KB Output is correct
23 Correct 11 ms 14472 KB Output is correct
24 Correct 9 ms 14420 KB Output is correct
25 Correct 8 ms 14416 KB Output is correct
26 Correct 8 ms 14432 KB Output is correct
27 Correct 9 ms 14460 KB Output is correct
28 Correct 9 ms 14420 KB Output is correct
29 Correct 8 ms 14416 KB Output is correct
30 Correct 9 ms 14492 KB Output is correct
31 Correct 8 ms 14460 KB Output is correct
32 Correct 9 ms 14416 KB Output is correct
33 Correct 8 ms 14416 KB Output is correct
34 Correct 10 ms 14400 KB Output is correct
35 Correct 9 ms 14416 KB Output is correct
36 Correct 9 ms 14492 KB Output is correct
37 Correct 8 ms 14544 KB Output is correct
38 Correct 9 ms 14628 KB Output is correct
39 Correct 8 ms 14416 KB Output is correct
40 Correct 8 ms 14416 KB Output is correct
41 Correct 8 ms 14416 KB Output is correct
42 Correct 8 ms 14416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 462 ms 17224 KB Output is correct
2 Correct 1016 ms 20008 KB Output is correct
3 Correct 843 ms 20040 KB Output is correct
4 Correct 841 ms 19504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 462 ms 17224 KB Output is correct
2 Correct 1016 ms 20008 KB Output is correct
3 Correct 843 ms 20040 KB Output is correct
4 Correct 841 ms 19504 KB Output is correct
5 Correct 874 ms 17208 KB Output is correct
6 Correct 888 ms 20012 KB Output is correct
7 Correct 751 ms 20024 KB Output is correct
8 Correct 788 ms 20040 KB Output is correct
9 Correct 476 ms 14544 KB Output is correct
10 Correct 952 ms 14672 KB Output is correct
11 Correct 824 ms 14672 KB Output is correct
12 Correct 904 ms 14672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14416 KB Output is correct
2 Correct 8 ms 14416 KB Output is correct
3 Correct 8 ms 14416 KB Output is correct
4 Correct 8 ms 14416 KB Output is correct
5 Correct 9 ms 14456 KB Output is correct
6 Correct 9 ms 14416 KB Output is correct
7 Correct 8 ms 14416 KB Output is correct
8 Correct 8 ms 14492 KB Output is correct
9 Correct 9 ms 14416 KB Output is correct
10 Correct 11 ms 14544 KB Output is correct
11 Correct 8 ms 14544 KB Output is correct
12 Correct 10 ms 14416 KB Output is correct
13 Correct 462 ms 17224 KB Output is correct
14 Correct 1016 ms 20008 KB Output is correct
15 Correct 843 ms 20040 KB Output is correct
16 Correct 841 ms 19504 KB Output is correct
17 Correct 874 ms 17208 KB Output is correct
18 Correct 888 ms 20012 KB Output is correct
19 Correct 751 ms 20024 KB Output is correct
20 Correct 788 ms 20040 KB Output is correct
21 Correct 476 ms 14544 KB Output is correct
22 Correct 952 ms 14672 KB Output is correct
23 Correct 824 ms 14672 KB Output is correct
24 Correct 904 ms 14672 KB Output is correct
25 Correct 1020 ms 23368 KB Output is correct
26 Correct 1084 ms 23496 KB Output is correct
27 Correct 1022 ms 23480 KB Output is correct
28 Correct 568 ms 23628 KB Output is correct
29 Correct 1010 ms 31280 KB Output is correct
30 Correct 928 ms 31304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14416 KB Output is correct
2 Correct 8 ms 14368 KB Output is correct
3 Correct 8 ms 14416 KB Output is correct
4 Correct 9 ms 14384 KB Output is correct
5 Correct 9 ms 14388 KB Output is correct
6 Correct 9 ms 14492 KB Output is correct
7 Correct 8 ms 14416 KB Output is correct
8 Correct 10 ms 14416 KB Output is correct
9 Correct 8 ms 14416 KB Output is correct
10 Correct 8 ms 14416 KB Output is correct
11 Correct 8 ms 14416 KB Output is correct
12 Correct 8 ms 14416 KB Output is correct
13 Correct 9 ms 14456 KB Output is correct
14 Correct 9 ms 14416 KB Output is correct
15 Correct 8 ms 14416 KB Output is correct
16 Correct 8 ms 14492 KB Output is correct
17 Correct 9 ms 14416 KB Output is correct
18 Correct 11 ms 14544 KB Output is correct
19 Correct 8 ms 14544 KB Output is correct
20 Correct 10 ms 14416 KB Output is correct
21 Correct 8 ms 14436 KB Output is correct
22 Correct 9 ms 14408 KB Output is correct
23 Correct 11 ms 14472 KB Output is correct
24 Correct 9 ms 14420 KB Output is correct
25 Correct 8 ms 14416 KB Output is correct
26 Correct 8 ms 14432 KB Output is correct
27 Correct 9 ms 14460 KB Output is correct
28 Correct 9 ms 14420 KB Output is correct
29 Correct 8 ms 14416 KB Output is correct
30 Correct 9 ms 14492 KB Output is correct
31 Correct 8 ms 14460 KB Output is correct
32 Correct 9 ms 14416 KB Output is correct
33 Correct 8 ms 14416 KB Output is correct
34 Correct 10 ms 14400 KB Output is correct
35 Correct 9 ms 14416 KB Output is correct
36 Correct 9 ms 14492 KB Output is correct
37 Correct 8 ms 14544 KB Output is correct
38 Correct 9 ms 14628 KB Output is correct
39 Correct 8 ms 14416 KB Output is correct
40 Correct 8 ms 14416 KB Output is correct
41 Correct 8 ms 14416 KB Output is correct
42 Correct 8 ms 14416 KB Output is correct
43 Correct 559 ms 14672 KB Output is correct
44 Correct 875 ms 14756 KB Output is correct
45 Correct 793 ms 14672 KB Output is correct
46 Correct 900 ms 14928 KB Output is correct
47 Correct 820 ms 14928 KB Output is correct
48 Correct 898 ms 14800 KB Output is correct
49 Correct 1024 ms 14924 KB Output is correct
50 Correct 915 ms 14928 KB Output is correct
51 Correct 799 ms 14672 KB Output is correct
52 Correct 891 ms 14800 KB Output is correct
53 Correct 689 ms 15056 KB Output is correct
54 Correct 998 ms 14800 KB Output is correct
55 Correct 837 ms 14800 KB Output is correct
56 Correct 933 ms 14672 KB Output is correct
57 Correct 654 ms 14684 KB Output is correct
58 Correct 978 ms 15184 KB Output is correct
59 Correct 930 ms 15420 KB Output is correct
60 Correct 871 ms 15412 KB Output is correct
61 Correct 847 ms 14800 KB Output is correct
62 Correct 900 ms 14672 KB Output is correct
63 Correct 988 ms 14672 KB Output is correct
64 Correct 846 ms 14672 KB Output is correct
65 Correct 502 ms 14544 KB Output is correct
66 Correct 899 ms 14672 KB Output is correct
67 Correct 802 ms 14672 KB Output is correct
68 Correct 816 ms 14784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14416 KB Output is correct
2 Correct 8 ms 14368 KB Output is correct
3 Correct 8 ms 14416 KB Output is correct
4 Correct 9 ms 14384 KB Output is correct
5 Correct 9 ms 14388 KB Output is correct
6 Correct 9 ms 14492 KB Output is correct
7 Correct 8 ms 14416 KB Output is correct
8 Correct 10 ms 14416 KB Output is correct
9 Correct 8 ms 14416 KB Output is correct
10 Correct 8 ms 14416 KB Output is correct
11 Correct 8 ms 14416 KB Output is correct
12 Correct 8 ms 14416 KB Output is correct
13 Correct 9 ms 14456 KB Output is correct
14 Correct 9 ms 14416 KB Output is correct
15 Correct 8 ms 14416 KB Output is correct
16 Correct 8 ms 14492 KB Output is correct
17 Correct 9 ms 14416 KB Output is correct
18 Correct 11 ms 14544 KB Output is correct
19 Correct 8 ms 14544 KB Output is correct
20 Correct 10 ms 14416 KB Output is correct
21 Correct 8 ms 14436 KB Output is correct
22 Correct 9 ms 14408 KB Output is correct
23 Correct 11 ms 14472 KB Output is correct
24 Correct 9 ms 14420 KB Output is correct
25 Correct 8 ms 14416 KB Output is correct
26 Correct 8 ms 14432 KB Output is correct
27 Correct 9 ms 14460 KB Output is correct
28 Correct 9 ms 14420 KB Output is correct
29 Correct 8 ms 14416 KB Output is correct
30 Correct 9 ms 14492 KB Output is correct
31 Correct 8 ms 14460 KB Output is correct
32 Correct 9 ms 14416 KB Output is correct
33 Correct 8 ms 14416 KB Output is correct
34 Correct 10 ms 14400 KB Output is correct
35 Correct 9 ms 14416 KB Output is correct
36 Correct 9 ms 14492 KB Output is correct
37 Correct 8 ms 14544 KB Output is correct
38 Correct 9 ms 14628 KB Output is correct
39 Correct 8 ms 14416 KB Output is correct
40 Correct 8 ms 14416 KB Output is correct
41 Correct 8 ms 14416 KB Output is correct
42 Correct 8 ms 14416 KB Output is correct
43 Correct 462 ms 17224 KB Output is correct
44 Correct 1016 ms 20008 KB Output is correct
45 Correct 843 ms 20040 KB Output is correct
46 Correct 841 ms 19504 KB Output is correct
47 Correct 874 ms 17208 KB Output is correct
48 Correct 888 ms 20012 KB Output is correct
49 Correct 751 ms 20024 KB Output is correct
50 Correct 788 ms 20040 KB Output is correct
51 Correct 476 ms 14544 KB Output is correct
52 Correct 952 ms 14672 KB Output is correct
53 Correct 824 ms 14672 KB Output is correct
54 Correct 904 ms 14672 KB Output is correct
55 Correct 1020 ms 23368 KB Output is correct
56 Correct 1084 ms 23496 KB Output is correct
57 Correct 1022 ms 23480 KB Output is correct
58 Correct 568 ms 23628 KB Output is correct
59 Correct 1010 ms 31280 KB Output is correct
60 Correct 928 ms 31304 KB Output is correct
61 Correct 559 ms 14672 KB Output is correct
62 Correct 875 ms 14756 KB Output is correct
63 Correct 793 ms 14672 KB Output is correct
64 Correct 900 ms 14928 KB Output is correct
65 Correct 820 ms 14928 KB Output is correct
66 Correct 898 ms 14800 KB Output is correct
67 Correct 1024 ms 14924 KB Output is correct
68 Correct 915 ms 14928 KB Output is correct
69 Correct 799 ms 14672 KB Output is correct
70 Correct 891 ms 14800 KB Output is correct
71 Correct 689 ms 15056 KB Output is correct
72 Correct 998 ms 14800 KB Output is correct
73 Correct 837 ms 14800 KB Output is correct
74 Correct 933 ms 14672 KB Output is correct
75 Correct 654 ms 14684 KB Output is correct
76 Correct 978 ms 15184 KB Output is correct
77 Correct 930 ms 15420 KB Output is correct
78 Correct 871 ms 15412 KB Output is correct
79 Correct 847 ms 14800 KB Output is correct
80 Correct 900 ms 14672 KB Output is correct
81 Correct 988 ms 14672 KB Output is correct
82 Correct 846 ms 14672 KB Output is correct
83 Correct 502 ms 14544 KB Output is correct
84 Correct 899 ms 14672 KB Output is correct
85 Correct 802 ms 14672 KB Output is correct
86 Correct 816 ms 14784 KB Output is correct
87 Correct 7 ms 14416 KB Output is correct
88 Correct 675 ms 22856 KB Output is correct
89 Correct 1055 ms 20424 KB Output is correct
90 Correct 919 ms 19960 KB Output is correct
91 Correct 953 ms 23588 KB Output is correct
92 Correct 1043 ms 23648 KB Output is correct
93 Correct 1000 ms 22600 KB Output is correct
94 Correct 644 ms 23584 KB Output is correct
95 Correct 999 ms 23600 KB Output is correct
96 Correct 966 ms 20416 KB Output is correct
97 Correct 838 ms 20520 KB Output is correct
98 Correct 816 ms 26960 KB Output is correct
99 Correct 992 ms 23512 KB Output is correct
100 Correct 1037 ms 21128 KB Output is correct
101 Correct 1092 ms 20416 KB Output is correct
102 Correct 1042 ms 19336 KB Output is correct
103 Correct 996 ms 31284 KB Output is correct
104 Correct 970 ms 32968 KB Output is correct
105 Correct 903 ms 33056 KB Output is correct
106 Correct 1006 ms 21676 KB Output is correct
107 Correct 806 ms 19904 KB Output is correct
108 Correct 955 ms 19840 KB Output is correct
109 Correct 892 ms 19400 KB Output is correct