#include "message.h" #include "teatr.h" #include "bits/stdc++.h" using namespace std; const int E = 1000001; const int CH = 1800; int Z[E], Y[E]; void sendintarr(int dest, int *a, size_t n){ for(int i = 0;i < n;i++){ if(i > 0 && i % CH == 0) Send(dest); PutInt(dest, a[i]); } Send(dest); } int getintarr(int src, int *a, size_t n){ for(int i = 0;i < n;i++){ if(i % CH == 0) Receive(src); a[i] = GetInt(src); } } int M1(int *a, size_t n, size_t m){ int i = 0, j = n, k = 0, t[n + m]; long long res = 0; while(i < n && j < n + m){ if(a[i] <= a[j]) t[k++] = a[i++]; else { t[k++] = a[j++]; res += n - i; } } while(i < n) t[k++] = a[i++]; while(j < n + m) t[k++] = a[j++]; for(int i = 0;i < n + m;i++) a[i] = t[i]; return res; } long long ms(int *a, size_t n){ if(n < 2) return 0; return ms(a, n / 2) + ms(a + n / 2, n - n / 2) + M1(a, n / 2, n - n / 2); } void stat(int *a, size_t n, int *S){ for(int i = 0;i < E;i++) S[i] = 0; for(int i = 0;i < n;i++) S[a[i]]++; } long long sumstat(int *a, int *b){ int c[E]; c[E - 1] = 0; for(int i = E - 2;i >= 0;i--) c[i] = c[i + 1] + b[i + 1]; long long res = 0; for(int i = 1;i < E;i++) res += ((long long)a[i]) * c[i]; for(int i = 1;i < E;i++) a[i] += b[i]; return res; } int main(){ int id = MyNodeId(); int M = NumberOfNodes(); int N = GetN(); if(id != 0) return 0; int A[N]; for(int i = 0;i < N;i++) A[i] = GetElement(i); cout << ms(A, N) << endl; }
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 | #include "message.h" #include "teatr.h" #include "bits/stdc++.h" using namespace std; const int E = 1000001; const int CH = 1800; int Z[E], Y[E]; void sendintarr(int dest, int *a, size_t n){ for(int i = 0;i < n;i++){ if(i > 0 && i % CH == 0) Send(dest); PutInt(dest, a[i]); } Send(dest); } int getintarr(int src, int *a, size_t n){ for(int i = 0;i < n;i++){ if(i % CH == 0) Receive(src); a[i] = GetInt(src); } } int M1(int *a, size_t n, size_t m){ int i = 0, j = n, k = 0, t[n + m]; long long res = 0; while(i < n && j < n + m){ if(a[i] <= a[j]) t[k++] = a[i++]; else { t[k++] = a[j++]; res += n - i; } } while(i < n) t[k++] = a[i++]; while(j < n + m) t[k++] = a[j++]; for(int i = 0;i < n + m;i++) a[i] = t[i]; return res; } long long ms(int *a, size_t n){ if(n < 2) return 0; return ms(a, n / 2) + ms(a + n / 2, n - n / 2) + M1(a, n / 2, n - n / 2); } void stat(int *a, size_t n, int *S){ for(int i = 0;i < E;i++) S[i] = 0; for(int i = 0;i < n;i++) S[a[i]]++; } long long sumstat(int *a, int *b){ int c[E]; c[E - 1] = 0; for(int i = E - 2;i >= 0;i--) c[i] = c[i + 1] + b[i + 1]; long long res = 0; for(int i = 1;i < E;i++) res += ((long long)a[i]) * c[i]; for(int i = 1;i < E;i++) a[i] += b[i]; return res; } int main(){ int id = MyNodeId(); int M = NumberOfNodes(); int N = GetN(); if(id != 0) return 0; int A[N]; for(int i = 0;i < N;i++) A[i] = GetElement(i); cout << ms(A, N) << endl; } |