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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include "message.h"
#include "teatr.h"
#include "bits/stdc++.h"
#include <map>

using namespace std;

int myNodeId = 0;
int nodeCount = 0;
int maxUsedNodes = 0;


long minimum = 0;
long maximum = 0;
unsigned long long sum = 0;

struct node {
   long val;
   long count;
   long count_great;
   node* left;
   node* right;
};

node* createNewNode(long x, long count)
{
    node* nn = new node;
    nn->val = x;
    nn->count = 1;
	nn->count_great = count;
    nn->left  = nullptr;
    nn->right = nullptr;

    return nn;
}

void bstIncrement(node* &cn)
{
	if(cn!=nullptr)
	{
		cn->count_great++;
		bstIncrement(cn->left);
		bstIncrement(cn->right);
	}
}

node* bstInsert(node* &cn, long x, long count)
{
    if(cn == nullptr) {
        cn = createNewNode(x, count);
        return cn;
    }

    if(x < cn->val)
    {
        if(cn->left == nullptr) {
            cn->left = createNewNode(x, cn->count+cn->count_great);
            return cn->left;
        } else {
            return bstInsert(cn->left, x, cn->count+cn->count_great);
        }
    }

    if( x > cn->val )
    {
		cn->count_great++;
        if(cn->right == nullptr) {
            cn->right = createNewNode(x, count);
            return cn->right;
        } else {
            return bstInsert(cn->right, x, count);
        }
    }

	if(x == cn->val)
	{
		//Just update left sub-tree
		cn->count++;
		bstIncrement(cn->left);
		return cn;
	}
}

node* bstFind(node* cn, long x)
{
    if(cn!=nullptr)
    {
        if (x == cn->val)
                return cn;
        else if(x < cn->val)
            return bstFind(cn->left, x);
        else
            return bstFind(cn->right, x);
    }
}

void bstPrint(node* &cn)
{
    if(cn!=nullptr)
    {
        cout<<"["<<cn->val<<"|"<<cn->count_great<<"]"<<endl;
        bstPrint(cn->left);
        bstPrint(cn->right);
    }
}

node* root = nullptr;
//const int LONG_HIST_TABLE_SIZE = 1000000;

//long hist[LONG_HIST_TABLE_SIZE];
//long prevHist[LONG_HIST_TABLE_SIZE];
map<long, long> histMap;
map<long, long> prevHistMap;

void doWork(long start, long stop)
{
    long elem;
	node* cn;
	for(long i = start; i<stop; i++)
	{
		elem = GetElement(i)-1;
        histMap[elem]++;
        cn = bstInsert(root, elem, 0);
        sum+=cn->count_great;
	}

    //cout<<"sum="<<sum<<endl;
    //bstPrint(root);

    for(long i=0; i<start; i++)
    {
        elem = GetElement(i)-1;
        prevHistMap[elem]++;
    }

    for(map<long, long>::iterator hm=histMap.begin(); hm!=histMap.end(); hm++)
        for(map<long, long>::iterator phm=prevHistMap.begin(); phm!=prevHistMap.end(); phm++)
        {
            if(hm->first<phm->first)
            {
                sum+=hm->second*phm->second;
            }
        }

    if(myNodeId>0)
    {
        //cout<<"sum="<<sum<<endl;
        PutLL(0, sum);
        Send(0);
    }
}

const int SHORT_HIST_TABLE_SIZE = 5;

long hist[SHORT_HIST_TABLE_SIZE];

void doWorkJust5(long start, long stop)
{
    //cout<<start<<" "<<stop<<endl;
	long elem;
	node* cn;
	for(long i = start; i<stop; i++)
	{
		elem = GetElement(i)-1;
        hist[elem]++;
        cn = bstInsert(root, elem, 0);
        sum+=cn->count_great;
	}

    //cout<<"sum="<<sum<<endl;
    //bstPrint(root);

    if(myNodeId > 0)
    {
        int rNode = Receive(-1);
        long x;
        long recHist[SHORT_HIST_TABLE_SIZE];
        for(long i=0; i<SHORT_HIST_TABLE_SIZE; i++)
        {
            x = GetLL(rNode);
            for(long j=i-1; j>=0; j--)
            {
                sum+=x*hist[j];
            }
            recHist[i] = x;
        }

        for(long i=0; i<SHORT_HIST_TABLE_SIZE; i++)
        {
            hist[i]+=recHist[i];
        }
    }

    if(myNodeId < maxUsedNodes-1)
    {
        for(long i=0; i<SHORT_HIST_TABLE_SIZE; i++)
            PutLL(myNodeId+1, hist[i]);
        Send(myNodeId+1);

    }
    if(myNodeId>0)
    {
        //cout<<"sum="<<sum<<endl;
        PutLL(0, sum);
        Send(0);
    }
}

map<long, long> recHistMap;
long recMin;

bool IsGreaterThan (pair<const long, long> e) { return (e.first>recMin); }

void justSmallSetsWorks(long start, long stop)
{
    //cout<<start<<" "<<stop<<endl;
	long elem;
	node* cn;
	for(long i = start; i<stop; i++)
	{
		elem = GetElement(i)-1;
        histMap[elem]++;
        if(elem<minimum)
            elem = minimum;
        cn = bstInsert(root, elem, 0);
        sum+=cn->count_great;
	}

    //cout<<"sum="<<sum<<endl;
    //bstPrint(root);

    if(maxUsedNodes>1)
    {
        if(myNodeId == maxUsedNodes-1)
        {
            PutLL(myNodeId-1, minimum);
            Send(myNodeId-1);
        }else
        {
            int rNode = Receive(-1);
            recMin = GetLL(rNode);

            if(myNodeId>0)
            {
                if(recMin<minimum)
                    PutLL(myNodeId-1, minimum);
                else
                    PutLL(myNodeId-1, recMin);
                Send(myNodeId-1);
            }else
            {
                long countElems = count_if(histMap.begin(), histMap.end(), IsGreaterThan);
                PutLL(myNodeId+1, countElems);
                for(map<long, long>::iterator hm=histMap.begin(); hm!=histMap.end(); hm++)
                {
                    if(hm->first>recMin)
                    {
                        PutLL(myNodeId+1, hm->first);
                        PutLL(myNodeId+1, hm->second);
                    }
                }
                Send(myNodeId+1);
            }
        }

        if(myNodeId > 0)
        {
            int rNode = Receive(-1);
            long x = GetLL(rNode);
            long e, c;
            for(long i=0; i<x; i++)
            {
                e = GetLL(rNode);
                c = GetLL(rNode);
                recHistMap[e] = c;
                for(map<long, long>::iterator hm=histMap.begin(); hm!=histMap.end(); hm++)
                {
                    if(hm->first<e)
                        sum+=hm->second*c;
                }
            }

            for(map<long, long>::iterator rhm=recHistMap.begin(); rhm!=recHistMap.end(); rhm++)
            {
                histMap[rhm->first]+=rhm->second;
            }

            long countElems = count_if(histMap.begin(), histMap.end(), IsGreaterThan);

            if(myNodeId<maxUsedNodes-1)
            {
                PutLL(myNodeId+1, countElems);
                for(map<long, long>::iterator hm=histMap.begin(); hm!=histMap.end(); hm++)
                {
                    if(hm->first>recMin)
                    {
                        PutLL(myNodeId+1, hm->first);
                        PutLL(myNodeId+1, hm->second);
                    }
                }
                Send(myNodeId+1);
            }
        }

        if(myNodeId>0)
        {
            //cout<<"sum="<<sum<<endl;
            PutLL(0, sum);
            Send(0);
        }
    }
}

int main() {

	myNodeId = MyNodeId();
	nodeCount = NumberOfNodes();
	int n = GetN();

    maxUsedNodes = n/nodeCount>=10?nodeCount:n/10+1;

    long start = (n/(maxUsedNodes))*(myNodeId);
    long stop = start+(n/(maxUsedNodes));

    if(myNodeId == maxUsedNodes-1)
        stop = n;

	if(myNodeId>0)
	{
	    /*Worker*/
	    if(myNodeId<maxUsedNodes)
        {
            justSmallSetsWorks(start, stop);
        }
	}
	else
	{
		/*Main instance*/
		int rNode;
		int nodesReported = 0;

        justSmallSetsWorks(start, stop);

		while(nodesReported<maxUsedNodes-1)
		{
			rNode = Receive(-1);
			sum += GetLL(rNode);
			nodesReported++;
		}

		cout << sum << endl;
	}
}