标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3109 | Accepted: 1148 |
Description
Input
Output
Sample Input
1 3 1 2 3
Sample Output
1
Source
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int N=2e4+5,M=1e5+5; inline int read(){ char c=getchar();int x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();} return x*f; } int T,n,m,a[N],bit[M],f[N],g[N]; inline int lowbit(int x){return x&-x;} inline void add(int p,int v,int bit[]){ for(int i=p;i<=m;i+=lowbit(i)) bit[i]+=v; } inline int sum(int p,int bit[]){ int ans=0; for(int i=p;i>0;i-=lowbit(i)) ans+=bit[i]; return ans; } int main(){ T=read(); while(T--){ n=read(); for(int i=1;i<=n;i++) a[i]=read(),m=max(m,a[i]); memset(bit,0,sizeof(bit)); for(int i=1;i<=n;i++){ add(a[i],1,bit); f[i]=sum(a[i]-1,bit); } memset(bit,0,sizeof(bit)); for(int i=n;i>=1;i--){ add(a[i],1,bit); g[i]=sum(a[i]-1,bit); } ll ans=0; for(int i=2;i<=n-1;i++) ans+=f[i]*(n-i-g[i])+g[i]*(i-1-f[i]);//,printf("t%d %d %d\n",i,f[i],g[i]); printf("%lld\n",ans); } }
标签:
原文地址:http://www.cnblogs.com/candy99/p/5962425.html