QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#232992#7439. 铃原露露nhuang685100 ✓1092ms121416kbC++2010.0kb2023-10-31 08:59:302023-10-31 08:59:30

Judging History

你现在查看的是最新测评结果

  • [2023-10-31 08:59:30]
  • 评测
  • 测评结果:100
  • 用时:1092ms
  • 内存:121416kb
  • [2023-10-31 08:59:30]
  • 提交

answer

/**
 * @file
 * @author n685
 * @brief
 * @date 2023-10-24
 *
 *
 */
#include <bits/stdc++.h>

#ifdef LOCAL
std::ifstream cin;
std::ofstream cout;
using std::cerr;
#else
using std::cin;
using std::cout;
#define cerr                                                                   \
  if (false)                                                                   \
  std::cerr
#endif

#ifdef LOCAL
#include "dd/debug.h"
#else
#define dbg(...) 42
#define dbgR(...) 4242
#define dbgP(...) 420
#define dbgRP(...) 420420
void nline() {}
#endif

template <class Node> struct LSeg {
  using T = typename Node::ValueType;
  using RT = typename Node::RT;
  int h, sz;
  std::vector<Node> val;

  void push(int l, int r) {
    l += sz, r += sz;
    for (int s = h - 1, k = (1 << (h - 1)); s >= 1; --s, k /= 2)
      for (int i = (l >> s); i <= (r >> s); ++i)
        val[i].push(val[2 * i], val[2 * i + 1], k);
  }
  void build(int l, int r) {
    l += sz, r += sz;
    l /= 2, r /= 2;
    for (int k = 2; l >= 1; l /= 2, r /= 2, k *= 2)
      for (int i = l; i <= r; ++i)
        val[i].pull(val[2 * i], val[2 * i + 1]);
  }

  LSeg() {}
  LSeg(int _sz) {
    if (_sz == 1) {
      h = 1;
      sz = 1;
    } else {
      h = std::__lg(_sz - 1) + 2;
      sz = (1 << (h - 1));
    }
    val.resize(2 * sz);
  }
  LSeg(const std::vector<T> &v) : LSeg(static_cast<int>(v.size())) {
    for (int i = 0; i < static_cast<int>(v.size()); ++i)
      val[i + sz] = v[i];
    build(0, sz - 1);
  }

  void upd(int l, int r, const std::function<void(Node &, int)> &f) {
    if (l > r)
      return;
    push(l, l), push(r, r);

    bool cl = false, cr = false;
    int k = 1;
    for (l += sz, r += sz; l <= r; l /= 2, r /= 2, k *= 2) {
      if (cl)
        val[l - 1].pull(val[2 * l - 2], val[2 * l - 1]);
      if (cr)
        val[r + 1].pull(val[2 * r + 2], val[2 * r + 3]);
      if (l % 2 == 1)
        f(val[l++], k), cl = true;
      if (r % 2 == 0)
        f(val[r--], k), cr = true;
    }
    for (--l, ++r; r >= 1; l /= 2, r /= 2) {
      if (cl)
        val[l].pull(val[2 * l], val[2 * l + 1]);
      if (cr && (!cl || l != r))
        val[r].pull(val[2 * r], val[2 * r + 1]);
    }
  }
  void set(int l, int r, T v) {
    upd(l, r, [&v](Node &n, int k) { n.set(v, k); });
  }
  void upd(int l, int r) {
    upd(l, r, [](Node &n, int k) { n.upd(k); });
  }
  RT query(int l, int r) {
    if (l > r)
      return Node::ID;
    push(l, l), push(r, r);

    RT ll = Node::ID, rr = Node::ID;
    for (l += sz, r += sz; l <= r; l /= 2, r /= 2) {
      if (l % 2 == 1)
        ll = Node::comb(val[l++], ll);
      if (r % 2 == 0)
        rr = Node::comb(rr, val[r--]);
    }
    return Node::comb(ll, rr);
  }

  int walkL(int l, int r, const std::function<bool(int64_t)> &c) {
    if (l > r)
      return -1;
    push(l, l);
    l += sz;
    if (c(val[l]))
      return l - sz;

    int ind = -1;
    int s = 1;
    for (; l > 1; l /= 2, s *= 2) {
      if (l % 2 == 0 && c(val[l + 1])) {
        ind = l + 1;
        break;
      }
    }
    if (ind == -1)
      return -1;

    while (ind < sz) {
      val[ind].push(val[2 * ind], val[2 * ind + 1], s);
      if (c(val[2 * ind]))
        ind = 2 * ind;
      else if (c(val[2 * ind + 1]))
        ind = 2 * ind + 1;
      else
        return -1;
      s /= 2;
    }
    if (c(val[ind]) && ind - sz <= r)
      return ind - sz;
    else
      return -1;
  }
  int walkR(int l, int r, const std::function<bool(int64_t)> &c) {
    if (l > r)
      return -1;
    push(r, r);
    r += sz;
    if (c(val[r]))
      return r - sz;

    int ind = -1;
    int s = 1;
    for (; r > 1; r /= 2, s *= 2) {
      if (r % 2 == 1 && c(val[r - 1])) {
        ind = r - 1;
        break;
      }
    }
    if (ind == -1)
      return -1;

    while (ind < sz) {
      val[ind].push(val[2 * ind], val[2 * ind + 1], s);
      if (c(val[2 * ind + 1]))
        ind = 2 * ind + 1;
      else if (c(val[2 * ind]))
        ind = 2 * ind;
      else
        return -1;
      s /= 2;
    }
    if (c(val[ind]) && ind - sz >= l)
      return ind - sz;
    else
      return -1;
  }
};
template <class T> struct NodeMax {
  using ValueType = T;
  using RT = T;
  static constexpr RT ID = (RT)-1e9;
  static constexpr T ID2 = (T)-1e9;
  T val = 0;
  T ls = ID2;
  NodeMax() {}
  NodeMax(T v) : val(v) {}
  operator RT() { return val; }
  static RT comb(RT a, RT b) { return std::max(a, b); }
  void set(T v, [[maybe_unused]] int k) {
    val = std::max(val, v);
    ls = std::max(ls, v);
  }
  void pull(NodeMax &ll, NodeMax &rr) {
    if (ls != ID2)
      return;
    val = std::max(ll.val, rr.val);
  }
  void push(NodeMax &ll, NodeMax &rr, int k) {
    if (ls == ID2) {
      return;
    } else {
      ll.set(ls, k / 2);
      rr.set(ls, k / 2);
    }
    ls = ID2;
  }
};
template <class T> struct NodeMin {
  using ValueType = T;
  using RT = T;
  static constexpr RT ID = (RT)1e9;
  static constexpr T ID2 = (T)1e9;
  T val = 0;
  T ls = ID2;
  NodeMin() {}
  NodeMin(T v) : val(v) {}
  operator RT() { return val; }
  static RT comb(RT a, RT b) { return std::min(a, b); }
  void set(T v, [[maybe_unused]] int k) {
    val = std::min(val, v);
    ls = std::min(ls, v);
  }
  void pull(NodeMin &ll, NodeMin &rr) {
    if (ls != ID2)
      return;
    val = std::min(ll.val, rr.val);
  }
  void push(NodeMin &ll, NodeMin &rr, int k) {
    if (ls == ID2) {
      return;
    } else {
      ll.set(ls, k / 2);
      rr.set(ls, k / 2);
    }
    ls = ID2;
  }
};
using Vec = std::array<int64_t, 2>;
using Mat = std::array<Vec, 2>;
const Mat I = {Vec{1, 0}, {0, 1}};
Vec operator+(Vec a, Vec b) { return Vec{a[0] + b[0], a[1] + b[1]}; }
Mat operator*(Mat a, Mat b) {
  Mat ans{};
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 2; ++j)
      for (int k = 0; k < 2; ++k)
        ans[i][j] += a[i][k] * b[k][j];
  }
  return ans;
}
Vec operator*(Mat a, Vec b) {
  Vec ans{};
  for (int i = 0; i < 2; ++i)
    for (int k = 0; k < 2; ++k)
      ans[i] += a[i][k] * b[k];
  return ans;
}
template <class T> struct Node2 {
  using ValueType = T;
  using RT = T;
  static constexpr RT ID = 0;
  // T val = 0;
  Vec val{};
  Mat l = I;
  Node2() {}
  Node2(T v) : val{v, 0} {}
  operator RT() { return val[1]; }
  static RT comb(RT a, RT b) { return a + b; }
  void updM(Mat m, int k) {
    val = m * val;
    l = m * l;
  }
  void upd(int k) { updM(Mat{Vec{1, 0}, {1, 1}}, k); }
  void set(T v, int k) {
    // k is here for debugging purposes
    assert(k == 1);
    val[0] = v;
  }
  void pull(Node2 &ll, Node2 &rr) {
    if (l != I)
      return;
    val = ll.val + rr.val;
  }
  void push(Node2 &ll, Node2 &rr, int k) {
    if (l == I) {
      return;
    } else {
      ll.updM(l, k / 2);
      rr.updM(l, k / 2);
    }
    l = I;
  }
};

int main() {
#ifdef LOCAL
  cin.open("input.txt");
  cout.rdbuf()->pubsetbuf(0, 0);
  cout.open("output.txt");
#else
  cin.tie(nullptr)->sync_with_stdio(false);
#endif

  int n, m;
  cin >> n >> m;

  std::vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    a[i]--;
  }
  int rt = a[0];
  std::vector<std::vector<int>> adj(n);
  for (int i = 1; i < n; ++i) {
    int f;
    cin >> f;
    --f;
    adj[a[f]].push_back(a[i]);
  }

  LSeg<NodeMin<int>> rr(n);
  for (int i = 0; i < n; ++i)
    rr.val[i + rr.sz] = NodeMin<int>(n - 1);
  rr.build(0, n - 1);

  std::vector<std::vector<int>> add(n), rem(n);
  auto dfs = [&](auto &self, int node) -> std::set<int> * {
    if ((int)adj[node].size() == 0)
      return new std::set<int>{node};
    std::set<int> *mx = nullptr;
    std::vector<std::set<int> *> rest;
    for (int i : adj[node]) {
      std::set<int> *p = self(self, i);
      if (!mx || (int)p->size() > (int)mx->size()) {
        if (mx)
          rest.push_back(mx);
        mx = p;
      } else {
        rest.push_back(p);
      }
    }

    for (auto &s : rest) {
      for (int u : *s) {
        auto it = mx->upper_bound(u);
        if (it != mx->end()) {
          int v = *it;
          if (node < u) {
            // rectangle: (node, u], [v, n]
            dbg(node + 1, u, v, n - 1);
            rr.set(node + 1, u, v - 1);
          } else if (v < node) {
            // rectangle: [1, u], [v, node)
            dbg(0, u, v, node - 1);
            add[v].push_back(u + 1);
            rem[node].push_back(u + 1);
          }
        }
        if (it != mx->begin()) {
          int v = *std::prev(it);
          if (node < v) {
            // rectangle: (node, v], [u, n]
            dbg(node + 1, v, u, n - 1);
            rr.set(node + 1, v, u - 1);
          } else if (u < node) {
            // rectangle: [1, v], [u, node)
            dbg(0, v, u, node - 1);
            add[u].push_back(v + 1);
            rem[node].push_back(v + 1);
          }
        }
      }
      mx->merge(*s);
    }

    mx->insert(node);
    return mx;
  };
  dfs(dfs, rt);
  rr.push(0, n - 1);
  std::vector<std::vector<int>> mx(n + 1), mi(n);
  for (int i = 0; i < n; ++i) {
    // ans += std::max(0, (int)rr.val[i + rr.sz] - (int)ll.val[i + ll.sz] + 1);
    assert((int)rr.val[i + rr.sz] >= i);
    mx[(int)rr.val[i + rr.sz] + 1].push_back(i);
    mi[i].push_back(i);
  }
  std::vector<int64_t> ans(m);
  std::vector<std::vector<std::pair<int, int>>> q(n);
  for (int i = 0; i < m; ++i) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    q[r].emplace_back(l, i);
  }

  LSeg<Node2<int64_t>> seg(n);
  std::multiset<int> miX;
  miX.insert(0);
  for (int i = 0; i < n; ++i) {
    for (int j : mx[i])
      seg.set(j, j, 0);
    for (int j : mi[i])
      seg.set(j, j, 1);
    for (int j : rem[i])
      miX.erase(miX.find(j));
    for (int j : add[i])
      miX.insert(j);
    if (*miX.rbegin() < n)
      seg.upd(*miX.rbegin(), n - 1);
    for (auto [l, ind] : q[i])
      ans[ind] = seg.query(l, i);
  }
  for (auto &v : ans)
    cout << v << '\n';
}

详细

Subtask #1:

score: 25
Accepted

Test #1:

score: 25
Accepted
time: 1ms
memory: 3684kb

input:

100 100
5 29 12 16 25 36 18 37 27 47 34 40 20 3 1 42 26 19 33 41 6 22 8 58 32 62 24 15 35 17 59 30 50 61 43 49 39 67 44 21 13 31 68 69 65 64 10 28 38 54 70 63 9 46 66 52 23 7 48 60 55 56 51 2 57 11 53 14 45 4 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
...

output:

9
87
37
4
13
13
48
17
175
32
46
29
66
40
23
28
43
113
34
37
38
39
85
28
8
64
37
67
1
42
104
7
43
38
4
53
8
14
86
16
61
36
78
7
45
74
84
17
4
51
74
312
80
26
6
56
27
4
83
11
20
39
8
194
78
67
75
66
51
16
2
14
6
47
80
47
11
91
62
59
99
81
92
9
1
43
92
7
22
55
86
56
32
17
27
83
70
10
24
26

result:

ok 100 numbers

Test #2:

score: 0
Accepted
time: 1ms
memory: 3604kb

input:

100 100
1 2 3 4 5 6 7 8 9 10 14 11 17 13 12 15 16 18 19 20 25 22 27 26 23 21 24 28 29 30 31 35 32 34 33 41 36 37 39 40 38 47 45 42 44 49 43 48 46 50 53 54 52 51 55 56 57 58 61 59 63 62 60 65 68 64 67 66 69 70 71 72 73 74 75 76 77 78 81 84 82 79 83 80 85 86 87 92 91 90 89 88 93 94 95 96 97 98 99 100
...

output:

200
52
97
176
147
86
184
184
14
105
177
208
39
36
198
2
19
103
78
174
173
13
27
27
180
152
193
21
45
20
20
169
68
103
17
18
129
20
11
41
152
54
26
189
32
179
158
95
16
3
214
140
172
11
177
3
6
184
2
1
171
5
110
8
6
40
10
11
29
118
198
127
9
17
134
184
177
10
1
8
25
131
24
13
175
11
9
196
191
4
137
1...

result:

ok 100 numbers

Test #3:

score: 0
Accepted
time: 1ms
memory: 3676kb

input:

98 98
1 3 2 5 4 6 7 8 9 10 11 12 13 14 15 16 17 19 18 20 21 23 22 25 24 26 28 27 29 30 31 32 33 34 35 36 37 38 39 40 41 43 42 44 45 47 46 49 48 51 50 52 53 55 54 56 57 58 59 60 61 62 63 64 65 67 66 68 69 70 71 72 73 74 75 76 79 77 78 80 81 83 82 84 85 87 86 88 90 89 91 92 93 94 96 95 97 98
1
2
3
4
1...

output:

36
327
67
48
325
176
206
288
10
102
773
157
10
239
93
396
383
511
596
289
28
236
66
99
460
244
228
332
81
187
409
254
6
3
105
23
15
242
184
76
310
21
268
295
15
55
303
212
240
214
184
322
24
160
197
192
106
1
11
487
458
105
32
156
84
312
2
480
223
92
112
36
327
304
99
55
66
273
321
159
29
615
292
28...

result:

ok 98 numbers

Test #4:

score: 0
Accepted
time: 1ms
memory: 3688kb

input:

100 99
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 77 37 67 49 60 69 71 62 82 84 63 75 50 66 56 57 44 40 36 42 85 81 46 76 83 45 79 47 68 52 80 72 43 74 48 34 65 61 38 51 59 53 78 55 41 73 39 58 54 64 70 35 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
1...

output:

85
58
14
8
40
29
93
50
74
35
40
73
178
2
11
1
104
35
34
7
27
8
14
7
8
11
61
40
53
71
14
42
57
46
88
51
27
57
49
37
2
28
55
43
96
97
4
20
52
83
59
31
38
18
24
24
8
2
31
57
13
1
22
31
39
61
105
31
57
27
15
39
4
34
3
39
45
25
69
33
5
81
1
7
48
34
26
74
58
5
27
6
6
13
66
41
86
32
13

result:

ok 99 numbers

Test #5:

score: 0
Accepted
time: 1ms
memory: 3672kb

input:

100 100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
...

output:

9
55
375
78
211
144
173
513
116
3
21
166
154
1
204
709
470
202
66
270
78
139
529
61
666
71
91
67
10
10
412
25
375
185
109
420
100
158
6
46
567
57
15
183
211
91
323
45
195
280
649
329
78
615
78
55
150
3
130
86
534
388
345
646
443
32
140
457
561
667
36
276
3
146
28
67
489
301
678
1
392
274
141
36
739
...

result:

ok 100 numbers

Test #6:

score: 0
Accepted
time: 1ms
memory: 3820kb

input:

100 100
1 2 3 17 76 53 67 31 84 35 58 61 50 85 5 56 49 66 6 38 42 39 51 63 48 57 59 78 45 18 23 25 44 72 36 26 54 86 19 79 80 87 90 40 46 22 33 82 14 70 27 92 20 28 41 74 7 60 100 93 12 55 32 98 43 62 97 81 88 34 29 95 37 16 30 21 71 52 8 47 24 89 91 15 99 64 4 69 68 65 9 94 83 11 96 77 75 13 10 73
...

output:

113
131
213
176
5
16
66
54
3
59
2
78
29
13
28
35
5
86
10
218
185
32
34
111
218
55
147
88
16
44
25
76
104
17
111
17
92
1
79
68
11
27
207
26
88
58
42
23
126
80
31
68
169
2
28
30
3
81
187
40
33
152
178
24
22
63
204
56
22
77
64
68
139
1
121
29
12
143
5
94
196
71
155
149
118
2
71
80
117
183
137
3
99
114
...

result:

ok 100 numbers

Test #7:

score: 0
Accepted
time: 1ms
memory: 3916kb

input:

100 100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 53 67 46 58 47 34 49 75 51 32 40 56 36 44 35 38 37 48 33 61 64 52 43 39 62 45 65 72 73 76 60 42 78 54 66 77 68 70 63 71 41 50 55 69 57 74 59 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
...

output:

17
36
7
114
153
96
91
13
11
153
70
114
57
10
22
89
1
32
138
21
22
56
121
23
118
5
42
34
32
2
141
45
8
150
149
159
105
159
83
1
11
22
141
117
170
33
14
142
131
142
164
114
143
62
91
161
44
96
60
38
55
38
99
141
32
107
122
164
49
161
138
18
88
6
3
34
182
32
125
195
73
149
65
21
170
122
189
6
62
14
83
...

result:

ok 100 numbers

Test #8:

score: 0
Accepted
time: 1ms
memory: 3684kb

input:

100 98
55 2 3 4 100 5 6 7 8 9 10 11 12 24 14 15 16 30 17 18 19 20 21 22 72 13 25 26 27 28 29 31 32 33 34 35 36 37 38 39 40 91 41 42 43 84 45 46 47 48 49 50 51 52 53 54 1 56 57 58 59 60 61 62 90 63 64 65 66 67 68 69 70 71 23 73 74 75 76 77 78 79 80 81 82 83 44 85 86 87 88 89 92 93 94 99 95 96 97 98
1...

output:

18
478
190
36
21
28
201
98
2246
1286
527
171
78
617
435
36
339
200
827
465
351
230
21
231
10
6
219
78
1048
253
190
237
1854
2144
66
6
67
359
107
2699
1
668
106
253
239
28
1018
55
136
2713
2140
556
15
91
313
36
572
228
36
66
2384
542
10
229
70
45
378
45
120
91
231
3
344
429
78
1636
459
36
205
476
257...

result:

ok 98 numbers

Test #9:

score: 0
Accepted
time: 1ms
memory: 3596kb

input:

100 100
65 23 4 24 2 83 9 69 25 52 86 29 50 5 68 66 54 30 57 31 35 26 75 74 6 79 37 8 11 58 80 12 60 87 59 91 85 89 7 13 56 18 3 17 61 43 62 63 94 46 92 67 20 21 99 88 15 93 64 34 70 40 14 19 10 27 100 51 78 55 82 36 77 32 39 90 16 96 48 22 1 71 33 47 49 53 84 76 95 38 41 81 44 97 45 28 72 42 73 98
...

output:

15
5
4
252
70
144
39
43
13
44
488
25
339
61
32
60
200
103
294
226
38
38
4
68
143
30
305
19
14
41
14
49
63
94
60
55
356
22
40
336
211
52
108
58
294
63
12
338
62
74
318
18
13
95
70
371
2
19
66
37
102
78
331
161
83
29
55
23
397
24
43
72
63
97
26
86
50
18
21
33
132
352
81
38
49
53
36
201
17
105
209
91
4...

result:

ok 100 numbers

Test #10:

score: 0
Accepted
time: 1ms
memory: 3856kb

input:

100 99
3 2 4 5 67 14 32 22 77 83 16 27 15 39 20 42 57 31 82 68 17 80 40 65 84 43 52 18 85 33 70 86 63 72 58 24 25 44 10 8 21 100 28 93 73 23 1 98 19 29 91 41 66 30 13 95 36 53 60 74 7 64 99 49 97 45 47 35 11 46 87 26 48 71 81 59 79 88 76 61 34 12 75 90 55 94 6 37 62 9 50 38 92 51 96 78 89 54 69 56
1...

output:

27
94
143
50
15
52
192
85
88
142
30
84
92
28
169
139
80
6
101
32
41
148
26
36
89
18
62
1
290
94
118
46
172
119
87
156
58
170
15
81
113
40
35
186
60
181
114
29
116
31
73
83
171
53
138
65
205
283
125
185
52
217
31
63
9
28
3
99
87
62
204
9
112
49
103
18
61
217
93
189
48
144
80
45
166
152
88
8
36
20
61
...

result:

ok 99 numbers

Subtask #2:

score: 25
Accepted

Dependency #1:

100%
Accepted

Test #11:

score: 25
Accepted
time: 4ms
memory: 4976kb

input:

3000 3000
15 2125 1789 1745 2433 755 747 1188 116 2493 2185 789 1878 1756 2326 500 1213 1733 1064 217 1790 48 779 35 1119 2235 2553 1421 916 1974 438 1579 1847 423 1011 2969 541 1155 1045 1021 2748 2670 868 229 929 2896 1339 2456 1985 2798 2008 144 2648 1120 41 1362 453 353 467 581 1594 2804 2252 19...

output:

2703
1522
637
1783
2789
267
1381
1616
816
1412
2165
441
1652
743
1310
1629
1721
1182
1011
769
88
2706
1920
478
1003
321
1866
851
673
203
1827
937
616
1859
1813
912
1481
883
898
412
635
118
2119
2131
1251
520
764
751
171
915
98
2469
2721
606
444
1189
81
657
2411
329
1985
750
2487
297
731
14
1307
721
...

result:

ok 3000 numbers

Test #12:

score: 0
Accepted
time: 7ms
memory: 4632kb

input:

3000 3000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 31 22 25 41 52 46 28 29 40 50 20 33 26 37 47 42 21 30 51 27 23 34 43 38 48 32 24 44 35 49 36 39 45 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 93 83 104 91 95 85 80 99 76 100 81 88 92 94 89 103 75 105 96 106 78 101 84 86 10...

output:

1077
31918
11644
5487
95
27783
23665
18988
6345
193
8206
11569
17650
39540
6136
22
38144
22011
18990
244
8305
15913
17114
5987
974
7511
30
1770
1456
15843
27748
100
31210
7096
24432
491
32500
49322
345
32099
1186
521
19251
409
17578
40428
24460
3084
399
237
412
1225
38701
16492
18461
31688
9088
4816...

result:

ok 3000 numbers

Test #13:

score: 0
Accepted
time: 8ms
memory: 4628kb

input:

2998 2999
1 2 3 4 5 6 7 18 12 19 14 21 11 23 15 16 8 9 10 13 17 20 22 24 25 26 27 28 29 30 32 40 33 36 41 38 31 42 37 35 34 39 43 44 45 46 47 48 49 53 56 54 50 57 60 61 63 55 62 51 59 52 58 64 65 72 69 70 77 67 73 83 68 76 75 78 66 79 81 71 74 100 89 90 87 84 88 106 86 92 82 80 105 85 104 101 103 96...

output:

1009
2014
107
637
926
1877
493
793
812
234
2159
559
1234
558
1107
72
693
140
931
550
644
1749
1141
1372
1742
740
873
535
620
1140
336
233
1492
142
1823
718
760
1334
202
1169
443
1298
574
1943
2141
182
955
391
440
1615
364
480
912
243
1391
990
138
228
993
118
2102
502
39
603
2014
1169
1668
1575
2035
...

result:

ok 2999 numbers

Test #14:

score: 0
Accepted
time: 7ms
memory: 4980kb

input:

3000 3000
2983 1 2369 923 2 526 843 2699 3 1199 2700 162 844 2703 658 845 449 163 2569 660 4 389 406 312 2863 313 2709 378 846 924 905 329 330 2618 2891 626 2710 915 2552 2901 2789 2805 2506 2788 2806 2588 2646 499 180 2892 394 702 5 13 331 524 2570 625 6 7 2962 768 2807 77 44 2980 2629 498 737 415 ...

output:

185462
1635
49385
481053
32189
21595
62503
478436
153819
227754
302577
98349
479418
104361
41193
289326
53628
6726
5356
104
288957
55259
181
260970
2495
481646
477679
236815
427038
318466
937
471125
483701
266
315331
802
434094
20166
79799
477965
51484
174286
237835
4083
666
8256
4539
20910
15132
43...

result:

ok 3000 numbers

Test #15:

score: 0
Accepted
time: 10ms
memory: 4712kb

input:

2999 2998
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...

output:

2585
1535
756
1435
27
1128
1072
500
872
20
337
789
509
1819
368
1898
64
1336
1023
1648
865
2248
279
1695
1751
1251
1055
35
2793
865
378
1735
2449
1922
529
610
243
2459
396
2537
1053
1671
902
842
297
1928
2012
287
1019
406
982
1652
1083
521
1697
2642
2389
1253
994
380
1067
345
985
546
424
1025
900
32...

result:

ok 2998 numbers

Test #16:

score: 0
Accepted
time: 7ms
memory: 4700kb

input:

3000 3000
1484 1 2 1485 116 595 2552 1950 1175 1615 7 1190 2728 2365 2356 2767 1195 1716 2389 2390 2402 2132 2740 2411 1729 529 11 1201 2679 869 2778 2420 2702 1487 1733 2708 1776 1229 881 540 882 906 21 2714 1720 2729 2404 2680 2686 1717 1736 1798 1241 1804 1730 1488 1743 22 2781 1191 1819 544 1489...

output:

26301
20397
5156
48
19293
12923
14640
3760
6380
235
27740
12642
1040
1825
14815
7164
20462
10332
3987
8541
2062
12761
19971
21321
10132
19592
7441
497
6497
6810
439
19761
9566
1161
13394
24715
3237
8154
5318
5399
19510
16128
27061
28116
20809
1310
4788
20131
9728
13959
153
11583
22353
5560
20450
495...

result:

ok 3000 numbers

Test #17:

score: 0
Accepted
time: 7ms
memory: 4700kb

input:

3000 2998
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 27 21 22 23 24 26 25 1291 1292 1293 1294 1295 1296 1317 1297 1298 1299 1300 1301 1302 1303 1910 1305 1306 1307 1309 1308 1431 1432 1456 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1455 1451 145...

output:

77730
57676
192331
66289
4357
44250
21220
152156
18766
44509
220280
55095
145519
157116
156439
10422
172778
78390
229452
226887
43338
133290
5461
36689
56192
274052
8703
90214
11682
3236
23328
244398
5285
14298
10781
136322
15584
124641
138163
128729
51884
14645
189462
111548
152757
5414
2882
78976
...

result:

ok 2998 numbers

Test #18:

score: 0
Accepted
time: 8ms
memory: 4732kb

input:

3000 3000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...

output:

315
127827
80
127310
128091
11050
172
51
36009
1151
1285
146
11134
158
127144
127176
127497
45953
60623
128489
59
7117
128359
392
56548
128283
127780
21121
201
142
123580
127080
128282
11182
11417
465
318
886
129
127974
111297
742
9071
5806
127556
38239
20
127933
127001
129148
129143
761
127773
408
...

result:

ok 3000 numbers

Test #19:

score: 0
Accepted
time: 6ms
memory: 4692kb

input:

3000 3000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...

output:

1737
994
1805
1533
322
483
134
657
618
2127
2857
644
106
2532
619
981
2718
1957
2334
1872
1487
1748
2151
884
2664
1539
296
157
197
657
538
1088
389
1758
867
2295
1792
864
1156
1431
1407
822
1901
624
1260
639
1592
658
510
272
1471
812
1959
1739
2504
22
698
2648
433
1649
321
1710
1884
1900
574
258
196...

result:

ok 3000 numbers

Test #20:

score: 0
Accepted
time: 6ms
memory: 5032kb

input:

3000 3000
1 8 6 5 2 3 4 7 9 10 11 14 15 13 12 16 17 18 20 22 21 24 19 23 25 26 27 28 29 30 31 35 36 34 33 39 32 37 40 38 41 45 43 42 47 51 44 46 50 48 52 49 53 54 55 57 58 60 61 56 59 62 63 64 65 66 67 68 69 74 71 72 73 70 75 78 77 76 79 80 81 82 83 88 84 86 85 90 89 91 87 92 96 95 93 98 94 101 102 ...

output:

17205
43873
46971
14099
411240
30473
1061193
1064380
541705
7668
1176
33134
56428
144453
15292
38781
171232
193488
315579
90444
31878
8646
1014069
306081
15914
2453
196878
609281
1045458
1095262
100733
18915
1077390
295540
315207
6179
17766
61535
63190
166743
573022
586439
1596
191271
23264
286523
1...

result:

ok 3000 numbers

Subtask #3:

score: 25
Accepted

Test #21:

score: 25
Accepted
time: 719ms
memory: 107112kb

input:

200000 1
73119 155820 110077 139724 136809 18709 57745 43535 89117 43647 20295 60551 108184 188031 180437 52363 72969 130559 179796 75852 53879 96998 63387 76458 193661 142318 28260 40465 80050 188507 143795 141018 94880 71333 7644 109237 105208 109509 9779 159914 135096 47638 175577 182927 173100 1...

output:

216932

result:

ok 1 number(s): "216932"

Test #22:

score: 0
Accepted
time: 514ms
memory: 85516kb

input:

200000 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 69 130 61 135 87 128 73 64 63 77 62 102 139 98 88 132 95 93 80 78 86 83 72 107 101 138 89 124 76 65 120 108 67 110 79 60 82 92...

output:

87087190

result:

ok 1 number(s): "87087190"

Test #23:

score: 0
Accepted
time: 423ms
memory: 110676kb

input:

200000 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100...

output:

2076001703

result:

ok 1 number(s): "2076001703"

Test #24:

score: 0
Accepted
time: 474ms
memory: 112012kb

input:

200000 1
47227 15222 1 601 13189 48081 179711 183599 15223 34950 38918 47228 183600 15224 602 66302 48290 15225 25808 194847 603 604 191310 26619 1705 15226 191431 52924 48260 197639 56975 66099 16584 15874 17996 18105 183601 54245 20399 34983 27509 66303 18106 191432 52925 56976 191433 194848 19764...

output:

2023389577

result:

ok 1 number(s): "2023389577"

Test #25:

score: 0
Accepted
time: 426ms
memory: 103836kb

input:

200000 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 17 15 18 19 22 20 23 21 26 25 27 24 28 29 30 33 32 35 31 37 36 34 39 38 41 40 45 42 47 43 44 46 48 49 50 51 52 55 54 53 56 57 58 60 63 61 62 59 64 66 65 67 68 69 72 71 70 74 76 75 73 77 78 79 80 81 82 83 84 85 86 87 88 90 89 91 92 93 94 95 97 98 96 99 100...

output:

2417178387

result:

ok 1 number(s): "2417178387"

Test #26:

score: 0
Accepted
time: 567ms
memory: 100316kb

input:

200000 1
1 2 3 4 5 6 7 19 56 43 58 30 54 42 16 13 38 27 12 31 44 46 36 29 28 26 37 59 41 53 50 35 11 17 52 40 48 34 60 23 9 51 55 49 32 33 66 20 47 67 68 24 71 86 80 64 39 78 75 10 62 83 94 73 91 97 101 85 138 93 22 98 104 99 14 18 109 102 116 107 8 79 69 25 74 110 82 65 72 100 88 45 120 95 131 108 ...

output:

554647120

result:

ok 1 number(s): "554647120"

Test #27:

score: 0
Accepted
time: 430ms
memory: 97064kb

input:

200000 1
1 2 3 74880 4 49878 5 28854 21274 5791 7 6 116048 116047 116046 37131 37129 37130 25975 25974 99094 99095 99096 37132 8 47470 183292 183293 9 10 74521 74522 25976 116049 47471 47472 47473 47474 47475 47476 47477 47478 47483 47479 47482 47480 47481 74524 74523 116050 37135 37134 37133 74525 ...

output:

872886033

result:

ok 1 number(s): "872886033"

Test #28:

score: 0
Accepted
time: 672ms
memory: 108356kb

input:

200000 1
1 6 2 3 5 8 10 4 7 9 11 12 13 14 15 16 17 22 18 19 21 20 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 50 51 48 49 55 52 53 54 56 57 58 59 60 64 65 63 62 61 67 70 66 68 69 71 75 73 76 74 72 78 80 77 79 84 83 81 82 85 86 87 90 88 89 97 91 93 92 94 95 102 98 96 10...

output:

2222736478

result:

ok 1 number(s): "2222736478"

Test #29:

score: 0
Accepted
time: 654ms
memory: 108408kb

input:

200000 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 38 61 78 49 104 93 103 53 95 87 60 43 44 35 50 45 89 46 72 67 63 56 77 102 86 79 41 70 59 96 88 54 57 69 51 98 55 91 84 68 42 80 73 66 81 48 90 71 47 37 94 40 83 65 100 75 99 39 85 76 36 52 97 74 92...

output:

2222744424

result:

ok 1 number(s): "2222744424"

Test #30:

score: 0
Accepted
time: 448ms
memory: 89328kb

input:

200000 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100...

output:

555293737

result:

ok 1 number(s): "555293737"

Subtask #4:

score: 25
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Test #31:

score: 25
Accepted
time: 655ms
memory: 97744kb

input:

200000 200000
1 2 3 4 145749 5 133025 23551 17831 50284 100194 19934 139894 79696 19882 40072 100332 100547 60415 21 221 159611 179989 139916 120035 6 120144 120036 159820 79794 40012 229 79802 159822 40185 120176 20240 19935 100589 120154 159552 19868 159895 100650 20126 159658 180054 19867 60547 8...

output:

1904338
37752159
27431196
43760236
63609227
43923562
32018787
19205770
76022343
32057076
142237
85074144
69463933
9305890
125331
43758254
75955178
76005912
75902842
43791546
12651685
70816
31981370
97539607
32189760
4106501
32099539
32253222
45343
29478
32209566
82468943
8839
64733753
43789450
12324...

result:

ok 200000 numbers

Test #32:

score: 0
Accepted
time: 892ms
memory: 111968kb

input:

200000 200000
12668 2854 195172 73237 10412 132601 112747 116917 144393 24876 173745 109156 83975 191904 27767 161246 40947 145735 74967 28995 89341 128066 33544 116349 71702 151912 13692 198915 54690 107558 187881 28625 184248 164531 177056 54571 81827 14915 95265 113168 191541 37545 168641 189477 ...

output:

16396
68371
117855
32234
15278
94253
55013
13769
27165
57555
88424
14508
44713
7373
5875
141903
65634
116061
55338
10995
111836
89391
9127
66112
117475
99736
156942
11368
271
105953
112303
113560
123927
7566
86944
30886
26378
107138
162097
75986
7847
91862
50948
122221
777
99755
45155
35527
39007
10...

result:

ok 200000 numbers

Test #33:

score: 0
Accepted
time: 637ms
memory: 115880kb

input:

199999 199998
33287 556 33288 140 4725 557 25033 165263 33289 25428 157517 168803 39067 172014 33290 4726 2196 22594 160798 43613 185758 172015 185293 168537 153616 185759 21485 155512 42506 4741 558 176184 192002 42694 179435 42957 33291 27139 5839 23711 29330 149852 19888 14036 171400 199231 44571...

output:

4302366
73372942
9146874
642723
150086475
1609410476
862070268
174349741
144219308
1180069423
1610546080
1470091799
1610566942
571043812
236560
931623892
127577581
523869
45433665
1068958856
1611995058
21818423
614515
397452160
1611085643
203997429
312520
307720
6880
1611203337
995453767
131301558
1...

result:

ok 199998 numbers

Test #34:

score: 0
Accepted
time: 737ms
memory: 120492kb

input:

200000 199998
193983 51434 171563 133625 48430 127240 38014 24530 47109 54199 22363 159893 110235 137716 104265 193293 154951 112326 92474 191904 44746 187507 149690 69202 47709 41460 197860 99443 106467 148452 134558 28588 197953 39473 174305 6529 7738 86072 50921 33884 149437 86392 44327 41805 151...

output:

30242
13188
15337
208402
273
24494
74772
61130
216834
66505
38403
81331
36468
104559
9893
4459
7056
13530
72065
33701
185338
80226
52190
14230
8371
129622
21562
125520
133655
71660
45286
233180
65511
93255
181874
67419
150695
81920
14430
35027
115103
22981
12296
186189
69206
166065
49251
95359
20210...

result:

ok 199998 numbers

Test #35:

score: 0
Accepted
time: 743ms
memory: 100172kb

input:

200000 200000
131223 5420 25764 131224 25765 167149 131225 1 75014 25766 5421 131226 41170 46063 82407 183023 115313 102494 131227 67028 67671 110373 195425 131228 195523 22248 9822 6931 5435 171292 72605 25767 5501 44244 390 117373 117374 181451 37056 159233 77058 136706 110451 131229 50450 61739 8...

output:

2386864
1878587
840214
2241532
342184
50390
2456237
486722
1685267
532996
1790005
1117954
112871
1073338
1651107
284939
862058
23600
1913096
2896978
238121
1570810
1116608
2514371
156773
516707
1976585
1578369
4051336
1122460
1542635
3285555
751512
197843
257812
2913558
851266
3106923
1797076
221638...

result:

ok 200000 numbers

Test #36:

score: 0
Accepted
time: 1092ms
memory: 97660kb

input:

200000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 9...

output:

85800
26411
58578
49317
73460
66962
86867
144503
149413
45106
86115
21825
51859
9107
166061
25254
3719
35324
50814
106798
66019
5474
23720
15035
28444
88065
93957
74705
68059
112026
105501
45109
139154
55233
20959
72104
74419
72867
84594
52323
26458
32666
85151
72792
64563
31089
10695
100640
47320
6...

result:

ok 200000 numbers

Test #37:

score: 0
Accepted
time: 1077ms
memory: 97604kb

input:

199998 199999
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 9...

output:

32422
30406
30271
73201
15930
5954
4270
75865
186653
73869
150321
63340
68087
141053
29142
66349
66845
159539
17198
30425
103999
78750
19185
80524
35701
15473
111902
26859
105808
11334
53510
2173
4119
55459
5443
128821
163421
3859
37581
68897
80253
65667
1689
175453
2437
64594
23899
10210
38737
4847...

result:

ok 199999 numbers

Test #38:

score: 0
Accepted
time: 730ms
memory: 121416kb

input:

199998 199999
64692 8749 145775 24315 181963 82271 29531 75314 43826 81110 143113 184334 57339 164395 157015 10575 8616 165254 65589 136727 113349 82352 51213 183782 183721 30574 80242 108504 111182 138116 91706 164556 176309 34274 95259 113136 91736 133213 103070 82911 27510 119144 37161 178768 114...

output:

207588
34775
19697
78873
22919
69057
110798
101568
161096
32514
222409
16552
87775
7960
8756
122009
71418
2729
152054
58389
72345
82738
218137
12556
188009
17512
74078
91436
77192
89522
130131
51411
89852
150473
161022
34029
76072
31957
32672
144449
87442
186306
98171
196755
51160
226604
85120
17767...

result:

ok 199999 numbers

Test #39:

score: 0
Accepted
time: 571ms
memory: 95752kb

input:

200000 200000
181174 158259 1 136577 109001 2 3 57200 4 5583 37277 64111 137750 37278 86231 86232 86233 86234 5 7 6 102816 102817 102818 37279 37280 37282 37281 102819 102824 102823 102820 102821 102822 121235 121236 102825 102826 102827 102828 102830 102829 137752 137751 174497 86235 8 64112 64114 ...

output:

4191095
14143526
292266180
11577456
230152982
4481396
169883356
168936766
481193
3360927
1788849
106479675
291052552
105811374
191043354
171094354
1958239
92087546
32950483
95763609
88431109
92078005
71124686
317029
229031989
105733662
948618
106315620
3367976
16708033
70661468
76511406
126604215
46...

result:

ok 200000 numbers

Test #40:

score: 0
Accepted
time: 613ms
memory: 108680kb

input:

200000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 9...

output:

310294926
324490009
125693284
214030647
50798961
369729067
13263217
36275346
620845477
165480828
132168521
53814103
72258797
969790875
84067432
588983550
816554277
22385942
83016304
400712285
30613977
193018517
81748726
517776281
100736391
1220484908
243553485
22594958
92180361
167201930
298170639
6...

result:

ok 200000 numbers