結果
問題 | No.777 再帰的ケーキ |
ユーザー |
|
提出日時 | 2019-03-11 19:15:22 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 519 ms / 2,000 ms |
コード長 | 3,635 bytes |
コンパイル時間 | 1,550 ms |
コンパイル使用メモリ | 126,536 KB |
実行使用メモリ | 32,296 KB |
最終ジャッジ日時 | 2024-06-23 15:39:37 |
合計ジャッジ時間 | 6,744 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 33 |
ソースコード
// includes#include <cstdio>#include <cstdint>#include <iostream>#include <iomanip>#include <string>#include <queue>#include <stack>#include <vector>#include <set>#include <map>#include <unordered_map>#include <algorithm>#include <utility>#include <functional>#include <cmath>#include <climits>#include <bitset>#include <list>#include <random>// macros#define ll long long int#define pb emplace_back#define mk make_pair#define pq priority_queue#define FOR(i, a, b) for(int i=(a);i<(b);++i)#define rep(i, n) FOR(i, 0, n)#define rrep(i, n) for(int i=((int)(n)-1);i>=0;i--)#define all(x) (x).begin(),(x).end()#define sz(x) ((int)(x).size())#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())using namespace std;// typestypedef pair<int, int> P;typedef pair<ll, int> Pl;typedef pair<ll, ll> Pll;typedef pair<double, double> Pd;// constantsconst int inf = 1e9;const ll linf = 1LL << 50;const double EPS = 1e-10;const int mod = 1e9 + 7;// solvetemplate <class T>bool chmax(T &a, const T &b){if(a < b){a = b; return 1;} return 0;}template <class T>bool chmin(T &a, const T &b){if(a > b){a = b; return 1;} return 0;}struct cake{ll a, b, c;cake(){}cake(ll a, ll b, ll c): a(a), b(b), c(c){}};ll dp[200001];bool comp(const cake &x, const cake &y){if(x.a != y.a)return x.a < y.a;return x.b < y.b;}template<typename T, typename E>struct SegmentTree_ {function<T(T, T)> f;function<T(T, E)> g;int n;T def;vector<T> vec;SegmentTree_(){}SegmentTree_(int n_, function<T(T, T)> f_, function<T(T, E)> g_, T def_, vector<T> v=vector<T>()){f = f_;g = g_;def = def_;// initialize vectorn = 1;while(n < n_){n *= 2;}vec = vector<T>(2*n -1, def);// initialize segment treefor(int i = 0; i < v.size(); i++){vec[i + n - 1] = v[i];}for(int i = n - 2; i >= 0; i--){vec[i] = f(vec[2*i+1], vec[2*i+2]);}}void update(int k, E val){k = k + n - 1;vec[k] = g(vec[k], val);while(k > 0){k = (k - 1) / 2;vec[k] = f(vec[2*k+1], vec[2*k+2]);}}// [l, r) -> [a, b) (at k)T query(int a, int b, int k, int l, int r){if(r <= a || b <= l)return def;if(a <= l && r <= b)return vec[k];T ld = query(a, b, 2*k+1, l, (l+r)/2);T rd = query(a, b, 2*k+2, (l+r)/2, r);return f(ld, rd);}T query(int a, int b){return query(a, b, 0, 0, n);}};template<typename T, typename E>using SegmentTree = struct SegmentTree_<T, E>;using SegmentTreeI = SegmentTree<int, int>;int main(int argc, char const* argv[]){ios_base::sync_with_stdio(false);cin.tie(0);int n;cin >> n;vector<ll> a(n), b(n), c(n);rep(i, n)cin >> a[i] >> b[i] >> c[i];vector<cake> vec(n);rep(i, n)vec[i] = cake(a[i], b[i], c[i]);sort(all(vec), comp);b.pb(0);sort(all(b));UNIQUE(b);map<ll, int> mp;for(int i = 0; i < sz(b); i++)mp[b[i]] = i;SegmentTree<ll, ll> seg = SegmentTree<ll, ll>(sz(b)+1, [](ll a, ll b){return max(a, b);},[](ll a,ll b){return b;}, 0, vector<ll>(sz(b)+1, 0));vector<cake> curr;for(int i = 0; i < n; i++){dp[i+1] = vec[i].c;dp[i+1] = max(dp[i+1], vec[i].c + seg.query(0, mp[vec[i].b]));curr.pb(vec[i]);if(i + 1 < n && vec[i+1].a != vec[i].a){reverse(all(curr));for(int j = 0; j < sz(curr); j++){seg.update(mp[curr[j].b], max(seg.query(mp[curr[j].b], mp[curr[j].b]+1), dp[i+1-j]));}curr.clear();}}ll res = 0;rep(i, n){res = max(res, dp[i+1]);}cout << res << endl;return 0;}