fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define fi first
  4. #define se second
  5. #define ll long long
  6. #define int long long
  7.  
  8. struct line{
  9. ll a, b;
  10. line(){a = 0; b = 0;}
  11. line(ll a, ll b){
  12. this->a = a;
  13. this->b = b;
  14. }
  15. ll operator()(int x){
  16. return a * x + b;
  17. }
  18. };
  19.  
  20. const int M = 1e9;
  21.  
  22. struct node {
  23. line tr;
  24. node *lpt, *rpt;
  25.  
  26. node() : tr(), lpt(nullptr), rpt(nullptr) {}
  27.  
  28. int divi (int a, int b) {
  29. return a / b - ((a ^ b) < 0 && a % b);
  30. }
  31.  
  32. void update (line f, int l = 0, int r = M) {
  33. if (l == r) {
  34. tr = (f(l) > tr(l) ? f : tr);
  35. return;
  36. }
  37. int mid = divi(l + r, 2);
  38. if (f(mid) > tr(mid)) swap(tr, f);
  39. if (f.a < tr.a) {
  40. if (lpt == nullptr) lpt = new node(); // khởi tạo bộ nhớ cho cây con trái
  41. lpt->update(f, l, mid); // trường hợp 1.1
  42. }
  43. if (f.a > tr.a) {
  44. if (rpt == nullptr) rpt = new node(); // khởi tạo bộ nhớ cho cây con phải
  45. rpt->update(f, mid + 1, r); // trường hợp 1.2
  46. }
  47. }
  48.  
  49. ll query (int pos, int l = 0, int r = M) {
  50. ll cur = tr(pos);
  51. int mid = divi(l + r, 2);
  52. if (l == r) return cur;
  53. if (pos <= mid)
  54. return max(cur, lpt == nullptr ? LLONG_MIN : lpt->query(pos, l, mid));
  55. else
  56. return max(cur, rpt == nullptr ? LLONG_MIN : rpt->query(pos, mid + 1, r));
  57. }
  58. };
  59.  
  60. signed main() {
  61. int n; cin >> n;
  62. vector<pair<pair<int, int>, int>> a(n);
  63.  
  64. for(int i = 0; i < n; i++){
  65. cin >> a[i].fi.fi >> a[i].fi.se >> a[i].se;
  66. }
  67. sort(a.begin(), a.end());
  68. node lich;
  69. lich.update(line(-a[0].first.first, a[0].first.first * a[0].first.second - a[0].se));
  70. ll mx = a[0].first.first * a[0].first.second - a[0].se;
  71.  
  72. for(int i = 1; i < n; i++){
  73. ll dp = a[i].first.first * a[i].first.second + lich.query(a[i].first.second) - a[i].se;
  74.  
  75. mx = max(mx, dp);
  76. lich.update(line(-a[i].first.first, dp));
  77. }
  78. cout << mx;
  79.  
  80. }
Success #stdin #stdout 0.01s 5288KB
stdin
4
6 2 4
1 6 2
2 4 3
5 3 8
stdout
10