Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created July 8, 2019 08:41
Show Gist options
  • Save surinoel/8303aae261eade7d8894f6dcf7eb4926 to your computer and use it in GitHub Desktop.
Save surinoel/8303aae261eade7d8894f6dcf7eb4926 to your computer and use it in GitHub Desktop.
#include <cstdio>
int a[500001];
int b[500001];
long long ans = 0;
void merge(int start, int end) {
int mid = (start + end) / 2;
int i = start;
int j = mid + 1;
int k = 0;
while (i <= mid && j <= end) {
if (a[i] <= a[j]) {
b[k++] = a[i++];
}
else {
ans += (j - k - start);
b[k++] = a[j++];
}
}
while (i <= mid) {
b[k++] = a[i++];
}
while (j <= end) {
b[k++] = a[j++];
}
for (int i = start; i <= end; i++) {
a[i] = b[i - start];
}
}
void sort(int start, int end) {
if (start == end) {
return;
}
int mid = (start + end) / 2;
sort(start, mid);
sort(mid + 1, end);
merge(start, end);
}
int main(void) {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
sort(0, n - 1);
printf("%ld\n", ans);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment