結果

問題 No.2332 Make a Sequence
ユーザー t98slidert98slider
提出日時 2023-05-28 17:44:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 3,822 bytes
コンパイル時間 2,710 ms
コンパイル使用メモリ 177,796 KB
実行使用メモリ 25,428 KB
最終ジャッジ日時 2023-08-27 14:46:06
合計ジャッジ時間 12,843 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 66 ms
9,328 KB
testcase_08 AC 26 ms
5,852 KB
testcase_09 AC 55 ms
8,128 KB
testcase_10 AC 50 ms
8,440 KB
testcase_11 AC 36 ms
6,732 KB
testcase_12 AC 81 ms
11,212 KB
testcase_13 AC 81 ms
11,880 KB
testcase_14 AC 81 ms
10,988 KB
testcase_15 AC 81 ms
11,524 KB
testcase_16 AC 80 ms
11,136 KB
testcase_17 AC 80 ms
10,996 KB
testcase_18 AC 80 ms
11,012 KB
testcase_19 AC 81 ms
11,496 KB
testcase_20 AC 81 ms
12,128 KB
testcase_21 AC 81 ms
11,300 KB
testcase_22 AC 81 ms
11,052 KB
testcase_23 AC 82 ms
11,708 KB
testcase_24 AC 82 ms
10,980 KB
testcase_25 AC 82 ms
11,408 KB
testcase_26 AC 81 ms
11,024 KB
testcase_27 AC 82 ms
11,972 KB
testcase_28 AC 83 ms
12,080 KB
testcase_29 AC 83 ms
12,220 KB
testcase_30 AC 83 ms
12,088 KB
testcase_31 AC 84 ms
12,108 KB
testcase_32 AC 96 ms
16,836 KB
testcase_33 AC 96 ms
16,848 KB
testcase_34 AC 96 ms
16,944 KB
testcase_35 AC 96 ms
16,724 KB
testcase_36 AC 95 ms
16,776 KB
testcase_37 AC 95 ms
16,332 KB
testcase_38 AC 96 ms
16,708 KB
testcase_39 AC 102 ms
18,104 KB
testcase_40 AC 95 ms
16,248 KB
testcase_41 AC 93 ms
15,404 KB
testcase_42 AC 137 ms
24,724 KB
testcase_43 AC 136 ms
24,796 KB
testcase_44 AC 131 ms
24,528 KB
testcase_45 AC 137 ms
25,416 KB
testcase_46 AC 137 ms
25,428 KB
testcase_47 AC 165 ms
24,200 KB
testcase_48 AC 164 ms
24,172 KB
testcase_49 AC 164 ms
24,108 KB
testcase_50 AC 165 ms
24,176 KB
testcase_51 AC 165 ms
24,356 KB
testcase_52 AC 86 ms
13,164 KB
testcase_53 AC 85 ms
12,524 KB
testcase_54 AC 84 ms
12,156 KB
testcase_55 AC 86 ms
12,900 KB
testcase_56 AC 85 ms
13,028 KB
testcase_57 AC 85 ms
12,068 KB
testcase_58 AC 84 ms
12,100 KB
testcase_59 AC 83 ms
12,076 KB
testcase_60 AC 85 ms
12,896 KB
testcase_61 AC 83 ms
12,016 KB
testcase_62 AC 167 ms
21,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template <class T> std::vector<int> z_algorithm(const std::vector<T>& s) {
    int n = int(s.size());
    if (n == 0) return {};
    std::vector<int> z(n);
    z[0] = 0;
    for (int i = 1, j = 0; i < n; i++) {
        int& k = z[i];
        k = (j + z[j] <= i) ? 0 : std::min(j + z[j] - i, z[i - j]);
        while (i + k < n && s[k] == s[i + k]) k++;
        if (j + z[j] < i + z[i]) j = i;
    }
    z[0] = n;
    return z;
}
std::vector<int> z_algorithm(const std::string& s) {
    int n = int(s.size());
    std::vector<int> s2(n);
    for (int i = 0; i < n; i++) {
        s2[i] = s[i];
    }
    return z_algorithm(s2);
}

class LiChaoTree{
    struct Line{
        long long a, b;
        long long get(long long x){return a * x + b; }
        Line(long long a, long long b) : a(a), b(b) {}
    };
    struct Node {
        Node *left, *right;
        Line line;
        Node(Line line) : left(nullptr), right(nullptr), line(line) {}
    };
    const long long inf = 1ll << 60;
    const Line inf_line = Line{0, inf};

    Node *root;
    long long lx, rx;
    Node* _add_line(Node *nd, Line line, long long l, long long r){
        if(l == r) return nullptr;
        if(nd == nullptr) return new Node(line);
        long long m = (l + r) >> 1;

        bool left = (line.get(l) <= nd->line.get(l));
        bool mid = (line.get(m) <= nd->line.get(m));
        bool right = (line.get(r) <= nd->line.get(r));
        if(left && right)nd->line = line;
        if(left == right)return nd;
        if(mid) std::swap(nd->line, line);
        if(left != mid){
            nd->left = _add_line(nd->left, line, l, m);
        }else{
            nd->right = _add_line(nd->right, line, m, r);
        }
        return nd;
    }
    Node* _add_segment_line(long long a, long long b, Node *nd, Line line, long long l, long long r) {
        if(r <= a || b <= l) return nd;
        if(a <= l && r <= b) return _add_line(nd, line, l, r);
        if(nd == nullptr) nd = new Node(inf_line);
        long long m = (l + r) >> 1;
        nd->left = _add_segment_line(a, b, nd->left, line, l, m);
        nd->right = _add_segment_line(a, b, nd->right, line, m, r);
        return nd;
    }
    long long query(long long x, long long l, long long r){
        Node *nd = root;
        long long res = inf;
        while(r > l && nd != nullptr) {
            long long m = (l + r) >> 1;
            res = std::min(res, nd->line.get(x));
            if(x < m) {
                r = m;
                nd = nd->left;
            } else {
                l = m;
                nd = nd->right;
            }
        }
        return res;
    }
    public:
    // [xl, xr)
    LiChaoTree(long long lx, long long rx) : lx(lx), rx(rx), root(nullptr) {}
    void add_line(long long a, long long b) {
        Line line(a, b);
        root = _add_line(root, line, lx, rx);
    }
    void add_segment_line(long long a, long long b, long long l, long long r) {
        Line line = Line{a, b};
        root = _add_segment_line(l, r, root, line, lx, rx);
    }
    long long get(long long x) {
        return query(x, lx, rx);
    }
};


int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    vector<ll> a(n), b(m), c(m);
    for(auto &&v : a) cin >> v;
    for(auto &&v : b) cin >> v;
    for(auto &&v : c) cin >> v;
    a.emplace_back(-1);
    a.insert(a.end(), b.begin(), b.end());
    auto z = z_algorithm(a);
    LiChaoTree LT(0, m + 1);
    n++;
    LT.add_segment_line(0, 0, 0, 1);
    for(int i = 0; i < m; i++){
        if(z[i + n] == 0) continue;
        ll v = LT.get(i);
        LT.add_segment_line(c[i], -c[i] * i + v, i, i + z[i + n] + 1);
    }
    ll ans = LT.get(m);
    if(ans >> 60) ans = -1;
    cout << ans << '\n';
}
0