結果
問題 | No.913 木の燃やし方 |
ユーザー |
![]() |
提出日時 | 2019-10-18 23:40:57 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,484 ms / 3,000 ms |
コード長 | 4,327 bytes |
コンパイル時間 | 1,192 ms |
コンパイル使用メモリ | 117,536 KB |
実行使用メモリ | 51,968 KB |
最終ジャッジ日時 | 2024-06-25 20:00:11 |
合計ジャッジ時間 | 24,878 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
#include <cstdio>#include <cstring>#include <iostream>#include <string>#include <cmath>#include <bitset>#include <vector>#include <map>#include <set>#include <queue>#include <deque>#include <algorithm>#include <complex>#include <unordered_map>#include <unordered_set>#include <random>#include <cassert>#include <fstream>#include <utility>#include <functional>#include <time.h>#include <stack>#define popcount __builtin_popcountusing namespace std;typedef long long int ll;typedef pair<int, ll> P;typedef pair<P, ll> Pl;typedef __int128_t lll;using CHT_TYPE = lll;class ConvexHullTrickDynamic {private:// 直線 **************************************************************struct Line {CHT_TYPE a, b; // y = ax + bmutable std::function<const Line*()> getSuc; // 次の直線へのポインタ (ソートで用いる)bool operator<(const Line& rhs) const {// 取得クエリでは次の直線との差分でソートif (rhs.b == IS_QUERY) {const Line* suc = getSuc();if (suc == nullptr) return false;const CHT_TYPE& x = rhs.a;return (suc->a - a)*x + suc->b - b > 0;}if (b == IS_QUERY) {const Line* suc = rhs.getSuc();if (suc == nullptr) return true;const CHT_TYPE& x = a;return (suc->a - rhs.a)*x + suc->b - rhs.b < 0;}// 通常の直線どうしは傾きソートreturn a < rhs.a;}};// 直線集合 **********************************************************class LinesSet : public std::multiset<Line> {private:// true -> 最小値クエリ, false -> 最大値クエリbool flagMin;public:// コンストラクタ ( 第一引数falseで最大値クエリ,デフォルトで最小値クエリ )LinesSet(bool flagMin = true) : flagMin(flagMin) {};// 直線lが不必要であるかどうかinline bool isBad(iterator l) {const auto&& nel = std::next(l);if (l == begin()) { // lが傾き最小のときif (nel == end()) return false; // lしかないなら必要return l->a == nel->a && l->b <= nel->b;}else {const auto&& prl = std::prev(l);if (nel == end()) return l->a == prl->a && l->b <= prl->b;return (prl->b - l->b) * (nel->a - l->a) >= (nel->b - l->b) * (prl->a - l->a);}}// 直線y=ax+bを追加するinline void add(CHT_TYPE a, CHT_TYPE b) {if (flagMin) a = -a, b = -b;auto&& it = insert({a, b});it->getSuc = [=] { return (std::next(it) == end() ? nullptr : &*std::next(it)); };if (isBad(it)) { erase(it); return; }while (std::next(it) != end() && isBad(std::next(it))) erase(std::next(it));while (it != begin() && isBad(std::prev(it))) erase(std::prev(it));}// 直線群の中でxの時に最小(最大)となる値を返すinline CHT_TYPE get(CHT_TYPE x) {auto&& l = *lower_bound(Line{x, IS_QUERY});if (flagMin) return -l.a * x - l.b;else return l.a * x + l.b;}};static const CHT_TYPE IS_QUERY = std::numeric_limits<CHT_TYPE>::lowest();LinesSet linesSet;public:// コンストラクタ ( 第一引数falseで最大値クエリ,デフォルトで最小値クエリ )ConvexHullTrickDynamic(bool flagMin = true) : linesSet(flagMin) {}// 直線y=ax+bを追加するinline void add(CHT_TYPE a, CHT_TYPE b) { linesSet.add(a, b); }// あるxのときの直線集合での最小値を求めるinline CHT_TYPE get(CHT_TYPE x) { return linesSet.get(x); }};int n;ll a[200020];ll s[200020];ll ans[200020];void solve(int l, int r){if(l>r) return;if(l==r){ans[l]=min(ans[l], 1+a[l]);return;}int m=(l+r)/2;ConvexHullTrickDynamic cht(true), cht2(true);for(int i=r; i>=m; i--){ll a0=-2*(i+1), b0=(ll)(i+1)*(i+1)+s[i+1];cht.add(a0, b0);}ll mn=1e18;for(int i=l; i<=m; i++){mn=min(mn, (ll)cht.get(i)+(ll)i*i-s[i]);ans[i]=min(ans[i], mn);}for(int i=l; i<=m; i++){ll a0=-2*i, b0=(ll)i*i-s[i];cht2.add(a0, b0);}mn=1e18;for(int i=r; i>=m; i--){mn=min(mn, (ll)cht2.get(i+1)+(ll)(i+1)*(i+1)+s[i+1]);ans[i]=min(ans[i], mn);}solve(l, m-1);solve(m+1, r);}int main(){cin>>n;s[0]=0;for(int i=0; i<n; i++){cin>>a[i];s[i+1]=s[i]+a[i];}const ll INF=1e18;fill(ans, ans+n, INF);solve(0, n-1);for(int i=0; i<n; i++){printf("%lld\n", ans[i]);}return 0;}