結果

問題 No.1826 Fruits Collecting
コンテスト
ユーザー KKT89
提出日時 2026-05-10 00:06:24
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 106 ms / 2,000 ms
コード長 2,470 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,502 ms
コンパイル使用メモリ 353,308 KB
実行使用メモリ 19,688 KB
最終ジャッジ日時 2026-05-10 00:06:35
合計ジャッジ時間 8,278 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }

template <class S, S (*op)(S, S), S (*e)()> struct segtree {
    int n;
    vector<S> tree;
    segtree() : segtree(0) {}
    segtree(int n) : n(n), tree(vector<S>(n << 1, e())) {}
    segtree(const vector<S> &v) : n((int)v.size()) {
        tree.resize(n * 2);
        for (int i = 0; i < n; ++i) {
            tree[n + i] = v[i];
        }
        for (int i = n - 1; i >= 1; --i) {
            update(i);
        }
    }
    void update(int k) { tree[k] = op(tree[k << 1 | 0], tree[k << 1 | 1]); }
    S operator[](int i) { return tree[i + n]; }
    void set(int i, S x) {
        i += n;
        tree[i] = x;
        while (i >>= 1) {
            update(i);
        }
    }
    // [l,r)
    S query(int l, int r) {
        S sml = e(), smr = e();
        for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if (l & 1) sml = op(sml, tree[l++]);
            if (r & 1) smr = op(tree[--r], smr);
        }
        return op(sml, smr);
    }
};

using S = ll;
S op(S a, S b) { return max(a, b); }
S e() { return -(1LL << 60); }

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    vector<ll> t(n), x(n), va(n);
    for (int i = 0; i < n; ++i) {
        cin >> t[i] >> x[i] >> va[i];
    }

    t.push_back(0);
    x.push_back(0);
    va.push_back(0);
    n += 1;

    vector<tuple<ll, int, ll>> v(n);
    vector<ll> vv = {0};

    for (int i = 0; i < n; ++i) {
        vv.push_back(t[i] - x[i]);
    }

    sort(vv.begin(), vv.end());
    vv.erase(unique(vv.begin(), vv.end()), vv.end());

    for (int i = 0; i < n; ++i) {
        int idx = lower_bound(vv.begin(), vv.end(), t[i] - x[i]) - vv.begin();
        v[i] = {t[i] + x[i], idx, va[i]};
    }

    sort(v.begin(), v.end());

    int m = vv.size();
    segtree<S, op, e> seg(m);

    bool f = false;

    for (int i = 0; i < n; ++i) {
        auto [_, a, b] = v[i];

        if (_ == 0 && vv[a] == 0 && b == 0) {
            seg.set(a, 0);
            f = true;
            continue;
        }

        if (!f) continue;

        ll best = seg.query(0, a + 1);

        // 到達不能
        if (best == e()) continue;

        b += best;

        seg.set(a, max(seg[a], b));
    }

    cout << max(seg.query(0, m), 0LL) << endl;
}
0