树状数组判断三元逆序对~
#include<bits/stdc++.h> using namespace std; const int maxn=1e6+14; int a[maxn]; int c[maxn*8]; long long l[maxn],r[maxn]; int lowbit (int n) { return n&-n; } int main () { int N; scanf ("%d",&N); for (int i=0;i<N;i++) { scanf ("%d",&a[i]); a[i]+=10010; } for (int i=0;i<N;i++) { for (int j=a[i]+1;j<maxn;j+=lowbit(j)) l[i]+=c[j]; for (int j=a[i];j>0;j-=lowbit(j)) c[j]++; } memset (c,0,sizeof(c)); for (int i=N-1;i>0;i--) { for (int j=a[i]-1;j>0;j-=lowbit(j)) r[i]+=c[j]; for (int j=a[i];j<maxn;j+=lowbit(j)) c[j]++; } long long ans=0; for (int i=0;i<N;i++) ans+=(long long)l[i]*r[i]; printf ("%lld",ans); return 0; }