#include <bits/stdc++.h> #include "message.h" #include "teatr.h" using namespace std; /*int GetN () { return 10; } int GetElement (int k) { return k; }*/ /* int MyNodeId () { return 0; } int Receive (int a) { return a; } void Send (int a) { } int GetInt (int a) { return a; } void PutInt (int a, int b) { } void PutLL (int a, long long b) { } long long GetLL (int a) { return 1LL; }*/ typedef long long LL; const int LIMIT = 1e6, N = 1e6 + 7, TREE = (1 << 21), MAX = 1e6; int T[TREE], A[N], Cnt[N], Suf[N]; int n, p = 1; LL result; int Sum (int L, int R) { int sum = 0; while (L < R) { if (L & 1) sum += T[L++]; if (R & 1) sum += T[--R]; L >>= 1; R >>= 1; } return sum; } void Add (int v) { ++T[v]; v >>= 1; while (v) { T[v] = T[v + v] + T[v + v + 1]; v >>= 1; } } void Read (int L, int R) { for (int i = L; i < R; ++i) A[i] = GetElement(i); } void Solve (int L, int R) { p = (TREE >> 1); for (int i = L; i < R; ++i) { int k = A[i]; result += (LL)Sum(p + k, 2 * p); Add(k - 1 + p); } } int main () { n = GetN(); int id = MyNodeId(); if (n <= LIMIT) { if (MyNodeId() != 0) return 0; Read(0, n); Solve(0, n); printf("%lld", result); return 0; } int d = n / NumberOfNodes(); int L = id * d, R = (id + 1) * d; if (id == 99) R = n; Read(L, R); Solve(L, R); if (id != 99) { Receive(id + 1); result += GetLL(id + 1); int it = 1; for (int i = 0; i < 1000; ++i) Cnt[it++] = GetInt(id + 1); for (int i = 0; i < 999; ++i) { Receive(id + 1); for (int j = 0; j < 1000; ++j) Cnt[it++] = GetInt(id + 1); } } for (int i = MAX; i > 0; --i) Suf[i] = Suf[i + 1] + Cnt[i]; for (int i = L; i < R; ++i) result += (LL)Suf[A[i] + 1]; if (id != 0) { PutLL(id - 1, result); int it = 1; for (int i = 0; i < 1000; ++i) { for (int j = 0; j < 1000; ++j) PutInt(id - 1, Cnt[it++]); Send(id - 1); } } if (id == 0) printf("%lld", result); 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 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 | #include <bits/stdc++.h> #include "message.h" #include "teatr.h" using namespace std; /*int GetN () { return 10; } int GetElement (int k) { return k; }*/ /* int MyNodeId () { return 0; } int Receive (int a) { return a; } void Send (int a) { } int GetInt (int a) { return a; } void PutInt (int a, int b) { } void PutLL (int a, long long b) { } long long GetLL (int a) { return 1LL; }*/ typedef long long LL; const int LIMIT = 1e6, N = 1e6 + 7, TREE = (1 << 21), MAX = 1e6; int T[TREE], A[N], Cnt[N], Suf[N]; int n, p = 1; LL result; int Sum (int L, int R) { int sum = 0; while (L < R) { if (L & 1) sum += T[L++]; if (R & 1) sum += T[--R]; L >>= 1; R >>= 1; } return sum; } void Add (int v) { ++T[v]; v >>= 1; while (v) { T[v] = T[v + v] + T[v + v + 1]; v >>= 1; } } void Read (int L, int R) { for (int i = L; i < R; ++i) A[i] = GetElement(i); } void Solve (int L, int R) { p = (TREE >> 1); for (int i = L; i < R; ++i) { int k = A[i]; result += (LL)Sum(p + k, 2 * p); Add(k - 1 + p); } } int main () { n = GetN(); int id = MyNodeId(); if (n <= LIMIT) { if (MyNodeId() != 0) return 0; Read(0, n); Solve(0, n); printf("%lld", result); return 0; } int d = n / NumberOfNodes(); int L = id * d, R = (id + 1) * d; if (id == 99) R = n; Read(L, R); Solve(L, R); if (id != 99) { Receive(id + 1); result += GetLL(id + 1); int it = 1; for (int i = 0; i < 1000; ++i) Cnt[it++] = GetInt(id + 1); for (int i = 0; i < 999; ++i) { Receive(id + 1); for (int j = 0; j < 1000; ++j) Cnt[it++] = GetInt(id + 1); } } for (int i = MAX; i > 0; --i) Suf[i] = Suf[i + 1] + Cnt[i]; for (int i = L; i < R; ++i) result += (LL)Suf[A[i] + 1]; if (id != 0) { PutLL(id - 1, result); int it = 1; for (int i = 0; i < 1000; ++i) { for (int j = 0; j < 1000; ++j) PutInt(id - 1, Cnt[it++]); Send(id - 1); } } if (id == 0) printf("%lld", result); return 0; } |