Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1002 Accepted Submission(s): 408
Please calculate how many quad (a,b,c,d) satisfy:
1. 1≤a<b<c<d≤n
2. Aa<Ab
3. Ac<Ad
Each test case begins with a line contains an integer n.
The next line follows n integers A1,A2,…,An.
[Technical Specification]
1 <= T <= 100
1 <= n <= 50000
1 <= Ai <= n
#include<stdio.h> #include<iostream> #include<string.h> #include <stdlib.h> #include<math.h> #include<algorithm> using namespace std; typedef long long LL; const int N = 50005; int n; int c[N],a[N]; LL l[N],r[N]; int lowbit(int x) { return x&-x; } void update(int idx,int v) { for(int i=idx; i<=n; i+=lowbit(i)) { c[i]+=v; } } int getsum(int idx) { int sum = 0; for(int i=idx; i>0; i-=lowbit(i)) { sum+=c[i]; } return sum; } int main() { int tcase; scanf("%d",&tcase); while(tcase--) { memset(c,0,sizeof(c)); memset(l,0,sizeof(l)); scanf("%d",&n); int cnt = 0; for(int i=1; i<=n; i++) { scanf("%d",&a[i]); update(a[i],1); l[i] = getsum(a[i]-1); ///a[i]之前小于a[i]的数 } memset(c,0,sizeof(c)); memset(r,0,sizeof(r)); for(int i=n; i>=1; i--) { update(a[i],1); r[i]=n-i-getsum(a[i]-1);///统计每个数后面比它大的 r[i]+=r[i+1]; ///记录后缀和 } LL ans = 0; for(int i=2;i<n-1;i++){ ans+=l[i]*r[i+1]; } printf("%lld\n",ans); } return 0; }