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
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include "teatr.h"
#include "message.h"
#define debug if(myID==0)
using namespace std;

int myID;

vector<pair<int,int>>get_blocks_data(int n){
	auto bsize = [n](int k)->int{
		return (k >= n%100)?(n/100):(n/100+1);
	};

	vector<pair<int,int>>Res(100);
	for(int i=0,pos=0;i<100;i++){
		int bs = bsize(i);
		Res[i] = {pos, bs};
		pos += bs;
	}

	return Res;
}

void MergeSortInv(vector<int>&vec, long long &inv){
	int s = vec.size();
	if(s<=1)
		return;

	vector<int>Left((s+1)/2);
	copy(vec.data(), vec.data()+(s+1)/2, Left.data());
	MergeSortInv(Left, inv);
	
	vector<int>Right(s/2);
	copy(vec.data()+(s+1)/2, vec.data()+vec.size(), Right.data());
	MergeSortInv(Right, inv);

	int pA=0, pB=0, pC=0;
	while(pA<Left.size() && pB<Right.size()){
		if(Left[pA]<=Right[pB])
			vec[pC++]=Left[pA++];
		else{
			inv += Left.size()-pA;
			vec[pC++]=Right[pB++];
		}
	}

	while(pA<Left.size())
		vec[pC++]=Left[pA++];
	while(pB<Right.size())
		vec[pC++]=Right[pB++];
}

void UseCounterL(const vector<int>&cnt, const vector<int>&vec, long long &inv){
	for(int c=1,posV=0;c<=1000000;c++){
		while(vec[posV]<c)
			posV++;
		inv += (long long)cnt[c]*posV;
	}
}

void UseCounterR(const vector<int>&cnt, const vector<int>&vec, long long &inv){
	int len = vec.size()-1;
	for(int c=1,posV=0;c<=1000000;c++){
		while(vec[posV]<=c)
			posV++;
		inv += (long long)cnt[c]*(len-posV);
	}
}

int main(){
	ios::sync_with_stdio(false);

	myID = MyNodeId();
	int n = GetN();

	if(n<100){
		if(myID!=0)
			return 0;

		long long inv=0;
		vector<int>V(n);
		for(int i=0;i<n;i++)
			V[i]=GetElement(i);

		MergeSortInv(V, inv);

		cout << inv << "\n";
		return 0;
	}

	auto blocks = get_blocks_data(n);
	int bs = blocks[myID].second;
	long long inv=0;

	vector<int>myBlock;
	myBlock.reserve(bs+1);
	for(int i=0,pos=blocks[myID].first; i<bs; i++,pos++)
		myBlock.push_back(GetElement(pos));
	MergeSortInv(myBlock, inv);
	myBlock.push_back(2000000000);

	vector<int>CounterL(1000001, 0),CounterR(1000001, 0);
	int btd = (myID<50)?(50):(49);
	for(int b=(myID+1)%100,i=0; i<btd ; b=(b+1)%100,i++)
		if(b<myID)
			for(int i=0,pos=blocks[b].first;i<blocks[b].second;i++,pos++)
				CounterL[GetElement(pos)]++;
		else
			for(int i=0,pos=blocks[b].first;i<blocks[b].second;i++,pos++)
				CounterR[GetElement(pos)]++;

	UseCounterL(CounterL, myBlock, inv);
	UseCounterR(CounterR, myBlock, inv);

	if(myID==0){
		for(int i=1;i<100;i++){
			Receive(i);
			inv += GetLL(i);
		}
		cout << inv << "\n";
	}
	else{
		PutLL(0, inv);
		Send(0);
	}

	return 0;
}