結果

問題 No.2332 Make a Sequence
ユーザー dyktr_06dyktr_06
提出日時 2023-04-20 13:07:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 246 ms / 2,000 ms
コード長 4,435 bytes
コンパイル時間 2,311 ms
コンパイル使用メモリ 207,488 KB
実行使用メモリ 29,824 KB
最終ジャッジ日時 2024-10-15 11:41:01
合計ジャッジ時間 20,154 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,248 KB
testcase_02 AC 3 ms
5,248 KB
testcase_03 AC 1 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 143 ms
25,472 KB
testcase_08 AC 26 ms
5,760 KB
testcase_09 AC 110 ms
19,328 KB
testcase_10 AC 130 ms
24,832 KB
testcase_11 AC 111 ms
17,280 KB
testcase_12 AC 173 ms
29,824 KB
testcase_13 AC 246 ms
29,824 KB
testcase_14 AC 197 ms
29,824 KB
testcase_15 AC 196 ms
29,824 KB
testcase_16 AC 196 ms
29,824 KB
testcase_17 AC 170 ms
29,696 KB
testcase_18 AC 171 ms
29,696 KB
testcase_19 AC 173 ms
29,824 KB
testcase_20 AC 173 ms
29,824 KB
testcase_21 AC 172 ms
29,824 KB
testcase_22 AC 170 ms
29,824 KB
testcase_23 AC 169 ms
29,696 KB
testcase_24 AC 171 ms
29,696 KB
testcase_25 AC 169 ms
29,696 KB
testcase_26 AC 174 ms
29,696 KB
testcase_27 AC 174 ms
29,184 KB
testcase_28 AC 170 ms
29,312 KB
testcase_29 AC 170 ms
29,440 KB
testcase_30 AC 169 ms
29,440 KB
testcase_31 AC 204 ms
29,312 KB
testcase_32 AC 195 ms
28,032 KB
testcase_33 AC 194 ms
27,776 KB
testcase_34 AC 181 ms
27,904 KB
testcase_35 AC 184 ms
27,776 KB
testcase_36 AC 193 ms
27,904 KB
testcase_37 AC 172 ms
28,928 KB
testcase_38 AC 183 ms
28,928 KB
testcase_39 AC 174 ms
28,672 KB
testcase_40 AC 170 ms
28,672 KB
testcase_41 AC 200 ms
29,056 KB
testcase_42 AC 172 ms
26,752 KB
testcase_43 AC 172 ms
26,880 KB
testcase_44 AC 170 ms
27,136 KB
testcase_45 AC 173 ms
27,392 KB
testcase_46 AC 174 ms
27,008 KB
testcase_47 AC 205 ms
25,088 KB
testcase_48 AC 204 ms
25,088 KB
testcase_49 AC 229 ms
25,088 KB
testcase_50 AC 228 ms
24,960 KB
testcase_51 AC 226 ms
24,960 KB
testcase_52 AC 172 ms
29,184 KB
testcase_53 AC 168 ms
29,184 KB
testcase_54 AC 227 ms
29,312 KB
testcase_55 AC 218 ms
29,184 KB
testcase_56 AC 169 ms
29,312 KB
testcase_57 AC 194 ms
29,568 KB
testcase_58 AC 166 ms
29,568 KB
testcase_59 AC 166 ms
29,568 KB
testcase_60 AC 167 ms
29,568 KB
testcase_61 AC 164 ms
29,440 KB
testcase_62 AC 192 ms
27,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
    }
}
0