题解:CF1527C Sequence Pair Weight
思路
小学二年级题目。
这是一道求贡献题,可以用 map 来维护。 表示当前,值为 的所有位置之和。
对于每个测试用例,遍历 从 到 。此时,当前元素的值为 ,前面的所有相同值的位置之和就是 。这一轮的贡献就是 。然后把当前位置加入 map,即 。
别忘了 long long,不然受着。
CODE
Code
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
int t;
cin >> t;
while(t--){
map<int, ll> m;
int n;
cin >> n;
long long ans = 0;
int a[100001] = {0};
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++){
ans += m[a[i]] * (n - i + 1);
m[a[i]] += i;
}
cout << ans << '\n';
}
return 0;
}