結果

問題 No.777 再帰的ケーキ
ユーザー toshiki_fulltoshiki_full
提出日時 2019-05-15 00:45:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 504 ms / 2,000 ms
コード長 2,668 bytes
コンパイル時間 2,118 ms
コンパイル使用メモリ 193,148 KB
実行使用メモリ 21,540 KB
最終ジャッジ日時 2023-09-27 11:32:37
合計ジャッジ時間 7,035 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 5 ms
4,380 KB
testcase_22 AC 5 ms
4,380 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 5 ms
4,376 KB
testcase_26 AC 5 ms
4,380 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 456 ms
21,248 KB
testcase_29 AC 460 ms
21,292 KB
testcase_30 AC 504 ms
21,232 KB
testcase_31 AC 500 ms
21,540 KB
testcase_32 AC 336 ms
21,540 KB
testcase_33 AC 105 ms
9,720 KB
testcase_34 AC 163 ms
12,648 KB
testcase_35 AC 406 ms
16,672 KB
testcase_36 AC 321 ms
21,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template< typename T >
class segment_tree {
  const vector<int> coord;
  const map<int, int> mp;
  const int sz;
  const int Sz;
  vector<T> seg;
  const function<T(T, T)> o;
  const T id;

  public:
    segment_tree (
        const vector<int> coord,
        const function<T(T, T)> o,
        const T id
      ) :
      coord([&](){
          vector<int> v = coord;
          int p = 1;
          for (; p < (int)v.size(); p <<= 1) {}
          v.resize(p, (int)((1LL << 31) - 1));
          return v;
        }()),
      mp([&](){
          map<int, int> m;
          int i = 0;
          for (auto const& e : coord) {
            m[e] = i++;
          }
          return m;
        }()),
      sz(coord.size()),
      Sz(sz << 1),
      seg(Sz, id),
      o(o),
      id(id)
      {
      }

    void update (int k, const T& x) {
      auto it = mp.find(k);
      assert(it != mp.end());
      k = it->second;
      k += sz;
      seg[k] = x;
      while(k >>= 1) {
        seg[k] = o(seg[2 * k], seg[2 * k + 1]);
      }
    }
    void add (int k, const T& x) {
      update(k, x + seg[k + sz]);
    }
    T query (int l, int r) {
      l = lower_bound(coord.begin(), coord.end(), l) - coord.begin();
      r = lower_bound(coord.begin(), coord.end(), r) - coord.begin();
      T L = id, R = id;
      for(l += sz, r += sz; l < r; l >>= 1, r >>= 1) {
        if(l & 1) L = o(L, seg[l++]);
        if(r & 1) R = o(seg[--r], R);
      }
      return o(L, R);
    }

    T at (int k) const {
      auto it = mp.find(k);
      assert(it != mp.end());
      return seg[it->second + sz];
    }
};
int main() {
  int n;
  cin >> n;
  vector<int> coord(n, 0);
  vector<tuple<int, int, long long>> abc(n);
  for (int i = 0; i < n; i++) {
    int a, b; long long c;
    cin >> a >> b >> c;
    coord[i] = b;
    abc[i] = make_tuple(a, b, c);
  }
  coord.push_back(0);
  sort(coord.begin(), coord.end());
  sort(abc.begin(), abc.end(), [](
      decltype(abc)::value_type p,
      decltype(abc)::value_type q
    ) {
      int a, b, x, y;
      long long c, z;
      tie(a, b, c) = p;
      tie(x, y, z) = q;
      if (a < x) return true;
      if (a > x) return false;
      if (b > y) return true;
      return false;
    });
  constexpr long long inf = 1LL << 60;
  segment_tree<long long> sgt(
      coord,
      [](long long a, long long b){return max(a, b);},
      -inf
    );
  sgt.update(0, 0);
  for (auto const& p : abc) {
    int b; long long c;
    tie(ignore, b, c) = p;
    long long tmp = sgt.query(0, b) + c;
    if (sgt.at(b) < tmp) sgt.update(b, tmp);
  }
  cout << sgt.query(0, 1 << 30) << endl;
  return 0;
}
0