Submission #826136

# Submission time Handle Problem Language Result Execution time Memory
826136 2023-08-15T10:36:22 Z KoD Sequence (APIO23_sequence) C++17
100 / 100
771 ms 46632 KB
#include "sequence.h"
#include <algorithm>
#include <utility>
#include <vector>
using std::pair;
using std::vector;

struct node {
  int min, max, sum;
  static node id() { return {0, 0, 0}; }
  static node one(int x) { return {std::min(x, 0), std::max(x, 0), x}; }
  node operator+(const node& n) const { return node(*this) += n; }
  node& operator+=(const node& n) {
    min = std::min(min, n.min + sum);
    max = std::max(max, n.max + sum);
    sum += n.sum;
    return *this;
  }
};

struct segment_tree {
  explicit segment_tree(int n, const node& x) : size(n), data(2 * n, x) {
    for (int i = n - 1; i > 0; --i) fetch(i);
  }

  void set(int i, const node& n) {
    i += size;
    data[i] = n;
    while (i >>= 1) fetch(i);
  }

  node fold(int l, int r) {
    l += size, r += size;
    node ret_l = node::id(), ret_r = node::id();
    for (; l < r; l >>= 1, r >>= 1) {
      if (l & 1) ret_l += data[l++];
      if (r & 1) ret_r = data[--r] + ret_r;
    }
    return ret_l + ret_r;
  }

 private:
  int size;
  vector<node> data;

  void fetch(int i) { data[i] = data[2 * i + 0] + data[2 * i + 1]; }
};

int sequence(int N, vector<int> A) {
  vector<vector<int>> C(N);
  for (int i = 0; i < N; ++i) {
    A[i] -= 1;
    C[A[i]].push_back(i);
  }
  int ret = 0;
  for (int i = 0, j = 0; i < N; ++i) {
    const int c = (int)C[i].size();
    if (j <= N / 2 && N / 2 < j + c) ret = std::max(ret, c);
    j += c;
  }
  for (int phase = 0; phase < 2; ++phase) {
    segment_tree seg(N, node::one(-1));
    for (int x = 0; x < N; ++x) {
      const auto& v = C[x];
      const int n = (int)v.size();
      for (int i = 0, j = 0; i < n; ++i) {
        const auto [l0, r0, $0] = seg.fold(0, v[i]);
        while (j < n) {
          const int offset = seg.fold(0, v[j] + 1).sum;
          const auto [l1, r1, $1] = seg.fold(v[j] + 1, N);
          if (std::max(l0, l1 + offset) > std::min(r0, r1 + offset)) break;
          j += 1;
        }
        ret = std::max(ret, j - i);
      }
      for (const int i : v) seg.set(i, node::one(+1));
    }
    std::reverse(C.begin(), C.end());
  }
  return ret;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 288 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 292 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 288 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 288 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 292 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 288 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 420 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 2 ms 416 KB Output is correct
18 Correct 1 ms 420 KB Output is correct
19 Correct 2 ms 420 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 2 ms 420 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 288 KB Output is correct
2 Correct 422 ms 40924 KB Output is correct
3 Correct 423 ms 40920 KB Output is correct
4 Correct 440 ms 30760 KB Output is correct
5 Correct 413 ms 39880 KB Output is correct
6 Correct 418 ms 39852 KB Output is correct
7 Correct 379 ms 31532 KB Output is correct
8 Correct 393 ms 31676 KB Output is correct
9 Correct 422 ms 30764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 496 ms 30896 KB Output is correct
3 Correct 514 ms 30748 KB Output is correct
4 Correct 504 ms 30844 KB Output is correct
5 Correct 491 ms 30924 KB Output is correct
6 Correct 459 ms 30752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 564 ms 46576 KB Output is correct
2 Correct 541 ms 46632 KB Output is correct
3 Correct 556 ms 46044 KB Output is correct
4 Correct 562 ms 46008 KB Output is correct
5 Correct 501 ms 42736 KB Output is correct
6 Correct 499 ms 42604 KB Output is correct
7 Correct 428 ms 41500 KB Output is correct
8 Correct 422 ms 41144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 288 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 292 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 288 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 420 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 2 ms 416 KB Output is correct
18 Correct 1 ms 420 KB Output is correct
19 Correct 2 ms 420 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 2 ms 420 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 80 ms 6652 KB Output is correct
24 Correct 85 ms 6776 KB Output is correct
25 Correct 79 ms 6720 KB Output is correct
26 Correct 89 ms 5588 KB Output is correct
27 Correct 91 ms 5588 KB Output is correct
28 Correct 88 ms 5588 KB Output is correct
29 Correct 81 ms 5244 KB Output is correct
30 Correct 74 ms 5240 KB Output is correct
31 Correct 75 ms 5580 KB Output is correct
32 Correct 57 ms 7636 KB Output is correct
33 Correct 82 ms 6484 KB Output is correct
34 Correct 80 ms 6540 KB Output is correct
35 Correct 82 ms 6396 KB Output is correct
36 Correct 81 ms 6496 KB Output is correct
37 Correct 80 ms 6508 KB Output is correct
38 Correct 81 ms 6484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 288 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 292 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 288 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 420 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 2 ms 416 KB Output is correct
18 Correct 1 ms 420 KB Output is correct
19 Correct 2 ms 420 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 2 ms 420 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 422 ms 40924 KB Output is correct
24 Correct 423 ms 40920 KB Output is correct
25 Correct 440 ms 30760 KB Output is correct
26 Correct 413 ms 39880 KB Output is correct
27 Correct 418 ms 39852 KB Output is correct
28 Correct 379 ms 31532 KB Output is correct
29 Correct 393 ms 31676 KB Output is correct
30 Correct 422 ms 30764 KB Output is correct
31 Correct 496 ms 30896 KB Output is correct
32 Correct 514 ms 30748 KB Output is correct
33 Correct 504 ms 30844 KB Output is correct
34 Correct 491 ms 30924 KB Output is correct
35 Correct 459 ms 30752 KB Output is correct
36 Correct 564 ms 46576 KB Output is correct
37 Correct 541 ms 46632 KB Output is correct
38 Correct 556 ms 46044 KB Output is correct
39 Correct 562 ms 46008 KB Output is correct
40 Correct 501 ms 42736 KB Output is correct
41 Correct 499 ms 42604 KB Output is correct
42 Correct 428 ms 41500 KB Output is correct
43 Correct 422 ms 41144 KB Output is correct
44 Correct 80 ms 6652 KB Output is correct
45 Correct 85 ms 6776 KB Output is correct
46 Correct 79 ms 6720 KB Output is correct
47 Correct 89 ms 5588 KB Output is correct
48 Correct 91 ms 5588 KB Output is correct
49 Correct 88 ms 5588 KB Output is correct
50 Correct 81 ms 5244 KB Output is correct
51 Correct 74 ms 5240 KB Output is correct
52 Correct 75 ms 5580 KB Output is correct
53 Correct 57 ms 7636 KB Output is correct
54 Correct 82 ms 6484 KB Output is correct
55 Correct 80 ms 6540 KB Output is correct
56 Correct 82 ms 6396 KB Output is correct
57 Correct 81 ms 6496 KB Output is correct
58 Correct 80 ms 6508 KB Output is correct
59 Correct 81 ms 6484 KB Output is correct
60 Correct 771 ms 41036 KB Output is correct
61 Correct 768 ms 41008 KB Output is correct
62 Correct 726 ms 40924 KB Output is correct
63 Correct 660 ms 32764 KB Output is correct
64 Correct 677 ms 32768 KB Output is correct
65 Correct 691 ms 32776 KB Output is correct
66 Correct 516 ms 30908 KB Output is correct
67 Correct 498 ms 31036 KB Output is correct
68 Correct 534 ms 33212 KB Output is correct
69 Correct 385 ms 46596 KB Output is correct
70 Correct 715 ms 39984 KB Output is correct
71 Correct 714 ms 39768 KB Output is correct
72 Correct 676 ms 39264 KB Output is correct
73 Correct 702 ms 39672 KB Output is correct
74 Correct 697 ms 39292 KB Output is correct
75 Correct 715 ms 39668 KB Output is correct