Submission #1024792

# Submission time Handle Problem Language Result Execution time Memory
1024792 2024-07-16T10:22:31 Z aibark Digital Circuit (IOI22_circuit) C++17
100 / 100
833 ms 32096 KB
#include "circuit.h"

#include <bits/stdc++.h>
using namespace std;

const int NN = (int)2e5 + 123, mod = (int)1e9 + 2022;

int n, m;
vector<int> p, a;
vector<int> g[NN];
long long sz[NN], ans[NN];

void dfs(int v) {
  sz[v] = max(1, (int)g[v].size());
  for (int to : g[v]) {
    dfs(to);
    sz[v] = sz[v] * sz[to] % mod;
  }
}

void dfs1(int v) {
  long long A = ans[v];
  for (int to : g[v]) {
    ans[to] = A;
    A = A * sz[to] % mod;
  }
  A = 1;
  reverse(g[v].begin(), g[v].end());
  for (int to : g[v]) {
    ans[to] = ans[to] * A % mod;
    A = A * sz[to] % mod;
  }

  for (int to : g[v])
    dfs1(to);
}

long long t[2][4 * NN];
int add[4 * NN];

void build(int v, int tl, int tr) {
  if (tl == tr) {
    t[1][v] = ans[tl + n];
    t[0][v] = 0;
    if (a[tl] == 0)
      swap(t[1][v], t[0][v]);
    return;
  }
  int tm = (tl + tr) >> 1;
  build(v + v, tl, tm);
  build(v + v + 1, tm + 1, tr);
  t[1][v] = t[1][v + v] + t[1][v + v + 1];
  t[0][v] = t[0][v + v] + t[0][v + v + 1];
}

void push(int v) {
  if (add[v]) {
    add[v + v] ^= 1;
    add[v + v + 1] ^= 1;
    swap(t[0][v + v], t[1][v + v]);
    swap(t[0][v + v + 1], t[1][v + v + 1]);
    add[v] = 0;
  }
}

void upd(int v, int tl, int tr, int l, int r) {
  if (tr < l || r < tl)
    return;
  if (l <= tl && tr <= r) {
    swap(t[0][v], t[1][v]);
    add[v] ^= 1;
    return;
  }
  push(v);
  int tm = (tl + tr) >> 1;
  upd(v + v, tl, tm, l, r);
  upd(v + v + 1, tm + 1, tr, l, r);
  t[1][v] = t[1][v + v] + t[1][v + v + 1];
  t[0][v] = t[0][v + v] + t[0][v + v + 1];
}

void init(int N, int M, vector<int> P, vector<int> A) {
  n = N, m = M;
  p = P, a = A;
  for (int i = 1; i < n + m; i++)
    g[p[i]].push_back(i);
  dfs(0);
  ans[0] = 1;
  dfs1(0);
  build(1, 0, m - 1);
}

int count_ways(int L, int R) {
  // long long ansa = 0;
  // for (int i = L; i <= R; i++)
  //   a[i - n] ^= 1;
  // for (int i = n; i < n + m; i++) {
  //   // cout << i << " " << a[i - n] << " " << ans[i] << endl;
  //   ansa = ansa + a[i - n] * ans[i];
  // }
  // return ansa;
  upd(1, 0, m - 1, L - n, R - n);
  return t[1][1] % mod;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13400 KB Output is correct
2 Correct 2 ms 13400 KB Output is correct
3 Correct 2 ms 13400 KB Output is correct
4 Correct 2 ms 13400 KB Output is correct
5 Correct 2 ms 13400 KB Output is correct
6 Correct 2 ms 13400 KB Output is correct
7 Correct 2 ms 13400 KB Output is correct
8 Correct 2 ms 13400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13400 KB Output is correct
2 Correct 2 ms 13400 KB Output is correct
3 Correct 2 ms 13400 KB Output is correct
4 Correct 2 ms 13400 KB Output is correct
5 Correct 2 ms 13400 KB Output is correct
6 Correct 3 ms 13400 KB Output is correct
7 Correct 3 ms 13400 KB Output is correct
8 Correct 2 ms 13400 KB Output is correct
9 Correct 2 ms 13400 KB Output is correct
10 Correct 2 ms 13400 KB Output is correct
11 Correct 2 ms 13400 KB Output is correct
12 Correct 2 ms 13400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13400 KB Output is correct
2 Correct 2 ms 13400 KB Output is correct
3 Correct 2 ms 13400 KB Output is correct
4 Correct 2 ms 13400 KB Output is correct
5 Correct 2 ms 13400 KB Output is correct
6 Correct 2 ms 13400 KB Output is correct
7 Correct 2 ms 13400 KB Output is correct
8 Correct 2 ms 13400 KB Output is correct
9 Correct 2 ms 13400 KB Output is correct
10 Correct 2 ms 13400 KB Output is correct
11 Correct 2 ms 13400 KB Output is correct
12 Correct 2 ms 13400 KB Output is correct
13 Correct 2 ms 13400 KB Output is correct
14 Correct 3 ms 13400 KB Output is correct
15 Correct 3 ms 13400 KB Output is correct
16 Correct 2 ms 13400 KB Output is correct
17 Correct 2 ms 13400 KB Output is correct
18 Correct 2 ms 13400 KB Output is correct
19 Correct 2 ms 13400 KB Output is correct
20 Correct 2 ms 13400 KB Output is correct
21 Correct 3 ms 13400 KB Output is correct
22 Correct 2 ms 13400 KB Output is correct
23 Correct 2 ms 13400 KB Output is correct
24 Correct 3 ms 13488 KB Output is correct
25 Correct 2 ms 13400 KB Output is correct
26 Correct 2 ms 13400 KB Output is correct
27 Correct 2 ms 13400 KB Output is correct
28 Correct 2 ms 13400 KB Output is correct
29 Correct 2 ms 13400 KB Output is correct
30 Correct 3 ms 13400 KB Output is correct
31 Correct 2 ms 13400 KB Output is correct
32 Correct 3 ms 13400 KB Output is correct
33 Correct 4 ms 13400 KB Output is correct
34 Correct 2 ms 13400 KB Output is correct
35 Correct 2 ms 13400 KB Output is correct
36 Correct 2 ms 13400 KB Output is correct
37 Correct 3 ms 13400 KB Output is correct
38 Correct 2 ms 13400 KB Output is correct
39 Correct 3 ms 13400 KB Output is correct
40 Correct 2 ms 13400 KB Output is correct
41 Correct 2 ms 13652 KB Output is correct
42 Correct 2 ms 13400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 422 ms 17400 KB Output is correct
2 Correct 667 ms 17752 KB Output is correct
3 Correct 672 ms 17752 KB Output is correct
4 Correct 637 ms 19148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 422 ms 17400 KB Output is correct
2 Correct 667 ms 17752 KB Output is correct
3 Correct 672 ms 17752 KB Output is correct
4 Correct 637 ms 19148 KB Output is correct
5 Correct 529 ms 17528 KB Output is correct
6 Correct 670 ms 19324 KB Output is correct
7 Correct 715 ms 19320 KB Output is correct
8 Correct 641 ms 19180 KB Output is correct
9 Correct 306 ms 13400 KB Output is correct
10 Correct 708 ms 13656 KB Output is correct
11 Correct 671 ms 13656 KB Output is correct
12 Correct 650 ms 13656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13400 KB Output is correct
2 Correct 2 ms 13400 KB Output is correct
3 Correct 2 ms 13400 KB Output is correct
4 Correct 2 ms 13400 KB Output is correct
5 Correct 2 ms 13400 KB Output is correct
6 Correct 3 ms 13400 KB Output is correct
7 Correct 3 ms 13400 KB Output is correct
8 Correct 2 ms 13400 KB Output is correct
9 Correct 2 ms 13400 KB Output is correct
10 Correct 2 ms 13400 KB Output is correct
11 Correct 2 ms 13400 KB Output is correct
12 Correct 2 ms 13400 KB Output is correct
13 Correct 422 ms 17400 KB Output is correct
14 Correct 667 ms 17752 KB Output is correct
15 Correct 672 ms 17752 KB Output is correct
16 Correct 637 ms 19148 KB Output is correct
17 Correct 529 ms 17528 KB Output is correct
18 Correct 670 ms 19324 KB Output is correct
19 Correct 715 ms 19320 KB Output is correct
20 Correct 641 ms 19180 KB Output is correct
21 Correct 306 ms 13400 KB Output is correct
22 Correct 708 ms 13656 KB Output is correct
23 Correct 671 ms 13656 KB Output is correct
24 Correct 650 ms 13656 KB Output is correct
25 Correct 783 ms 25212 KB Output is correct
26 Correct 716 ms 25316 KB Output is correct
27 Correct 833 ms 25308 KB Output is correct
28 Correct 597 ms 24144 KB Output is correct
29 Correct 716 ms 31568 KB Output is correct
30 Correct 771 ms 30288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13400 KB Output is correct
2 Correct 2 ms 13400 KB Output is correct
3 Correct 2 ms 13400 KB Output is correct
4 Correct 2 ms 13400 KB Output is correct
5 Correct 2 ms 13400 KB Output is correct
6 Correct 2 ms 13400 KB Output is correct
7 Correct 2 ms 13400 KB Output is correct
8 Correct 2 ms 13400 KB Output is correct
9 Correct 2 ms 13400 KB Output is correct
10 Correct 2 ms 13400 KB Output is correct
11 Correct 2 ms 13400 KB Output is correct
12 Correct 2 ms 13400 KB Output is correct
13 Correct 2 ms 13400 KB Output is correct
14 Correct 3 ms 13400 KB Output is correct
15 Correct 3 ms 13400 KB Output is correct
16 Correct 2 ms 13400 KB Output is correct
17 Correct 2 ms 13400 KB Output is correct
18 Correct 2 ms 13400 KB Output is correct
19 Correct 2 ms 13400 KB Output is correct
20 Correct 2 ms 13400 KB Output is correct
21 Correct 3 ms 13400 KB Output is correct
22 Correct 2 ms 13400 KB Output is correct
23 Correct 2 ms 13400 KB Output is correct
24 Correct 3 ms 13488 KB Output is correct
25 Correct 2 ms 13400 KB Output is correct
26 Correct 2 ms 13400 KB Output is correct
27 Correct 2 ms 13400 KB Output is correct
28 Correct 2 ms 13400 KB Output is correct
29 Correct 2 ms 13400 KB Output is correct
30 Correct 3 ms 13400 KB Output is correct
31 Correct 2 ms 13400 KB Output is correct
32 Correct 3 ms 13400 KB Output is correct
33 Correct 4 ms 13400 KB Output is correct
34 Correct 2 ms 13400 KB Output is correct
35 Correct 2 ms 13400 KB Output is correct
36 Correct 2 ms 13400 KB Output is correct
37 Correct 3 ms 13400 KB Output is correct
38 Correct 2 ms 13400 KB Output is correct
39 Correct 3 ms 13400 KB Output is correct
40 Correct 2 ms 13400 KB Output is correct
41 Correct 2 ms 13652 KB Output is correct
42 Correct 2 ms 13400 KB Output is correct
43 Correct 474 ms 13656 KB Output is correct
44 Correct 622 ms 13656 KB Output is correct
45 Correct 689 ms 13656 KB Output is correct
46 Correct 604 ms 13656 KB Output is correct
47 Correct 663 ms 13656 KB Output is correct
48 Correct 669 ms 13656 KB Output is correct
49 Correct 639 ms 13656 KB Output is correct
50 Correct 600 ms 13656 KB Output is correct
51 Correct 625 ms 13656 KB Output is correct
52 Correct 603 ms 13656 KB Output is correct
53 Correct 542 ms 13912 KB Output is correct
54 Correct 653 ms 13656 KB Output is correct
55 Correct 724 ms 13656 KB Output is correct
56 Correct 660 ms 13656 KB Output is correct
57 Correct 694 ms 13656 KB Output is correct
58 Correct 677 ms 14168 KB Output is correct
59 Correct 653 ms 14168 KB Output is correct
60 Correct 639 ms 14168 KB Output is correct
61 Correct 670 ms 13656 KB Output is correct
62 Correct 618 ms 13656 KB Output is correct
63 Correct 603 ms 13656 KB Output is correct
64 Correct 631 ms 13656 KB Output is correct
65 Correct 323 ms 13400 KB Output is correct
66 Correct 673 ms 13656 KB Output is correct
67 Correct 657 ms 13656 KB Output is correct
68 Correct 653 ms 13656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13400 KB Output is correct
2 Correct 2 ms 13400 KB Output is correct
3 Correct 2 ms 13400 KB Output is correct
4 Correct 2 ms 13400 KB Output is correct
5 Correct 2 ms 13400 KB Output is correct
6 Correct 2 ms 13400 KB Output is correct
7 Correct 2 ms 13400 KB Output is correct
8 Correct 2 ms 13400 KB Output is correct
9 Correct 2 ms 13400 KB Output is correct
10 Correct 2 ms 13400 KB Output is correct
11 Correct 2 ms 13400 KB Output is correct
12 Correct 2 ms 13400 KB Output is correct
13 Correct 2 ms 13400 KB Output is correct
14 Correct 3 ms 13400 KB Output is correct
15 Correct 3 ms 13400 KB Output is correct
16 Correct 2 ms 13400 KB Output is correct
17 Correct 2 ms 13400 KB Output is correct
18 Correct 2 ms 13400 KB Output is correct
19 Correct 2 ms 13400 KB Output is correct
20 Correct 2 ms 13400 KB Output is correct
21 Correct 3 ms 13400 KB Output is correct
22 Correct 2 ms 13400 KB Output is correct
23 Correct 2 ms 13400 KB Output is correct
24 Correct 3 ms 13488 KB Output is correct
25 Correct 2 ms 13400 KB Output is correct
26 Correct 2 ms 13400 KB Output is correct
27 Correct 2 ms 13400 KB Output is correct
28 Correct 2 ms 13400 KB Output is correct
29 Correct 2 ms 13400 KB Output is correct
30 Correct 3 ms 13400 KB Output is correct
31 Correct 2 ms 13400 KB Output is correct
32 Correct 3 ms 13400 KB Output is correct
33 Correct 4 ms 13400 KB Output is correct
34 Correct 2 ms 13400 KB Output is correct
35 Correct 2 ms 13400 KB Output is correct
36 Correct 2 ms 13400 KB Output is correct
37 Correct 3 ms 13400 KB Output is correct
38 Correct 2 ms 13400 KB Output is correct
39 Correct 3 ms 13400 KB Output is correct
40 Correct 2 ms 13400 KB Output is correct
41 Correct 2 ms 13652 KB Output is correct
42 Correct 2 ms 13400 KB Output is correct
43 Correct 422 ms 17400 KB Output is correct
44 Correct 667 ms 17752 KB Output is correct
45 Correct 672 ms 17752 KB Output is correct
46 Correct 637 ms 19148 KB Output is correct
47 Correct 529 ms 17528 KB Output is correct
48 Correct 670 ms 19324 KB Output is correct
49 Correct 715 ms 19320 KB Output is correct
50 Correct 641 ms 19180 KB Output is correct
51 Correct 306 ms 13400 KB Output is correct
52 Correct 708 ms 13656 KB Output is correct
53 Correct 671 ms 13656 KB Output is correct
54 Correct 650 ms 13656 KB Output is correct
55 Correct 783 ms 25212 KB Output is correct
56 Correct 716 ms 25316 KB Output is correct
57 Correct 833 ms 25308 KB Output is correct
58 Correct 597 ms 24144 KB Output is correct
59 Correct 716 ms 31568 KB Output is correct
60 Correct 771 ms 30288 KB Output is correct
61 Correct 474 ms 13656 KB Output is correct
62 Correct 622 ms 13656 KB Output is correct
63 Correct 689 ms 13656 KB Output is correct
64 Correct 604 ms 13656 KB Output is correct
65 Correct 663 ms 13656 KB Output is correct
66 Correct 669 ms 13656 KB Output is correct
67 Correct 639 ms 13656 KB Output is correct
68 Correct 600 ms 13656 KB Output is correct
69 Correct 625 ms 13656 KB Output is correct
70 Correct 603 ms 13656 KB Output is correct
71 Correct 542 ms 13912 KB Output is correct
72 Correct 653 ms 13656 KB Output is correct
73 Correct 724 ms 13656 KB Output is correct
74 Correct 660 ms 13656 KB Output is correct
75 Correct 694 ms 13656 KB Output is correct
76 Correct 677 ms 14168 KB Output is correct
77 Correct 653 ms 14168 KB Output is correct
78 Correct 639 ms 14168 KB Output is correct
79 Correct 670 ms 13656 KB Output is correct
80 Correct 618 ms 13656 KB Output is correct
81 Correct 603 ms 13656 KB Output is correct
82 Correct 631 ms 13656 KB Output is correct
83 Correct 323 ms 13400 KB Output is correct
84 Correct 673 ms 13656 KB Output is correct
85 Correct 657 ms 13656 KB Output is correct
86 Correct 653 ms 13656 KB Output is correct
87 Correct 2 ms 13400 KB Output is correct
88 Correct 475 ms 24852 KB Output is correct
89 Correct 689 ms 19572 KB Output is correct
90 Correct 707 ms 19268 KB Output is correct
91 Correct 586 ms 24352 KB Output is correct
92 Correct 605 ms 24220 KB Output is correct
93 Correct 660 ms 25428 KB Output is correct
94 Correct 584 ms 24224 KB Output is correct
95 Correct 616 ms 25272 KB Output is correct
96 Correct 519 ms 20432 KB Output is correct
97 Correct 644 ms 20716 KB Output is correct
98 Correct 586 ms 23896 KB Output is correct
99 Correct 728 ms 24244 KB Output is correct
100 Correct 703 ms 23352 KB Output is correct
101 Correct 665 ms 21688 KB Output is correct
102 Correct 663 ms 21884 KB Output is correct
103 Correct 710 ms 31564 KB Output is correct
104 Correct 696 ms 32096 KB Output is correct
105 Correct 698 ms 31944 KB Output is correct
106 Correct 736 ms 19048 KB Output is correct
107 Correct 683 ms 22508 KB Output is correct
108 Correct 687 ms 21136 KB Output is correct
109 Correct 711 ms 20772 KB Output is correct