結果
問題 | No.913 木の燃やし方 |
ユーザー |
|
提出日時 | 2024-12-10 00:44:20 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 486 ms / 3,000 ms |
コード長 | 4,316 bytes |
コンパイル時間 | 4,256 ms |
コンパイル使用メモリ | 284,796 KB |
実行使用メモリ | 11,228 KB |
最終ジャッジ日時 | 2024-12-10 00:44:46 |
合計ジャッジ時間 | 22,173 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
#include <bits/stdc++.h>#define rep1(n) for(int i = 0; i < (int)n; i++)#define rep2(i, n) for(int i = 0; i < (int)n; i++)#define rep3(i, l, r) for(int i = (int)l; i < (int)r; i++)#define overloadrep(a, b, c, x, ...) x#define rep(...) overloadrep(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)#define rrep(i, n) for(int i = (n)-1; i >= 0; i--)#define rrep3(i, l, r) for(int i = (r)-1; i >= l; i--)#define all(v) v.begin(), v.end()#define si(v) (int)v.size()using namespace std;template<typename T1, typename T2>ostream& operator<<(ostream& os, const pair<T1, T2>& p) {os << "(" << p.first << ", " << p.second << ")";return os;}template<typename T>bool chmin(T &a, const T &b) {if (a > b) {a = b;return true;}return false;}using ll = long long;using vi = vector<int>;using vl = vector<ll>;using pll = pair<ll, ll>;const int iinf = 1e9 + 10;const ll linf = 1e18 + 100;template<bool isMin = true> struct CHT {#define x first#define y secondCHT() = default;deque<pll> v;bool empty() { return v.empty(); }void clear() { return v.clear(); }inline int sgn(ll x) { return !x ? 0 : (x < 0 ? -1 : 1); }using D = long double;inline bool check(const pll& a, const pll& b, const pll& c) {if(b.y == a.y or c.y == b.y) return sgn(b.x - a.x) * sgn(c.y - b.y) >= sgn(c.x - b.x) * sgn(b.y - a.y);return D(b.x - a.x) * sgn(c.y - b.y) / D(abs(b.y - a.y)) >= D(c.x - b.x) * sgn(b.y - a.y) / D(abs(c.y - b.y));}void add(ll a, ll b) {if(!isMin) a *= -1, b *= -1;pll line(a, b);if(empty()) v.emplace_front(line);else {if(ll c = v[0].x; c <= a) {if(c == a) {if(v[0].y <= b) return;v.pop_front();}while(si(v) >= 2 and check(line, v[0], v[1])) v.pop_front();v.emplace_front(line);} else {assert(a <= v.back().x);if(v.back().x == a) {if(v.back().y <= b) return;v.pop_back();}while(si(v) >= 2 and check(v[si(v) - 2], v.back(), line)) v.pop_back();v.emplace_back(line);}}}ll get_y(const pll& a, const ll& x) { return a.x * x + a.y; }ll query(ll x) {assert(!empty());int l = -1, r = si(v) - 1;while(l + 1 < r) {int m = (l + r) >> 1;if(get_y(v[m], x) >= get_y(v[m + 1], x)) l = m;else r = m;}return get_y(v[r], x) * (isMin ? 1 : -1);}ll query_monotone_inc(ll x) {assert(!empty());while(si(v) >= 2 and get_y(v[0], x) >= get_y(v[1], x)) v.pop_front();return get_y(v[0], x) * (isMin ? 1 : -1);}ll query_monotone_dec(ll x) {assert(!empty());while(si(v) >= 2 and get_y(v.back(), x) >= get_y(v.end()[-2], x)) v.pop_back();return get_y(v.back(), x) * (isMin ? 1 : -1);}#undef x#undef y};void solve() {int n;cin >> n;vector<ll> a(n);rep(i, n)cin >> a[i];vector<ll> b(n+1);rep(i, n)b[i+1] = b[i] + a[i];vector<ll> ans(n, linf);auto dfs = [&](auto self, int l, int r) -> void {if (r - l == 1) {chmin(ans[l], a[l] + 1);return;}// cout << l << " " << r << endl;int m = (l + r) / 2;CHT<true> cht;rep(i, m, r) {cht.add(-2 * (i+1), b[i+1] + ((ll)(i+1)*(i+1)));}ll res = linf;rep(i, l, m) {chmin(res, cht.query_monotone_inc(i) + ((ll)i * i) - b[i]);chmin(ans[i], res);}cht.clear();rep(i, l, m) {cht.add(-2 * i, -b[i] + ((ll)i * i));}res = linf;rrep3(i, m, r) {chmin(res, cht.query_monotone_dec(i+1) + ((ll)(i+1)*(i+1)) + b[i+1]);chmin(ans[i], res);}// rep(i, l, r) {// if (i > l) cout << " ";// cout << ans[i];// }// cout << endl;self(self, l, m);self(self, m, r);};dfs(dfs, 0, n);rep(i, n) {cout << ans[i] << endl;}}int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int T = 1;// int T;cin >> T;while(T--) {solve();}}