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
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <queue>
#include <bitset>
#include <utility>
#include <stack>
#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define MP make_pair
#define FOR(v,p,k) for(auto v=(p);v<=(k);++v)
#define FORD(v,p,k) for(auto v=(p);v>=(k);--v)
#define REP(i,n) for(auto i=0;i<(n);++i)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define PB push_back
#define ST first
#define ND second
#define SIZE(x) (int)x.size()
#define ALL(c) c.begin(),c.end()

#define ODD(x) ((x)%2)
#define EVEN(x) (!(ODD(x)))

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

//copied from https://www.geeksforgeeks.org/count-inversions-array-set-3-using-bit/

// Returns sum of arr[0..index]. This function assumes
// that the array is preprocessed and partial sums of
// array elements are stored in BITree[].
int getSum(int BITree[], int index)
{
    int sum = 0; // Initialize result

    // Traverse ancestors of BITree[index]
    while (index > 0)
    {
        // Add current element of BITree to sum
        sum += BITree[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 BITree[], int n, int index, int val)
{
    // Traverse all ancestors and add 'val'
    while (index <= n)
    {
       // Add 'val' to current node of BI Tree
       BITree[index] += val;

       // Update index to that of parent in update View
       index += index & (-index);
    }
}

int MAX_ELEM = 1000000;
int tab[1000009] = {0};

LL getInvCount(int firstHeight, int lastHeight)
{
    LL invcount = 0; // Initialize result

    // Traverse all elements from right.
    FORD(i, lastHeight, firstHeight)
    {
        int x = GetElement(i);
        // Get count of elements smaller than arr[i]
        invcount += getSum(tab, x-1);

        // Add current element to BIT
        updateBIT(tab, MAX_ELEM, x, 1);
    }

    return invcount;
}


int main() {
  int nodesNumber = 100;//NumberOfNodes();
  int nodeID = MyNodeId();
  int N = GetN();

  /**
  if (N < 10000) {
    if (nodeID == 0) {
        LL res = getInvCount(0, N-1);
        printf("%lld\n", res);
    }
    return 0;
  }
  */

  int bigIntervals = 13; // so 91 = (13*12/2 + 13) machines should work
  PII NO_WORK = MP(1, 0);
  vector<PII> intervalIds;
  FOR(i, 0, bigIntervals-1) {
    FOR(j, i, bigIntervals-1) {
        intervalIds.PB(MP(i,j));
    }
  }
  REP(i, nodesNumber) intervalIds.PB(NO_WORK);


  int nodeID_1 = intervalIds[nodeID].ST;
  int nodeID_2 = intervalIds[nodeID].ND;

  LL res = 0;
  if (nodeID_1 == nodeID_2) {
    int firstHeight = (LL) N * nodeID_1 / bigIntervals;
    int lastHeight = (LL) N * (nodeID_1 + 1) / bigIntervals - 1;
    res += getInvCount(firstHeight, lastHeight);
  }

  if (nodeID_1 < nodeID_2) {
    int leftIntervalFirstHeight = (LL) N * nodeID_1 / bigIntervals;
    int leftIntervalLastHeight = (LL) N * (nodeID_1+1) / bigIntervals - 1;
    int rightIntervalFirstHeight = (LL) N * nodeID_2 / bigIntervals;
    int rightIntervalLastHeight = (LL) N * (nodeID_2+1) / bigIntervals - 1;

    FOR(i, rightIntervalFirstHeight, rightIntervalLastHeight) {
      ++tab[GetElement(i)];
    }

    FOR(i, 0, MAX_ELEM) {
      tab[i+1] += tab[i];
    }

    FOR(i, leftIntervalFirstHeight, leftIntervalLastHeight) {
      res += tab[GetElement(i)-1];
    }
  }
  if (nodeID > 0) {
    PutLL(0, res);
    Send(0);
  } else {
    REP(i, nodesNumber-1) {
      int receivedNodeID = Receive(-1);
      res += GetLL(receivedNodeID);
    }
    printf("%lld\n", res);
  }
  return 0;
}