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
// Jedna instancja wypisuje maksymalny wzrost.

#include "message.h"
#include "teatr.h"
#include "bits/stdc++.h"

constexpr int bucketSize = 1000002;
using namespace std;
using ll = long long int;
int id = -1;
int n;
// smaller
// Inv.cpp : Defines the entry point for the console application.
struct Tree {

	vector<int> val;

	int read(int x) {
		int o = 0;
		while (x!=0)
		{
			o += val[x];
			x /= 2;
		}
		return o;
	}

	void update(int a, int b, int i, int j, int x, int v) {
		if (a>b || a>j || b<i)
		{
			return;
		}
		if (a>=i && b<=j)
		{
			val[x] += v;
			return;
		}

		update(a, (a + b) / 2, i, j, x * 2, v);
		update((a + b) / 2 + 1, b, i, j, x * 2 + 1, v);

	}


};

ll solveSmall(int startPos,int lenght)
{
	Tree t;
	vector<int> we(lenght);

	map<int, int> scale;
	for (int i = 0; i < lenght; i++)
	{
		we[i] = GetElement(startPos+i);
		scale[we[i]] = 0;
	}
	int ct = 0;
	for (auto i = scale.begin(); i != scale.end(); i++)
	{
		i->second = ct++;
	}
	//copy(we.begin(), we.end(), t.val.begin() + s);
	int s = 1;
	while (s<scale.size())
	{
		s = s << 1;
	}
	t.val.resize(s * 2 + 1);
	ll ov = 0;
	for (int i = lenght - 1; i >= 0; i--)
	{
		ov += t.read(scale[we[i]] + s);
		t.update(1, s, scale[we[i]]+2, s, 1, 1);
	}
	return ov;
}

// bigger

ll bigSolve(int startPos,int lenght){
    if(startPos+lenght>=n ){
      return 0;
    }
    vector<int> data1(bucketSize);
    vector<ll> data2(bucketSize);
    int a = 0;
    for(int i = 0; i < lenght;i++){
      a = GetElement(startPos+i);
      data1[a]++;
    }
    
    for(int i = startPos+lenght; i < min(n,startPos+2*lenght); i++)
    {
      a = GetElement(i);
      data2[a]++;
    }
    
    ll sum =0;
    ll wyn = 0;
    for(int i = 0; i < bucketSize-1; i++)
    {
      sum += data2[i];
      wyn+= sum*data1[i+1];
    }
    
    return wyn;

}

void comunicate(ll val){
  PutLL(0,val);
  Send(0);
  exit(0);
}

int main() {
  n = GetN();
  id = MyNodeId();
  ll wyn =0;
  // small nodes
  if(id>=75){
    if(n>(id-75)*2*2000000){
      // uruchamiamy male solve 2n
      wyn += solveSmall( ((id-75)*2)*2000000,min(2000000,n-(id-75)*2*2000000) );
     
    }
    if(n>((id-75)*2 +1)*2000000){
      // uruchamiamy male solve 2n+1
      wyn += solveSmall( ((id-75)*2+1)*2000000,min(2000000,n-((id-75)*2+1)*2000000) );
    }
    
  }

  if(id>=50 && id<75){
    if(n>(id-50)*4000000){
      // uruchamiamy male solve
      wyn = bigSolve((id-50)*4000000 , 2000000);
    }
  }
  if(id>=37 && id<50){
    if(n>(id-37)*8000000){
      // uruchamiamy male solve
      wyn = bigSolve((id-37)*8000000 , 4000000);
    }
  }
  if(id>=30 && id<37){
    if(n>(id-30)*16000000){
      // uruchamiamy male solve
      wyn = bigSolve((id-30)*16000000 , 8000000);
    }
  }
  if(id>=26 && id<30){
    if(n>(id-26)*32000000){
      // uruchamiamy male solve
      wyn = bigSolve((id-26)*16000000 , 16000000);
    }
  }
  if(id>=24 && id<26){
    if(n>(id-25)*64000000){
      // uruchamiamy male solve
      wyn = bigSolve((id-24)*64000000 , 32000000);
    }
  }
  if(id>=23 && id<24){
    if(n>(id-23)*128000000){
      // uruchamiamy male solve
      wyn = bigSolve((id-23)*128000000 , 64000000);
    }
  }
  if(id!=0){
    PutLL(0,wyn);
    Send(0);
    if(wyn!=0){
  //    cout << wyn<<"\n";
    }
    return 0;
  }
  if(id==0){
    ll wyn2 = 0;
     for(int i = 1; i < 75; i++)
    {
      Receive(i);
      wyn2+=GetLL(i);
    }
    for(int i = 75; i < 100; i++)
    {
      Receive(i);
      wyn+=GetLL(i);
    }
  //  cout << "wyn "<< wyn << " \nwyn2 " << wyn2 << "\n";
    cout  << wyn+wyn2;
  }
  return 0;
}