結果

問題 No.2332 Make a Sequence
ユーザー ei1333333ei1333333
提出日時 2023-05-28 14:11:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 249 ms / 2,000 ms
コード長 6,732 bytes
コンパイル時間 3,012 ms
コンパイル使用メモリ 213,980 KB
実行使用メモリ 44,092 KB
最終ジャッジ日時 2023-08-27 09:12:32
合計ジャッジ時間 17,175 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 144 ms
28,796 KB
testcase_08 AC 34 ms
13,800 KB
testcase_09 AC 112 ms
24,440 KB
testcase_10 AC 124 ms
22,860 KB
testcase_11 AC 86 ms
17,484 KB
testcase_12 AC 170 ms
33,516 KB
testcase_13 AC 170 ms
35,148 KB
testcase_14 AC 169 ms
34,160 KB
testcase_15 AC 171 ms
33,996 KB
testcase_16 AC 172 ms
33,872 KB
testcase_17 AC 170 ms
34,040 KB
testcase_18 AC 170 ms
33,384 KB
testcase_19 AC 171 ms
34,480 KB
testcase_20 AC 171 ms
34,952 KB
testcase_21 AC 172 ms
33,616 KB
testcase_22 AC 171 ms
33,676 KB
testcase_23 AC 171 ms
35,152 KB
testcase_24 AC 173 ms
34,188 KB
testcase_25 AC 169 ms
34,292 KB
testcase_26 AC 171 ms
34,624 KB
testcase_27 AC 171 ms
34,524 KB
testcase_28 AC 172 ms
34,032 KB
testcase_29 AC 171 ms
33,996 KB
testcase_30 AC 172 ms
33,548 KB
testcase_31 AC 172 ms
33,960 KB
testcase_32 AC 179 ms
34,676 KB
testcase_33 AC 177 ms
34,796 KB
testcase_34 AC 176 ms
34,812 KB
testcase_35 AC 178 ms
34,768 KB
testcase_36 AC 177 ms
35,064 KB
testcase_37 AC 178 ms
35,892 KB
testcase_38 AC 177 ms
36,344 KB
testcase_39 AC 180 ms
37,900 KB
testcase_40 AC 176 ms
35,852 KB
testcase_41 AC 175 ms
34,916 KB
testcase_42 AC 196 ms
42,004 KB
testcase_43 AC 197 ms
41,932 KB
testcase_44 AC 201 ms
41,580 KB
testcase_45 AC 203 ms
43,496 KB
testcase_46 AC 202 ms
44,092 KB
testcase_47 AC 249 ms
41,888 KB
testcase_48 AC 249 ms
41,392 KB
testcase_49 AC 249 ms
41,184 KB
testcase_50 AC 248 ms
42,436 KB
testcase_51 AC 247 ms
42,024 KB
testcase_52 AC 171 ms
35,248 KB
testcase_53 AC 171 ms
33,700 KB
testcase_54 AC 171 ms
33,800 KB
testcase_55 AC 172 ms
33,624 KB
testcase_56 AC 172 ms
33,612 KB
testcase_57 AC 170 ms
34,976 KB
testcase_58 AC 168 ms
33,652 KB
testcase_59 AC 170 ms
34,248 KB
testcase_60 AC 170 ms
34,660 KB
testcase_61 AC 169 ms
33,356 KB
testcase_62 AC 211 ms
41,144 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