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
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cassert>
#include <limits.h>

using namespace std;

#define ST first
#define ND second
#define MP make_pair
#define PB push_back

typedef long long LL;
typedef long double LD;

typedef vector<int> VI;
typedef pair<int,int> PII;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(VAR(i,a);i<=(b);++i)
#define FORD(i,a,b) for(VAR(i,a);i>=(b);--i)
#define FORE(a,b) for(VAR(a,(b).begin());a!=(b).end();++a)
#define VAR(a,b) __typeof(b) a=(b)
#define SIZE(a) ((int)((a).size()))
#define ALL(x) (x).begin(),(x).end()
#define CLR(x,a) memset(x,a,sizeof(x))
#define ZERO(x) memset(x,0,sizeof(x))
#define S(t,i) scanf("%" ## t, &i)
#define SI(i) scanf("%d", &i)

#include "message.h"
#include "teatr.h"


const int MAXN=1000000+1;

int n, m;
int nodeId;
long long int invCount = 0;
int bit[MAXN];
int freq[MAXN];
int stride;
long long int getSum(int index)
{
    long long int sum = 0;
    
    while (index > 0)
    {
        // Add current element of BITree to sum
        sum += bit[index];
        
        // Move index to parent node in getSum View
        index -= index & (-index);
    }
    return sum;
}

// Updates a node in Binary Index Tree (BITree) at given index
// in BITree.  The given value 'val' is added to BITree[i] and
// all of its ancestors in tree.
void updateBIT(int n, int index)
{
    // Traverse all ancestors and add 'val'
    while (index <= n)
    {
        // Add 'val' to current node of BI Tree
        bit[index] += 1;
        
        // Update index to that of parent in update View
        index += index & (-index);
    }
}

void getInvCount(int n1, int n2)
{
    int i = n2;
    for (i = n2; i >= n1; i--)
    {
        int k = GetElement(i);
        freq[k]++;
        invCount += getSum(k-1);
        updateBIT(1000000, k);
    }
    int all = n2-n1+1;
    FOR(i, 2, 1000000) {
        freq[i] += freq[i-1];
    }
    n1--;
    while (n1 >= 0) {
        REP(i, stride) {
            int k = GetElement(n1);
            invCount += freq[k-1];
            n1--;
        }
        n1 -= stride;
    }
    n2++;
    n2 += stride;
    while (n2 < n) {
        int repeat = min(stride, n-n2);
        REP(i, repeat) {
            int k = GetElement(n2);
            invCount += all - freq[k];
            n2++;
        }
        n2 += stride;
    }
}

int main() {
    n = GetN();
    nodeId = MyNodeId();
    int numberOfNodes = NumberOfNodes();
    
    if (n <= 5*1000000) {
        if (nodeId == 0) {
            getInvCount(0, n-1);
        }
    } else {
        stride = (n + numberOfNodes - 1) / numberOfNodes;
        getInvCount(nodeId*stride, min(n, (nodeId+1)*stride) - 1);
        if (nodeId == 0) {
            FOR(i, 1, numberOfNodes-1) {
                Receive(i);
                invCount += GetLL(i);
            }
        } else {
            PutLL(0, invCount);
            Send(0);
        }
    }
    
    
    if (nodeId == 0) {
        printf("%lld\n", invCount);
    }
    return 0;
}