結果

問題 No.777 再帰的ケーキ
ユーザー PachicobuePachicobue
提出日時 2018-12-24 02:38:01
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 485 ms / 2,000 ms
コード長 2,734 bytes
コンパイル時間 2,474 ms
コンパイル使用メモリ 221,220 KB
実行使用メモリ 33,884 KB
最終ジャッジ日時 2023-10-26 02:55:51
合計ジャッジ時間 6,959 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 4 ms
4,348 KB
testcase_22 AC 4 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 4 ms
4,348 KB
testcase_26 AC 4 ms
4,348 KB
testcase_27 AC 3 ms
4,348 KB
testcase_28 AC 457 ms
33,884 KB
testcase_29 AC 475 ms
33,884 KB
testcase_30 AC 485 ms
33,884 KB
testcase_31 AC 468 ms
33,884 KB
testcase_32 AC 239 ms
33,884 KB
testcase_33 AC 58 ms
10,456 KB
testcase_34 AC 84 ms
11,972 KB
testcase_35 AC 285 ms
22,796 KB
testcase_36 AC 245 ms
33,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define show(x) std::cerr << #x << " = " << (x) << std::endl
using ll = long long;
using ull = unsigned long long;
template <typename T>
constexpr T INF = std::numeric_limits<T>::max() / 10;
struct Cake
{
    ll A, B, C;
    bool operator<(const Cake& c) const { return A != c.A ? A < c.A : B != c.B ? B > c.B : C > c.C; }
};
constexpr std::size_t PC(ull v) { return v = (v & 0x5555555555555555ULL) + (v >> 1 & 0x5555555555555555ULL), v = (v & 0x3333333333333333ULL) + (v >> 2 & 0x3333333333333333ULL), v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL, static_cast<std::size_t>(v * 0x0101010101010101ULL >> 56 & 0x7f); }
constexpr std::size_t LG(ull v) { return v == 0 ? 0 : (v--, v |= (v >> 1), v |= (v >> 2), v |= (v >> 4), v |= (v >> 8), v |= (v >> 16), v |= (v >> 32), PC(v)); }
constexpr ull SZ(const ull v) { return 1ULL << LG(v); }
template <typename Monoid>
class SegmentTree
{
public:
    using BaseMonoid = Monoid;
    using T = typename Monoid::T;
    SegmentTree(const std::size_t n) : data_num(n), half(SZ(n)), value(half << 1, Monoid::id()) {}
    T get(const std::size_t a) const { return value[a + half]; }
    void set(std::size_t a, const T& val)
    {
        value[a += half] = val;
        while (a >>= 1) { up(a); }
    }
    T accumulate(std::size_t L, std::size_t R) const
    {
        T accl = Monoid::id(), accr = Monoid::id();
        for (L += half, R += half; L < R; L >>= 1, R >>= 1) {
            if (L & 1) { accl = acc(accl, value[L++]); }
            if (R & 1) { accr = acc(value[--R], accr); }
        }
        return acc(accl, accr);
    }

private:
    void up(const std::size_t i) { value[i] = acc(value[i << 1], value[i << 1 | 1]); }
    const std::size_t data_num, half;
    std::vector<T> value;
    const Monoid acc{};
};
struct Max
{
    using T = ll;
    T operator()(const T& a, const T& b) const { return std::max(a, b); }
    static constexpr T id() { return -INF<T>; }
};

int main()
{
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    int N;
    std::cin >> N;
    std::set<ll> st;
    std::vector<Cake> cake(N);
    for (int i = 0; i < N; i++) { std::cin >> cake[i].A >> cake[i].B >> cake[i].C, st.insert(cake[i].B); }
    std::map<ll, int> zip;
    for (const ll e : st) {
        static int i = 1;
        zip[e] = i, i++;
    }
    for (int i = 0; i < N; i++) { cake[i].B = zip[cake[i].B]; }
    std::sort(cake.begin(), cake.end());
    SegmentTree<Max> seg(N + 1);
    seg.set(0, 0LL);
    for (int i = 0; i < N; i++) {
        const ll B = cake[i].B, C = cake[i].C;
        const ll max = std::max(seg.get(B), seg.accumulate(0, B) + C);
        seg.set(B, max);
    }
    std::cout << seg.accumulate(0, 2 * N + 2) << std::endl;
    return 0;
}
0