求逆序数的傻逼题。
用归并排序或者树状数组或者线段树
归并排序代码:
# include<stdio.h> # define MAXN 500100 int a[MAXN],b[MAXN]; long long ans; void merge(int l, int r) { int k, p, q; if(l == r) return; int mid= (l+r)>>1; merge(l,mid); merge(mid+1,r); p = l; q = mid+1; k = l; while(p<=mid || q<=r) { if(p>mid || (q<=r && a[p]>a[q])) { b[k++]=a[q++]; ans+=mid-p+1; } else b[k++]=a[p++]; } for(k=l; k<=r; k++) a[k] = b[k]; return ; } int main() { int n; while(~scanf("%d",&n)) { ans = 0; for(int i=0;i<n;i++) scanf("%d",a+i); merge(0,n-1); printf("%I64d\n",ans); } return 0; }
时间: 2024-10-20 06:44:53