#line 1 "Library/src/debug.hpp" #ifdef ONLINE_JUDGE #define debug(x) void(0) #else #define _GLIBCXX_DEBUG #define debug(x) std::cerr << __LINE__ << " : " << #x << " = " << (x) << std::endl #endif /** * @brief Debugger */ #line 2 "Library/src/data-structure/CHT.hpp" #include namespace kyopro { /** * @tparam query_type trueにすると, クエリで最大値を求めるように変更する */ template class CHT { class line { public: T a, b; bool is_query; mutable T nxt_a, nxt_b; mutable bool has_nxt; T get(T x) const { return a * x + b; } T get_nxt(T x) const { return nxt_a * x + nxt_b; } line(T a, T b, bool q = false) : a(a), b(b), is_query(q), has_nxt(false) {} friend bool operator<(const line& l, const line& r) { if (l.is_query) { if (!r.has_nxt) return true; return r.get(l.a) < r.get_nxt(l.a); } if (r.is_query) { if (!l.has_nxt) return false; return l.get(r.a) > l.get_nxt(r.a); } return l.a == r.a ? l.b < r.b : l.a < r.a; } }; std::set ls; bool is_needed(const typename std::set::iterator& it) { if (it != ls.begin() && it->a == prev(it)->a) { return it->b < prev(it)->b; } if (next(it) != ls.end() && it->a == next(it)->a) { return it->b < next(it)->b; } if (it == ls.begin() || next(it) == ls.end()) { return true; } return 1. * (it->b - prev(it)->b) * (next(it)->a - it->a) < 1. * (it->b - next(it)->b) * (prev(it)->a - it->a); } public: void insert(T a, T b) { if (query_type) { ls.emplace(-a, -b); } else { ls.emplace(a, b); } const line& ln = (query_type ? line{-a, -b} : line{a, b}); auto it = ls.find(ln); if (!is_needed(it)) { ls.erase(it); return; } while (it != ls.begin() && !is_needed(prev(it))) { ls.erase(prev(it)); } while (next(it) != ls.end() && !is_needed(next(it))) { ls.erase(next(it)); } if (it != ls.begin()) { prev(it)->has_nxt = true; prev(it)->nxt_a = it->a; prev(it)->nxt_b = it->b; } if (next(it) != ls.end()) { it->has_nxt = true; it->nxt_a = next(it)->a; it->nxt_b = next(it)->b; } } T operator()(T x) const { const auto& it = ls.lower_bound(line(x, 0, true)); if (query_type) { return -it->a * x - it->b; } else { return it->a * x + it->b; } } }; }; // namespace kyopro /** * @brief Convex Hull Trick */ #line 2 "Library/src/stream.hpp" #include #include #include #line 2 "Library/src/internal/type_traits.hpp" #include #include #include #include #include namespace kyopro { namespace internal { template struct first_enabled {}; template struct first_enabled, Args...> { using type = T; }; template struct first_enabled, Args...> : first_enabled {}; template struct first_enabled { using type = T; }; template using first_enabled_t = typename first_enabled::type; template * = nullptr> struct int_least { using type = first_enabled_t, std::enable_if, std::enable_if, std::enable_if, std::enable_if>; }; template * = nullptr> struct uint_least { using type = first_enabled_t, std::enable_if, std::enable_if, std::enable_if, std::enable_if>; }; template using int_least_t = typename int_least::type; template using uint_least_t = typename uint_least::type; template using double_size_uint_t = uint_least_t<2 * std::numeric_limits::digits>; template using double_size_int_t = int_least_t<2 * std::numeric_limits::digits>; struct modint_base {}; template using is_modint = std::is_base_of; template using is_modint_t = std::enable_if_t::value>; // is_integral template using is_integral_t = std::enable_if_t || std::is_same_v || std::is_same_v>; }; // namespace internal }; // namespace kyopro /** * @brief Type Traits * @see https://qiita.com/kazatsuyu/items/f8c3b304e7f8b35263d8 */ #line 6 "Library/src/stream.hpp" namespace kyopro { inline void single_read(char& c) { c = getchar_unlocked(); while (isspace(c)) c = getchar_unlocked(); } template * = nullptr> inline void single_read(T& a) { a = 0; bool is_negative = false; char c = getchar_unlocked(); while (isspace(c)) { c = getchar_unlocked(); } if (c == '-') is_negative = true, c = getchar_unlocked(); while (isdigit(c)) { a = 10 * a + (c - '0'); c = getchar_unlocked(); } if (is_negative) a *= -1; } template * = nullptr> inline void single_read(T& a) { long long x; single_read(x); a = T(x); } inline void single_read(std::string& str) noexcept { char c = getchar_unlocked(); while (isspace(c)) c = getchar_unlocked(); while (!isspace(c)) { str += c; c = getchar_unlocked(); } } template inline void read(T& x) noexcept {single_read(x);} template inline void read(Head& head, Tail&... tail) noexcept { single_read(head), read(tail...); } inline void single_write(char c) noexcept { putchar_unlocked(c); } template * = nullptr> inline void single_write(T a) noexcept { if (!a) { putchar_unlocked('0'); return; } if constexpr (std::is_signed_v) { if (a < 0) putchar_unlocked('-'), a *= -1; } constexpr int d = std::numeric_limits::digits10; char s[d + 1]; int now = d + 1; while (a) { s[--now] = (char)'0' + a % 10; a /= 10; } while (now <= d) putchar_unlocked(s[now++]); } template * = nullptr> inline void single_write(T a) noexcept { single_write(a.val()); } inline void single_write(const std::string& str) noexcept { for (auto c : str) { putchar_unlocked(c); } } template inline void write(T x) noexcept { single_write(x); } template inline void write(Head head, Tail... tail) noexcept { single_write(head); putchar_unlocked(' '); write(tail...); } template inline void put(Args... x) noexcept { write(x...); putchar_unlocked('\n'); } }; // namespace kyopro /** * @brief Fast IO(高速入出力) */ #line 2 "Library/src/template.hpp" #include #define rep(i, n) for (int i = 0; i < (n); i++) #define all(x) std::begin(x), std::end(x) #define popcount(x) __builtin_popcountll(x) using i128 = __int128_t; using ll = long long; using ld = long double; using graph = std::vector>; using P = std::pair; constexpr int inf = std::numeric_limits::max() / 2; constexpr ll infl = std::numeric_limits::max() / 2; const long double pi = acosl(-1); constexpr int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 0}; constexpr int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0}; template constexpr inline bool chmax(T1& a, T2 b) { return a < b && (a = b, true); } template constexpr inline bool chmin(T1& a, T2 b) { return a > b && (a = b, true); } /** * @brief Template */ #line 5 "a.cpp" using namespace std; using namespace kyopro; int main() { int n; read(n); vector a(n, 0); rep(i, n) read(a[i]); vector sa(n + 1, 0LL); rep(i, n) sa[i + 1] = sa[i] + a[i]; vector answer(n, infl); // calc for all i \in [l,r) auto f = [&](const auto& f, int l, int r) -> void { if (l == r) return; if (r - l == 1) { chmin(answer[l], 1 + a[l]); return; } int m = (l + r) / 2; // mを燃やす? // mを燃やさない(mをまたがない)なら下に潜る f(f, l, m); f(f, m, r); // 燃やすなら... { CHT cht; for (int i = m + 1; i <= r; ++i) { cht.insert(-2 * i, ll(i) * i + sa[i]); } ll pref_min = infl; for (int i = 0; i <= m; ++i) { chmin(pref_min, ll(i) * i - sa[i] + cht(i)); chmin(answer[i], pref_min); } } { CHT cht; for (int i = l; i <= m; ++i) { // debug(i); // debug(2 * i); // debug(i * i - sa[i]); cht.insert(-2 * i, (ll)i * i - sa[i]); } ll suff_min = infl; for (int i = r - 1; i > m; --i) { // debug(suff_min); chmin(answer[i], suff_min); chmin(suff_min, ll(i) * i + sa[i] + cht(i)); } } }; f(f, 0, n); rep(i, n) put(answer[i]); }