//gcc 5.4.0 #include <bits/stdc++.h> #include "teatr.h" #include "message.h" using namespace std; int t[1000111], temp[1000111]; long long res = 0; void mergeSort(int l, int r) { if(l == r) return; int mid = (l+r)/2; mergeSort(l, mid); mergeSort(mid+1, r); int in1 = l, in2 = mid+1, in3 = 0; while ((in1 <= mid) && (in2 <= r)) { if (t[in1] <= t[in2]) { in3++; temp[in3] = t[in1]; in1++; } else { in3++; temp[in3] = t[in2]; in2++; res += (mid - in1+1); } } while (in1 <= mid){ in3++; temp[in3] = t[in1]; in1++; } while (in2 <= r){ in3++; temp[in3] = t[in2]; in2++; } int pom = 0; for (int i = l; i <= r; i++) { pom++; t[i] = temp[pom]; } } int main(void) { if(MyNodeId() != 0) return 0; int n = GetN(); for(int i = 0; i < n; i++) { t[i] = GetElement(i); } mergeSort(0, n-1); printf("%lld\n", res); }
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 | //gcc 5.4.0 #include <bits/stdc++.h> #include "teatr.h" #include "message.h" using namespace std; int t[1000111], temp[1000111]; long long res = 0; void mergeSort(int l, int r) { if(l == r) return; int mid = (l+r)/2; mergeSort(l, mid); mergeSort(mid+1, r); int in1 = l, in2 = mid+1, in3 = 0; while ((in1 <= mid) && (in2 <= r)) { if (t[in1] <= t[in2]) { in3++; temp[in3] = t[in1]; in1++; } else { in3++; temp[in3] = t[in2]; in2++; res += (mid - in1+1); } } while (in1 <= mid){ in3++; temp[in3] = t[in1]; in1++; } while (in2 <= r){ in3++; temp[in3] = t[in2]; in2++; } int pom = 0; for (int i = l; i <= r; i++) { pom++; t[i] = temp[pom]; } } int main(void) { if(MyNodeId() != 0) return 0; int n = GetN(); for(int i = 0; i < n; i++) { t[i] = GetElement(i); } mergeSort(0, n-1); printf("%lld\n", res); } |