結果

問題 No.2332 Make a Sequence
ユーザー ForestedForested
提出日時 2023-05-28 14:05:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 201 ms / 2,000 ms
コード長 6,462 bytes
コンパイル時間 1,429 ms
コンパイル使用メモリ 136,240 KB
実行使用メモリ 23,808 KB
最終ジャッジ日時 2024-06-08 04:39:26
合計ジャッジ時間 10,828 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 56 ms
8,192 KB
testcase_08 AC 21 ms
5,376 KB
testcase_09 AC 45 ms
7,552 KB
testcase_10 AC 44 ms
6,912 KB
testcase_11 AC 36 ms
5,888 KB
testcase_12 AC 70 ms
9,472 KB
testcase_13 AC 70 ms
9,600 KB
testcase_14 AC 69 ms
9,472 KB
testcase_15 AC 68 ms
9,472 KB
testcase_16 AC 69 ms
9,472 KB
testcase_17 AC 68 ms
9,472 KB
testcase_18 AC 69 ms
9,472 KB
testcase_19 AC 69 ms
9,344 KB
testcase_20 AC 70 ms
9,344 KB
testcase_21 AC 70 ms
9,472 KB
testcase_22 AC 71 ms
9,600 KB
testcase_23 AC 71 ms
9,728 KB
testcase_24 AC 71 ms
9,856 KB
testcase_25 AC 72 ms
9,728 KB
testcase_26 AC 72 ms
9,728 KB
testcase_27 AC 77 ms
10,624 KB
testcase_28 AC 86 ms
10,624 KB
testcase_29 AC 76 ms
10,624 KB
testcase_30 AC 76 ms
10,624 KB
testcase_31 AC 76 ms
10,752 KB
testcase_32 AC 91 ms
15,232 KB
testcase_33 AC 92 ms
15,232 KB
testcase_34 AC 91 ms
15,232 KB
testcase_35 AC 93 ms
15,232 KB
testcase_36 AC 90 ms
15,360 KB
testcase_37 AC 89 ms
13,952 KB
testcase_38 AC 91 ms
14,208 KB
testcase_39 AC 93 ms
15,616 KB
testcase_40 AC 91 ms
13,952 KB
testcase_41 AC 85 ms
13,312 KB
testcase_42 AC 124 ms
21,120 KB
testcase_43 AC 124 ms
20,864 KB
testcase_44 AC 123 ms
20,864 KB
testcase_45 AC 147 ms
21,760 KB
testcase_46 AC 126 ms
21,632 KB
testcase_47 AC 199 ms
23,808 KB
testcase_48 AC 198 ms
23,808 KB
testcase_49 AC 199 ms
23,808 KB
testcase_50 AC 199 ms
23,808 KB
testcase_51 AC 201 ms
23,808 KB
testcase_52 AC 85 ms
11,520 KB
testcase_53 AC 76 ms
10,880 KB
testcase_54 AC 76 ms
10,624 KB
testcase_55 AC 79 ms
11,264 KB
testcase_56 AC 78 ms
11,520 KB
testcase_57 AC 77 ms
10,496 KB
testcase_58 AC 76 ms
10,624 KB
testcase_59 AC 76 ms
10,624 KB
testcase_60 AC 80 ms
11,136 KB
testcase_61 AC 78 ms
10,496 KB
testcase_62 AC 150 ms
20,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef LOCAL
#define FAST_IO
#endif

// ============
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define OVERRIDE(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32)(n); ++i)
#define REP3(i, m, n) for (i32 i = (i32)(m); i < (i32)(n); ++i)
#define REP(...) OVERRIDE(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER(i, n) for (i32 i = (i32)(n) - 1; i >= 0; --i)
#define ALL(x) begin(x), end(x)

using namespace std;

using u32 = unsigned int;
using u64 = unsigned long long;
using i32 = signed int;
using i64 = signed long long;
using f64 = double;
using f80 = long double;

template <typename T>
using Vec = vector<T>;

template <typename T>
bool chmin(T &x, const T &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}

template <typename T>
Vec<tuple<i32, i32, T>> runlength(const Vec<T> &a) {
    if (a.empty()) {
        return Vec<tuple<i32, i32, T>>();
    }
    Vec<tuple<i32, i32, T>> ret;
    i32 prv = 0;
    REP(i, 1, a.size()) {
        if (a[i - 1] != a[i]) {
            ret.emplace_back(prv, i, a[i - 1]);
            prv = i;
        }
    }
    ret.emplace_back(prv, (i32)a.size(), a.back());
    return ret;
}

#ifdef INT128

using u128 = __uint128_t;
using i128 = __int128_t;

istream &operator>>(istream &is, i128 &x) {
    i64 v;
    is >> v;
    x = v;
    return is;
}
ostream &operator<<(ostream &os, i128 x) {
    os << (i64)x;
    return os;
}
istream &operator>>(istream &is, u128 &x) {
    u64 v;
    is >> v;
    x = v;
    return is;
}
ostream &operator<<(ostream &os, u128 x) {
    os << (u64)x;
    return os;
}

#endif

[[maybe_unused]] constexpr i32 INF = 1000000100;
[[maybe_unused]] constexpr i64 INF64 = 3000000000000000100;
struct SetUpIO {
    SetUpIO() {
#ifdef FAST_IO
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
#endif
        cout << fixed << setprecision(15);
    }
} set_up_io;
// ============

#ifdef DEBUGF
#else
#define DBG(x) (void)0
#endif

// ============

#include <vector>

template <typename Cont>
std::vector<int> z_algorithm(const Cont &s) {
    if (s.empty()) {
        return std::vector<int>(0);
    }
    std::vector<int> z(s.size());
    z[0] = (int) s.size();
    int i = 1, j = 0;
    while (i < (int) s.size()) {
        while (i + j < (int) s.size() && s[i + j] == s[j]) {
            ++j;
        }
        z[i] = j;
        if (j == 0) {
            ++i;
            continue;
        }
        int k = 1;
        while (k < j && k + z[k] < j) {
            z[i + k] = z[k];
            ++k;
        }
        i += k;
        j -= k;
    }
    return z;
}

// ============

// https://ei1333.github.io/library/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() {
    i32 n, m;
    cin >> n >> m;
    Vec<i32> a(n), b(m);
    Vec<i64> c(m);
    REP(i, n) {
        cin >> a[i];
    }
    REP(i, m) {
        cin >> b[i];
    }
    REP(i, m) {
        cin >> c[i];
    }
    Vec<i32> conc(n + m + 1);
    REP(i, n) {
        conc[i] = a[i];
    }
    conc[n] = -1;
    REP(i, m) {
        conc[n + 1 + i] = b[i];
    }
    Vec<i32> z = z_algorithm(conc);
    z.erase(z.begin(), z.begin() + (n + 1));
    DynamicLiChaoTree<i64, -1, 1000000, INF64> lct;
    lct.add_segment(0, 1, 0, 0);
    REP(i, m) {
        i64 dp = lct.query(i);
        if (z[i] != 0) {
            lct.add_segment(i + 1, i + z[i] + 1, c[i], dp - i * c[i]);
        }
    }
    i64 ans = lct.query(m);
    if (ans == INF64) {
        cout << -1 << '\n';
    } else {
        cout << ans << '\n';
    }
}
0