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
#include <bits/stdc++.h>

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

using namespace std;
#define PII pair<int, int>
#define VI vector<int>
#define VPII vector<PII>
#define LL long long
#define LD long double
#define f first
#define s second
#define MP make_pair
#define PB push_back
#define ALL(c) (c).begin(), (c).end()
#define SIZ(c) (int)(c).size()
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i)
#define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i)

#define Sim template<class n
Sim, class s> ostream & operator << (ostream &p, pair<n, s> x)
{return p << "<" << x.st << ", " << x.nd << ">";}
Sim> auto operator << (ostream &p, n y) -> 
typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type 
{int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";}
void dor() {cerr << endl;}
Sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);}
Sim, class s> void mini(n &p, s y) {if(p>y) p = y;}
Sim, class s> void maxi(n &p, s y) {if(p<y) p = y;}
#ifdef DEB
#define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__)
#else
#define debug(...)
#endif 

#define I(x) #x " = ", (x), " "
#define A(a, i) #a "[" #i " = ", i, "] = ", a[i], " "

VPII podzial(int n, int ile) // zwraca podział 1..n na przedziały o podobnej długości
  {
  VPII res;
  int last = 0;
  REP(i, ile)
    {
    int a = last + 1;
    int b = a + n/ile + (n%ile > i) - 1; // to check, może na =
    
    mini(b, n);
    res.PB(MP(a, b));
    last = b;
    }
  assert(last == n);
  return res;
  }

const int BASE = 1 << 20;
int cnt[BASE];
int t[BASE*2];

void insert(int x)
    {
    x += BASE;
    while(x)
        {
        t[x]++;
        x /= 2;
        }
    }
int query(int a, int b)
    {
    a += BASE - 1;
    b += BASE + 1;
    int res = 0;
    while(a / 2 != b / 2)
        {
        if(a % 2 == 0)res += t[a+1];
        if(b % 2 == 1)res += t[b-1];
        a /= 2;
        b /= 2;
        }
    return res;
    }


LL calculate(int a, int b)
    {
    LL result = 0;
    FOR(i, 0, a-1)
        cnt[GetElement(i)]++;

    FORD(i, BASE-2, 0)
        cnt[i] += cnt[i+1];

    FOR(i, a, b)
        {
        int val = GetElement(i);
        result += cnt[val+1];
        insert(val);
        result += query(val+1, BASE-2);
        }
    return result;
    }


int main() 
    {
    int NODES = NumberOfNodes();
    int ID = MyNodeId();
    int N = GetN();

    VPII V = podzial(N, NODES);
    REP(i, V.size())
      {
      V[i].f--;
      V[i].s--;
      }
    
    LL res = calculate(V[ID].f, V[ID].s);
    PutLL(0, res);
    Send(0);

    if(ID == 0)
      {
      LL result = 0;
      REP(i, NODES)
        {
        Receive(i);
        LL x = GetLL(i);
        result += x;
        }
      printf("%lld\n", result);
      }
    }