2019牛客暑期多校训练营(第八场) B Beauty Values

题目描述
Gromah and LZR have entered the second level. There is a sequence a_1, a_2, \cdots, a_na1​,a2​,⋯,an​ on the wall.

There is also a note board saying “the beauty value of a sequence is the number of different elements in the sequence”.

LZR soon comes up with the password of this level, which is the sum of the beauty values of all successive subintervals of the sequence on the wall.

Please help them determine the password!

输入描述:
The first line contains one positive integer n_{}n​, denoting the length of the sequence.

The second line contains n_{}n​ positive integers a_1, a_2, \cdots, a_na1​,a2​,⋯,an​, denoting the sequence.

1 \le a_i \le n \le 10^51≤ai​≤n≤105

输出描述:
Print a non-negative integer in a single line, denoting the answer.

题目大意:
给出一个序列。让求出所有的子序列中含有不同数字的个数,然后求和

分析:
当处理第一个1的时候,此时i=1,那么这个1会对区间[1,1],[1,2],[1,3],[1,4]有影响,那么第一轮的答案就加4
当i=2时,需要判断2这个数前一次出现的位置,对样例来说,是没有出现过的,那么2对[2,2],[2,3],[2,4],[1,2],[1,3],[1,4]这些区间有贡献值的
当i=3时,这个1就不会对前一个1所贡献的区间有影响了,那么当前这个1所贡献的区间是[3,3],[3,4],[2,3],[2,4]
当i=4时,贡献区间时[4,4],[3,4],[2,4],[1,4]

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<map>
#define rep(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
using namespace std;
const int N=1e5+10;
const int INF=0x3f3f3f3f;
int a[N];
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int n;
    scanf("%d",&n);
    ll ans=0;
    rep(i,1,n)
    {int val;
        scanf("%d",&val);
//求原序列有多少个子区间包含至少一个该数字,相当于求该数字对多少个区间有贡献
        ans+=1ll*(i-a[val])*(n-i+1);//本身的与前一次出现的区间所构成的贡献值
        cout<<ans<<endl;
        a[val]=i;
    }
    printf("%lld",ans);
    return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值