結果

問題 No.777 再帰的ケーキ
ユーザー yumakmcyumakmc
提出日時 2019-03-18 22:08:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 846 ms / 2,000 ms
コード長 2,150 bytes
コンパイル時間 2,237 ms
コンパイル使用メモリ 181,660 KB
実行使用メモリ 48,768 KB
最終ジャッジ日時 2023-09-26 11:40:39
合計ジャッジ時間 9,239 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,304 KB
testcase_01 AC 4 ms
7,204 KB
testcase_02 AC 4 ms
7,520 KB
testcase_03 AC 4 ms
7,236 KB
testcase_04 AC 4 ms
7,316 KB
testcase_05 AC 4 ms
7,312 KB
testcase_06 AC 4 ms
7,252 KB
testcase_07 AC 4 ms
7,200 KB
testcase_08 AC 4 ms
7,360 KB
testcase_09 AC 4 ms
7,292 KB
testcase_10 AC 4 ms
7,196 KB
testcase_11 AC 4 ms
7,232 KB
testcase_12 AC 4 ms
7,288 KB
testcase_13 AC 4 ms
7,296 KB
testcase_14 AC 4 ms
7,288 KB
testcase_15 AC 4 ms
7,328 KB
testcase_16 AC 4 ms
7,404 KB
testcase_17 AC 3 ms
7,252 KB
testcase_18 AC 4 ms
7,268 KB
testcase_19 AC 4 ms
7,356 KB
testcase_20 AC 3 ms
7,512 KB
testcase_21 AC 7 ms
7,552 KB
testcase_22 AC 7 ms
7,576 KB
testcase_23 AC 4 ms
7,300 KB
testcase_24 AC 4 ms
7,236 KB
testcase_25 AC 7 ms
7,556 KB
testcase_26 AC 6 ms
7,816 KB
testcase_27 AC 5 ms
7,200 KB
testcase_28 AC 846 ms
48,448 KB
testcase_29 AC 822 ms
48,640 KB
testcase_30 AC 844 ms
48,628 KB
testcase_31 AC 839 ms
48,628 KB
testcase_32 AC 464 ms
48,580 KB
testcase_33 AC 82 ms
15,732 KB
testcase_34 AC 121 ms
18,472 KB
testcase_35 AC 541 ms
29,704 KB
testcase_36 AC 457 ms
48,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"

using namespace std;
#pragma warning(disable:4996)

#define Seg_Max_N (1<<18) 

using Value = long long int;
const Value ini =0;
struct segtree {
	int N;
	vector<Value>dat;

	segtree() {}
	segtree(int n) :dat(2 * Seg_Max_N) {
		N = 1;
		while (N < n) N *= 2;
		for (int i = 0; i < 2 * N - 1; i++) {
			dat[i] = ini;
		}
	}
	Value connect(const Value&l, const Value&r) {
		return max(l,r);
	}
	// update k th element
	void update(int k, Value a) {
		k += N - 1;
		dat[k] = a;

		while (k > 0) {
			k = (k - 1) / 2;
			const Value al(dat[k * 2 + 1]);
			const Value ar(dat[k * 2 + 2]);
			dat[k] = connect(al, ar);
		}
	}
	// min [a, b)
	Value  query(int a, int b) { return query(a, b, 0, 0, N); }
	Value  query(int a, int b, int k, int l, int r) {
		if (r <= a or b <= l) return ini;
		if (a <= l and r <= b) return dat[k];
		const int m = (l + r) / 2;
		const Value al(query(a, b, k * 2 + 1, l, m));
		const Value ar(query(a, b, k * 2 + 2, m, r));
		return connect(al, ar);
	}
};

struct cake {
	int a;
	int b;
	long long int c;
};
bool operator<(const cake&l, const cake&r) {
	return l.a<r.a;
}
template<typename T> struct Compress {
	map<T, int>mp;
	map<int, T>revmp;

	Compress(vector<T>vs) {
		setmp(vs);
	}

	Compress() :mp(), revmp() {

	}
	void setmp(vector<T>vs) {
		sort(vs.begin(), vs.end());
		vs.erase(unique(vs.begin(), vs.end()), vs.end());
		for (int i = 0; i < static_cast<int>(vs.size()); ++i) {
			mp[vs[i]] = i;
			revmp[i] = vs[i];
		}
	}

};

int main() {
	int N;cin>>N;
	map<int,vector<cake>>mp;
	vector<int>bs{ 0 };
	for (int i = 0; i < N; ++i) {
		int a,b;
		long long int c;
		scanf("%d %d %lld",&a,&b,&c);
		bs.push_back(b);
		mp[a].push_back(cake{ a,b,c });
	}
	Compress<int> cb(bs);
	segtree seg(N+1);
	seg.update(0,0);
	for (auto m : mp) {
		vector<long long int>nums;
		for (auto c : m.second) {
			long long int num = seg.query(0, cb.mp[c.b]);
			nums.push_back(num);
		}

		int id = 0;
		for (auto c : m.second) {
			seg.update(cb.mp[c.b], max(seg.query(cb.mp[c.b], cb.mp[c.b] + 1), nums[id] + c.c));
			id++;
		}
	}
	long long int answer=seg.query(0,N+1);
	cout<<answer<<endl;
	return 0;
	
}
0