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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
#include <cmath>
#include <queue>
#include <utility>
#include <tuple>
#include <stack>

using namespace std;

struct tree
{
    int idx;
    list<tree> children;
};

template <template <class> class U, class T>
void print_vector(U<T> &v)
{
    for (T x : v)
    {
        cout << x << " ";
    }
    cout << endl;
}

bool intersect(int i, int j, vector<int> &perm)
{
    return (i < j && perm[i] > perm[j]) || (i > j && perm[i] < perm[j]);
}

vector<int> generate_reverse_perm(vector<int> &perm)
{
    vector<int> result(perm.size());
    for (int i = 0; i < perm.size(); ++i)
    {
        result[perm[i]] = i;
    }
    return result;
}

vector<int> generate_prefix_max_t2b(vector<int> &perm)
{
    vector<int> result(perm.size());
    result[0] = perm[0];
    for (int i = 1; i < perm.size(); ++i)
    {
        result[i] = max(perm[i], result[i - 1]);
    }
    return result;
}

vector<int> generate_prefix_max_b2t(vector<int> &rev_perm)
{
    vector<int> result(rev_perm.size());
    result[0] = rev_perm[0];
    for (int i = 1; i < rev_perm.size(); ++i)
    {
        result[i] = max(rev_perm[i], result[i - 1]);
    }
    return result;
}

vector<int> generate_suffix_min_t2b(vector<int> &perm)
{
    vector<int> result(perm.size());
    result[perm.size() - 1] = perm[perm.size() - 1];
    for (int i = perm.size() - 2; i >= 0; --i)
    {
        result[i] = min(perm[i], result[i + 1]);
    }
    return result;
}

vector<int> generate_suffix_min_b2t(vector<int> &rev_perm)
{
    vector<int> result(rev_perm.size());
    result[rev_perm.size() - 1] = rev_perm[rev_perm.size() - 1];
    for (int i = rev_perm.size() - 2; i >= 0; --i)
    {
        result[i] = min(rev_perm[i], result[i + 1]);
    }
    return result;
}

bool is_cw_max(int i, vector<int> &perm, vector<int> &prefix_max_b2t)
{
    return prefix_max_b2t[perm[i]] == i;
}

bool is_ccw_max(int i, vector<int> &perm, vector<int> &prefix_max_t2b)
{
    return prefix_max_t2b[i] == perm[i];
}

int r_ccw(int i, vector<int> &rev_perm, vector<int> &prefix_max_t2b)
{
    return rev_perm[prefix_max_t2b[i]];
}

int r_cw(int i, vector<int> &perm, vector<int> &prefix_max_b2t)
{
    return prefix_max_b2t[perm[i]];
}

int l_cw(int i, vector<int> &rev_perm, vector<int> &suffix_min_t2b)
{
    return rev_perm[suffix_min_t2b[i]];
}

int l_ccw(int i, vector<int> &perm, vector<int> &suffix_min_b2t)
{
    return suffix_min_b2t[perm[i]];
}

tuple<vector<int>, list<int>, vector<list<int>>> bfs_maximal_vertices(vector<int> &perm, vector<int> &rev_perm, vector<int> &prefix_max_t2b, vector<int> &prefix_max_b2t)
{
    vector<int> distances;
    distances.assign(perm.size(), -1);
    vector<list<int>> children(perm.size());

    for (int i = 0; i < perm.size(); ++i)
    {
        children[i] = list<int>();
    }

    list<int> sorted_order_by_distance;
    list<int> component_beginnings;
    list<int> actual_ccw;
    // component_beginnings.push_back(0);
    // distances[0] = 0;
    int up_idx = 0, down_idx = 0;
    int latest_parite_dist = -1;
    int max_ccw = -1;
    int max_assigned_ccw = -1;

    while (up_idx < perm.size())
    {
        if (!is_cw_max(up_idx, perm, prefix_max_b2t) && !is_ccw_max(up_idx, perm, prefix_max_t2b))
        {
            ++up_idx;
            continue;
        }
        if (distances[up_idx] >= 0) // is cw
        {
            actual_ccw.pop_front();
            ++up_idx;
            continue;
        }

        if (max_ccw < up_idx)
        {
            component_beginnings.push_back(up_idx);
            distances[up_idx] = 0;
            latest_parite_dist = 0;
            // parent[up_idx] = -1;
        }
        else
        {
            if (max_assigned_ccw < up_idx)
            {
                latest_parite_dist += 2;
                max_assigned_ccw = max_ccw;
            }
            distances[up_idx] = latest_parite_dist;
            // parent[up_idx] = actual_ccw.front();
            children[actual_ccw.front()].push_back(up_idx);
        }
        sorted_order_by_distance.push_back(up_idx);

        while (down_idx < perm[up_idx])
        {
            if (!is_cw_max(rev_perm[down_idx], perm, prefix_max_b2t) && !is_ccw_max(rev_perm[down_idx], perm, prefix_max_t2b))
            {
                ++down_idx;
                continue;
            }
            if (distances[rev_perm[down_idx]] >= 0)
            {
                ++down_idx;
                continue;
            }
            distances[rev_perm[down_idx]] = distances[up_idx] + 1;
            sorted_order_by_distance.push_back(up_idx);
            max_ccw = rev_perm[down_idx]; // moze byc max tutaj ale mamy pewnosc ze bedzie wieksze
            actual_ccw.push_back(rev_perm[down_idx]);
            // parent[rev_perm[down_idx]] = up_idx;
            children[up_idx].push_back(rev_perm[down_idx]);
            ++down_idx;
        }
        ++up_idx;
    }

    return {distances, component_beginnings, children};
}

void generate_postorder(vector<int> &postorder, int component_beginning, vector<list<int>> &children)
{
    int i = 0;
    stack<int> s;
    stack<int> s2;
    s.push(component_beginning);

    while (!s.empty())
    {
        int x = s.top();
        s.pop();
        s2.push(x);
        for (int child : children[x])
        {
            s.push(child);
        }
    }
    while (!s2.empty())
    {
        int x = s2.top();
        s2.pop();
        postorder[x] = i;
        ++i;
    }
}

vector<int> generate_maximal_component_sizes(list<int> &component_indices, vector<int> &perm, vector<int> &prefix_max_t2b, vector<int> &prefix_max_b2t)
{
    vector<int> res;
    res.assign(perm.size(), 0);
    vector<int> not_maximal;
    not_maximal.assign(perm.size(), 0);

    auto it = component_indices.begin();
    for (int i = 0; i < perm.size(); ++i)
    {
        if (*it == i)
        {
            not_maximal[i] = 0;
            ++it;
        }
        else
        {
            not_maximal[i] = not_maximal[i - 1] + ((is_ccw_max(i, perm, prefix_max_t2b) || is_cw_max(i, perm, prefix_max_b2t)) ? 0 : 1);
        }
    }

    int prev_i = 0;
    for (int i : component_indices)
    {
        res[i] = i - prev_i;
        prev_i = i;
    }
    res[res.size() - 1] = res.size() - prev_i;
    int size = res[res.size() - 1] - not_maximal[res.size() - 1];
    for (int i = res.size() - 1; i > 0; --i)
    {
        if (res[i] == 0)
        {
            res[i] = size;
        }
        else
        {
            int x = size;
            size = res[i] - not_maximal[i - 1];
            res[i] = x;
            // swap(size, res[i]);
        }
    }
    res[0] = size;
    return res;
}

vector<double> generate_coords(tuple<vector<int>, list<int>, vector<list<int>>> &result, vector<int> &perm, vector<int> &rev_perm, vector<int> &prefix_max_t2b, vector<int> &prefix_max_b2t)
{
    vector<int> postorder(perm.size());
    for (int b : get<1>(result))
    {
        generate_postorder(postorder, b, get<2>(result));
    }
    vector<double> res(perm.size());

    vector<int> sizes = generate_maximal_component_sizes(get<1>(result), perm, prefix_max_t2b, prefix_max_b2t);
    for (int i = 0; i < res.size(); ++i)
    {
        res[i] = (double)(get<0>(result)[i]) + ((double)postorder[i]) / ((double)sizes[i]);
    }
    return res;
}

vector<int> generate_component_indices(int n, list<int> &component_beginnings)
{
    vector<int> result;
    result.assign(n, 0);
    for (int b : component_beginnings)
    {
        result[b] = 1;
    }
    for (int i = 1; i < n; ++i)
    {
        result[i] += result[i - 1];
    }
    return result;
}

unsigned long long myround(int i, int j, double d, bool is_even)
{
    auto x = static_cast<unsigned long long>(d);
    if (is_even)
    {
        return x % 2 == 0 ? x : (x + 1);
    }
    else
    {
        return x % 2 == 0 ? (x + 1) : x;
    }
}

int main()
{
    int n;
    cin >> n;
    vector<int> perm(n);
    for (int i = 0; i < n; ++i)
    {
        cin >> perm[i];
        --perm[i];
    }

    auto rev_perm = generate_reverse_perm(perm);

    // print_vector(perm);
    // print_vector(rev_perm);

    auto prefix_max_t2b = generate_prefix_max_t2b(perm);
    auto suffix_min_t2b = generate_suffix_min_t2b(perm);

    auto prefix_max_b2t = generate_prefix_max_b2t(rev_perm);
    auto suffix_min_b2t = generate_suffix_min_b2t(rev_perm);

    // print_vector(prefix_max_t2b);
    // print_vector(prefix_max_b2t);
    // print_vector(suffix_min_t2b);
    // print_vector(suffix_min_b2t);

    auto bfs = bfs_maximal_vertices(perm, rev_perm, prefix_max_t2b, prefix_max_b2t);

    // print_vector(get<0>(bfs));
    // print_vector(get<1>(bfs));

    auto coords = generate_coords(bfs, perm, rev_perm, prefix_max_t2b, prefix_max_b2t);

    // print_vector(coords);

    auto component_indices = generate_component_indices(n, get<1>(bfs));

    // print_vector(component_indices);

    vector<long long> sums(n, 0);
    for (int i = 0; i < n; ++i)
    {
        // long long sum = 0;
        for (int j = i + 1; j < n; ++j)
        {
            if (component_indices[r_ccw(i, rev_perm, prefix_max_t2b)] != component_indices[r_ccw(j, rev_perm, prefix_max_t2b)])
            {
                continue;
            }

            if (intersect(i, j, perm))
            {
                ++sums[i];
                ++sums[j];
                continue;
            }

            int s = min(i, j);
            int t = max(i, j);

            const int r_cw_s = r_cw(s, perm, prefix_max_b2t);
            const int r_ccw_s = r_ccw(s, rev_perm, prefix_max_t2b);
            const int l_cw_t = l_cw(t, rev_perm, suffix_min_t2b);
            const int l_ccw_t = l_ccw(t, perm, suffix_min_b2t);

            // cout << s << " " << t << " " << r_cw_s << " " << r_ccw_s << " " << l_cw_t << " " << l_ccw_t << endl;

            if (intersect(r_cw_s, t, perm) || intersect(r_ccw_s, t, perm))
            {
                sums[i] += 2;
                sums[j] += 2;
                continue;
            }

            double rcws = coords[r_cw_s], rccws = coords[r_ccw_s],
                   lcwt = coords[l_cw_t], lccwt = coords[l_ccw_t];

            // cout << rcws << " " << rccws << " " << lcwt << " " << lccwt << endl;

            auto rd1 = myround(r_cw_s, l_cw_t, ceil(abs(lcwt - rcws)), true);
            auto rd2 = myround(r_cw_s, l_ccw_t, ceil(abs(lccwt - rcws)), false);
            auto rd3 = myround(r_ccw_s, l_cw_t, ceil(abs(lcwt - rccws)), false);
            auto rd4 = myround(r_ccw_s, l_ccw_t, ceil(abs(lccwt - rccws)), true);

            // cout << rd1 << " " << rd2 << " " << rd3 << " " << rd4 << endl;
            sums[i] += 2 + min(min(rd1, rd2), min(rd3, rd4));
            sums[j] += 2 + min(min(rd1, rd2), min(rd3, rd4));
            // sum += 2 + min(min(rd1, rd2), min(rd3, rd4));
        }
        // cout << sum << " ";
    }
    for (int i = 0; i < n; ++i)
    {
        cout << sums[i] << " ";
    }
    cout << endl;
    return 0;
}