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
#include <cstdio>

#include <algorithm>
#include <vector>
#include <deque>

int perm[200 * 1000];
int distances[200 * 1000];

int main() {
    int n;
    scanf("%d", &n);

    for (int i = 0; i < n; i++) {
        int j;
        scanf("%d", &j);
        j -= 1;
        perm[i] = j;
    }

    std::vector<std::vector<int>> edges;
    edges.reserve(n);
    for (int i = 0; i < n; i++) {
        std::vector<int> i_edges;
        for (int j = 0; j < i; j++) {
            if (perm[i] < perm[j]) {
                i_edges.push_back(j);
                edges[j].push_back(i);
            }
        }
        edges.push_back(std::move(i_edges));
    }

    std::deque<int> deq;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            distances[j] = -1;
        }

        long long int distance_sum = 0;

        deq.push_back(i);
        distances[i] = 0;
        while (!deq.empty()) {
            int current = deq.front();
            deq.pop_front();

            for (int nb : edges[current]) {
                if (distances[nb] == -1) {
                    distances[nb] = distances[current] + 1;
                    distance_sum += distances[nb];
                    deq.push_back(nb);
                }
            }
        }

        printf("%lld ", distance_sum);
    }

    putchar('\n');

    return 0;
}