#include #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--) #define all(x) (x).begin(), (x).end() #define sz(x) int(x.size()) #define yn(joken) cout<<((joken) ? "Yes" : "No")<; using vl = vector; using vs = vector; using vc = vector; using vd = vector; using vvi = vector>; using vvl = vector>; using vvd = vector>; const int INF = 1e9; const ll LINF = 1e18; template bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } template vector make_vec(size_t a) { return vector(a); } template auto make_vec(size_t a, Ts... ts) { return vector(ts...))>(a, make_vec(ts...)); } template istream& operator>>(istream& is, vector& v) { for (int i = 0; i < int(v.size()); i++) { is >> v[i]; } return is; } template ostream& operator<<(ostream& os, const vector& v) { for (int i = 0; i < int(v.size()); i++) { os << v[i]; if (i < int(v.size()) - 1) os << ' '; } return os; } // すでに追加したものを削除できるslope trick // Query query(): Query{lx,rx,min_f}を返す,[lx,rx]でmin_fを取る // add_all(T a): f(x)にaを加算する // add_a_minus_x(T a): f(x)にmax(a-x,0)を加算する, \_ // add_x_minus_a(T a): f(x)にmax(x-a,0)を加算する, _/ // add_abs(T a): f(x)に|x-a|を加算する, \/ // erase_a_minus_x(T a): f(x)からmax(a-x,0)を削除する, \_ // erase_x_minus_a(T a): f(x)からmax(x-a,0)を削除する, _/ // erase_abs(T a): f(x)から|x-a|を削除する, \/ // clear_right(): f(x)<-min(f(t)),t<=x , \_/ -> \_ // clear_left(): f(x)<-min(f(t)),x<=t , \_/ -> _/ // shift(T a,T b): f(x)<-min(f(t)),x-b<=t<=x-a // shift(T a): f(x)<-f(x-a),shift(a,a)してるだけ // get(T x): f(x)を返す, fを破壊しO(sz(L)+sz(R)) // merge(SlopeTrick x): f(x)にg(x)を加算する, gを破壊する template struct ErasableSlopeTrick{ const T INF = numeric_limits::max() / 3; set S_l,S_r; map mp_l,mp_r; T min_f, offset_l, offset_r; private: void insert_R(const T &a){ S_r.insert(a - offset_r); mp_r[a - offset_r]++; } T top_R() const{ if (S_r.empty()) return INF; else return *S_r.begin() + offset_r; } T pop_R(){ T val = top_R(); if (!S_r.empty()){ mp_r[val]--; if(mp_r[val]==0) S_r.erase(val); } return val; } void insert_L(const T &a){ S_l.insert(a - offset_l); mp_l[a - offset_l]++; } T top_L() const{ if (S_l.empty()) return -INF; else return *--S_l.end() + offset_l; } T pop_L(){ T val = top_L(); if (!S_l.empty()){ mp_l[val]--; if(mp_l[val]==0) S_l.erase(val); } return val; } size_t size(){ return S_l.size() + S_r.size(); } public: ErasableSlopeTrick() : min_f(0), offset_l(0), offset_r(0) {} struct Query{ T lx, rx, min_f; }; Query query() const{ return (Query){top_L(), top_R(), min_f}; } void add_all(const T &a){ min_f += a; } void add_a_minus_x(const T &a){ insert_R(a); insert_L(pop_R()); min_f += max(T(0), a - top_L()); } void add_x_minus_a(const T &a){ insert_L(a); insert_R(pop_L()); min_f += max(T(0), top_R() - a); } void add_abs(const T &a){ add_a_minus_x(a); add_x_minus_a(a); } void erase_a_minus_x(const T &a){ if(S_l.find(a - offset_l)==S_l.end()){ mp_r[a - offset_r]--; if(mp_r[a - offset_r]==0) S_r.erase(a - offset_r); insert_R(pop_L()); } else{ mp_l[a - offset_l]--; if(mp_l[a - offset_l]==0) S_l.erase(a - offset_l); } min_f -= max(T(0), a - top_R()); } void erase_x_minus_a(const T &a){ if(S_l.find(a - offset_l)==S_l.end()){ mp_r[a - offset_r]--; if(mp_r[a - offset_r]==0) S_r.erase(a - offset_r); } else{ mp_l[a - offset_l]--; if(mp_l[a- offset_l]==0) S_l.erase(a - offset_l); insert_L(pop_R()); } min_f -= max(T(0), top_L() - a); } void erase_abs(const T &a){ erase_a_minus_x(a); erase_x_minus_a(a); } void clear_right(){ if(!S_r.empty()) S_r.clear(); } void clear_left(){ if(!S_l.empty()) S_l.clear(); } void shift(const T &a, const T &b){ assert(a <= b); offset_l += a; offset_r += b; } void shift(const T &a){ shift(a, a); } T get(const T &x){ T ret = min_f; while (!S_l.empty()) ret += max(T(0), pop_L() - x); while (!S_r.empty()) ret += max(T(0), x - pop_R()); return ret; } void merge(ErasableSlopeTrick &st){ if (st.size() > size()){ swap(st.S_l, S_l); swap(st.S_r, S_r); swap(st.min_f, min_f); swap(st.offset_l, offset_l); swap(st.offset_r, offset_r); } while (!st.S_r.empty()) add_x_minus_a(st.pop_R()); while (!st.S_l.empty()) add_a_minus_x(st.pop_L()); min_f += st.min_f; } }; // Mo(N): 扱う区間の幅のmax, 最適なバケットサイズを計算するのに使う // add(l,r): クエリの対象となる範囲として半開区間[l,r)を追加 // build(add_left,add_right,erase_left,erase_right,out): 各種関数を渡す // add/erase_left/right: 左端/右端を1動かして要素を追加/削除する // out: クエリごとの答えが出たら、それを反映させる struct Mo{ int n; vector> lr; explicit Mo(int n) : n(n) {} void add(int l, int r){ lr.emplace_back(l, r); } template void build(const AL &add_left, const AR &add_right, const EL &erase_left, const ER &erase_right, const O &out){ int q = (int)lr.size(); // バケットサイズの理想は n*sqrt(q) だがsqrt(q)がでかすぎると0になっちゃうので int bs; if(q<=10000) bs = n / min(n, sqrt(q)); else bs = n / (min(n, sqrt(q))*5); vector ord(q); iota(begin(ord), end(ord), 0); sort(begin(ord), end(ord), [&](int a, int b) { int ablock = lr[a].first / bs, bblock = lr[b].first / bs; if (ablock != bblock) return ablock < bblock; return (ablock & 1) ? lr[a].second > lr[b].second : lr[a].second < lr[b].second; }); // 今,半開区間[l,r)をfoldしてますよ(のはず) int l = 0, r = 0; for (auto idx : ord){ while (l > lr[idx].first) add_left(--l); while (r < lr[idx].second) add_right(r++); while (l < lr[idx].first) erase_left(l++); while (r > lr[idx].second) erase_right(--r); out(idx); } } template void build(const A &add, const E &erase, const O &out){ build(add, add, erase, erase, out); } }; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int N; cin>>N; vl A(N); cin>>A; ll sm=0; for(int i=1;i<=N;i++) sm+=N/i; set S1,S2; S1.insert(-LINF); S2.insert(LINF); map mp1,mp2; map> idx1,idx2; vl V1(N*N),V2(N*N); auto add_left=[&](int p){ ll x=A[p]; if(x<=*--S1.end()){ S1.insert(x); mp1[x]++; } else{ S2.insert(x); mp2[x]++; } while(sz(S1)>=sz(S2)+2){ auto M=*--S1.end(); mp1[M]--; if(mp1[M]==0) S1.erase(M); S2.insert(M); mp2[M]++; } while(sz(S1)=sz(S2)+2){ auto M=*--S1.end(); mp1[M]--; if(mp1[M]==0) S1.erase(M); S2.insert(M); mp2[M]++; } while(sz(S1)=sz(S2)+2){ auto M=*--S1.end(); mp1[M]--; if(mp1[M]==0) S1.erase(M); S2.insert(M); mp2[M]++; } while(sz(S1)=sz(S2)+2){ auto M=*--S1.end(); mp1[M]--; if(mp1[M]==0) S1.erase(M); S2.insert(M); mp2[M]++; } while(sz(S1)