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
#include <algorithm>

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

const int czapa = 16384;
int tree[2 * czapa + 1];

int main() {
  int ID = MyNodeId();
  int n = GetN();

  int upper_bound = 10000 * (ID + 1);
  int lower_bound = 10000 * ID;
  for (int i = 0; i < 2 * czapa + 1; ++i) tree[i] = 0;

  long long total = 0;
  for (int i = 0; i < n; ++i) {
    int elem = GetElement(i) - 1;
    if (elem < lower_bound) continue;
    if (elem >= upper_bound) elem = upper_bound;
    elem = elem - lower_bound + czapa;
    while (elem > 1) {
      ++tree[elem];
      if ((elem & 1) == 0) total += tree[elem + 1];
      elem = elem >> 1;
    }
    
  }
  PutLL(0, total);
  Send(0);
  if (ID == 0) {
    long long result = 0;
    for (int i = 0; i < 100; ++i) {
      result += GetLL(Receive(-1));
    }
    printf("%lld\n", result);
  }
  return 0;
}