Given an integer array
a
[1
..n
]
.
Count how many subsegment
[
L, R
]
satisfying
R
−
L
+ 1
≥
1
and there is a kind of integer whose number
of occurrences is strictly greater than the sum of others in
a
[
L..R
]
Input
The fifirst line contains an integer
T
(
T
≤
15)
. Then
T
test cases follow.
For each test case, input two lines.
For the fifirst line, there is only one integer
n
(1
≤
n
≤
10
6
)
.
The second line contains
n
integers describing the array
a
[1
..n
]
, while the restriction
0
≤
a
i
≤
10
6
is
guaranteed.
Output
For each test case, output a integer per line, denoting the answer of the problem.
![](https://img-blog.csdnimg.cn/20210817182242279.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUzNDAzMjM4,size_16,color_FFFFFF,t_70)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
const int N=2e6+3;
vector<int>G[N];
int main(){
ios::sync_with_stdio(0);cin.tie(0);
int n,t;
cin>>t;
while(t--){
cin>>n;
unordered_set<int>s;
vector<int>a(n+1);
for(int i=1;i<=n;++i){
cin>>a[i]; s.insert(a[i]); G[a[i]].pb(i);
}
ll ans=0;
for(auto num:s){ // 枚举每个数作为众数
ll res=0;// 答案
ll sum=0;// 当前前缀和
unordered_map<int,int>f1,f2;// 前缀和为sum的点有f1[sum]个
G[num].pb(n+1);
ll k=0,minn=0;
for(int j=1;j<=n;++j){
if(j>G[num][k]) k++;
if(a[j]!=num&&sum==minn){
ll len=G[num][k]-j-1;
f2[sum+1]--;
f2[sum-len]++;
j+=len;
sum-=len+1;
}
else if (a[j]==num){
f1[sum]+=f2[sum];
f2[sum+1]+=f2[sum];
f2[sum]=0;
f1[sum]++;
res+=f1[sum];
sum++;
ans+=res;
}
else{
f1[sum]++;
sum--;
res-=f1[sum];
ans+=res;
}
if(minn>sum)minn=sum;
}
}
cout<<ans<<'\n';
for(auto &i:s)G[i].clear();
}
return 0;
}