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
139
140
141
142
143
144
145
146
147
148
#include<bits/stdc++.h>
#include "message.h"
#include "teatr.h"
#define VAR(i,n) __typeof(n) i = (n)
#define loop(i,j,s) for(int i=j;i<s;i++)
#define loopback(i,j,s) for(int i=j;i>=s;i--)
#define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++)
#define pln( x ) cout << x << "\n"
#define ps( x ) cout << x << " "
#define entr cout << "\n"
#define pcnt(i) __builtin_popcount(i)
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define SIZE(c) (c).size()
#define ALL(c) (c).begin(), (c).end()
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> PII;
const int INFTY=20000000;
const int MAX=1000100;
const int MOD=10000000;

void coutTab(int* tab,int n){
	loop(i,0,n){
		cout<<tab[i]<<" ";
	}
	cout<<"\n";
}
//------------------------------------------
int nums[MAX];
int cnt[MAX];
int L[MAX],R[MAX];
ll merge(int s, int e, int m){
	loop(i,s,m) L[i-s]=nums[i];
	loop(j,m,e) R[j-m]=nums[j];
	int i=0,j=0;
	ll res=0;
	int Lsize = m-s;
	int Rsize = e-m;
	while(i<Lsize||j<Rsize){
		if(j>=Rsize||(i<Lsize&&L[i]<=R[j])){
			nums[s+i+j]=L[i];
			i++;
		}else{
			nums[s+i+j]=R[j];
			res+=Lsize-i;
			j++;
		}
	}
	return res;
}
ll count(int i, int j){
	if(j-i<=1) return 0;
	ll res = 0;
	int m = (i+j)/2;
	res = count(i,m) + count(m,j);
	res += merge(i,j,m);
	return res;
}
vector<PII> minimize(int s, int e){
	int maxnum = 0;
	loop(i,s,e) 
		maxnum=max(maxnum, GetElement(i));
	loop(i,0,maxnum+1){
		cnt[i]=0;
	}
	loop(i,s,e){
		cnt[GetElement(i)]++;	
	}
	vector<PII> res;
	loop(i,1,maxnum+1){
		if(cnt[i]>0){
			res.pb({i, cnt[i]});
		}
	}
	return res;
}
ll mergeFromNodes(vector<PII> rec, vector<PII> cur){
	vector<ll> pref(rec.size());
	pref[0]=rec[0].ss;
	loop(i,1,rec.size()){
		pref[i]=pref[i-1]+rec[i].ss;
	}
	ll inv = 0;
	int i=0,j=0;
	//pln("start");
	while(i<rec.size()||j<cur.size()){
		//if(i<rec.size()) ps(rec[i].ff);
		//if(j<cur.size()) ps(cur[j].ff);
		//entr;
		if(j>=cur.size()||(i<rec.size()&&rec[i].ff<cur[j].ff)){
			//pln("first");
			i++;
		}else if(i<rec.size()&&j<cur.size()&&rec[i].ff==cur[j].ff){
			i++;	
			ll rp = pref[rec.size()-1];
			if(i!=0) rp -= pref[i-1];
			inv += rp*cur[j].ss;
			j++;
		}else{	
			ll rp = pref[rec.size()-1];
			if(i!=0) rp -= pref[i-1];
			inv += rp*cur[j].ss;
			j++;
		}
	}
	//pln(inv);
	//pln("end");
	return inv;	
}
int main(){
	ios_base::sync_with_stdio(0);
	int n = GetN();
	int nn = NumberOfNodes();
	int id = MyNodeId();
	if(n<=100000){
		nn=1;
		if(id!=0) return 0;
	}
	int s = n*id/nn;
	int e = n*(id+1)/nn;
	//cout<<"Node "<<id<<" "<<s<<" "<<e<<"\n";
	loop(i,s,e){
		nums[i-s]=GetElement(i);
	}
	ll res = count(0,e-s);
	//pln(res);
	vector<PII> mini = minimize(s,e);
	vector<PII> rest = minimize(e,n);
	res+=mergeFromNodes(mini, rest);
	if(id != 0){
		PutLL(0, res);
		Send(0);
	}else{
		ll resAll = res;
		//ps("From 0");pln(resAll);
		loop(i,1,nn){
			int inst = Receive(-1);
			ll rec = GetLL(inst);
			resAll += rec;
			//ps("From");ps(inst);pln(rec);
		}
		pln(resAll);		
	}
}