#include <iostream> #include "message.h" #include "teatr.h" const int z = 1024 * 1024, N = 1000000; int pref[N + 7]; int drzewo[2 * z + 7]; void add(int pos) { pos += z; while (pos) { drzewo[pos]++; pos /= 2; } } int get(int p) { int k = N, ret = 0; p += z - 1; k += z + 1; while (p + 1 < k) { if (p % 2 == 0) ret += drzewo[p + 1]; if (k % 2 == 1) ret += drzewo[k - 1]; p /= 2; k /= 2; } return ret; } int main() { int id = MyNodeId(); int n = GetN(); int p = id * N, k = p + N - 1, x; k = std::min(k, n - 1); if (p > k) { PutLL(0, 0); Send(0); return 0; } for (int i = 0; i < p; i++) { x = GetElement(i); pref[x]++; } for (int i = 1; i <= N; i++) { pref[i] += pref[i - 1]; } long long ret = 0; for (int i = p; i <= k; i++) { x = GetElement(i); ret += pref[N] - pref[x]; ret += get(x + 1); add(x); } if (id == 0) { long long wynik = ret; for (int i = 1; i < 100; i++) { Receive(i); wynik += GetLL(i); } printf("%lld", wynik); } else { PutLL(0, ret); Send(0); } return 0; }
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 | #include <iostream> #include "message.h" #include "teatr.h" const int z = 1024 * 1024, N = 1000000; int pref[N + 7]; int drzewo[2 * z + 7]; void add(int pos) { pos += z; while (pos) { drzewo[pos]++; pos /= 2; } } int get(int p) { int k = N, ret = 0; p += z - 1; k += z + 1; while (p + 1 < k) { if (p % 2 == 0) ret += drzewo[p + 1]; if (k % 2 == 1) ret += drzewo[k - 1]; p /= 2; k /= 2; } return ret; } int main() { int id = MyNodeId(); int n = GetN(); int p = id * N, k = p + N - 1, x; k = std::min(k, n - 1); if (p > k) { PutLL(0, 0); Send(0); return 0; } for (int i = 0; i < p; i++) { x = GetElement(i); pref[x]++; } for (int i = 1; i <= N; i++) { pref[i] += pref[i - 1]; } long long ret = 0; for (int i = p; i <= k; i++) { x = GetElement(i); ret += pref[N] - pref[x]; ret += get(x + 1); add(x); } if (id == 0) { long long wynik = ret; for (int i = 1; i < 100; i++) { Receive(i); wynik += GetLL(i); } printf("%lld", wynik); } else { PutLL(0, ret); Send(0); } return 0; } |