Cod sursa(job #1785090)

Utilizator giotoPopescu Ioan gioto Data 20 octombrie 2016 21:11:05
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.55 kb
#include <cstdio>
#include <cstring>
#include <vector>
#define NMAX 1048578
#define List 666013
using namespace std;

vector<unsigned int> Hash[List];
vector<pair <unsigned int, unsigned int> > Value[List];
int n, l, u, V = 0;
unsigned int a[NMAX], v[NMAX];
inline void add_hash(unsigned int x){
    unsigned int L = x % List;
    vector<unsigned int> :: iterator it;
    for(it = Hash[L].begin() ; it != Hash[L].end() ; ++it)
        if(*it == x)
            return ;
    ++V;
    Hash[L].push_back(x);
    Value[L].push_back(make_pair(x, V));
}
inline unsigned int get_value(unsigned int x){
    unsigned int L = x % List;
    for(int i = 0 ; i < Value[L].size() ; ++i)
        if(Value[L][i].first == x)
            return Value[L][i].second;
}
inline long long Sol(int Length){
    if(Length == 0)
        return 0;
    long long X = 0;
    long long NR = 0, j = 1;
    memset(v, 0, NMAX);
    for(int i = 1 ; i <= n ; ++i){
        ++v[a[i]];
        if(v[a[i]] == 1)
            ++NR;
        while(NR > Length){
            --v[a[j]];
            if(v[a[j]] == 0)
                --NR;
            ++j;
        }
        X += i - j + 1;
    }
    return X;
}
int main()
{
    freopen("secv5.in", "r", stdin);
    freopen("secv5.out", "w", stdout);
    scanf("%d%d%d", &n, &l, &u);
    for(int i = 1 ; i <= n ; ++i){
        scanf("%d", &a[i]);
        add_hash(a[i]);
    }
    for(int i = 1 ; i <= n ; ++i)
        a[i] = get_value(a[i]);
    printf("%lld", (long long)Sol(u) - (long long)Sol(l - 1));
    return 0;
}