結果
問題 | No.777 再帰的ケーキ |
ユーザー |
![]() |
提出日時 | 2018-12-24 03:29:06 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 277 ms / 2,000 ms |
コード長 | 3,032 bytes |
コンパイル時間 | 2,302 ms |
コンパイル使用メモリ | 197,192 KB |
実行使用メモリ | 40,196 KB |
最終ジャッジ日時 | 2024-09-25 10:56:36 |
合計ジャッジ時間 | 5,701 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 33 |
ソースコード
#include <bits/stdc++.h>using namespace std;using i64=int64_t;#define rep(i,x,y) for(i64 i=i64(x),i##_max_for_repmacro=i64(y); i<i##_max_for_repmacro; ++i)#define debug(x) #x << "=" << (x)#ifdef DEBUG#define _GLIBCXX_DEBUG#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl#else#define print(x)#endifconst int inf=1.01e9;const i64 inf64=4.01e18;const double eps=1e-9;template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){os << "[";for (const auto &v : vec) {os << v << ",";}os << "]";return os;}template <class T, T op(T, T)>class segtree {public:int n, size_;vector<T> dat;T id_;segtree() = default;segtree(int size, T id, T initial_value) { init(size, id, initial_value); }void init(int size, T id, T initial_value) {size_ = size;id_ = id;n = 1;while (n < size) n *= 2;dat.assign(2 * n - 1, id);for (int i = 0; i < size; ++i) update(i, initial_value);}int size() const { return size_; }void update(int k, T a) {k += n - 1; // leafdat[k] = a;while (k > 0) {k = (k - 1) / 2;dat[k] = op(dat[k * 2 + 1], dat[k * 2 + 2]);}}T at(int index) { return dat[index + n - 1]; }void add(int k, T a) { update(k, at(k) + a); }T query(int a, int b) { return query(a, b, 0, 0, n); }T query(int a, int b, int k, int l, int r) {if (r <= a or b <= l) return id_;if (a <= l and r <= b) return dat[k];int m = (l + r) / 2;return op(query(a, b, k * 2 + 1, l, m), query(a, b, k * 2 + 2, m, r));}};i64 mymax(i64 a,i64 b){return max(a,b);}void solve(){i64 N;cin >> N;vector<tuple<i64,i64,i64>> ABC(N);rep(i,0,N){i64 A,B,C;cin >> A >> B >> C;ABC[i]=make_tuple(A,B,C);}sort(begin(ABC),end(ABC));vector<pair<i64,i64>> bi(N);rep(i,0,N) bi[i]=make_pair(get<1>(ABC[i]),i);sort(begin(bi),end(bi));vector<i64> rev1(N),rev2(N);{i64 j=0;rep(i, 0, N){rev1[bi[i].second] = i;if(i and bi[i].first>bi[i-1].first) j=i;rev2[bi[i].second]=j;}}segtree<i64,mymax> seg(N,-inf64,-inf64);map<i64,vector<tuple<i64,i64,i64>>> mp;rep(i,0,N){i64 a,b,c;tie(a,b,c)=ABC[i];mp[a].push_back(make_tuple(b,c,i));}for(const auto& p:mp){vector<pair<i64,i64>> iv;for(const auto& t:p.second){i64 b,c,i;tie(b,c,i)=t;i64 tmp=max(i64(0),seg.query(0,rev2[i]));iv.push_back(make_pair(rev1[i],tmp+c));}for(auto& tmp:iv) seg.update(tmp.first,tmp.second);}cout << seg.query(0,N) << endl;}int main(){std::cin.tie(0);std::ios::sync_with_stdio(false);cout.setf(ios::fixed);cout.precision(16);solve();return 0;}