Skip to content

Instantly share code, notes, and snippets.

@morris821028
Last active October 19, 2020 22:54
Show Gist options
  • Save morris821028/28592e1531907f18dba7e2298cb2a902 to your computer and use it in GitHub Desktop.
Save morris821028/28592e1531907f18dba7e2298cb2a902 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <memory.h>
#define GET(x) (cnt[x>>5]>>(x&31)&1)
#define SET(x) (cnt[x>>5] |= 1<<(x&31))
#define REV(x) (cnt[x>>5] ^= 1<<(x&31))
long long ret = 0;
void divide(int a[], int b[], int n) {
if (n <= 1) return 0;
int mid = n/2;
int l = 0, r = mid*2;
static int cnt[4096];
int sum = 0;
for (int i = 0; i < 2*n; i++) {
if (a[i] <= mid) {
sum++;
b[l++] = a[i];
} else {
b[r++] = a[i] - mid;
if (GET(a[i]-mid) == 0) {
ret -= sum;
SET(a[i]-mid);
} else {
ret += sum;
REV(a[i]-mid);
}
}
}
divide(b, a, l/2);
divide(b+mid*2, a, r/2-mid);
}
int main() {
int n;
static int b[200005];
static int a[200005];
scanf("%d", &n);
for (int i = 0; i < 2*n; i++)
scanf("%d", &a[i]);
divide(a, b, n);
printf("%lld\n", ret);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment