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

using namespace std;

#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define klar(v) memset(v, 0, sizeof(v))
#define bust ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define gcd(a, b) __gcd(a, b);
#define debug(x) cout << #x << " " << x << endl;
#define endl "\n"

typedef vector<int> vi;
typedef vector<pair<int, int> > vpii;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef long long ll;
const int maxn = 1e6+100, base = 1<<20;

int zlicz[6];
int arr[maxn];
int tree[maxn];

void add(int it, int v){
    while(it > 0){
        tree[it] += v;
        it -= it&(-it);
    }
}

ll query(int it){
    it++;
    ll ret = 0;
    while(it < maxn){
        ret += tree[it];
        it += it&(-it);
    }
    return ret;
}

int main(){
    int alln = GetN();
    int maxNodes = NumberOfNodes();
    int myId = MyNodeId();
    int start = myId*1000000;
    int n = (myId+1)*1000000;
    if(alln <= 1000000){
        if(myId != 0)return 0;
        int n = alln;
        for(int i = 0; i < n; i++){
            arr[i] = GetElement(i);
        }
        ll ans = 0;
        for(int i = 0; i < n; i++){
            ans += query(arr[i]);
            add(arr[i], 1);
        }
        cout << ans << endl;
        return 0;
    }
    if(start < alln){
        if(n >= alln){
            n = alln;
        }
        if(myId != 0){
            PutInt(myId-1, 1);
            Send(myId-1);
        }
    }
    else if(start >= alln){
        if(myId != 0){
            PutInt(myId-1, 0);
            Send(myId-1);
        }
        if(myId != maxNodes-1)Receive(myId+1);
        PutLL(0, 0);
        Send(0);
        return 0;
    }
    for(int i = start; i < n; i++){
        arr[i-start] = GetElement(i);
        zlicz[arr[i-start]]++;
    }
    if(myId != maxNodes-1){
        Receive(myId+1);
        if(GetInt(myId+1)){
            Receive(myId+1);
            for(int i = 1; i <= 5; i++){
                zlicz[i] += GetInt(myId+1);
            }
        }
    }
    if(myId != 0){
        for(int i = 1; i <= 5; i++)
            PutInt(myId-1, zlicz[i]);
        Send(myId-1);
    }
    ll ans = 0;
    for(int i = start; i < n; i++){
        zlicz[arr[i-start]]--;
        for(int j = 1; j < arr[i-start]; j++)
            ans += 1LL*zlicz[j];
    }
    if(myId != 0){
        PutLL(0, ans);
        Send(0);
        return 0;
    }
    for(int i = 1; i < maxNodes; i++){
        Receive(i);
        ans += GetLL(i);
    }
    cout << ans << endl;
}