結果

問題 No.777 再帰的ケーキ
ユーザー tomatoma
提出日時 2019-09-25 00:42:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 904 ms / 2,000 ms
コード長 2,184 bytes
コンパイル時間 2,110 ms
コンパイル使用メモリ 195,460 KB
実行使用メモリ 62,060 KB
最終ジャッジ日時 2023-10-19 18:36:47
合計ジャッジ時間 10,650 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 6 ms
4,348 KB
testcase_22 AC 6 ms
4,348 KB
testcase_23 AC 3 ms
4,348 KB
testcase_24 AC 3 ms
4,348 KB
testcase_25 AC 6 ms
4,348 KB
testcase_26 AC 7 ms
4,348 KB
testcase_27 AC 4 ms
4,348 KB
testcase_28 AC 868 ms
62,060 KB
testcase_29 AC 865 ms
62,060 KB
testcase_30 AC 904 ms
62,060 KB
testcase_31 AC 889 ms
62,060 KB
testcase_32 AC 498 ms
62,060 KB
testcase_33 AC 131 ms
21,760 KB
testcase_34 AC 188 ms
24,780 KB
testcase_35 AC 577 ms
39,688 KB
testcase_36 AC 493 ms
62,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include"bits/stdc++.h"
using namespace std;
#define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))
using ll = long long;

template<typename T>
class SegmentTree {
private:
	using F = function<T(T, T)>; // モノイド型
	int n; // 横幅
	F f;   // モノイド
	T e;   // モノイド単位元
	vector<T> data;

public:
	// init忘れに注意
	SegmentTree() {}
	SegmentTree(F f, T e) :f(f), e(e) {}
	void init(int n_) {
		n = 1;
		while (n < n_)n <<= 1;
		data.assign(n << 1, e);
	}
	void build(const vector<T>& v) {
		int n_ = v.size();
		init(n_);
		rep(i, n_)data[n + i] = v[i];
		for (int i = n - 1; i >= 0; i--) {
			data[i] = f(data[(i << 1) | 0], data[(i << 1) | 1]);
		}
	}
	void set_val(int idx, T val) {
		idx += n;
		data[idx] = val;
		while (idx >>= 1) {
			data[idx] = f(data[(idx << 1) | 0], data[(idx << 1) | 1]);
		}
	}
	T query(int a, int b) {
		// [a,b)
		T vl = e, vr = e;
		for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
			if (l & 1)vl = f(vl, data[l++]); // unknown
			if (r & 1)vr = f(data[--r], vr); // unknown
		}
		return f(vl, vr);
	}
};

// if sort with A, this problem can reduce to LIS
// but for A_i==A_i+1's case,
// unite A's value and batch process
int main()
{
	// input
	int N;
	cin >> N;
	vector<vector<ll>> abc(N, vector<ll>(3));
	rep(i, N)rep(j, 3)cin >> abc[i][j];

	// preprocess
	map<ll, vector<pair<ll, ll>>> cakes;

	int cnt = 0;
	set<ll> st;
	map<ll, ll> trans;
	for (const auto& row : abc)st.insert(row[1]);
	for (const auto& num : st)trans[num] = cnt++;
	for (const auto& row : abc) {
		cakes[row[0]].push_back({ trans[row[1]], row[2] });
	}

	// segment tree init
	auto f = [](ll l, ll r) {return max(l, r); };
	SegmentTree<ll> seg(f, 0);
	seg.init(cnt);

	// query
	for (auto& itr : cakes) {
		vector<pair<ll, ll>> batch;
		for (auto& cake : itr.second) {
			ll b, c;
			tie(b, c) = cake;
			ll val = seg.query(0, b);
			ll bef = seg.query(b, b + 1);
			batch.push_back({ b, val + c });
		}
		for (auto& p : batch) {
			ll b, val;
			tie(b, val) = p;
			ll now = seg.query(b, b + 1);
			ll next = max(now, val);
			seg.set_val(b, next);
		}
	}
	cout << seg.query(0, cnt) << endl;
	return 0;
}
0