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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <algorithm>
#include <bitset>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

// debugging stuff {{{
#ifdef DEBUG
  #define bDebug 1
  #define bDebug2 0
#else
  #define bDebug 0
  #define bDebug2 0
#endif
#define deb(a) #a << "=" << (a) << " "
#ifndef HOME
  #define assert(a) {}
#endif

template<class T> ostream& operator<<(ostream& os, vector<T> v) //{{{
{
  for(int i=0; i<v.size(); i++)
    os << v[i] << " ";
  os << endl;
  return os;
}  //}}}
// }}} end of debugging stuff

//c++11
#define typeof __typeof__

#define array(a, type, count) type *a = (type*)calloc(sizeof(type), count)
#define eps 1e-9
#define eq(a, b) ( (a) > (b) - eps && (a) < (b) + eps )
#define eraseAll(v) v.erase(v.begin(), v.end())
#define fi first
#define rep(i,n) for(long i=0; i<(n); i++)
#define rep2(i,a,b) for(long i=(a); i<=(b); i++)
#define rep2d(i,a,b) for(long i=(a); i>=(b); i--)
#define zeroMem(a) memset(a, 0, sizeof(a))
#define zeroMem2(a, n) memset(a, 0, sizeof(*a) * n)
#define fore(it,L) for(typeof(L.begin()) it=L.begin(); it!=L.end(); it++)
#define eraseAll(v) v.erase(v.begin(), v.end())
#define se second
#define setMin(a,b) { typeof(a) rv = (b); if (rv < a) a = rv; }
#define setMinP(a,b) { typeof(a) rv = (b); \
                       if (rv >= 0 && (a < 0 || rv < a)) a = rv; }
#define setMax(a,b) { typeof(a) rv = (b); if (rv > a) a = rv; } 
#define swap(a,b) { typeof(a) swapVar = a; a = b; b = swapVar; }
#define Int long long

#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)

//*********************** SOLUTION    ******************************
#define M 1000010
#define N 2500000L
long ac[2][M];
long *ad[2];
long n;
bool wczytane = false;
Int resGlobal = 0;

#ifndef TESTSTDIN
  #define TESTPROGRAMUx
#endif
#ifdef TESTPROGRAMU

  #include "message.h"

  long myGetN() { return 100000000L; }
  long myGetElem(long i) { return i % 1000000; }
  long myMyNodeId() { return MyNodeId(); }
#elif TESTSTDIN
long ain[N];
void wczytaj() {
  cin >> n;
  rep(i, n)
    cin >> ain[i];
}
long myGetN() {
  if (!wczytane) {
    wczytane = true;
    wczytaj();
  }
  return n;
}
long myGetElem(long i) { return ain[i]; }
long myMyNodeId() { return 0; }
#else
  #include "message.h"
  #include "teatr.h"

  long myGetN() { return GetN(); }
  long myGetElem(long i) { return GetElement(i); }
  long myMyNodeId() { return MyNodeId(); }
#endif

long myGetElemSafe(long i) {
  if (i >= myGetN())
    return M - 1;
  else
    return myGetElem(i);
}

Int scalajMale(long w, long poz, long src[], long dst[]) {
  long x1 = poz;
  long x2 = poz + w;
  long x1stop = x1 + w;
  long x2stop = x2 + w;
  long y = poz;
  Int res = 0;
  if (bDebug2) cerr << "scalaj " << deb(w) << deb(poz) << endl;

  rep(i, 2*w) {
    if (x1 >= x1stop || (x2 < x2stop && src[x1] > src[x2])) {
      res += x1stop - x1;
      if (bDebug2) cerr << "przep2 " << deb(src[x2]) << deb(x1) << deb(x2) << deb(y) << deb(res) << endl;
      dst[y] = src[x2];
      x2++;
    } else {
      if (bDebug2) cerr << "przep1 " << deb(src[x1]) << deb(x1) << deb(x2) << deb(y) << deb(res) << endl;
      dst[y] = src[x1];
      x1++;
    }
    y++;
  }
  return res;
}

void zbierzCzestosci(long istart, long istop, long ac[M]) {
  memset(ac, 0, M * sizeof(long));

  rep2(i, istart, istop - 1)
    ac[myGetElemSafe(i)]++;
  if (false && bDebug2) {
    cerr << "zebrane " << deb(istart) << deb(istop) << endl;
    rep(i, M) {
      if (ac[i])
        cerr << deb(i) << deb(ac[i]) << endl;
    }
  }
}

Int scalajDuze(long w, long poz) {
  long x1 = poz;
  long x2 = poz + w;
  long x1stop = x1 + w;
  long x2stop = x2 + w;
  Int res = 0;
  if (bDebug) cerr << "scalajDuze " << deb(w) << deb(poz) << endl;
  zbierzCzestosci(x1, x1stop, ac[0]);
  zbierzCzestosci(x2, x2stop, ac[1]);

  long i1 = 0;
  long i2 = 1;
  long c1 = x1stop - x1;
  while (true) {
    while (i1 < M && ac[0][i1] == 0) i1++;
    while (i2 < M && ac[1][i2] == 0) i2++;
    if (i1 >= M && i2 >= M)
      break;
    if (i1 >= M || (i2 < M && i1 > i2)) {
      res += ((Int)ac[1][i2]) * (Int)c1;
      if (bDebug2) cerr << "przep2 " << deb(i2) << deb(i1) << deb(i2) << deb(c1) << deb(res) << endl;
      i2++;
    } else {
      c1 -= ac[0][i1];
      if (bDebug2) cerr << "przep1 " << deb(i1) << deb(i1) << deb(i2) << deb(c1) << deb(res) << endl;
      i1++;
    }
  }
  assert(c1 == 0);
  return res;
}

int main (int argc, char **args)
{
  #ifdef HOME
  if (argc > 1) {
    freopen("tea/testy_moje/01.in", "r", stdin);
  }
  #endif
  cin.sync_with_stdio(false);
  cout.sync_with_stdio(false);

  array(a1, long, N);
  array(a2, long, N);
  ad[0] = a1;
  ad[1] = a2;
  long n = myGetN();
  long wSent = 0;

  if (bDebug) cerr << "my name is " << MyNodeId() << endl;

  // Rozsylamy kazdemu rowny kawalek.
  if (true) {
    long d = 2;
    while (NumberOfNodes() * d < n) d *= 2;
    if (MyNodeId() == 0) {
      rep(i, NumberOfNodes()) {
        if (bDebug) cerr << "Do " << i << " wysylamy " << deb(d) << endl;
        PutInt(i, i * d);
        PutInt(i, i * d + d);
        Send(i);
      }
    }
    wSent = d;
  }

  // Przetwarzamy swoj kawalek.
  if (true) {
    Int resLocal = 0;
    int src = Receive(-1);
    long istart = GetInt(src);
    long istop = GetInt(src);
    rep(i, N)
      ad[0][i] = ad[1][i] = M - 1;
    rep(i, istop - istart)
      ad[0][i] = myGetElemSafe(istart + i);

    long w = 1, j = 0;
    while (w < istop - istart) {
      if (bDebug)
        cerr << "instancja " << MyNodeId() << " scala " << deb(w) << endl;
        long x = 0;
        while (x < istop - istart) {
          resLocal += scalajMale(w, x, ad[j], ad[!j]);
          x += 2 * w;
        }
        j = !j;
      w *= 2;
    }
    if (bDebug) cerr << "instancja " << MyNodeId() << " zwraca wynik malego " << deb(resLocal) << endl;
    PutLL(src, resLocal);
    Send(src);
    if (bDebug) cerr << "instancja " << MyNodeId() << " zwrocila malego do " << deb(src) << endl;
  }

  if (MyNodeId() == 0) {
    if (bDebug) cerr << "Odbieramy od kazdego jego kawalek." << endl;
    rep(i, NumberOfNodes()) {
      if (bDebug) cerr << "Czekamy na malego od " << i << endl;
      int src = Receive(i);
      Int c = GetLL(src);
      resGlobal += c;
      if (bDebug) cerr << "Od " << src << " dostalismy " << c << endl;
    }
  }

  int cMsg = 0;
  int cMsgForMe = 0;
  if (true) {
    if (MyNodeId() == 0 && bDebug) cerr << "Rozsylamy duze kawalki do przetworzenia." << deb(wSent) << deb(n) << endl;
    int dst = 0;
    long w = wSent;
    while (w < n) {
      long x = 0;
      while (x < n) {
        if (MyNodeId() == 0) {
          if (bDebug) cerr << "Wysylamy z " << MyNodeId() << " -> " << deb(dst) << deb(w) << deb(x) << endl;
          PutInt(dst, w);
          PutInt(dst, x);
          Send(dst);
        } else {
          if (bDebug) cerr << "A " << MyNodeId() << " nie wysyla " << deb(dst) << deb(w) << deb(x) << endl;
        }
        if (dst == MyNodeId())
          cMsgForMe++;
        dst = (dst + 1) % NumberOfNodes();
        cMsg++;
        x += 2 * w;
      }
      w *= 2;
    }
  }
  if (bDebug) cerr << "ja to " << MyNodeId() << ": " << deb(cMsg) << deb(cMsgForMe) << endl;

  // Przetwarzamy duze kawalki.
  rep(i, cMsgForMe) {
    int src = Receive(-1);
    long w = GetInt(src);
    long x = GetInt(src);
    PutLL(0, scalajDuze(w, x));
    Send(0);
  }

  // Odbieramy duze kawalki.
  if (MyNodeId() == 0) {
    rep(i, cMsg) {
      int src = Receive(-1);
      resGlobal += GetLL(src);
    }
  }

  if (myMyNodeId() == 0)
    cout << resGlobal << endl;
}