#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template<typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template<typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio;
#ifdef DEBUG_
#include "../program_contest_library/memo/dump.hpp"
#else
#define dump(...)
#endif
const ll INF = 1LL<<30;
template <typename S>
struct sparseTable {
using T = typename S::T;
int n;
vector<int> log2;
vector<vector<T>> t;
sparseTable() {}
sparseTable(int nn) { construct(nn); }
void construct(int nn) {
n = nn;
log2.assign(n+1, 0);
for(int i=2; i<=n; ++i) log2[i] = log2[i >> 1] + 1;
t = vector<vector<T>>(log2[n]+1, vector<T>(n));
}
void init(vector<T> v) {
for(int i=0; i<n; ++i) t[0][i] = v[i];
for(int j=1; j<=log2[n]; ++j) {
int w = 1LL<<(j-1);
for (int i = 0; i+(w<<1) <= n; ++i) {
t[j][i] = S::op(t[j-1][i], t[j-1][i+w]);
}
}
}
// [l, r]
T query(int l, int r) {
int j = log2[r - l];
return S::op(t[j][l], t[j][r-(1 << j)+1]);
}
};
class LCA {
private:
const int n = 0;
const int log2_n = 0;
vector<vector<int>> par;
vector<vector<int>> g;
vector<int> depth; // 頂点iの深さ
vector<int> vs; // 頂点を訪問順に並べたもの
vector<int> depth_seq; // depth_seq[i] = (頂点vs[i]の深さ)
vector<int> id; // 頂点が初めてvsに登場するインデックス
struct minimum_st {
using T = PII;
static T op(const T& a, const T& b) { return min(a, b); }
};
sparseTable<minimum_st> st;
void dfs(int v, int p, int d, int &k) {
id[v] = k; vs[k] = v; depth_seq[k++] = d; depth[v] = d;
for(auto to: g[v]) if(to != p) {
dfs(to, v, d+1, k);
vs[k] = v; depth_seq[k++] = d;
}
}
public:
LCA(int n_=1e5) : n(n_), g(n), depth(n), vs(2*n-1), depth_seq(2*n-1), id(n) {}
// u-vに辺を張る
void add_edge(int u, int v) {
g[u].push_back(v);
g[v].push_back(u);
}
// rootを根として初期化
void build(int root = 0) {
int k = 0;
dfs(root, -1, 0, k);
vector<PII> v(2*n-1);
REP(i, 2*n-1) v[i] = {depth_seq[i], i};
st.construct(2*n-1);
st.init(v);
}
// uとvのlcaを返す O(1)
int get(int u, int v) {
if(id[u] > id[v]) swap(u, v);
return vs[st.query(id[u], id[v]).second];
}
int dist(int u, int v) {
return depth[u] + depth[v] - 2*depth[get(u,v)];
}
};
int main(void) {
int n;
cin >> n;
vector<int> a(n);
REP(i, n) cin >> a[i], a[i]--;
// 二分探索木をつくる
LCA lca(n);
vector<vector<int>> g(n);
{
set<int> st;
vector<int> lch(n, -1), rch(n, -1);
st.insert(a[0]);
FOR(i, 1, n) {
// a[i] が追加される位置
// a[i]以上の最小の要素の左の子
// a[i]未満の最大の要素の右の子
auto itr = st.lower_bound(a[i]);
if(itr != st.begin() && rch[*prev(itr)] == -1) {
rch[*prev(itr)] = a[i];
} else if(itr != st.end() && lch[*itr] == -1) {
lch[*itr] = a[i];
}
st.insert(a[i]);
}
auto dfs = [&](auto &&self, int v) -> void {
if(lch[v] != -1) {
g[v].push_back(lch[v]);
g[lch[v]].push_back(v);
lca.add_edge(v, lch[v]);
self(self, lch[v]);
}
if(rch[v] != -1) {
g[v].push_back(rch[v]);
g[rch[v]].push_back(v);
lca.add_edge(v, rch[v]);
self(self, rch[v]);
}
};
dfs(dfs, a[0]);
lca.build();
}
vector<int> sz(n), dead(n);
auto find_centroid = [&](int root) {
auto get_size = [&](auto &&self, int v, int p) -> void {
sz[v] = 1;
for(auto to: g[v]) if(to != p && !dead[to]) {
self(self, to, v);
sz[v] += sz[to];
}
};
get_size(get_size, root, -1);
auto dfs = [&](auto &&self, int v, int p) -> int {
for(auto to: g[v]) if(to != p && !dead[to]) {
if(sz[to] > sz[root]/2) return self(self, to, v);
}
return v;
};
return dfs(dfs, root, -1);
};
vector<int> par(n);
auto centroid_decomposition = [&](auto &&self, int root, int p) -> void {
int c = find_centroid(root);
dead[c] = true;
par[c] = p;
for(auto to: g[c]) if(!dead[to]) {
self(self, to, c);
}
dead[c] = false;
};
centroid_decomposition(centroid_decomposition, 0, -1);
ll ret = 0;
vector<ll> dist(n), contribution(n), cnt(n);
REP(i, n) {
{
int v = a[i];
ll add = dist[a[i]];
while(par[v] != -1) {
add += dist[par[v]]-contribution[v] + (cnt[par[v]]-cnt[v]) * lca.dist(par[v], a[i]);
v = par[v];
}
ret += add;
cout << ret << "\n";
}
{
int v = par[a[i]], pre = a[i];
cnt[a[i]]++;
while(v != -1) {
cnt[v]++;
const ll d = lca.dist(v, a[i]);
dist[v] += d;
contribution[pre] += d;
pre = v;
v = par[v];
}
}
}
cout << flush;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnVzaW5nIGxsID0gbG9uZyBsb25nOwp1c2luZyBQSUkgPSBwYWlyPGxsLCBsbD47CiNkZWZpbmUgRk9SKGksIGEsIG4pIGZvciAobGwgaSA9IChsbClhOyBpIDwgKGxsKW47ICsraSkKI2RlZmluZSBSRVAoaSwgbikgRk9SKGksIDAsIG4pCiNkZWZpbmUgQUxMKHgpIHguYmVnaW4oKSwgeC5lbmQoKQp0ZW1wbGF0ZTx0eXBlbmFtZSBUPiB2b2lkIGNobWluKFQgJmEsIGNvbnN0IFQgJmIpIHsgYSA9IG1pbihhLCBiKTsgfQp0ZW1wbGF0ZTx0eXBlbmFtZSBUPiB2b2lkIGNobWF4KFQgJmEsIGNvbnN0IFQgJmIpIHsgYSA9IG1heChhLCBiKTsgfQpzdHJ1Y3QgRmFzdElPIHtGYXN0SU8oKSB7IGNpbi50aWUoMCk7IGlvczo6c3luY193aXRoX3N0ZGlvKDApOyB9fWZhc3Rpb2Zhc3RpbzsKI2lmZGVmIERFQlVHXyAKI2luY2x1ZGUgIi4uL3Byb2dyYW1fY29udGVzdF9saWJyYXJ5L21lbW8vZHVtcC5ocHAiCiNlbHNlCiNkZWZpbmUgZHVtcCguLi4pCiNlbmRpZgpjb25zdCBsbCBJTkYgPSAxTEw8PDMwOwoKdGVtcGxhdGUgPHR5cGVuYW1lIFM+CnN0cnVjdCBzcGFyc2VUYWJsZSB7CiAgICB1c2luZyBUID0gdHlwZW5hbWUgUzo6VDsKICAgIGludCBuOwogICAgdmVjdG9yPGludD4gbG9nMjsKICAgIHZlY3Rvcjx2ZWN0b3I8VD4+IHQ7CgogICAgc3BhcnNlVGFibGUoKSB7fQogICAgc3BhcnNlVGFibGUoaW50IG5uKSB7IGNvbnN0cnVjdChubik7IH0KICAgIHZvaWQgY29uc3RydWN0KGludCBubikgewogICAgICAgIG4gPSBubjsKICAgICAgICBsb2cyLmFzc2lnbihuKzEsIDApOwogICAgICAgIGZvcihpbnQgaT0yOyBpPD1uOyArK2kpIGxvZzJbaV0gPSBsb2cyW2kgPj4gMV0gKyAxOwogICAgICAgIHQgPSB2ZWN0b3I8dmVjdG9yPFQ+Pihsb2cyW25dKzEsIHZlY3RvcjxUPihuKSk7CiAgICB9CiAgICB2b2lkIGluaXQodmVjdG9yPFQ+IHYpIHsKICAgICAgICBmb3IoaW50IGk9MDsgaTxuOyArK2kpIHRbMF1baV0gPSB2W2ldOwogICAgICAgIGZvcihpbnQgaj0xOyBqPD1sb2cyW25dOyArK2opIHsKICAgICAgICAgICAgaW50IHcgPSAxTEw8PChqLTEpOwogICAgICAgICAgICBmb3IgKGludCBpID0gMDsgaSsodzw8MSkgPD0gbjsgKytpKSB7CiAgICAgICAgICAgICAgICB0W2pdW2ldID0gUzo6b3AodFtqLTFdW2ldLCB0W2otMV1baSt3XSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICAvLyBbbCwgcl0KICAgIFQgcXVlcnkoaW50IGwsIGludCByKSB7CiAgICAgICAgaW50IGogPSBsb2cyW3IgLSBsXTsKICAgICAgICByZXR1cm4gUzo6b3AodFtqXVtsXSwgdFtqXVtyLSgxIDw8IGopKzFdKTsKICAgIH0KfTsKCmNsYXNzIExDQSB7CnByaXZhdGU6CiAgICBjb25zdCBpbnQgbiA9IDA7CiAgICBjb25zdCBpbnQgbG9nMl9uID0gMDsKICAgIHZlY3Rvcjx2ZWN0b3I8aW50Pj4gcGFyOwogICAgdmVjdG9yPHZlY3RvcjxpbnQ+PiBnOwogICAgdmVjdG9yPGludD4gZGVwdGg7ICAgICAvLyDpoILngrlp44Gu5rex44GVCiAgICB2ZWN0b3I8aW50PiB2czsgICAgICAgIC8vIOmggueCueOCkuioquWVj+mghuOBq+S4puOBueOBn+OCguOBrgogICAgdmVjdG9yPGludD4gZGVwdGhfc2VxOyAvLyBkZXB0aF9zZXFbaV0gPSAo6aCC54K5dnNbaV3jga7mt7HjgZUpCiAgICB2ZWN0b3I8aW50PiBpZDsgICAgICAgIC8vIOmggueCueOBjOWIneOCgeOBpnZz44Gr55m75aC044GZ44KL44Kk44Oz44OH44OD44Kv44K5CiAgICBzdHJ1Y3QgbWluaW11bV9zdCB7CiAgICAgICAgdXNpbmcgVCA9IFBJSTsKICAgICAgICBzdGF0aWMgVCBvcChjb25zdCBUJiBhLCBjb25zdCBUJiBiKSB7IHJldHVybiBtaW4oYSwgYik7IH0KICAgIH07IAogICAgc3BhcnNlVGFibGU8bWluaW11bV9zdD4gc3Q7CiAgICB2b2lkIGRmcyhpbnQgdiwgaW50IHAsIGludCBkLCBpbnQgJmspIHsKICAgICAgICBpZFt2XSA9IGs7IHZzW2tdID0gdjsgZGVwdGhfc2VxW2srK10gPSBkOyBkZXB0aFt2XSA9IGQ7CiAgICAgICAgZm9yKGF1dG8gdG86IGdbdl0pIGlmKHRvICE9IHApIHsKICAgICAgICAgICAgZGZzKHRvLCB2LCBkKzEsIGspOwogICAgICAgICAgICB2c1trXSA9IHY7IGRlcHRoX3NlcVtrKytdID0gZDsKICAgICAgICB9CiAgICB9CnB1YmxpYzoKICAgIExDQShpbnQgbl89MWU1KSA6IG4obl8pLCBnKG4pLCBkZXB0aChuKSwgdnMoMipuLTEpLCBkZXB0aF9zZXEoMipuLTEpLCBpZChuKSB7fQogICAgLy8gdS1244Gr6L6644KS5by144KLCiAgICB2b2lkIGFkZF9lZGdlKGludCB1LCBpbnQgdikgewogICAgICAgIGdbdV0ucHVzaF9iYWNrKHYpOwogICAgICAgIGdbdl0ucHVzaF9iYWNrKHUpOwogICAgfQogICAgLy8gcm9vdOOCkuagueOBqOOBl+OBpuWIneacn+WMlgogICAgdm9pZCBidWlsZChpbnQgcm9vdCA9IDApIHsKICAgICAgICBpbnQgayA9IDA7CiAgICAgICAgZGZzKHJvb3QsIC0xLCAwLCBrKTsKICAgICAgICB2ZWN0b3I8UElJPiB2KDIqbi0xKTsKICAgICAgICBSRVAoaSwgMipuLTEpIHZbaV0gPSB7ZGVwdGhfc2VxW2ldLCBpfTsKICAgICAgICBzdC5jb25zdHJ1Y3QoMipuLTEpOwogICAgICAgIHN0LmluaXQodik7CiAgICB9CiAgICAvLyB144GoduOBrmxjYeOCkui/lOOBmSBPKDEpCiAgICBpbnQgZ2V0KGludCB1LCBpbnQgdikgewogICAgICAgIGlmKGlkW3VdID4gaWRbdl0pIHN3YXAodSwgdik7CiAgICAgICAgcmV0dXJuIHZzW3N0LnF1ZXJ5KGlkW3VdLCBpZFt2XSkuc2Vjb25kXTsKICAgIH0KICAgIGludCBkaXN0KGludCB1LCBpbnQgdikgewogICAgICAgIHJldHVybiBkZXB0aFt1XSArIGRlcHRoW3ZdIC0gMipkZXB0aFtnZXQodSx2KV07CiAgICB9Cn07CgppbnQgbWFpbih2b2lkKSB7CiAgICBpbnQgbjsKICAgIGNpbiA+PiBuOwogICAgdmVjdG9yPGludD4gYShuKTsKICAgIFJFUChpLCBuKSBjaW4gPj4gYVtpXSwgYVtpXS0tOwoKICAgIC8vIOS6jOWIhuaOoue0ouacqOOCkuOBpOOBj+OCiwogICAgTENBIGxjYShuKTsKICAgIHZlY3Rvcjx2ZWN0b3I8aW50Pj4gZyhuKTsKICAgIHsKICAgICAgICBzZXQ8aW50PiBzdDsKICAgICAgICB2ZWN0b3I8aW50PiBsY2gobiwgLTEpLCByY2gobiwgLTEpOwoKICAgICAgICBzdC5pbnNlcnQoYVswXSk7ICAgICAgCiAgICAgICAgRk9SKGksIDEsIG4pIHsKICAgICAgICAgICAgLy8gYVtpXSDjgYzov73liqDjgZXjgozjgovkvY3nva4KICAgICAgICAgICAgLy8gYVtpXeS7peS4iuOBruacgOWwj+OBruimgee0oOOBruW3puOBruWtkAogICAgICAgICAgICAvLyBhW2ld5pyq5rqA44Gu5pyA5aSn44Gu6KaB57Sg44Gu5Y+z44Gu5a2QCgogICAgICAgICAgICBhdXRvIGl0ciA9IHN0Lmxvd2VyX2JvdW5kKGFbaV0pOwogICAgICAgICAgICBpZihpdHIgIT0gc3QuYmVnaW4oKSAmJiByY2hbKnByZXYoaXRyKV0gPT0gLTEpIHsKICAgICAgICAgICAgICAgIHJjaFsqcHJldihpdHIpXSA9IGFbaV07CiAgICAgICAgICAgIH0gZWxzZSBpZihpdHIgIT0gc3QuZW5kKCkgJiYgbGNoWyppdHJdID09IC0xKSB7CiAgICAgICAgICAgICAgICBsY2hbKml0cl0gPSBhW2ldOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHN0Lmluc2VydChhW2ldKTsKICAgICAgICB9CgogICAgICAgIGF1dG8gZGZzID0gWyZdKGF1dG8gJiZzZWxmLCBpbnQgdikgLT4gdm9pZCB7CiAgICAgICAgICAgIGlmKGxjaFt2XSAhPSAtMSkgewogICAgICAgICAgICAgICAgZ1t2XS5wdXNoX2JhY2sobGNoW3ZdKTsKICAgICAgICAgICAgICAgIGdbbGNoW3ZdXS5wdXNoX2JhY2sodik7CiAgICAgICAgICAgICAgICBsY2EuYWRkX2VkZ2UodiwgbGNoW3ZdKTsKICAgICAgICAgICAgICAgIHNlbGYoc2VsZiwgbGNoW3ZdKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZihyY2hbdl0gIT0gLTEpIHsKICAgICAgICAgICAgICAgIGdbdl0ucHVzaF9iYWNrKHJjaFt2XSk7CiAgICAgICAgICAgICAgICBnW3JjaFt2XV0ucHVzaF9iYWNrKHYpOwogICAgICAgICAgICAgICAgbGNhLmFkZF9lZGdlKHYsIHJjaFt2XSk7CiAgICAgICAgICAgICAgICBzZWxmKHNlbGYsIHJjaFt2XSk7CiAgICAgICAgICAgIH0KICAgICAgICB9OwogICAgICAgIGRmcyhkZnMsIGFbMF0pOwogICAgICAgIGxjYS5idWlsZCgpOwogICAgfQoKICAgIHZlY3RvcjxpbnQ+IHN6KG4pLCBkZWFkKG4pOwogICAgYXV0byBmaW5kX2NlbnRyb2lkID0gWyZdKGludCByb290KSB7CiAgICAgICAgYXV0byBnZXRfc2l6ZSA9IFsmXShhdXRvICYmc2VsZiwgaW50IHYsIGludCBwKSAtPiB2b2lkIHsKICAgICAgICAgICAgc3pbdl0gPSAxOwogICAgICAgICAgICBmb3IoYXV0byB0bzogZ1t2XSkgaWYodG8gIT0gcCAmJiAhZGVhZFt0b10pIHsKICAgICAgICAgICAgICAgIHNlbGYoc2VsZiwgdG8sIHYpOwogICAgICAgICAgICAgICAgc3pbdl0gKz0gc3pbdG9dOwogICAgICAgICAgICB9CiAgICAgICAgfTsKICAgICAgICBnZXRfc2l6ZShnZXRfc2l6ZSwgcm9vdCwgLTEpOwogICAgICAgIGF1dG8gZGZzID0gWyZdKGF1dG8gJiZzZWxmLCBpbnQgdiwgaW50IHApIC0+IGludCB7CiAgICAgICAgICAgIGZvcihhdXRvIHRvOiBnW3ZdKSBpZih0byAhPSBwICYmICFkZWFkW3RvXSkgewogICAgICAgICAgICAgICAgaWYoc3pbdG9dID4gc3pbcm9vdF0vMikgcmV0dXJuIHNlbGYoc2VsZiwgdG8sIHYpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiB2OwogICAgICAgIH07CiAgICAgICAgcmV0dXJuIGRmcyhkZnMsIHJvb3QsIC0xKTsKICAgIH07CgogICAgdmVjdG9yPGludD4gcGFyKG4pOwogICAgYXV0byBjZW50cm9pZF9kZWNvbXBvc2l0aW9uID0gWyZdKGF1dG8gJiZzZWxmLCBpbnQgcm9vdCwgaW50IHApIC0+IHZvaWQgewogICAgICAgIGludCBjID0gZmluZF9jZW50cm9pZChyb290KTsKICAgICAgICBkZWFkW2NdID0gdHJ1ZTsKICAgICAgICBwYXJbY10gPSBwOwogICAgICAgIGZvcihhdXRvIHRvOiBnW2NdKSBpZighZGVhZFt0b10pIHsKICAgICAgICAgICAgc2VsZihzZWxmLCB0bywgYyk7CiAgICAgICAgfQogICAgICAgIGRlYWRbY10gPSBmYWxzZTsKICAgIH07CiAgICBjZW50cm9pZF9kZWNvbXBvc2l0aW9uKGNlbnRyb2lkX2RlY29tcG9zaXRpb24sIDAsIC0xKTsKCiAgICBsbCByZXQgPSAwOwogICAgdmVjdG9yPGxsPiBkaXN0KG4pLCBjb250cmlidXRpb24obiksIGNudChuKTsKICAgIFJFUChpLCBuKSB7CiAgICAgICAgewogICAgICAgICAgICBpbnQgdiA9IGFbaV07CiAgICAgICAgICAgIGxsIGFkZCA9IGRpc3RbYVtpXV07CiAgICAgICAgICAgIHdoaWxlKHBhclt2XSAhPSAtMSkgewogICAgICAgICAgICAgICAgYWRkICs9IGRpc3RbcGFyW3ZdXS1jb250cmlidXRpb25bdl0gKyAoY250W3Bhclt2XV0tY250W3ZdKSAqIGxjYS5kaXN0KHBhclt2XSwgYVtpXSk7CiAgICAgICAgICAgICAgICB2ID0gcGFyW3ZdOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldCArPSBhZGQ7CiAgICAgICAgICAgIGNvdXQgPDwgcmV0IDw8ICJcbiI7CiAgICAgICAgfQogICAgICAgIHsKICAgICAgICAgICAgaW50IHYgPSBwYXJbYVtpXV0sIHByZSA9IGFbaV07CiAgICAgICAgICAgIGNudFthW2ldXSsrOwogICAgICAgICAgICB3aGlsZSh2ICE9IC0xKSB7CiAgICAgICAgICAgICAgICBjbnRbdl0rKzsKICAgICAgICAgICAgICAgIGNvbnN0IGxsIGQgPSBsY2EuZGlzdCh2LCBhW2ldKTsKICAgICAgICAgICAgICAgIGRpc3Rbdl0gKz0gZDsKICAgICAgICAgICAgICAgIGNvbnRyaWJ1dGlvbltwcmVdICs9IGQ7CiAgICAgICAgICAgICAgICBwcmUgPSB2OwogICAgICAgICAgICAgICAgdiA9IHBhclt2XTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIGNvdXQgPDwgZmx1c2g7CgogICAgcmV0dXJuIDA7Cn0=