ABC318_E

lzc369 / 2023-09-03 / 原文

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'

int n,a[300010],c[300010],t[300010],s;

signed main(){
 ios::sync_with_stdio(0);
 cin.tie(0);
 cout.tie(0);
 cin>>n;
 for(int x,i=1;i<=n;i++){
  cin>>x;
  if(a[x]){
   t[x]++;
   c[x]=c[x]+(i-a[x]-1)*t[x];
   s+=c[x];
   //cout<<i<<" "<<x<<" "<<s<<endl;
  }
  a[x]=i;
 }
 cout<<s;
 return 0;
}

对于这种类型的题目,如果需要统一储存相关数据,建议使用动态数组数组。

对于本题,在于该题目存在相关规律,可以通过前一个组合数量确定下一个新数的影响,从而实现n的复杂度,进一步优化代码。