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
// Arti1990, II UWr

#include <bits/stdc++.h>

#define forr(i, n)                  for(int i=0; i<n; i++)
#define FOREACH(iter, coll)         for(auto iter = coll.begin(); iter != coll.end(); ++iter)
#define FOREACHR(iter, coll)        for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter)
#define lbound(P,K,FUN)             ({auto SSS=P, PPP = P-1, KKK=(K)+1; while(PPP+1!=KKK) {SSS = (PPP+(KKK-PPP)/2); if(FUN(SSS)) KKK = SSS; else PPP = SSS;} PPP;})
#define testy()                     int _tests; cin>>_tests; FOR(_test, 1, _tests)
#define CLEAR(tab)                  memset(tab, 0, sizeof(tab))
#define CONTAIN(el, coll)           (coll.find(el) != coll.end())
#define FOR(i, a, b)                for(int i=a; i<=b; i++)
#define FORD(i, a, b)               for(int i=a; i>=b; i--)
#define MP                          make_pair
#define PB                          push_back
#define ff                          first
#define ss                          second
#define deb(X)                      X;

#define M 1000000007
#define INF 1000000007LL

using namespace std;

int n, res, S;
int p[1000007], skad[1000007], maxg[1000007], maxd[1000007];
int drz[1000007];
int przecd[1000007], przecg[1000007];
int numer[1000007];
int koniec[1000007], pom[1000007];
long long rres[1000007];
int byl_brut[1000007], odl_brut[1000007];
map <pair<int, int>, long long> ile_prawo, res_prawo;

long long brut(int nr)
{
    byl_brut[nr] = 1;
    odl_brut[nr] = 0;
    list <int> l;
    l.PB(nr);
    long long res = 0;
    while(!l.empty())
    {
        nr = l.front();
        l.pop_front();
        //cout << "wchodzi do " << nr << " na odl " << odl_brut[nr] << "\n";
        FOR(i, 1, n)
            if(!byl_brut[i] && ((i < nr && p[i] > p[nr]) || (i > nr && p[i] < p[nr])))
            {
                byl_brut[i] = 1;
                odl_brut[i] = odl_brut[nr]+1;
                res += odl_brut[i];
                l.PB(i);
            }
    }
    return res;
}

void dokladaj(int nr)
{
    nr += S;
    while(nr > 0)
    {
        drz[nr]++;
        nr /= 2;
    }
}

int policz(int nr)
{
    int res = 0;
    nr += S;
    while(nr > 1)
    {
        if((nr&1) == 0)
            res += drz[nr+1];
        nr /= 2;
    }
    return res;
}

long long policz_res_prawo(int d, int g)
{
    pair <int, int> p = {d, g};
    if(res_prawo.count(p) > 0)
        return res_prawo[p];
    res_prawo[p] = 0;
    long long x = ile_prawo[p] + policz_res_prawo(maxd[g], maxg[d]);
    res_prawo[p] = x;
    //cout << "res_prawo od " << d << ", " << g << " wyszlo " << x << '\n';
    return x;
}

void policz_reszte()
{

    forr(i, 2*S)
        drz[i] = 0;

    FOR(i, 1, n)
    {
        maxg[i] = max(maxg[i-1], p[i]);
        maxd[i] = max(maxd[i-1], skad[i]);
//        cout << i << ": maxg = " << maxg[i] << ", maxd = " << maxd[i] << '\n';
    }
    FORD(i, n, 1)
    {
        if(maxg[i] == i)
            koniec[i] = i;
        else
            koniec[i] = koniec[i+1];
        //cout << "koniec od " << i << " to " << koniec[i] << '\n';
    }

    ile_prawo.clear();
    res_prawo.clear();
    FOR(pocz, 1, n)
    {
        int pd = 0, pg = 0;
        int d = pocz, g = p[pocz];
//        cout << "sprawdza d = " << d << ", g = " << g << '\n';
        while(ile_prawo.count({d, g}) == 0)
        {
//            cout << "dorzuca d = " << d << ", g = " << g << '\n';
            ile_prawo[{d, g}] = 0;
            pg = g;
            pd = d;
            g = maxg[pd];
            d = maxd[pg];
        }
    }

    int poz = n;
    FOREACHR(it, ile_prawo)
    {
        int d = it->ff.ff;
        int g = it->ff.ss;
        while(poz > d)
        {
            //cout << " doklada " << poz << '\n';
            dokladaj(p[poz]);
            poz--;
        }
        it->ss = policz(g) - n + koniec[it->ff.ff];
        //cout << "liczy pare " << d << ", " << g << ", wyszlo " << (it->ss) << '\n';
    }

    FOR(i, 1, n)
        rres[numer[i]] += ile_prawo[{i, p[i]}] + policz_res_prawo(i, p[i]);

    //cout << "ile: " << ile_prawo.size() << '\n';
    res = max(res, (int)ile_prawo.size());

}

int solve()
{
    /*
    n = 200000;
    FOR(i, 1, n)
        p[i] = i;
    FOR(i, 1, n)
        swap(p[i], p[min(i+rand()%20, n)]);
    /**/
    cin >> n;
    FOR(i, 1, n)
        cin >> p[i];
    /**/

    FOR(i, 1, n)
    {
        skad[p[i]] = i;
        numer[i] = i;
    }
    S = 1;
    while(S < n+2)
        S *= 2;

    forr(i, 2*S)
        drz[i] = 0;

    FORD(i, n, 1)
    {
        przecg[i] = (n - p[i]) - policz(p[i]);
        przecd[i] = n-i - policz(p[i]);
        rres[i] = przecg[i] + przecd[i];
        dokladaj(p[i]);
        //cout << "poz: " << i << ", przecg: " << przecg[i] << ", przecd: " << przecd[i] << '\n';
    }

    policz_reszte();

    FOR(i, 1, n)
        pom[i] = p[i];
    FOR(i, 1, n)
    {
        numer[i] = n+1-i;
        p[i] = n+1-pom[n+1-i];
        skad[p[i]] = i;
        //cout << "nowe p[" << i << "] = " << p[i] << '\n';
    }

    policz_reszte();

    FOR(i, 1, n)
        cout << rres[i] << " ";
    cout << '\n';

    return 0;
}

void solve_brut()
{
    FOR(i, 1, n)
        pom[i] = p[i];
    FOR(i, 1, n)
    {
        numer[i] = n+1-i;
        p[i] = n+1-pom[n+1-i];
        skad[p[i]] = i;
        //cout << "nowe p[" << i << "] = " << p[i] << '\n';
    }
    FOR(i, 1, n)
    {
        FOR(j, 1, n)
            byl_brut[j] = 0;
        long long x = brut(i);
        //cout << x << " ";
        if(x != rres[i])
        {
            cout << "NIE DZIALA!" << '\n';
            exit(777);
        }
    }
}

int main()
{
    //srand(time(0));
    std::ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    //forr(i, 1000)
    {
        //cout << "test " << i << '\n';
        solve();
        //solve_brut();
    }
    //cout << "res: " << res << '\n';

    return 0;
}