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
#include <bits/stdc++.h>

using namespace std;

struct TestCase
{
  size_t N;
  vector<size_t> permutation;
};

TestCase read_test_case();
void solve_test_case(const TestCase &);

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve_test_case(read_test_case());
}

TestCase read_test_case()
{
  TestCase tc;
  cin >> tc.N;
  tc.permutation.resize(tc.N);
  for (auto &x : tc.permutation) cin >> x;
  return tc;
}

struct Graph
{
  vector<vector<size_t>> nodes;
};

size_t get_sum_from(const Graph &graph, size_t source);

void solve_test_case(const TestCase &tc)
{
  Graph graph;
  graph.nodes.resize(tc.N);
  for (size_t i = 0; i < tc.N; i++)
    for (size_t j = 0; j < i; j++)
    {
      if (tc.permutation[i] < tc.permutation[j])
      {
        graph.nodes[i].push_back(j);
        graph.nodes[j].push_back(i);
      }
    }

  for (size_t i = 0; i < tc.N; i++) cout << get_sum_from(graph, i) << " ";
  cout << "\n";
}

size_t get_sum_from(const Graph &graph, size_t source)
{
  size_t sum = 0;

  vector<bool> visited(graph.nodes.size());
  queue<pair<size_t, size_t>> q;
  q.push({source, 0});
  visited[source] = true;

  while (!q.empty())
  {
    auto [current, d] = q.front();
    q.pop();
    sum += d;

    for (auto next : graph.nodes[current])
      if (!visited[next])
      {
        visited[next] = true;
        q.push({next, d + 1});
      }
  }

  return sum;
}