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


using namespace std;

const int MAX_BLOCKS = 22;
const int MAX_N = 100000000;
const int MAX_VAL = 1000001;

int blockLeft[MAX_BLOCKS];
int blockRight[MAX_BLOCKS];
int blockLen[MAX_BLOCKS];

int cnt[MAX_VAL];

namespace Fenwick {
    int data[MAX_VAL];

    void add(int i) {
        for (; i < MAX_VAL; i |= i + 1) data[i]++;
    }

    int sum(int i) {
        int s = 0;
        while (i >= 0) {
            s += data[i];
            i = (i & (i+1)) - 1;
        }

        return s;
    }
};

long long getInversionsBetweenBlocks(int i, int j, bool clr) {
    for (int k = blockLeft[j]; k <= blockRight[j]; k++) {
        cnt[GetElement(k)]++;
    }

    for (int k = 1; k < MAX_VAL; k++) {
        cnt[k] += cnt[k-1];
    }

    long long ans = 0;
    for (int k = blockLeft[i]; k <= blockRight[i]; k++) {
        ans += cnt[GetElement(k) - 1];
    }

    if (clr) {
        memset(cnt, 0, MAX_VAL * sizeof(int));
    }

    return ans;
}

long long getInversionsInBlock(int i) {
    long long ans = 0;
    for (int k = blockRight[i]; k >= blockLeft[i]; k--) {
        int val = GetElement(k);

        ans += Fenwick::sum(val - 1);
        Fenwick::add(val);
    }

    return ans;
}

void initBlocks(int n, int blocks) {
    int len = (n + blocks - 1) / blocks;

    for (int i = 0; i < blocks; i++) {
        int left = i * len;
        int right = min(left + len, n) - 1;

        blockLeft[i] = left;
        blockRight[i] = right;
        blockLen[i] = right - left + 1;
    }
}

int main() {
    ios_base::sync_with_stdio(0);

    int myId = MyNodeId();
    int numNodes = NumberOfNodes();

    int n = GetN();
    int blocks = min(n, MAX_BLOCKS);

    initBlocks(n, blocks);

    if (myId < blocks) {
        long long inv = getInversionsInBlock(myId);

        PutLL(0, inv);
        Send(0);
    } else {
        vector <pair<int,int>> blockPairs;
        for (int i = 0; i < blocks; i++) for (int j = i + 1; j < blocks; j++) {
            blockPairs.push_back({i, j});
        }

        int numPairs = blockPairs.size();

        int hereNodes = numNodes - blocks;
        int pairsPerNode = (numPairs + hereNodes - 1) / hereNodes;

        int myPairsLeft = pairsPerNode * (myId - blocks);
        int myPairsRight = min(numPairs, myPairsLeft + pairsPerNode) - 1;

        long long invInMyPairs = 0;

        for (int p = myPairsLeft; p <= myPairsRight; p++) {
            invInMyPairs += getInversionsBetweenBlocks(
                blockPairs[p].first, blockPairs[p].second, p < myPairsRight
            );
        }

        PutLL(0, invInMyPairs);
        Send(0);
    }

    if (myId == 0) {
        long long ans = 0;

        for (int i = 0; i < numNodes; i++) {
            Receive(i);
            ans += GetLL(i);
        }

        cout << ans;
    }

    return 0;
}