CF 1400D Zigzags(前缀和)

You are given an array a1,a2…ana1,a2…an. Calculate the number of tuples (i,j,k,l)(i,j,k,l) such that:

  • 1≤i<j<k<l≤n1≤i<j<k<l≤n;
  • ai=akai=ak and aj=alaj=al;

Input

The first line contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases.

The first line of each test case contains a single integer nn (4≤n≤30004≤n≤3000) — the size of the array aa.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n) — the array aa.

It's guaranteed that the sum of nn in one test doesn't exceed 30003000.

Output

For each test case, print the number of described tuples.

Example

input

Copy

2
5
2 2 2 2 2
6
1 3 3 1 2 3

output

Copy

5
2

Note

In the first test case, for any four indices i<j<k<li<j<k<l are valid, so the answer is the number of tuples.

In the second test case, there are 22 valid tuples:

  • (1,2,4,6)(1,2,4,6): a1=a4a1=a4 and a2=a6a2=a6;
  • (1,3,4,6)(1,3,4,6): a1=a4a1=a4 and a3=a6a3=a6.

题解:观察数据规模,容易想到只能枚举两个变量。我们选择枚举中间的两个变量j,k,这样方便我们用前缀和O(1)统计另外两个变量的数量。对于i,我们已知a[i]=a[k],1<=i<j,  对于l,我们已知 a[l]=a[j] 且 k<j<=n,因此我们只需统计每个数字的前缀和,在枚举j,k即可

代码:

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string.h>
#include<string>
using namespace std;
const int maxn=4000;
int n;
long long sum[maxn][maxn],a[maxn];

void init()
{
	for (int i=1;i<=n;i++)
	{
		for (int j=1;j<=n;j++)
			sum[j][i]=sum[j][i-1];
		cin>>a[i];
		int x=a[i];
		++sum[x][i];
	}	
}

int main()
{
	int t;
	cin>>t;
	for (int tt=1;tt<=t;tt++)
	{
		memset(sum,0,sizeof(sum));
		memset(a,0,sizeof(a));
		long long ans=0;
		cin>>n;
		init();
		for (int j=2;j<=n-2;j++)
			for (int k=j+1;k<=n-1;k++)
			{
				int x,y;
				x=a[j];y=a[k];
				ans+=sum[y][j-1]*(sum[x][n]-sum[x][k]);
			}
		cout<<ans<<endl;
	}
} 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值