Sequence II
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 410 Accepted Submission(s): 182
Problem Description
Long long ago, there is a sequence A with length n. All numbers in this sequence is no smaller than 1 and no bigger than n, and all numbers are different in this sequence.
Please calculate how many quad (a,b,c,d) satisfy:
1. 1≤a<b<c<d≤n
2. A
a
<A
b![]()
3. A
c
<A
d![]()
Please calculate how many quad (a,b,c,d) satisfy:
1. 1≤a<b<c<d≤n
2. A
3. A
Input
The first line contains a single integer T, indicating the number of test cases.
Each test case begins with a line contains an integer n.
The next line follows n integers A
1
,A
2
,…,A
n![]()
.
[Technical Specification]
1 <= T <= 100
1 <= n <= 50000
1 <= A
i![]()
<= n
Each test case begins with a line contains an integer n.
The next line follows n integers A
[Technical Specification]
1 <= T <= 100
1 <= n <= 50000
1 <= A
Output
For each case output one line contains a integer,the number of quad.
Sample Input
1 5 1 3 2 4 5
Sample Output
4#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn = 50005; int n; int C[maxn], c[maxn], a[maxn], b[maxn]; int lowbit(int i) { return i&(-i); } int sum(int i) { int res = 0; while (i > 0) { res += C[i]; i -= lowbit(i); } return res; } void add(int i) { while (i <= n) { C[i] += 1; i += lowbit(i); } } int main() { int t; scanf("%d", &t); while (t --) { scanf("%d", &n); memset(c, 0, (n+5)*sizeof(c[0])); memset(b, 0, (n+5)*sizeof(b[0])); memset(C, 0, (n+5)*sizeof(C[0])); for (int i = 1; i <= n; i ++) { scanf("%d", &a[i]); b[i] = sum(a[i]); add(a[i]); } memset(C, 0, (n+5)*sizeof(C[0])); for (int i = n; i >= 1; i --) { c[i] = sum(n)-sum(a[i])+c[i+1]; add(a[i]); } __int64 res, t1, t2; res = 0; for (int i = 2; i <= n-2; i ++) { t1 = b[i]; t2 = c[i+1]; res += t1 * t2; } printf("%I64d\n", res); } return 0; }