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
// Jedna instancja wypisuje maksymalny wzrost.

#include "message.h"
#include "teatr.h"
#include<bits/stdc++.h>
using namespace std;

#define int long long

#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define ST FI
#define ND SE
#define SZ(x) ((int)(x).size())

#define ALL(it, x) for(__typeof(x.begin()) it = x.begin(); it != x.end(); it++)
#define REP(i, x) for (int i = 0; i < x; i++)
#define FOR(i, x) for (int i = 1; i <= x; i++)
#define BACK(i, x) for (int i = x; i; i--)

typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;

template<typename TH> void _dbg(const char* s, TH h) { cerr<<s<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* s, TH h, TA... t) {
  while(*s != ',') {cerr<<*s++;} cerr<<"="<<h<<","; _dbg(s+1, t...);
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; ALL(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif

int res;
int n, m, NCNTS = 5;
vector<int> data;
vector<int> cnts, pref;
int id, b, e, prefres;

void sync(){
        if(id > 0){
          //cout << "-------ODBIERAM deep\n";
            Receive(id - 1);
            for (int d = 0; d < NCNTS; d++) {
                pref[d] = GetLL(id - 1);
            }
            prefres = GetLL(id - 1);
        }
        //cout << "previews res: " << prefres << endl;
        
        int r = 0;
        for (int pd = 0; pd < NCNTS; pd++) {
          for (int d = 0; d < pd; d++) {
            r += pref[pd] * cnts[d]; 
          }
        }
        
        for (int pd = 0; pd < NCNTS; pd++) {
          pref[pd] += cnts[pd];
        }
        prefres += res + r;
        //cout << "delta res " << r << " prefres: " << prefres << endl;

        if(id < m - 1){
          //cout << "--------WYSYLAM deep\n";
            for (int d = 0; d < NCNTS; d++) {
                PutLL(id + 1, pref[d]);
            }
            PutLL(id + 1, prefres);
            Send(id + 1);
        }
}

int32_t main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << setprecision(7) << fixed;  
  m = NumberOfNodes();
  id = MyNodeId();
  //if(MyNodeId() != 0) return 0;
  n = GetN();
  data.resize(n);
  cnts.resize(NCNTS);
  pref.resize(NCNTS);

  b = (id * n / m) ;
  e = ((id + 1) * n / m) ;
  for (int i = b; i < e; i++) {
    data[i - b] = GetElement(i);
    data[i - b]--;
  }
  //cout << id << " " << b << " " << e << endl;

  for (int i = 0; i + b < e; i++) {
    cnts[data[i]]++;

    int r = 0;
    for (int d = data[i] + 1; d < NCNTS; d++) {
      r += cnts[d];
    }
    //cerr << i << " " << r << endl;
    res += r;
  }

  sync();

  if (id == m -1)
    cout << prefres << endl;
}