1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//Franciszek Witt
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif
typedef long long ll;
struct mast {
    vector<int> st;
    int si = 1;
    void init(vector<int> &v) {
        int x = ssize(v);
        while(si <= (2 * (x + 1)))
            si *= 2;
        st.resize(si);
        fill(st.begin(), st.end(), -1);
        REP(i, x)
            st[i + si / 2] = v[i];
        for(int i = si / 2 - 1; i >= 1; --i)
            st[i] = max(st[i << 1], st[(i << 1) | 1]);
    }
    void ins(int x){
        x += si / 2;
        st[x] = -1;
        x >>= 1;
        while(x) {
            int mx = max(st[x << 1], st[(x << 1) | 1]);
            st[x] = mx;
            x >>= 1;
        }
    }
    int get(int b, int e) {
        b += si / 2;
        e += si / 2;
        int r = -1;
        while(b < e) {
            if(b & 1)
                r = max(r, st[b++]);
            if(!(e&1))
                r = max(r, st[e--]);
            b >>= 1;
            e >>= 1;
        }
        if(b == e)
            r = max(r, st[b]);
        return r;
    }
};
struct mist {
    vector<int> st;
    int si = 1;
    void init(vector<int> &v) {
        int x = ssize(v);
        while(si <= (2 * (x + 1)))
            si *= 2;
        st.resize(si);
        fill(st.begin(), st.end(), INT_MAX);
        REP(i, x)
            st[i + si / 2] = v[i];
        for(int i = si / 2 - 1; i >= 1; --i)
            st[i] = min(st[i << 1], st[(i << 1) | 1]);
    }
    void ins(int x){
        x += si / 2;
        st[x] = INT_MAX;
        x >>= 1;
        while(x) {
            int mx = min(st[x << 1], st[(x << 1) | 1]);
            st[x] = mx;
            x >>= 1;
        }
    }
    int get(int b, int e) {
        b += si / 2;
        e += si / 2;
        int r = INT_MAX;
        while(b < e) {
            if(b & 1)
                r = min(r, st[b++]);
            if(!(e&1))
                r = min(r, st[e--]);
            b >>= 1;
            e >>= 1;
        }
        if(b == e)
            r = min(r, st[b]);
        return r;
    }
};
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> v(n), vv(n);
    REP(i, n) {
        cin >> v[i];
        --v[i];
        vv[v[i]] = i;
    }

    vector<ll> odp(n);

    REP(w, n) {
        debug("w!", w);
        mast st0;
        mist st1;
        st0.init(v);
        st1.init(v);
        auto turnoff = [&] (int x) {
            st0.ins(x);
            st1.ins(x);
        };
        vector<int> odl(n + 1, -1);
        queue<int> qq;
        odl[w] = 0;
        qq.push(w);
        turnoff(w);
        while(!qq.empty()) {
            int x = qq.front();
            debug(x);
            int can = st0.get(0, x - 1);
            while(can > v[x]) {
                int ccan = vv[can];
                odl[ccan] = odl[x] + 1;
                qq.push(ccan);
                turnoff(ccan);
                can = st0.get(0, x - 1);
            }
            can = st1.get(x + 1, n - 1);
            debug( can);
            while(can < v[x]) {
                int ccan = vv[can];
                odl[ccan] = odl[x] + 1;
                qq.push(ccan);
                turnoff(ccan);
                can = st1.get(x + 1, n - 1);
            }
            qq.pop();
        }
        REP(i, n)
            odp[w] += (odl[i] == -1 ? 0 : odl[i]);
    }

    for(auto i : odp)
        cout << i << " ";
    cout << endl;
}