結果

問題 No.2332 Make a Sequence
ユーザー ei1333333ei1333333
提出日時 2023-05-28 14:11:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 229 ms / 2,000 ms
コード長 6,732 bytes
コンパイル時間 2,600 ms
コンパイル使用メモリ 216,944 KB
実行使用メモリ 43,056 KB
最終ジャッジ日時 2024-06-08 04:52:58
合計ジャッジ時間 16,437 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 128 ms
28,368 KB
testcase_08 AC 31 ms
12,800 KB
testcase_09 AC 98 ms
23,744 KB
testcase_10 AC 111 ms
22,856 KB
testcase_11 AC 77 ms
16,980 KB
testcase_12 AC 153 ms
33,468 KB
testcase_13 AC 152 ms
33,348 KB
testcase_14 AC 154 ms
33,472 KB
testcase_15 AC 155 ms
33,468 KB
testcase_16 AC 157 ms
33,344 KB
testcase_17 AC 152 ms
33,472 KB
testcase_18 AC 156 ms
33,476 KB
testcase_19 AC 153 ms
33,472 KB
testcase_20 AC 153 ms
33,472 KB
testcase_21 AC 152 ms
33,472 KB
testcase_22 AC 153 ms
33,348 KB
testcase_23 AC 154 ms
33,472 KB
testcase_24 AC 157 ms
33,596 KB
testcase_25 AC 159 ms
33,344 KB
testcase_26 AC 157 ms
33,472 KB
testcase_27 AC 156 ms
33,340 KB
testcase_28 AC 165 ms
33,216 KB
testcase_29 AC 157 ms
33,344 KB
testcase_30 AC 158 ms
33,344 KB
testcase_31 AC 157 ms
33,348 KB
testcase_32 AC 161 ms
34,368 KB
testcase_33 AC 163 ms
34,496 KB
testcase_34 AC 165 ms
34,500 KB
testcase_35 AC 166 ms
34,368 KB
testcase_36 AC 161 ms
34,236 KB
testcase_37 AC 162 ms
35,396 KB
testcase_38 AC 163 ms
35,396 KB
testcase_39 AC 170 ms
36,292 KB
testcase_40 AC 165 ms
35,396 KB
testcase_41 AC 164 ms
35,132 KB
testcase_42 AC 179 ms
40,276 KB
testcase_43 AC 183 ms
40,484 KB
testcase_44 AC 184 ms
41,132 KB
testcase_45 AC 188 ms
43,056 KB
testcase_46 AC 183 ms
42,692 KB
testcase_47 AC 226 ms
41,084 KB
testcase_48 AC 229 ms
41,340 KB
testcase_49 AC 226 ms
41,080 KB
testcase_50 AC 228 ms
41,212 KB
testcase_51 AC 225 ms
41,216 KB
testcase_52 AC 155 ms
33,856 KB
testcase_53 AC 155 ms
33,712 KB
testcase_54 AC 157 ms
33,468 KB
testcase_55 AC 155 ms
33,472 KB
testcase_56 AC 156 ms
33,728 KB
testcase_57 AC 156 ms
33,340 KB
testcase_58 AC 155 ms
33,216 KB
testcase_59 AC 159 ms
33,344 KB
testcase_60 AC 157 ms
33,600 KB
testcase_61 AC 151 ms
33,344 KB
testcase_62 AC 197 ms
40,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  explicit FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

#line 1 "string/z-algorithm.hpp"
/**
 * @brief Z Algorithm
 * @see https://heno239.hatenablog.com/entry/2020/07/07/140651
 * @docs docs/z-algorithm.md
 */
template< typename T = char >
struct ZAlgorithm {
private:
  vector< T > s;
  vector< int > deleted;
  vector< vector< int > > delete_hist;
  vector< int > z;
  queue< int > cur;
public:
  ZAlgorithm() : delete_hist{{}} {}

  template< typename S >
  ZAlgorithm(const S &s) : ZAlgorithm() {
    for(auto &c: s) add(c);
  }

  void add(const T &c) {
    s.emplace_back(c);
    delete_hist.emplace_back();
    deleted.emplace_back(0);
    z.emplace_back(0);
    z[0]++;

    int len = (int) s.size();
    if(len == 1) return;
    if(s[0] == c) cur.emplace(len - 1);
    else deleted[len - 1] = 1;

    auto set = [&](int t, int len) {
      deleted[t] = 1;
      delete_hist[len].emplace_back(t);
      z[t] = len - t - 1;
    };

    while(not cur.empty()) {
      int t = cur.front();
      if(deleted[t]) {
        cur.pop();
      } else if(s[len - t - 1] == s.back()) {
        break;
      } else {
        set(t, len);
        cur.pop();
      }
    }

    if(not cur.empty()) {
      int t = cur.front();
      for(auto &p: delete_hist[len - t]) {
        set(p + t, len);
      }
    }
  }

  int get(int k) const {
    return deleted[k] ? z[k] : (int) s.size() - k;
  }

  int operator[](int k) const {
    return get(k);
  }

  vector< int > get() {
    vector< int > ret(s.size());
    for(int i = 0; i < (int) s.size(); i++) {
      ret[i] = get(i);
    }
    return ret;
  }
};

#line 1 "structure/convex-hull-trick/dynamic-li-chao-tree.hpp"
/**
 * @brief Dynamic-Li-Chao-Tree
 * @docs docs/dynamic-li-chao-tree.md
*/
template< typename T, T x_low, T x_high, T id >
struct DynamicLiChaoTree {

  struct Line {
    T a, b;

    Line(T a, T b) : a(a), b(b) {}

    inline T get(T x) const { return a * x + b; }
  };

  struct Node {
    Line x;
    Node *l, *r;

    Node(const Line &x) : x{x}, l{nullptr}, r{nullptr} {}
  };

  Node *root;

  DynamicLiChaoTree() : root{nullptr} {}

  Node *add_line(Node *t, Line &x, const T &l, const T &r, const T &x_l, const T &x_r) {
    if(!t) return new Node(x);

    T t_l = t->x.get(l), t_r = t->x.get(r);

    if(t_l <= x_l && t_r <= x_r) {
      return t;
    } else if(t_l >= x_l && t_r >= x_r) {
      t->x = x;
      return t;
    } else {
      T m = (l + r) / 2;
      if(m == r) --m;
      T t_m = t->x.get(m), x_m = x.get(m);
      if(t_m > x_m) {
        swap(t->x, x);
        if(x_l >= t_l) t->l = add_line(t->l, x, l, m, t_l, t_m);
        else t->r = add_line(t->r, x, m + 1, r, t_m + x.a, t_r);
      } else {
        if(t_l >= x_l) t->l = add_line(t->l, x, l, m, x_l, x_m);
        else t->r = add_line(t->r, x, m + 1, r, x_m + x.a, x_r);
      }
      return t;
    }
  }

  void add_line(const T &a, const T &b) {
    Line x(a, b);
    root = add_line(root, x, x_low, x_high, x.get(x_low), x.get(x_high));
  }

  Node *add_segment(Node *t, Line &x, const T &a, const T &b, const T &l, const T &r, const T &x_l, const T &x_r) {
    if(r < a || b < l) return t;
    if(a <= l && r <= b) {
      Line y{x};
      return add_line(t, y, l, r, x_l, x_r);
    }
    if(t) {
      T t_l = t->x.get(l), t_r = t->x.get(r);
      if(t_l <= x_l && t_r <= x_r) return t;
    } else {
      t = new Node(Line(0, id));
    }
    T m = (l + r) / 2;
    if(m == r) --m;
    T x_m = x.get(m);
    t->l = add_segment(t->l, x, a, b, l, m, x_l, x_m);
    t->r = add_segment(t->r, x, a, b, m + 1, r, x_m + x.a, x_r);
    return t;
  }

  void add_segment(const T &l, const T &r, const T &a, const T &b) {
    Line x(a, b);
    root = add_segment(root, x, l, r - 1, x_low, x_high, x.get(x_low), x.get(x_high));
  }

  T query(const Node *t, const T &l, const T &r, const T &x) const {
    if(!t) return id;
    if(l == r) return t->x.get(x);
    T m = (l + r) / 2;
    if(m == r) --m;
    if(x <= m) return min(t->x.get(x), query(t->l, l, m, x));
    else return min(t->x.get(x), query(t->r, m + 1, r, x));
  }

  T query(const T &x) const {
    return query(root, x_low, x_high, x);
  }
};


int main() {
  int N, M;
  cin >> N >> M;
  vector< int64 > A(N), B(M), C(M);
  cin >> A >> B >> C;

  ZAlgorithm< int > z;
  for(int i = 0; i < N; i++) z.add(A[i]);
  z.add(-1);
  for(int i = 0; i < M; i++) z.add(B[i]);

  vector< int64 > dp(M + 1);
  fill_v(dp, infll);
  dp[0] = 0;
  DynamicLiChaoTree< int64, 0, 200001, infll > cht;

  for(int i = 0; i <= M; i++) {
    chmin(dp[i], cht.query(i));
    if(i < M) {
      int len = z.get(N + 1 + i);
      dp[i] -= C[i] * i;
      cht.add_segment(i + 1, i + len + 1, C[i], dp[i]);
    }
  }
  if(dp[M] >= infll) cout << -1 << endl;
  else cout << dp[M] << endl;
}
0