2 Yarik and Musical Notes
打表找到的规律
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+10;
int a[N];
void solve(){
int n;
cin>>n;
map<int,int>mp;
for(int i=1;i<=n;i++){
cin>>a[i];
mp[a[i]]++;
}
int ans=0;
for(int i=n;i>=1;i--){
if(a[i]==2){
ans+=mp[1];
ans+=mp[2]-1;
mp[2]--;
}else if(a[i]==1){
ans+=mp[1]-1;
ans+=mp[2];
mp[1]--;
}else{
ans+=mp[a[i]]-1;
mp[a[i]]--;
}
}
cout<<ans<<"\n";
}
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t=1;
cin>>t;
for(int i=1;i<=t;i++)solve();
return 0;
}