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

using namespace std;


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

#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define int4 int32_t
#define LL long long
#define int LL
#define FOR(i,a,b) for(int(i)=(a)-(int)((a)>(b));(i)!=(b)-(int)((a)>(b));(i)+=((a)<(b))?1:-1)
#define REP(b) FOR(i, 0, b)
#define REP1(b) FOR(i, 1, (b)+1)
#define REV(b) FOR(i, b, 0)
#define REV1(b) FOR(i, (b)+1, 1)
#define ITER(i, b) for (auto (i) : (b))
#define ALL(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define VI vector<int>
#define PII pair<int,int>
#define LD long double

template<class C> void minu(C& a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxu(C& a4, C b4) { a4 = max(a4, b4); }

template<class TH> void _dbg(const char *sdbg, TH h)
{
    while(*sdbg==' ') sdbg++;
    if(*sdbg!='\"' && *sdbg!='\'')cerr<<" "<<sdbg<<"="<<h<<"\n";
    else cerr<<" "<<h<<"\n";
}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a)
{

    while(*sdbg==' ') sdbg++;
    if(*sdbg!='\"' && *sdbg!='\'')
    {
        cerr<<" ";
        while(*sdbg!=',')cerr<<*sdbg++;
        cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
    }
    else
    {
        while(*sdbg!=',') sdbg++;
        cerr<<" "<<h<<","; _dbg(sdbg+1, a...);
    }
}

template<class T> ostream &operator<<(ostream &os, vector<T> V)
{
    os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]";
}

template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P)
{
    return os << "(" << P.fst << "," << P.snd << ")";
}

int XXXX_XXXX_XXXX;

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define scanf(...) (XXXX_XXXX_XXXX=scanf(__VA_ARGS__))
#else
#define debug(...) {}
#define cerr if(0)cout
#endif

const int roz=1000005;

int drz[roz];
int il[roz];

inline void add(int nr, int val)
{
    while(nr<roz)
    {
        drz[nr]+=val;
        nr+=nr&(-nr);
    }
}

inline int sum(int nr)
{
    int ret=0;
    while(nr>0)
    {
        ret+=drz[nr];
        nr-=nr&(-nr);
    }
    return ret;
}

int32_t main()
{   
    int n=GetN();
    int id=MyNodeId();
    int num=NumberOfNodes();
    int poc=n*id/num, kon=n*(id+1)/num;
    int wyn=0;
    REP(poc)
    {
        il[GetElement(i)]++;
    }
    REP1(1000000)
    {
        add(i, il[i]);
    }
    FOR(i, poc, kon)
    {
        int tmp=GetElement(i);
        wyn+=sum(1000000)-sum(tmp);
        add(tmp, 1);
    }
    if(id>0)
    {
        PutLL(0, wyn);
        Send(0);
    }
    else
    {
        FOR(i, 1, num)
        {
            Receive(i);
            wyn+=GetLL(i);
        }
        printf("%lld\n", wyn);
    }
}