题目链接:
https://codeforces.com/contest/1883/problem/F
思想:
用set存,枚举前缀,答案加上当前序列有多少个不同的数,删去当前数,然后当前数个数减一,如果个数减为0,从mp中减去这个数。
代码:
// Problem: F. You Are So Beautiful
// Contest: Codeforces - Codeforces Round 905 (Div. 3)
// URL: https://codeforces.com/contest/1883/problem/F
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5+5;
int n,a[N];
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin>>T;
while(T--){
cin>>n;
map<int,int> mp;
set<int> st;
for(int i=1;i<=n;i++){
cin>>a[i];
mp[a[i]]++;
st.insert(a[i]);
}
int ans=0;
for(int i=1;i<=n;i++){
if(st.count(a[i])) ans+=mp.size();
st.erase(a[i]);
mp[a[i]]--;
if(!mp[a[i]]) mp.erase(a[i]);
}
cout<<ans<<"\n";
}
return 0;
}