結果

問題 No.1117 数列分割
ユーザー keijakkeijak
提出日時 2021-12-31 23:57:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 5,733 bytes
コンパイル時間 2,406 ms
コンパイル使用メモリ 214,452 KB
実行使用メモリ 458,624 KB
最終ジャッジ日時 2024-10-09 09:13:10
合計ジャッジ時間 18,216 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 48 ms
30,592 KB
testcase_04 AC 78 ms
27,392 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 90 ms
34,432 KB
testcase_09 AC 19 ms
10,368 KB
testcase_10 AC 90 ms
32,256 KB
testcase_11 AC 437 ms
155,520 KB
testcase_12 AC 442 ms
177,664 KB
testcase_13 AC 580 ms
147,200 KB
testcase_14 AC 678 ms
221,440 KB
testcase_15 MLE -
testcase_16 MLE -
testcase_17 AC 88 ms
29,952 KB
testcase_18 MLE -
testcase_19 MLE -
testcase_20 AC 803 ms
222,464 KB
testcase_21 MLE -
testcase_22 MLE -
testcase_23 MLE -
testcase_24 MLE -
testcase_25 MLE -
testcase_26 AC 2 ms
5,248 KB
testcase_27 AC 24 ms
18,944 KB
testcase_28 AC 25 ms
19,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP_(i, a_, b_, a, b, ...) for (int i = (a), END_##i = (b); i < END_##i; ++i)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define ALL(x) std::begin(x), std::end(x)
using Int = long long;
using Uint = unsigned long long;
using Real = long double;

template<typename T, typename U>
inline bool chmax(T &a, U b) {
  return a < b and ((a = std::move(b)), true);
}
template<typename T, typename U>
inline bool chmin(T &a, U b) {
  return a > b and ((a = std::move(b)), true);
}
template<typename T>
inline int ssize(const T &a) {
  return (int) a.size();
}

struct Void {};

template<class T>
inline std::ostream &print_one(const T &x, char endc) {
  if constexpr (std::is_same<T, Void>::value) {
    return std::cout;  // print nothing
  } else if constexpr (std::is_same<T, bool>::value) {
    return std::cout << (x ? "Yes" : "No") << endc;
  } else {
    return std::cout << x << endc;
  }
}
template<class T>
inline std::ostream &print(const T &x) { return print_one(x, '\n'); }
template<typename T, typename... Ts>
std::ostream &print(const T &head, Ts... tail) {
  return print_one(head, ' '), print(tail...);
}
inline std::ostream &print() { return std::cout << '\n'; }

template<typename Container>
std::ostream &print_seq(const Container &seq,
                        const char *sep = " ",
                        const char *ends = "\n",
                        std::ostream &os = std::cout) {
  const auto itl = std::begin(seq), itr = std::end(seq);
  for (auto it = itl; it != itr; ++it) {
    if (it != itl) os << sep;
    os << *it;
  }
  return os << ends;
}

struct CastInput {
  template<typename T>
  operator T() const {
    T x;
    std::cin >> x;
    return x;
  }
  struct Sized {
    std::size_t n;
    template<typename T>
    operator T() const {
      T x(n);
      for (auto &e: x) std::cin >> e;
      return x;
    }
  };
  Sized operator()(std::size_t n) const { return {n}; }
} in;

#ifdef MY_DEBUG
#include "debug_dump.hpp"
#include "backward.hpp"
backward::SignalHandling kSignalHandling;
#else
#define DUMP(...)
#define cerr if(false)cerr
#endif

using namespace std;

template<typename Monoid>
struct SegmentTree {
  using T = typename Monoid::T;

  int n_;                // number of valid leaves.
  int offset_;           // where leaves start
  std::vector<T> data_;  // data size: 2*offset_

  inline int size() const { return n_; }
  inline int offset() const { return offset_; }

  explicit SegmentTree(int n) : n_(n) {
    offset_ = 1;
    while (offset_ < n_) offset_ <<= 1;
    data_.assign(2 * offset_, Monoid::id());
  }

  explicit SegmentTree(const std::vector<T> &leaves) : n_(leaves.size()) {
    offset_ = 1;
    while (offset_ < n_) offset_ <<= 1;
    data_.assign(2 * offset_, Monoid::id());
    for (int i = 0; i < n_; ++i) {
      data_[offset_ + i] = leaves[i];
    }
    for (int i = offset_ - 1; i > 0; --i) {
      data_[i] = Monoid::op(data_[i * 2], data_[i * 2 + 1]);
    }
  }

  // Sets i-th value (0-indexed) to x.
  void set(int i, const T &x) {
    int k = offset_ + i;
    data_[k] = x;
    // Update its ancestors.
    while (k > 1) {
      k >>= 1;
      data_[k] = Monoid::op(data_[k * 2], data_[k * 2 + 1]);
    }
  }

  // Queries by [l,r) range (0-indexed, half-open interval).
  T fold(int l, int r) const {
    l = std::max(l, 0) + offset_;
    r = std::min(r, offset_) + offset_;
    T vleft = Monoid::id(), vright = Monoid::id();
    for (; l < r; l >>= 1, r >>= 1) {
      if (l & 1) vleft = Monoid::op(vleft, data_[l++]);
      if (r & 1) vright = Monoid::op(data_[--r], vright);
    }
    return Monoid::op(vleft, vright);
  }

  T fold_all() const { return data_[1]; }

  // Returns i-th value (0-indexed).
  T operator[](int i) const { return data_[offset_ + i]; }

  friend std::ostream &operator<<(std::ostream &os, const SegmentTree &st) {
    os << "[";
    for (int i = 0; i < st.size(); ++i) {
      if (i != 0) os << ", ";
      const auto &x = st[i];
      os << x;
    }
    return os << "]";
  }
};

const Int kBig = 1e16;

struct MaxOp {
  using T = Int;
  static T op(const T &x, const T &y) { return std::max(x, y); }
  static constexpr T id() { return -kBig; }
};

using ST = SegmentTree<MaxOp>;

auto solve() {
  int n = in, K = in, M = in;
  vector<Int> a = in(n);
  vector<Int> acc(n + 1);
  REP(i, n) acc[i + 1] = acc[i] + a[i];
  vector<Int> acc_minus(n + 1);
  REP(i, n + 1) acc_minus[i] = -acc[i];

  vector<ST> seg_plus, seg_minus;
  REP(i, K + 1) {
    seg_plus.emplace_back(n + 2);
    seg_minus.emplace_back(n + 2);
  }
  seg_plus[0].set(0, 0);
  seg_minus[0].set(0, 0);

  auto dp = vector(n + 1, vector(K + 1, -kBig));
  dp[0][0] = 0;

  ST seg1(n + 1), seg2(n + 1);
  seg1.set(0, 0);
  seg2.set(0, 0);
  REP(k, 1, K + 1) {
    REP(i, n) {
      const int r = i + 1;
//      for (int l = max(r - M, 0); l < r; ++l) {
//        chmax(dp[r][k], (dp[l][k - 1] - acc[l]) + acc[r]);
//        chmax(dp[r][k], (dp[l][k - 1] - acc_minus[l]) + acc_minus[r]);
//      }
      if (Int v = seg1.fold(max(r - M, 0), r); v != -kBig) {
        chmax(dp[r][k], v + acc[r]);
      }
      if (Int v = seg2.fold(max(r - M, 0), r); v != -kBig) {
        chmax(dp[r][k], v + acc_minus[r]);
      }
    }
    vector<Int> nv1(n + 1), nv2(n + 1);
    for (int l = 0; l <= n; ++l) {
      nv1[l] = dp[l][k] - acc[l];
      nv2[l] = dp[l][k] - acc_minus[l];
    }
    ST nseg1(nv1), nseg2(nv2);
    seg1.data_ = move(nseg1.data_);
    seg2.data_ = move(nseg2.data_);
  }
  return dp[n][K];
}

int main() {
  std::ios::sync_with_stdio(false), cin.tie(nullptr);
  cout << std::fixed << std::setprecision(18);
  const int T = 1;//in;
  REP(t, T) {
    auto ans = solve();
    print(ans);
  }
}
0