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
#include <bits/stdc++.h>
 
using namespace std;
 
#define endl '\n'
#define L long long
#define MP make_pair
#define REP(i, n) for(int i = 0; i < n; ++i)
#define REPR(i, n) for(int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define FORR(i, a, b) for(int i = b - 1; i >= a; --i)
#define EB emplace_back
#define ST first
#define ND second
#define S size
#define RS resize
 
template<class T> using P = pair<T, T>;
template<class T> using V = vector<T>;

int n;
V<int> d, u, m;
V<int> ld, lu, rd, ru;

bool cross(int d1, int u1, int d2, int u2) {
    return (L)(d1 - d2) * (L)(u1 - u2) < 0LL;
}

int lastd, lastu;

void solve_one(int x, int k, int line, int moves) {
    /*if (moves == 1) cout << "new:\n";
    cout << "x: " << x + 1 << " k: " << k + 1 << " line: "
         << line << " moves: " << moves << '\n';
    REP(i, n) {
        cout << m[i] << ' ';
    }
    cout << '\n';*/

    if (x < k && line == 0) {
        if (m[x] == 0 || m[x] > moves) m[x] = moves;
        FOR(i, x + 1, lastd) {
            if (m[i] == 0 || m[i] > moves) {
                if (cross(k, d[k], i, d[i])) m[i] = moves;
                else m[i] = moves + 1;
            }
        }
        lastd = x;
        if (lu[x] < lu[k]) solve_one(lu[x], d[x], 1, moves + 1);
    }
    else if (x > k && line == 0) {
        if (m[x] == 0 || m[x] > moves) m[x] = moves;
        FOR(i, lastd + 1, x) {
            if (m[i] == 0 || m[i] > moves) {
                if (cross(k, d[k], i, d[i])) m[i] = moves;
                else m[i] = moves + 1;
            }
        }
        lastd = x;
        if (ru[x] > ru[k]) solve_one(ru[x], d[x], 1, moves + 1);
    }
    else if (x < k && line == 1) {
        if (m[u[x]] == 0 || m[u[x]] > moves) m[u[x]] = moves;
        FOR(i, x + 1, lastu) {
            if (m[u[i]] == 0 || m[u[i]] > moves) {
                if (cross(u[k], k, u[i], i)) m[u[i]] = moves;
                else m[u[i]] = moves + 1;
            }
        }
        lastu = x;
        if (ld[u[x]] < ld[u[k]]) solve_one(ld[u[x]], u[x], 0, moves + 1);
    }
    else {
        if (m[u[x]] == 0 || m[u[x]] > moves) m[u[x]] = moves;
        FOR(i, lastu + 1, x) {
            if (m[u[i]] == 0 || m[u[i]] > moves) {
                if (cross(u[k], k, u[i], i)) m[u[i]] = moves;
                else m[u[i]] = moves + 1;
            }
        }
        lastu = x;
        if (rd[u[x]] > rd[u[k]]) solve_one(rd[u[x]], u[x], 0, moves + 1);
    }
}

int solve(int k) {
    m.clear();
    m.RS(n);
    lastd = lastu = k;
    if (ld[k] != k) solve_one(ld[k], k, 0, 1);
    lastd = lastu = k;
    if (rd[k] != k) solve_one(rd[k], k, 0, 1);
    lastd = lastu = k;
    if (lu[k] != d[k]) solve_one(lu[k], d[k], 1, 1);
    lastd = lastu = k;
    if (ru[k] != d[k]) solve_one(ru[k], d[k], 1, 1);

    int res = 0;
    m[k] = 0;
    for (int val : m) res += val;
    return res;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    d.RS(n), u.RS(n);
    ld.RS(n), lu.RS(n), rd.RS(n), ru.RS(n);
    REP(i, n) {
        cin >> d[i];
        d[i]--;
        u[d[i]] = i;
    }

    REP(i, n) {
        ld[i] = rd[i] = i;
        lu[i] = ru[i] = d[i];
        REP(j, n) {
            if (cross(i, d[i], j, d[j])) {
                if (j < ld[i]) ld[i] = j;
                if (j > rd[i]) rd[i] = j;
                if (d[j] < lu[i]) lu[i] = d[j];
                if (d[j] > ru[i]) ru[i] = d[j];
            }
        }
    }

    REP(i, n) {
        cout << solve(i) << ' ';
    }
    cout << '\n';
}

// g++ -std=c++17 -Wall -Wextra -Wshadow -Wconversion -D_GLIBCXX_DEBUG -O3 lin.cpp -o a