結果

問題 No.844 split game
ユーザー kibunakibuna
提出日時 2019-06-28 23:26:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 165 ms / 2,000 ms
コード長 2,969 bytes
コンパイル時間 1,815 ms
コンパイル使用メモリ 177,400 KB
実行使用メモリ 10,452 KB
最終ジャッジ日時 2023-09-14 22:44:12
合計ジャッジ時間 7,145 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
5,944 KB
testcase_01 AC 64 ms
8,176 KB
testcase_02 AC 118 ms
7,272 KB
testcase_03 AC 99 ms
8,532 KB
testcase_04 AC 47 ms
5,628 KB
testcase_05 AC 142 ms
9,504 KB
testcase_06 AC 45 ms
5,764 KB
testcase_07 AC 48 ms
7,652 KB
testcase_08 AC 23 ms
4,628 KB
testcase_09 AC 92 ms
5,740 KB
testcase_10 AC 145 ms
8,020 KB
testcase_11 AC 150 ms
9,756 KB
testcase_12 AC 88 ms
5,424 KB
testcase_13 AC 55 ms
4,572 KB
testcase_14 AC 69 ms
6,844 KB
testcase_15 AC 164 ms
9,860 KB
testcase_16 AC 164 ms
10,452 KB
testcase_17 AC 163 ms
9,912 KB
testcase_18 AC 164 ms
10,256 KB
testcase_19 AC 163 ms
9,908 KB
testcase_20 AC 163 ms
9,908 KB
testcase_21 AC 162 ms
9,860 KB
testcase_22 AC 165 ms
9,908 KB
testcase_23 AC 6 ms
4,376 KB
testcase_24 AC 12 ms
4,376 KB
testcase_25 AC 6 ms
4,376 KB
testcase_26 AC 4 ms
4,380 KB
testcase_27 AC 9 ms
4,380 KB
testcase_28 AC 4 ms
4,376 KB
testcase_29 AC 10 ms
4,380 KB
testcase_30 AC 11 ms
4,376 KB
testcase_31 AC 9 ms
4,376 KB
testcase_32 AC 4 ms
4,376 KB
testcase_33 AC 10 ms
4,376 KB
testcase_34 AC 7 ms
4,376 KB
testcase_35 AC 13 ms
4,380 KB
testcase_36 AC 8 ms
4,376 KB
testcase_37 AC 18 ms
4,376 KB
testcase_38 AC 38 ms
4,584 KB
testcase_39 AC 84 ms
6,324 KB
testcase_40 AC 37 ms
6,052 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 1 ms
4,376 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 1 ms
4,376 KB
testcase_47 AC 2 ms
4,376 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,376 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 2 ms
4,376 KB
testcase_52 AC 2 ms
4,380 KB
testcase_53 AC 2 ms
4,380 KB
testcase_54 AC 2 ms
4,376 KB
testcase_55 AC 2 ms
4,380 KB
testcase_56 AC 2 ms
4,380 KB
testcase_57 AC 2 ms
4,380 KB
testcase_58 AC 2 ms
4,376 KB
testcase_59 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T>
bool chmax(T &a, const T &b) {
    return (a < b) ? (a = b, 1) : 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    return (b < a) ? (a = b, 1) : 0;
}

template <typename T = int>
struct SegmentTree {
    using F = function<T(T, T)>;
    int n;
    vector<T> dat;
    F func;
    T UNIT;

    SegmentTree(int n_, F func_, T UNIT_) : func(func_), UNIT(UNIT_) {
        n = 1;
        // full binary tree: num of leaves = n = 2^k >= n_
        while (n < n_)
            n *= 2;
        dat.assign(2 * n - 1, UNIT);
    }
    SegmentTree(vector<T> v_, F func_, T UNIT_) : func(func_), UNIT(UNIT_) {
        n = 1;
        while (n < v_.size())
            n *= 2;
        dat.assign(2 * n - 1, UNIT);
        for (int i = 0; i < v_.size(); ++i) {
            dat[n - 1 + i] = v_[i];
        }
        for (int i = n - 2; i >= 0; --i) {
            dat[i] = func(dat[2 * i + 1], dat[2 * i + 2]);
        }
    }
    void update(int k, T a) {
        // leaves are at index n-1 to 2*n-2
        k += n - 1;
        dat[k] = a;
        while (k > 0) {
            // k -> parent node
            k = (k - 1) / 2;
            // func(child nodes)
            dat[k] = func(dat[2 * k + 1], dat[2 * k + 2]);
        }
    }
    T query(int a, int b, T k, int l, int r) {
        if (r <= a || b <= l)
            return UNIT;
        if (a <= l && r <= b)
            return dat[k];
        else {
            T vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
            T vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
            return func(vl, vr);
        }
    }
    // get result of func() in [a, b)
    T query(int a, int b) { return query(a, b, 0, 0, n); }
};

int main() {
    ll n, m, a;
    cin >> n >> m >> a;
    vector<vector<pair<int, ll>>> rlp(n + 1);
    for (int i = 0; i < m; ++i) {
        ll l, r, p;
        cin >> l >> r >> p;
        l--;
        r--;
        rlp[r].emplace_back(l, p);
    }
    auto f = [](ll l, ll r) { return max(l, r); };
    SegmentTree<ll> dp(n + 2, f, -INF); // max score when you add line at n
    dp.update(0, 0);
    // vl dp(n + 1, 0);
    // 0 1 2 3 4 5
    //  0 1 2 3 4 5
    for (int i = 0; i <= n; ++i) {
        if (rlp[i].size() == 0) {
            ll temp = dp.query(0, i + 1) - a;
            dp.update(i + 1, temp);
        } else {
            ll tt = dp.query(0, i + 1) - a;
            for (auto &lp : rlp[i]) {
                ll temp = dp.query(lp.first, lp.first + 1) + lp.second;
                if (i != n - 1)
                    temp -= a;
                chmax(tt, temp);
            }
            dp.update(i + 1, tt);
        }
    }
    ll ret = dp.query(0, n + 2);
    cout << ret << "\n";
    return 0;
}
0