Cattle off more school eighth field B Beauty Values water problem

Meaning of the questions:

Given a sequence of different numbers ask you the number of sub-interval, the sum of all sub-interval is how much.

answer:

Each number can appear in the statistics the number of intervals. For each number, enum directly about the endpoint.

Note to heavy, so to start recording enumerate the left point after the first occurrence of each number where, at the next appearance, appear far from that number again.

#include<iostream>
#include<vector>
#define MAXN 100005
#define LL long long
using namespace std;
vector<int> last[MAXN];
LL ans;
int main(){
    int n;
    scanf("%d",&n);
    ans=0;
    for(int i=1;i<=n;i++){
        int tmp;
        scanf("%d",&tmp);
        int l,r;
        if(last[tmp].empty()){
            l=1;r=n;
        }else{
            l=last[tmp].back()+1;r=n;
        }
        last[tmp].push_back(i);
        LL t;
        if(i==l)t=r-l+1;
        else if(l==r)t=1;
        else t=1LL*(r-i+1)*(i-l+1);
        ans+=t;
//      printf("%lld\n",t);
    }
    printf("%lld\n",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/isakovsky/p/11348457.html