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
136
137
138
#include <cstdio>
#include <algorithm>
#include "message.h"
#include "teatr.h"

using namespace std;

typedef long long LL;
const int N = 1e6+2;

int t[N];
int t2[N];
int bit[N];

inline void updateBit(const int &id)
{
    for(int i = id+1;i > 0;i -= i&(-i))
    {
        ++bit[i];
        //cout << "U " << i << endl;
    }
}

inline int sumBit(const int &id)
{
    int res = 0;
    for(int i = id+1;i < N;i += i&(-i))
    {
        res += bit[i];
        //cout << "S " << i << endl;
    }
    return res;
}

int main()
{
    int n = (int)GetN();
    int nNodes = NumberOfNodes();
    int id = MyNodeId();

    if(id >= n)
        return 0;
    int siz = (int)(n/nNodes)+1;
    int a = 0, cta = 0;
    int b = 0, ctb = 0;
    int x;

    for(int i = 0;i < n;++i)
    {
        x = GetElement(i);
        ++t[x];
    }

    int ct = 0, ctid = -1, temp = 0, ctCur = 0;
    for(int i = 0;i < N;++i)
    {
        if(ct + t[i] <= siz)
        {
            ct += t[i];
            ctCur += t[i];
            temp = t[i];
        }
        else
        {
            temp = siz-ct;
            ctCur += temp;
            ct = siz;
        }
        if(ct == siz)
        {
            ++ctid;
            ct = 0;
            t[i] -= temp;
            temp = 0;
            b = i; ctb = ctCur;
            if(ctid == id)
                break;
            a = i;
            cta = ctCur;
            if(t[i] > 0)
            {
                --i;
                continue;
            }
        }
        ctCur = 0;
    }
    if(a == b && cta == ctb)
    {
        b = N-1;
        ctb = ctCur;
    }
    
    //scout << a << " " << cta << " " << b << " " << ctb << endl;

    LL res = 0;
    int bigger = 0, stricktBigger = 0;
    for(int i = 0;i < n;++i)
    {
        x = GetElement(i);
        if((x > a || (x==a && t2[x] >= cta)) && (x < b || (x==b && t2[x] < ctb)))
        {
            res += sumBit(x+1);
            if(x == b)
                res += stricktBigger;
            else
                res += bigger;

            updateBit(x);
        }
        else if(x > b)
        {
            ++stricktBigger;
            ++bigger;
        }
        else if(x == b && t2[x] >= ctb)
            ++bigger;
        t2[x]++;
    }
    if(id == 0)
    {
        nNodes = min(n, nNodes);
        for(int i = 1;i < nNodes;++i)
        {
            Receive(i);
            res += GetLL(i);
        }
        printf("%lld\n", res);
    }
    else
    {
        PutLL(0, res);
        Send(0);
        //cout << "R " << res << endl;
    }

    return 0;
}