結果

問題 No.2549 Paint Eggs
ユーザー ryota2357ryota2357
提出日時 2023-10-25 13:51:57
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 184 ms / 2,000 ms
コード長 3,002 bytes
コンパイル時間 933 ms
コンパイル使用メモリ 113,068 KB
実行使用メモリ 11,308 KB
最終ジャッジ日時 2023-10-25 13:52:06
合計ジャッジ時間 8,629 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 3 ms
4,372 KB
testcase_03 AC 2 ms
4,372 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 2 ms
4,372 KB
testcase_06 AC 2 ms
4,372 KB
testcase_07 AC 2 ms
4,372 KB
testcase_08 AC 1 ms
4,372 KB
testcase_09 AC 2 ms
4,372 KB
testcase_10 AC 136 ms
10,252 KB
testcase_11 AC 129 ms
10,780 KB
testcase_12 AC 106 ms
7,416 KB
testcase_13 AC 131 ms
10,252 KB
testcase_14 AC 93 ms
9,724 KB
testcase_15 AC 10 ms
4,372 KB
testcase_16 AC 119 ms
7,944 KB
testcase_17 AC 85 ms
6,888 KB
testcase_18 AC 69 ms
5,568 KB
testcase_19 AC 99 ms
7,416 KB
testcase_20 AC 174 ms
11,308 KB
testcase_21 AC 175 ms
11,308 KB
testcase_22 AC 182 ms
11,308 KB
testcase_23 AC 179 ms
11,308 KB
testcase_24 AC 184 ms
11,308 KB
testcase_25 AC 165 ms
11,308 KB
testcase_26 AC 166 ms
11,308 KB
testcase_27 AC 168 ms
11,308 KB
testcase_28 AC 180 ms
11,308 KB
testcase_29 AC 174 ms
11,308 KB
testcase_30 AC 178 ms
11,308 KB
testcase_31 AC 169 ms
11,308 KB
testcase_32 AC 175 ms
11,308 KB
testcase_33 AC 171 ms
11,308 KB
testcase_34 AC 168 ms
11,308 KB
testcase_35 AC 182 ms
11,308 KB
testcase_36 AC 178 ms
11,308 KB
testcase_37 AC 183 ms
11,308 KB
testcase_38 AC 168 ms
11,308 KB
testcase_39 AC 172 ms
11,308 KB
testcase_40 AC 180 ms
11,308 KB
testcase_41 AC 178 ms
11,308 KB
testcase_42 AC 179 ms
11,308 KB
testcase_43 AC 179 ms
11,308 KB
testcase_44 AC 181 ms
11,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <iostream>
#include <vector>
using namespace std;

#define INFL (2305843009213693951ll)

template <class T>
inline void chmin(T& a, const T& b) {
    if (a > b) a = b;
}

template <class T, T (*op)(T, T), T e>
struct SegmentTree
{
    int _originSize, _size, _log2;
    vector<T> _data;
    SegmentTree(int size) : SegmentTree(vector<T>(size, e)) {}
    SegmentTree(const vector<T>& vec) : _originSize(vec.size()), _size(1), _log2(1) {
        while (_originSize > 1 << _log2) {
            ++_log2;
        }
        _size <<= _log2;
        _data = vector<T>(2 * _size, e);
        for (int i = 0; i < _originSize; ++i) {
            _data[i + _size] = vec[i];
        }
        for (int i = _size - 1; i >= 1; --i) {
            _data[i] = op(_data[2 * i + 1], _data[2 * i]);
        }
    }

    void update(const int& index, const T value) {
        assert((uint)index < (uint)_size);
        int pos = index + _size;
        _data[pos] = value;
        for (int i = 1; i <= _log2; ++i) {
            int t = pos >> i;
            _data[t] = op(_data[2 * t + 1], _data[2 * t]);
        }
    }

    inline T get(const int& index) const {
        assert((uint)index < (uint)_size);
        return _data[index + _size];
    }

    T query(const int& l, const int& r) const {
        assert(0 <= l && l <= r && r <= _size);
        int nl = l + _size;
        int nr = r + _size;
        T retl = e, retr = e;
        while (nl < nr) {
            if (nl & 1) {
                retl = op(retl, _data[nl]);
                ++nl;
            }
            if (nr & 1) {
                --nr;
                retr = op(retr, _data[nr]);
            }
            nl >>= 1;
            nr >>= 1;
        }
        return op(retl, retr);
    }
};

long long min_op(long long a, long long b) {
    return min(a, b);
}

int main() {
    int n, m, k;
    cin >> n >> m >> k;
    vector<int> c(n);
    vector<long long> a(m);
    for (int i = 0; i < n; ++i) {
        cin >> c[i];
    }
    for (int i = 0; i < m; ++i) {
        cin >> a[i];
    }

    assert(1 <= k && k <= 200000);
    assert(1 <= n && n <= 200000);
    assert(1 <= m && m <= 200000);
    for (int i = 0; i < n; ++i) {
        assert(1 <= c[i] && c[i] <= m);
    }
    for (int i = 0; i < m; ++i) {
        assert(1 <= a[i] && a[i] <= 1000000000);
    }

    for (int i = 0; i < n; ++i) {
        c[i] -= 1;
    }

    SegmentTree<long long, min_op, INFL> st(m);
    for (int color = 0; color < m; ++color) {
        st.update(color, a[color] * k);
    }
    for (int i = 0; i < k; ++i) {
        int color = c[i];
        st.update(color, st.get(color) - a[color]);
    }

    long long ans = st.query(0, m);
    for (int i = k; i < n; ++i) {
        int rm_color = c[i - k];
        int in_color = c[i];
        st.update(rm_color, st.get(rm_color) + a[rm_color]);
        st.update(in_color, st.get(in_color) - a[in_color]);
        chmin(ans, st.query(0, m));
    }

    cout << ans << endl;
    return 0;
}
0