結果
| 問題 |
No.2458 Line Up Charged Balls
|
| コンテスト | |
| ユーザー |
Kude
|
| 提出日時 | 2023-09-01 23:13:18 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 76 ms / 2,000 ms |
| コード長 | 3,787 bytes |
| コンパイル時間 | 4,405 ms |
| コンパイル使用メモリ | 298,484 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2025-01-03 11:26:26 |
| 合計ジャッジ時間 | 6,677 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 25 |
ソースコード
#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
#line 1 "structure/convex-hull-trick/dynamic-li-chao-tree.hpp"
/**
* @brief Dynamic-Li-Chao-Tree
* @docs docs/dynamic-li-chao-tree.md
*/
template< typename T, T x_low, T x_high, T id >
struct DynamicLiChaoTree {
struct Line {
T a, b;
Line(T a, T b) : a(a), b(b) {}
inline T get(T x) const { return a * x + b; }
};
struct Node {
Line x;
Node *l, *r;
Node(const Line &x) : x{x}, l{nullptr}, r{nullptr} {}
~Node() {
if (l) delete l;
if (r) delete r;
}
};
Node *root;
DynamicLiChaoTree() : root{nullptr} {}
~DynamicLiChaoTree() {
if (root) delete root;
}
Node *add_line(Node *t, Line &x, const T &l, const T &r, const T &x_l, const T &x_r) {
if(!t) return new Node(x);
T t_l = t->x.get(l), t_r = t->x.get(r);
if(t_l <= x_l && t_r <= x_r) {
return t;
} else if(t_l >= x_l && t_r >= x_r) {
t->x = x;
return t;
} else {
T m = (l + r) / 2;
if(m == r) --m;
T t_m = t->x.get(m), x_m = x.get(m);
if(t_m > x_m) {
swap(t->x, x);
if(x_l >= t_l) t->l = add_line(t->l, x, l, m, t_l, t_m);
else t->r = add_line(t->r, x, m + 1, r, t_m + x.a, t_r);
} else {
if(t_l >= x_l) t->l = add_line(t->l, x, l, m, x_l, x_m);
else t->r = add_line(t->r, x, m + 1, r, x_m + x.a, x_r);
}
return t;
}
}
void add_line(const T &a, const T &b) {
Line x(a, b);
root = add_line(root, x, x_low, x_high, x.get(x_low), x.get(x_high));
}
Node *add_segment(Node *t, Line &x, const T &a, const T &b, const T &l, const T &r, const T &x_l, const T &x_r) {
if(r < a || b < l) return t;
if(a <= l && r <= b) {
Line y{x};
return add_line(t, y, l, r, x_l, x_r);
}
if(t) {
T t_l = t->x.get(l), t_r = t->x.get(r);
if(t_l <= x_l && t_r <= x_r) return t;
} else {
t = new Node(Line(0, id));
}
T m = (l + r) / 2;
if(m == r) --m;
T x_m = x.get(m);
t->l = add_segment(t->l, x, a, b, l, m, x_l, x_m);
t->r = add_segment(t->r, x, a, b, m + 1, r, x_m + x.a, x_r);
return t;
}
void add_segment(const T &l, const T &r, const T &a, const T &b) {
Line x(a, b);
root = add_segment(root, x, l, r - 1, x_low, x_high, x.get(x_low), x.get(x_high));
}
T query(const Node *t, const T &l, const T &r, const T &x) const {
if(!t) return id;
if(l == r) return t->x.get(x);
T m = (l + r) / 2;
if(m == r) --m;
if(x <= m) return min(t->x.get(x), query(t->l, l, m, x));
else return min(t->x.get(x), query(t->r, m + 1, r, x));
}
T query(const T &x) const {
return query(root, x_low, x_high, x);
}
};
} int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
constexpr long long INF = 1002003004005006007;
DynamicLiChaoTree<ll, -100010, 100010, INF> lct;
lct.add_line(0, 0);
ll ans = 0;
rep(_, n) {
int q;
cin >> q;
ll mx = -lct.query(q);
chmax(ans, mx);
// y = qx + mx
lct.add_line(-q, -mx);
}
cout << ans << '\n';
}
Kude