結果
問題 | No.913 木の燃やし方 |
ユーザー |
|
提出日時 | 2024-08-16 16:35:38 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 346 ms / 3,000 ms |
コード長 | 4,260 bytes |
コンパイル時間 | 3,039 ms |
コンパイル使用メモリ | 228,772 KB |
最終ジャッジ日時 | 2025-02-23 22:50:43 |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
コンパイルメッセージ
main.cpp:20:12: warning: use of ‘auto’ in parameter declaration only available with ‘-std=c++20’ or ‘-fconcepts’ 20 | bool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; } | ^~~~ main.cpp:20:21: warning: use of ‘auto’ in parameter declaration only available with ‘-std=c++20’ or ‘-fconcepts’ 20 | bool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; } | ^~~~ main.cpp:21:12: warning: use of ‘auto’ in parameter declaration only available with ‘-std=c++20’ or ‘-fconcepts’ 21 | bool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; } | ^~~~ main.cpp:21:21: warning: use of ‘auto’ in parameter declaration only available with ‘-std=c++20’ or ‘-fconcepts’ 21 | bool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; } | ^~~~
ソースコード
#pragma GCC optimize("Ofast")#pragma GCC optimize("unroll-loops")#include <bits/stdc++.h>using namespace std;#define ll long long#define pii pair<int, int>#define pll pair<ll, ll>#define vi vector<int>#define vl vector<ll>#define ov3(a, b, c, name, ...) name#define rep0(n) for(ll aaaaa = 0; aaaaa < (n); aaaaa++)#define rep1(i, n) for(ll i = 0; i < (n); i++)#define rep2(i, a, b) for(ll i = (a); i < (b); i++)#define rep(...) ov3(__VA_ARGS__, rep2, rep1, rep0)(__VA_ARGS__)#define fore(e, v) for(auto &&e : v)#define all(v) begin(v), end(v)#define si(a) (int)(size(a))bool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; }bool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; }const int inf = 11e8;const ll infl = 2e18;#define i128 __int128_ttemplate <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;elser = 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};int main() {int n;cin >> n;vl a(n);fore(e, a) cin >> e;vl c(n + 1);rep(i, n) c[i + 1] = c[i] + a[i];// (r - l) ^ 2 + c[r] - c[l]// (r ^ 2 + c[r]) - (c[l] - l^2) - 2lrvl res(n, infl);auto rec = [&](auto &&f, int l, int r) {if(l + 1 == r) {chmin(res[l], 1 + a[l]);return;}int mid = l + r >> 1;vl lres(mid - l);CHT<true> cht;rep(i, mid, r + 1) { cht.add(-2 * i, i * i + c[i]); }for(ll i = mid - 1; i >= l; i--) {ll t = -(c[i] - i * i);lres[i - l] = cht.query_monotone_dec(i) + t;}rep(i, si(lres)) {if(i) chmin(lres[i], lres[i - 1]);chmin(res[l + i], lres[i]);}cht.clear();rep(i, l, mid + 1) { cht.add(-2 * i, -(c[i] - i * i)); }vl rres(r - mid + 1);rep(i, mid + 1, r + 1) {ll t = (i * i + c[i]);rres[i - mid] = cht.query_monotone_inc(i) + t;}for(int i = r; i > mid; i--) {chmin(res[i - 1], rres[i - mid]);chmin(rres[i - mid - 1], rres[i - mid]);}f(f, l, mid), f(f, mid, r);};rec(rec, 0, n);rep(i, n) { cout << res[i] << '\n'; }}