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
#include "message.h"
#include "teatr.h"
#include <algorithm>
#include "bits/stdc++.h"
#define lld long long int
#define INF 999999999
#define XD (1 << 20)

using namespace std;

int ta[7700000];
int tb[7700000];
int dd[2 * XD];
int ld[2 * XD];
int rd[2 * XD];


lld ile(int k, int d) {
  if (d <  ld[k]) return dd[k];
  if (d >= rd[k]) return 0;
  return ile(2 * k, d) + ile(2 * k + 1, d);
}

int main() {
  int id = MyNodeId();
  if (id > 91) {
    return 0;
  }

  // result
  if (id == 0) {
    lld result = 0;
    for (int i = 1; i <= 91; i++) {
      int nr = Receive(-1);
      result += GetLL(nr);
    }
    cout << result << "\n";
    return 0;
  }

  // getting scope (a and b = parts)
  int a = 0, b = 0;
  for (int i = 1; i < id; i++) {
    if (a == b) { 
      b++; a = 0;
    } else {
      a++;
    }
  }

  // getting it right
  int n = GetN();
  if (a == b) {
    int l = n * a / 13, r = n * (a + 1) / 13;
    for (int i = l; i < r; i++) ta[i - l] = GetElement(i);

    a = r - l;
    lld result = 0;
    
    for (int i = XD; i < 2 * XD; i++) {
      ld[i] = i - XD;
      rd[i] = i - XD + 1;
    }
    for (int i = XD - 1; i > 0; i--) {
      ld[i] = min(ld[2 * i], ld[2 * i + 1]);
      rd[i] = max(rd[2 * i], rd[2 * i + 1]);
    }
    for (int i = 0; i < a; i++) {
      b = XD + ta[i];
      while (b) {
        dd[b]++;
        b = b >> 1;
      }
      result += ile(1, ta[i]);
    }
    PutLL(0, result);
    Send(0);
    return 0;
  }


  if (a < b) {
    int la = n * a / 13, ra = n * (a + 1) / 13;
    for (int i = la; i < ra; i++) ta[i - la] = GetElement(i);

    int lb = n * b / 13, rb = n * (b + 1) / 13;
    for (int i = lb; i < rb; i++) tb[i - lb] = GetElement(i);
    
    a = ra - la;
    b = rb - lb;
    sort(ta, ta + a); ta[a] = INF;
    sort(tb, tb + b);

    lld result = 0;
    int wa = 0;
    for (int i = 0; i < b; i++) {
      while (ta[wa] <= tb[i]) wa++;
      result += (a - wa);
    }
    PutLL(0, result);
    Send(0);
  }

  return 0;
}