Submission #627394

# Submission time Handle Problem Language Result Execution time Memory
627394 2022-08-12T14:19:16 Z Elegia Digital Circuit (IOI22_circuit) C++17
100 / 100
1238 ms 14896 KB
#include "circuit.h"

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

using ull = unsigned long long;

const int MOD = 1000002022;

int N, M;
std::vector<int> val;

std::vector<int> S0, S1, tag;

void update(int o) {
  S0[o] = (S0[o << 1] + S0[o << 1 | 1]) % MOD;
  S1[o] = (S1[o << 1] + S1[o << 1 | 1]) % MOD;
}
void apply(int o) {
  tag[o] ^= 1;
  std::swap(S0[o], S1[o]);
}
void push(int o) {
  if (tag[o]) {
    apply(o << 1); apply(o << 1 | 1);
    tag[o] = 0;
  }
}
void modify(int o, int L, int R, int l, int r) {
  if (L == l && R == r) return apply(o);
  push(o);
  int mid = (L + R) >> 1;
  if (r <= mid) modify(o << 1, L, mid, l, r);
  else if (l > mid) modify(o << 1 | 1, mid + 1, R, l, r);
  else {
    modify(o << 1, L, mid, l, mid);
    modify(o << 1 | 1, mid + 1, R, mid + 1, r);
  }
  update(o);
}

void init(int _N, int _M, std::vector<int> P, std::vector<int> A) {
  N = _N; M = _M;
  std::vector<int> prod(N + M);
  std::fill(prod.begin() + N, prod.end(), 1);
  for (int i = 1; i != N + M; ++i) ++prod[P[i]];
  for (int i = N - 1; i; --i) prod[P[i]] = prod[P[i]] * (ull)prod[i] % MOD;
  std::vector<std::vector<int>> ch(N);
  for (int i = 1; i != N + M; ++i) ch[P[i]].push_back(i);
  val.resize(N + M);
  val[0] = 1;
  for (int i = 0; i != N; ++i) {
    int prd = 1;
    std::vector<int> tmp;
    for (int j : ch[i]) {
      tmp.push_back(prd);
      prd = prd * (ull)prod[j] % MOD;
    }
    prd = val[i]; std::reverse(ch[i].begin(), ch[i].end());
    for (int j : ch[i]) {
      val[j] = tmp.back() * (ull)prd % MOD;
      prd = prd * (ull)prod[j] % MOD;
      tmp.pop_back();
    }
  }
  val.erase(val.begin(), val.begin() + N);

  S0.resize(M << 2); S1.resize(M << 2); tag.resize(M << 2);
  std::function<void(int, int, int)> build = [&](int o, int L, int R) {
    if (L == R) {
      S0[o] = val[L]; S1[o] = 0;
      if (A[L]) std::swap(S0[o], S1[o]);
      return;
    }
    int mid = (L + R) >> 1;
    build(o << 1, L, mid); build(o << 1 | 1, mid + 1, R);
    update(o);
  };
  build(1, 0, M - 1);
}

int count_ways(int L, int R) {
  L -= N; R -= N;
  modify(1, 0, M - 1, L, R);
  return S1[1];
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 288 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 ms 360 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 0 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 2 ms 432 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 488 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 288 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 360 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 0 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 2 ms 432 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 488 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 2 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Correct 2 ms 336 KB Output is correct
33 Correct 2 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 336 KB Output is correct
37 Correct 1 ms 336 KB Output is correct
38 Correct 1 ms 460 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 494 ms 4912 KB Output is correct
2 Correct 929 ms 9536 KB Output is correct
3 Correct 818 ms 9524 KB Output is correct
4 Correct 943 ms 9520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 494 ms 4912 KB Output is correct
2 Correct 929 ms 9536 KB Output is correct
3 Correct 818 ms 9524 KB Output is correct
4 Correct 943 ms 9520 KB Output is correct
5 Correct 770 ms 4916 KB Output is correct
6 Correct 879 ms 9544 KB Output is correct
7 Correct 924 ms 9576 KB Output is correct
8 Correct 952 ms 9544 KB Output is correct
9 Correct 456 ms 564 KB Output is correct
10 Correct 782 ms 852 KB Output is correct
11 Correct 851 ms 852 KB Output is correct
12 Correct 745 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 ms 360 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 0 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 2 ms 432 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 488 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 494 ms 4912 KB Output is correct
14 Correct 929 ms 9536 KB Output is correct
15 Correct 818 ms 9524 KB Output is correct
16 Correct 943 ms 9520 KB Output is correct
17 Correct 770 ms 4916 KB Output is correct
18 Correct 879 ms 9544 KB Output is correct
19 Correct 924 ms 9576 KB Output is correct
20 Correct 952 ms 9544 KB Output is correct
21 Correct 456 ms 564 KB Output is correct
22 Correct 782 ms 852 KB Output is correct
23 Correct 851 ms 852 KB Output is correct
24 Correct 745 ms 896 KB Output is correct
25 Correct 737 ms 14128 KB Output is correct
26 Correct 968 ms 14396 KB Output is correct
27 Correct 728 ms 14392 KB Output is correct
28 Correct 820 ms 14272 KB Output is correct
29 Correct 835 ms 14280 KB Output is correct
30 Correct 837 ms 14384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 288 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 360 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 0 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 2 ms 432 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 488 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 2 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Correct 2 ms 336 KB Output is correct
33 Correct 2 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 336 KB Output is correct
37 Correct 1 ms 336 KB Output is correct
38 Correct 1 ms 460 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
43 Correct 602 ms 676 KB Output is correct
44 Correct 730 ms 720 KB Output is correct
45 Correct 807 ms 720 KB Output is correct
46 Correct 864 ms 984 KB Output is correct
47 Correct 811 ms 988 KB Output is correct
48 Correct 702 ms 988 KB Output is correct
49 Correct 791 ms 988 KB Output is correct
50 Correct 643 ms 980 KB Output is correct
51 Correct 777 ms 668 KB Output is correct
52 Correct 881 ms 664 KB Output is correct
53 Correct 848 ms 628 KB Output is correct
54 Correct 964 ms 988 KB Output is correct
55 Correct 1006 ms 808 KB Output is correct
56 Correct 994 ms 756 KB Output is correct
57 Correct 652 ms 652 KB Output is correct
58 Correct 999 ms 980 KB Output is correct
59 Correct 1121 ms 1036 KB Output is correct
60 Correct 964 ms 1040 KB Output is correct
61 Correct 1032 ms 720 KB Output is correct
62 Correct 837 ms 676 KB Output is correct
63 Correct 1093 ms 656 KB Output is correct
64 Correct 719 ms 664 KB Output is correct
65 Correct 498 ms 564 KB Output is correct
66 Correct 1029 ms 852 KB Output is correct
67 Correct 899 ms 896 KB Output is correct
68 Correct 1008 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 288 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 360 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 0 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 2 ms 432 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 488 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 2 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Correct 2 ms 336 KB Output is correct
33 Correct 2 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 336 KB Output is correct
37 Correct 1 ms 336 KB Output is correct
38 Correct 1 ms 460 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
43 Correct 494 ms 4912 KB Output is correct
44 Correct 929 ms 9536 KB Output is correct
45 Correct 818 ms 9524 KB Output is correct
46 Correct 943 ms 9520 KB Output is correct
47 Correct 770 ms 4916 KB Output is correct
48 Correct 879 ms 9544 KB Output is correct
49 Correct 924 ms 9576 KB Output is correct
50 Correct 952 ms 9544 KB Output is correct
51 Correct 456 ms 564 KB Output is correct
52 Correct 782 ms 852 KB Output is correct
53 Correct 851 ms 852 KB Output is correct
54 Correct 745 ms 896 KB Output is correct
55 Correct 737 ms 14128 KB Output is correct
56 Correct 968 ms 14396 KB Output is correct
57 Correct 728 ms 14392 KB Output is correct
58 Correct 820 ms 14272 KB Output is correct
59 Correct 835 ms 14280 KB Output is correct
60 Correct 837 ms 14384 KB Output is correct
61 Correct 602 ms 676 KB Output is correct
62 Correct 730 ms 720 KB Output is correct
63 Correct 807 ms 720 KB Output is correct
64 Correct 864 ms 984 KB Output is correct
65 Correct 811 ms 988 KB Output is correct
66 Correct 702 ms 988 KB Output is correct
67 Correct 791 ms 988 KB Output is correct
68 Correct 643 ms 980 KB Output is correct
69 Correct 777 ms 668 KB Output is correct
70 Correct 881 ms 664 KB Output is correct
71 Correct 848 ms 628 KB Output is correct
72 Correct 964 ms 988 KB Output is correct
73 Correct 1006 ms 808 KB Output is correct
74 Correct 994 ms 756 KB Output is correct
75 Correct 652 ms 652 KB Output is correct
76 Correct 999 ms 980 KB Output is correct
77 Correct 1121 ms 1036 KB Output is correct
78 Correct 964 ms 1040 KB Output is correct
79 Correct 1032 ms 720 KB Output is correct
80 Correct 837 ms 676 KB Output is correct
81 Correct 1093 ms 656 KB Output is correct
82 Correct 719 ms 664 KB Output is correct
83 Correct 498 ms 564 KB Output is correct
84 Correct 1029 ms 852 KB Output is correct
85 Correct 899 ms 896 KB Output is correct
86 Correct 1008 ms 856 KB Output is correct
87 Correct 1 ms 208 KB Output is correct
88 Correct 601 ms 12692 KB Output is correct
89 Correct 989 ms 9212 KB Output is correct
90 Correct 1126 ms 9036 KB Output is correct
91 Correct 1134 ms 14388 KB Output is correct
92 Correct 838 ms 14412 KB Output is correct
93 Correct 904 ms 14476 KB Output is correct
94 Correct 948 ms 14396 KB Output is correct
95 Correct 1125 ms 14412 KB Output is correct
96 Correct 1024 ms 7916 KB Output is correct
97 Correct 1238 ms 7824 KB Output is correct
98 Correct 994 ms 7292 KB Output is correct
99 Correct 1145 ms 14308 KB Output is correct
100 Correct 924 ms 10744 KB Output is correct
101 Correct 997 ms 9556 KB Output is correct
102 Correct 1077 ms 7832 KB Output is correct
103 Correct 1135 ms 14364 KB Output is correct
104 Correct 1113 ms 14896 KB Output is correct
105 Correct 897 ms 14864 KB Output is correct
106 Correct 950 ms 8656 KB Output is correct
107 Correct 1071 ms 7836 KB Output is correct
108 Correct 1122 ms 8016 KB Output is correct
109 Correct 1095 ms 7860 KB Output is correct