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
#include <iostream>
#include "teatr.h"
#include "message.h"
using namespace std;

long long one = 1;

const int L = 1048576;
int D[2*L];

void update( int x )
{
    x += L;
    while ( x >= 1 )
    {
        D[x]++;
        x >>= 1;
    }
}

int sum( int a, int b = L, int left = 0, int right = L, int x = 1 )
{
    if ( left >= b || right <= a ) return 0;
    else if ( left >= a && right <= b ) return D[x];
    else
    {
        int mid = ( left + right ) / 2;
        int l = sum( a, b, left, mid, 2*x );
        int r = sum( a, b, mid, right, 2*x+1 );
        return l+r;
    }
}



int main()
{
    int n = GetN(), id = MyNodeId(), x;

    if ( id ) return 0;

    long long answer = 0;

    for ( int i = 0; i < n; ++i )
    {
        x = GetElement( i );
        answer += sum( x+1 );
        update( x );
    }

    cout << answer << endl;
}