2019 牛客多校 第八场 B Beauty Values (贡献)

链接:https://ac.nowcoder.com/acm/contest/888/B

题意:给出一个序列,求它的全部子区间的beauty(区间的数的种类数目)。 i<1e5 ,ai<1e5

题解:考虑每一个种类对答案的贡献,即考虑每一个数对答案的贡献,看这个数在多少区间出现过。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn=1e5+5;
int n, a[maxn], pre[maxn];

int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    cin>>n;
    for(int i=1; i<=n; i++) cin>>a[i];
    ll ans=0;
    for(int i=1; i<=n; i++)
    {
        ans+=1ll*(i-pre[a[i]])*(n-i+1);
        pre[a[i]]=i;
    }
    cout<<ans;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Yokel062/p/11768045.html