#include #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,fma,abm,mmx,avx,avx2") #define rep(i, n) for (ll i = 0; i < (int)(n); i++) #define rrep(i, n) for (ll i = (int)(n) - 1; i >= 0; i--) #define all(x) (x).begin(), (x).end() #define sz(x) ll(x.size()) #define yn(joken) cout<<((joken) ? "Yes" : "No")<<"\n" #define YN(joken) cout<<((joken) ? "YES" : "NO")<<"\n" #define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end()) using namespace std; using ll = long long; using pii = pair; using pll = pair; using vi = vector; using vl = vector; using vpi = vector>; using vpl = vector>; using vs = vector; using vc = vector; using vd = vector; using vld = vector; using vvi = vector>; using vvl = vector>; using vvs = vector>; using vvc = vector>; using vvd = vector>; using vvld = vector>; using vvvi = vector>>; using vvvl = vector>>; using vvvvi = vector>>>; using vvvvl = vector>>>; template using priq = priority_queue; template using priqg = priority_queue, greater>; const int INF = 1e9; const ll LINF = 2e18; template inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); } template inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); } template vi iota(int n) { vi a(n); return iota(a.begin(), a.end(), 0), a; } template vi iota(const vector &a, bool greater = false) { vi ret(a.size()); iota(ret.begin(), ret.end(), 0); sort(ret.begin(), ret.end(), [&](int i, int j) { if(greater) return a[i] > a[j]; return a[i] < a[j]; }); return ret; } template void rearrange(const vector &id) {} template void rearrange_exec(const vector &id, vector &v) { vector w(v.size()); rep(i, sz(id)) w[i] = v[id[i]]; v.swap(w); } // 並び替える順番, 並び替えるvector template void rearrange(const vector &id, Head &a, Tail &...tail) { rearrange_exec(id, a); rearrange(id, tail...); } template vector RUI(const vector &v) { vector res(v.size() + 1); for(int i = 0; i < v.size(); i++) res[i + 1] = res[i] + v[i]; return res; } // 反時計周りに 90 度回転 template void roth(vector> &v) { if(empty(v)) return; int n = v.size(), m = v[0].size(); vector> res(m, vector(n)); rep(i, n) rep(j, m) res[m - 1 - j][i] = v[i][j]; v.swap(res); } // 時計周りに 90 度回転 template void rott(vector> &v) { if(empty(v)) return; int n = v.size(), m = v[0].size(); vector> res(m, vector(n)); rep(i, n) rep(j, m) res[j][n - 1 - i] = v[i][j]; v.swap(res); } bool ispow2(int i) { return i && (i & -i) == i; } bool ispow2(ll i) { return i && (i & -i) == i; } template T ceil(T x, S y) { // x/y以上の最小の整数を返す assert(y); return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y)); } template T floor(T x, S y) { // x/y以下の最大の整数を返す assert(y); return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1))); } template vector> RunLength(const vector &v) { vector> res; for(auto &e : v) { if(res.empty() || res.back().first != e) res.emplace_back(e, 1); else res.back().second++; } return res; } vector> RunLength(const string &v) { vector> res; for(auto &e : v) { if(res.empty() || res.back().first != e) res.emplace_back(e, 1); else res.back().second++; } return res; } template T bin_search(T ok, T ng, const F &f) { while(abs(ok - ng) > 1) { T mid = ok + ng >> 1; (f(mid) ? ok : ng) = mid; } return ok; } template T bin_search_double(T ok, T ng, const F &f, int iter = 80) { while(iter--) { T mid = (ok + ng) / 2; (f(mid) ? ok : ng) = mid; } return ok; } template istream& operator>>(istream& is, vector& v) { for (int i = 0; i < int(v.size()); i++) { is >> v[i]; } return is; } namespace aux { template struct tp { static void output(std::ostream &os, const T &v) { os << std::get(v) << (&os == &cerr ? ", " : " "); tp::output(os, v); } }; template struct tp { static void output(std::ostream &os, const T &v) { os << std::get(v); } }; } // namespace aux template std::ostream &operator<<(std::ostream &os, const std::tuple &t) { if(&os == &cerr) { os << '('; } aux::tp, 0, sizeof...(Ts) - 1>::output(os, t); if(&os == &cerr) { os << ')'; } return os; } template std::ostream &operator<<(std::ostream &os, const stack &_st) { auto st = _st; vector res; while(!empty(st)) res.emplace_back(st.top()), st.pop(); reverse(all(res)); return os << res; } template std::ostream &operator<<(std::ostream &os, const queue &_qu) { auto qu = _qu; vector res; while(!empty(qu)) res.emplace_back(qu.front()), qu.pop(); return os << res; } template std::ostream &operator<<(std::ostream &os, const deque &_dq) { auto dq = _dq; vector res; while(!empty(dq)) res.emplace_back(dq.front()), dq.pop_front(); return os << res; } template std::ostream &operator<<(std::ostream &os, const priority_queue &_pq) { auto pq = _pq; vector res; while(!empty(pq)) res.emplace_back(pq.top()), pq.pop(); return os << res; } template ostream &operator<<(ostream &os, const pair &p) { if(&os == &cerr) { return os << "(" << p.first << ", " << p.second << ")"; } return os << p.first << " " << p.second; } template std::basic_ostream &operator<<(std::basic_ostream &os, const Container &x) { bool f = true; if(&os == &cerr) os << "["; for(auto &y : x) { if(&os == &cerr) os << (f ? "" : ", ") << y; else os << (f ? "" : " ") << y; f = false; } if(&os == &cerr) os << "]"; return os; } static uint32_t RandXor(){ static uint32_t x=123456789; static uint32_t y=362436069; static uint32_t z=521288629; static uint32_t w=88675123; uint32_t t; t=x^(x<<11); x=y; y=z; z=w; return w=(w^(w>>19))^(t^(t>>8)); } static double Rand01(){ return (RandXor()+0.5)*(1.0/UINT_MAX); } template void rshuffle(vector &V){ random_device seed_gen; mt19937 engine(seed_gen()); shuffle(V.begin(),V.end(),engine); } // auto seg=get_dual_segment_tree(N,h,OM0,commutative); のように宣言する // N: 元配列の要素数 // h: 作用fと要素aに対して, f*f->f および f*a->a のはず // OM0: 単位元 // commutative: 作用が可換かどうか, デフォルトはtrue // update(l,r,x): 半開区間[l,r)にxを作用させる // seg[k]でk番目の要素を取得できる template struct DualSegmentTree{ DualSegmentTree(int n, const H h, const OperatorMonoid &OM0, bool commutative) : h(h), OM0(OM0), commutative(commutative){ sz = 1; height = 0; while (sz < n) sz <<= 1, height++; lazy.assign(2 * sz, OM0); } void update(int a, int b, const OperatorMonoid &x){ a+=sz; b+=sz-1; if(!commutative) thrust(a); if(!commutative) thrust(b); for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1){ if (l & 1) lazy[l] = h(lazy[l], x), ++l; if (r & 1) --r, lazy[r] = h(lazy[r], x); } } vector get_all(){ for(int i=1;i(lazy.begin()+sz,lazy.end()); } OperatorMonoid operator[](int k){ if(commutative){ OperatorMonoid ret=OM0; k+=sz; while(k){ ret=h(ret,lazy[k]); k>>=1; } return ret; } else{ thrust(k += sz); return lazy[k]; } } private: int sz, height; vector lazy; const H h; const OperatorMonoid OM0; bool commutative; inline void propagate(int k){ if (lazy[k] != OM0){ lazy[2 * k] = h(lazy[2 * k], lazy[k]); lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]); lazy[k] = OM0; } } inline void thrust(int k){ for (int i = height; i > 0; i--) propagate(k >> i); } }; template DualSegmentTree get_dual_segment_tree(int N, const H &h, const OperatorMonoid &OM0,bool commutative=true){ return {N, h, OM0, commutative}; } // DynamicLiChaoTree LCT(ll min_index,ll max_index); などする // xの動きうる最大範囲が[min_index,max_index) // void add_line(T a,T b): 直線 ax+b を追加 // void add_segment_line(T Llim,T Rlim,T a,T b): [Llim,Rlim]の線分 ax+b を追加 // T get(T x): min(ax+b) を返す template struct DynamicLiChaoTree{ private: struct Line{ T slope, intercept; Line() : slope(0), intercept(numeric_limits::max()) {} Line(T a, T b):slope(a),intercept(b) {} T f(T x) {return slope * x + intercept;} }; struct node { Line line; node *left, *right; node(Line line): line(line), left(nullptr), right(nullptr) {} }; const long long min_index,max_index; node* root; node* add_line(node* &t, long long l, long long r, Line line) const { if(l == r) return nullptr; if (!t) { t = new node(line); return t; } long long m = (l + r) >> 1; bool L = (line.f(l) <= t->line.f(l)); bool M = (line.f(m) <= t->line.f(m)); bool R = (line.f(r) <= t->line.f(r)); if(L && R){ t->line = line; return t; } if(!L && !R){ return t; } if(M){ swap(t->line, line); } if(L != M){ t->left=add_line(t->left, l, m, line); } else{ t->right=add_line(t->right, m + 1, r, line); } return t; } node* add_segment_line(node* &t, long long Llim, long long Rlim, long long l, long long r, Line line){ if(r <= Llim || Rlim <= l) return t; if(Llim <= l && r <= Rlim) return add_line(t, l, r, line); if(t == nullptr) t = new node(Line()); long long m = (l + r) >> 1; t->left = add_segment_line(t->left, Llim, Rlim, l, m, line); t->right = add_segment_line(t->right, Llim, Rlim, m, r, line); return t; } public: DynamicLiChaoTree(T min_index,T max_index): min_index(min_index), max_index(max_index), root(nullptr){} void add_line(T a, T b){ Line line = Line{a, b}; add_line(root, min_index, max_index, line); } void add_segment_line(T Llim, T Rlim, T a, T b){ Line line = Line{a,b}; add_segment_line(root, Llim, Rlim, min_index, max_index, line); } T get(long long x){ node *t = root; long long l = min_index, r = max_index; T ret = numeric_limits::max(); while(r - l > 0 && t != nullptr){ long long m = (l + r) >> 1; ret = min(ret, t->line.f(x)); if(x < m){ r = m; t = t->left; } else{ l = m; t = t->right; } } return ret; } }; void solve(){ ll N; cin>>N; vl A(N); cin>>A; auto h=[&](ll l,ll r){ return min(l,r); }; auto seg=get_dual_segment_tree(N,h,LINF); auto calc=[&](auto &&self,ll l,ll r)->void{ if(r-l<=0) return; if(r-l==1){ seg.update(l,r,1+A[l]); return; } ll m=(l+r)/2; self(self,l,m); self(self,m,r); DynamicLiChaoTree DLC1(1,m-l+1); ll smy=0; for(ll i=m;i=l;i--){ smx+=A[i]; seg.update(i,m,DLC1.get(m-i)+(m-i)*(m-i)+smx); } DynamicLiChaoTree DLC2(1,r-m+1); smx=0; for(ll i=m-1;i>=l;i--){ smx+=A[i]; DLC2.add_line(2*(m-i),smx+(m-i)*(m-i)); } smy=0; for(ll i=m;i