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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include<bits/stdc++.h>
#include "message.h"
#include "teatr.h"

using namespace std;
using i64 = long long;
using i32 = int;


const i32 MAX_K = 1500;
const i32 inf = INT_MAX / 3;

namespace shitty_old_code {
	const i32 MAX = (i32)2e6 + 7;
	int tab[MAX], pom[MAX];
	i64 wyn = 0;
	void merge(int p,int q,int k)
	{
		int i=p,j=q+1,cur=p;
		while(i<=q&&j<=k)
		{
			if(tab[i]>tab[j])
			{
				pom[cur]=tab[j];
				cur++;
				j++;
				wyn+=(q-i+1);
	//          printf("%d %dfdfd\n",q,wyn);
			}
			else
			{
				pom[cur]=tab[i];
				i++;
				cur++;
			}
		}
		if(i<=q)
		{
			while (i <= q)
			{
			pom[cur] = tab[i];
			i++;
			cur++;
			}
		}
		else
		{
			while (j <= k)
			{
				pom[cur] = tab[j];
				j++;
				cur++;
			}
		}

		for(int a =p;a<=k;a++)
		{
			tab[a]=pom[a];
		}
	}
	i64 solve(int p,int k)
	{
		if(p<k)
		{
			int q=(p+k)/2;
			solve(p,q);
			solve(q+1,k);
			merge(p,q,k);

		}
		return wyn;
	}
}
struct JobDispacher {
	JobDispacher(i32 size):base(1) {
		while(size > base)
			base *= 2;
		data.resize(2 * base, inf);
		for(i32 i = 0; i < size ; ++i) {
			data[base + i] = i;
		}
		for(i32 i = base - 1 ; i > 0 ; --i) {
			get(i) = min(leftNode(i), rightNode(i));
		}
	}
	i32& get(i32 node)  { return data[node]; }
	i32 left(i32 node) const { return 2 * node; }
	i32 right(i32 node) const { return 2 * node + 1;}
	i32& leftNode(i32 node) { return data[left(node)]; }
	i32& rightNode(i32 node) { return data[right(node)]; }
	vector<i32> data;
	i32 base;
};

void send(i32 node, i64 val, i32 maximum, const vector<i32>& vt) {
	PutLL(node, val);
	PutInt(node, maximum);
	for(auto& el : vt) {
		PutInt(node, el);
	}
	Send(node);
}

void receive(i32 node, i64& val, i32& maximum, vector<i32>& vt) {
	Receive(node);
	val = GetLL(node);
	maximum = GetInt(node);
	for(auto& el: vt) {
		el = GetInt(node);
	}
}

i32 main() {
	auto nodes = NumberOfNodes();
	auto myId = MyNodeId();
	auto seqSize = GetN();

	auto dispacher = JobDispacher(nodes);
	auto chunkSize = seqSize / nodes + (seqSize % nodes != 0);
	i32 startRange = myId * chunkSize;
	i32 endRange = (myId + 1) * chunkSize;
	endRange = min(seqSize, endRange);
	auto len = endRange - startRange;
	auto counter = vector<i32>(MAX_K);
	auto maximum = -1;
	for(i32 i = 0 ; i < len ; ++i) {
		auto k = GetElement(i + startRange) - 1;
		shitty_old_code::tab[i] = k;
		++counter[k];
		maximum = max(maximum, k);
	}
	auto score = shitty_old_code::solve(0, len - 1);

	auto treePos = dispacher.base + myId;
	treePos /= 2;
	i64 recvPartial;
	i32 recvMaximum;
	auto recvCounter = vector<i32>(MAX_K);
	while(treePos > 0) {
		auto target = dispacher.get(treePos);
		auto sender = dispacher.rightNode(treePos);
		if(target != myId) {
			send(target, score, maximum, counter);
			return 0;
		} else if(sender != inf && sender != myId) {
			receive(sender, recvPartial, recvMaximum, recvCounter);
			score += recvPartial;
			maximum = max(maximum, recvMaximum);
			i64 sum = 0;
			for(i32 i = maximum - 1; i >= 0 ; --i) {
				sum += counter[i + 1];
				score += static_cast<i64>(recvCounter[i]) * sum;
			}
			for(i32 i = 0 ; i <= recvMaximum ; ++i) {
				counter[i] += recvCounter[i];
			}
		}
		treePos /= 2;
	}
	if(myId == 0) {
		cout << score << '\n';
	}

}