結果
問題 | No.2332 Make a Sequence |
ユーザー | dyktr_06 |
提出日時 | 2023-04-18 20:21:48 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 204 ms / 2,000 ms |
コード長 | 4,435 bytes |
コンパイル時間 | 2,975 ms |
コンパイル使用メモリ | 207,228 KB |
実行使用メモリ | 29,824 KB |
最終ジャッジ日時 | 2024-10-13 20:24:02 |
合計ジャッジ時間 | 18,085 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 61 |
ソースコード
#include <bits/stdc++.h> using namespace std; 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 = nullptr; DynamicLiChaoTree() {} private: 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(t_l <= x_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(x_l <= t_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; } } 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; } T query_(const Node *t, const T &l, const T &r, const T &x_c) const { if(!t) return id; if(l == r) return t->x.get(x_c); T m = (l + r) / 2; if(m == r) --m; if(x_c <= m){ return min(t->x.get(x_c), query_(t->l, l, m, x_c)); }else{ return min(t->x.get(x_c), query_(t->r, m + 1, r, x_c)); } } public: 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)); } 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 T &x) const { return query_(root, x_low, x_high, x); } }; template <typename T> vector<int> z_algorithm(const T &s) { int n = s.size(); 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] = 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; } const long long INF = 1LL << 60; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<int> a(n), b(m); vector<long long> c(m); for(int i = 0; i < n; i++){ cin >> a[i]; } for(int i = 0; i < m; i++){ cin >> b[i]; } for(int i = 0; i < m; i++){ cin >> c[i]; } vector<int> d(n + m); for(int i = 0; i < n; i++){ d[i] = a[i]; } for(int i = 0; i < m; i++){ d[i + n] = b[i]; } vector<int> z = z_algorithm(d); DynamicLiChaoTree<long long, 0, 1 << 20, INF> dlct; vector<long long> dp(m + 1, INF); dp[0] = 0; for(int i = 0; i <= m; i++){ if(i >= 1){ dp[i] = min(dp[i], dlct.query(i)); } if(i < m){ int add = min(n, z[i + n]); dlct.add_segment(i, i + add + 1, c[i], dp[i] - c[i] * i); } } if(dp[m] == INF){ cout << -1 << endl; }else{ cout << dp[m] << endl; } }