結果

問題 No.2332 Make a Sequence
ユーザー 👑 emthrmemthrm
提出日時 2023-05-28 16:45:14
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 143 ms / 2,000 ms
コード長 4,285 bytes
コンパイル時間 3,447 ms
コンパイル使用メモリ 260,268 KB
実行使用メモリ 21,012 KB
最終ジャッジ日時 2023-08-27 14:11:36
合計ジャッジ時間 14,595 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 103 ms
19,004 KB
testcase_08 AC 23 ms
4,788 KB
testcase_09 AC 75 ms
12,748 KB
testcase_10 AC 87 ms
18,484 KB
testcase_11 AC 61 ms
11,580 KB
testcase_12 AC 122 ms
21,012 KB
testcase_13 AC 121 ms
20,284 KB
testcase_14 AC 119 ms
20,456 KB
testcase_15 AC 119 ms
20,356 KB
testcase_16 AC 117 ms
20,360 KB
testcase_17 AC 118 ms
20,400 KB
testcase_18 AC 117 ms
20,480 KB
testcase_19 AC 118 ms
20,268 KB
testcase_20 AC 118 ms
20,280 KB
testcase_21 AC 117 ms
20,332 KB
testcase_22 AC 117 ms
20,968 KB
testcase_23 AC 115 ms
20,292 KB
testcase_24 AC 113 ms
20,272 KB
testcase_25 AC 118 ms
20,216 KB
testcase_26 AC 115 ms
20,312 KB
testcase_27 AC 120 ms
20,332 KB
testcase_28 AC 116 ms
20,352 KB
testcase_29 AC 121 ms
20,228 KB
testcase_30 AC 118 ms
20,220 KB
testcase_31 AC 119 ms
20,220 KB
testcase_32 AC 115 ms
20,652 KB
testcase_33 AC 118 ms
20,224 KB
testcase_34 AC 117 ms
20,216 KB
testcase_35 AC 118 ms
20,288 KB
testcase_36 AC 117 ms
20,352 KB
testcase_37 AC 116 ms
20,524 KB
testcase_38 AC 117 ms
20,276 KB
testcase_39 AC 120 ms
20,212 KB
testcase_40 AC 123 ms
20,220 KB
testcase_41 AC 123 ms
20,656 KB
testcase_42 AC 128 ms
20,224 KB
testcase_43 AC 123 ms
20,360 KB
testcase_44 AC 123 ms
20,216 KB
testcase_45 AC 123 ms
20,280 KB
testcase_46 AC 122 ms
20,892 KB
testcase_47 AC 139 ms
20,280 KB
testcase_48 AC 136 ms
20,312 KB
testcase_49 AC 139 ms
20,208 KB
testcase_50 AC 143 ms
20,308 KB
testcase_51 AC 140 ms
20,556 KB
testcase_52 AC 117 ms
20,432 KB
testcase_53 AC 118 ms
20,572 KB
testcase_54 AC 118 ms
20,228 KB
testcase_55 AC 120 ms
20,468 KB
testcase_56 AC 119 ms
20,644 KB
testcase_57 AC 118 ms
20,364 KB
testcase_58 AC 124 ms
20,328 KB
testcase_59 AC 119 ms
20,268 KB
testcase_60 AC 117 ms
20,348 KB
testcase_61 AC 115 ms
20,284 KB
testcase_62 AC 129 ms
20,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

template <typename T>
std::vector<int> z_algorithm(const T &s) {
  const int n = s.size();
  std::vector<int> res(n, 0);
  for (int i = 1, j = 0; i < n; ++i) {
    if (i + res[i - j] < j + res[j]) {
      res[i] = res[i - j];
    } else {
      res[i] = std::max(j + res[j] - i, 0);
      while (i + res[i] < n && s[i + res[i]] == s[res[i]]) ++res[i];
      j = i;
    }
  }
  res[0] = n;
  return res;
}

template <typename T, bool IS_MINIMIZED = true>
struct LiChaoTree {
  struct Line {
    T a, b;
    explicit Line(const T a, const T b) : a(a), b(b) {}
    T f(const T x) const { return a * x + b; }
  };

  explicit LiChaoTree(const std::vector<T>& xs_, const T inf) : n(1), xs(xs_) {
    std::sort(xs.begin(), xs.end());
    xs.erase(std::unique(xs.begin(), xs.end()), xs.end());
    assert(xs.size() > 0);
    n = std::bit_ceil(xs.size());
    const T xs_back = xs.back();
    xs.resize(n, xs_back);
    dat.assign(n << 1, Line(0, inf));
  }

  void add(T a, T b) {
    if constexpr (!IS_MINIMIZED) {
      a = -a;
      b = -b;
    }
    Line line(a, b);
    add(&line, 1, 0, n);
  }

  void add(T a, T b, T left, T right) {
    if constexpr (!IS_MINIMIZED) {
      a = -a;
      b = -b;
    }
    for (int len = 1,
             node_l = std::distance(
                 xs.begin(), std::lower_bound(xs.begin(), xs.end(), left)),
             node_r = std::distance(
                 xs.begin(), std::lower_bound(xs.begin(), xs.end(), right)),
             l = node_l + n, r = node_r + n;
         l < r;
         l >>= 1, r >>= 1, len <<= 1) {
      if (l & 1) {
        Line line(a, b);
        add(&line, l++, node_l, node_l + len);
        node_l += len;
      }
      if (r & 1) {
        Line line(a, b);
        node_r -= len;
        add(&line, --r, node_r, node_r + len);
      }
    }
  }

  T query(const T x) const {
    int node = n + std::distance(xs.begin(),
                                 std::lower_bound(xs.begin(), xs.end(), x));
    T res = dat[node].f(x);
    while (node >>= 1) {
      if (dat[node].f(x) < res) res = dat[node].f(x);
    }
    return IS_MINIMIZED ? res : -res;
  }

 private:
  int n;
  std::vector<T> xs;
  std::vector<Line> dat;

  void add(Line* line, int node, int left, int right) {
    const bool flag_l = dat[node].f(xs[left]) <= line->f(xs[left]);
    const bool flag_r = dat[node].f(xs[right - 1]) <= line->f(xs[right - 1]);
    if (flag_l && flag_r) return;
    if (!flag_l && !flag_r) {
      std::swap(dat[node], *line);
      return;
    }
    const int mid = std::midpoint(left, right);
    if (line->f(xs[mid]) < dat[node].f(xs[mid])) std::swap(dat[node], *line);
    if (line->f(xs[left]) <= dat[node].f(xs[left])) {
      add(line, node << 1, left, mid);
    } else {
      add(line, (node << 1) + 1, mid, right);
    }
  }
};

int main() {
  int n, m; cin >> n >> m;
  vector<int> a(n + m + 1, -1), c(m);
  REP(i, n) cin >> a[i];
  REP(i, m) cin >> a[n + 1 + i];
  REP(i, m) cin >> c[i];
  const vector<int> z = z_algorithm(a);
  vector<ll> queries(m);
  iota(ALL(queries), 1);
  LiChaoTree<ll> li_chao_tree(queries, LINF);
  vector<ll> dp(m + 1, LINF);
  dp[0] = 0;
  REP(i, m + 1) {
    if (i > 0) chmin(dp[i], li_chao_tree.query(i));
    if (i < m) li_chao_tree.add(c[i], dp[i] - ll{i} * c[i], i + 1, i + z[n + 1 + i] + 1);
  }
  // REP(i, m + 1) cout << dp[i] << " \n"[i == m];
  cout << (dp[m] == LINF ? -1 : dp[m]) << '\n';
  return 0;
}
0