Tokitsukaze and Strange Inequality

Tokitsukaze has a permutation pp of length nn. Recall that a permutation pp of length nn is a sequence p1,p2,…,pnp1,p2,…,pn consisting of nn distinct integers, each of which from 11 to nn (1≤pi≤n1≤pi≤n).

She wants to know how many different indices tuples [a,b,c,d][a,b,c,d] (1≤a<b<c<d≤n1≤a<b<c<d≤n) in this permutation satisfy the following two inequalities:

pa<pcpa<pc and pb>pdpb>pd.

Note that two tuples [a1,b1,c1,d1][a1,b1,c1,d1] and [a2,b2,c2,d2][a2,b2,c2,d2] are considered to be different if a1≠a2a1≠a2 or b1≠b2b1≠b2 or c1≠c2c1≠c2 or d1≠d2d1≠d2.

Input

The first line contains one integer tt (1≤t≤10001≤t≤1000) — the number of test cases. Each test case consists of two lines.

The first line contains a single integer nn (4≤n≤50004≤n≤5000) — the length of permutation pp.

The second line contains nn integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n) — the permutation pp.

It is guaranteed that the sum of nn over all test cases does not exceed 50005000.

Output

For each test case, print a single integer — the number of different [a,b,c,d][a,b,c,d] tuples.

Example

input

Copy

3
6
5 3 6 1 4 2
4
1 2 3 4
10
5 1 6 2 8 3 4 10 9 7

output

Copy

3
0
28

Note

In the first test case, there are 33 different [a,b,c,d][a,b,c,d] tuples.

p1=5p1=5, p2=3p2=3, p3=6p3=6, p4=1p4=1, where p1<p3p1<p3 and p2>p4p2>p4 satisfies the inequality, so one of [a,b,c,d][a,b,c,d] tuples is [1,2,3,4][1,2,3,4].

Similarly, other two tuples are [1,2,3,6][1,2,3,6], [2,3,5,6][2,3,5,6].

思路:我们只要枚举中间的B与C即可,与B匹配的是D,只需要在B的后面找到符合的D即可(也就是D的值小于B的值),在C的前面找到A,A符合条件即可(也就是A的值小于C),我们用二维数组存B所对应大于D的个数,存C对应大于A的个数。在对应时:二维的B对应找的是D,D的下标要大于C,二维的C对应找的是A,A的下标要小于B。(两个二维的 i  对应的分别是B和C,而 j 才是要找的数)。

完整代码:

#include <bits/stdc++.h>

using namespace std;

//#define int long long
const int mod=1e9+7;

const int N=5010;
int a[N],sumb[N][N],sumc[N][N];

void solve()
{
    int n;
    cin>>n;

    for(int i=0;i<=n;i++)
    {
        for(int j=0;j<=n;j++)
        {
            sumb[i][j]=0;
            sumc[i][j]=0;
        }
    }

    for(int i=1;i<=n;i++)cin>>a[i];

    for(int i=1;i<=n;i++)
    {
        int cnt=0;
        for(int j=1;j<i;j++)
        {
            if(a[j]<a[i])cnt++;
            sumc[i][j]=cnt;
        }
    }
    for(int i=1;i<=n;i++)
    {
        int cnt=0;
        for(int j=n;j>i;j--)
        {
            if(a[j]<a[i])cnt++;
            sumb[i][j]=cnt;
        }
    }

    long long res=0;
    for(int b=2;b<=n-2;b++)
    {
        for(int c=b+1;c<=n-1;c++)
        {
            int res1=sumb[b][c+1];
            int res2=sumc[c][b-1];
            res+=res1*res2;
        }
    }
    cout<<res<<endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值