Cod sursa(job #1252487)

Utilizator OnimushaLordTiberiu Copaciu OnimushaLord Data 30 octombrie 2014 20:08:35
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.51 kb
# include <cstdio>
# include <vector>
# define LL long long
# define MOD 666013
# define N (1<<20)+5

using namespace std;

bool ok;
vector <pair <int, int> > h[MOD];
vector <pair <int, int> > :: iterator it;

int distincte=0;
int n,l,u,a[N],i;

inline void sterge(int x)
{
    int poz=x%MOD;
    for(it=h[poz].begin(); it!=h[poz].end(); ++it)
        if(it->first==x)
        {
            --it->second;
            if(!it->second) distincte--;
        }
}
inline void adauga(int x)
{
    int poz=x%MOD;
    ok=false;
    for(it=h[poz].begin(); it!=h[poz].end(); ++it)
        if(it->first==x)
        {
            ++it->second;
            ok=true;
        }
    if(!ok)
    {
        if(!distincte && !h[poz].empty()) distincte++;
        h[poz].push_back(make_pair(x,1));
        distincte++;
    }
}
inline LL nr(int x)
{
    int i,st,dr;
    for(i=0; i<MOD; ++i)
        h[i].clear();
    long long ans=0;
    st=dr=1;
    ans=distincte=0;

    while(dr<=n)
    {
        adauga(a[dr]);
        while(distincte>x)
        {
            sterge(a[st]);
            st++;
        }
        ans+=(LL) dr-st+1;
        dr++;
    }
    return ans;
}

int main()
{
    freopen("secv5.in", "r", stdin);
    freopen("secv5.out", "w", stdout);

    scanf("%d %d %d\n", &n, &l, &u);
    for(i=1; i<=n; ++i)
        scanf("%d", a+i);

    long long solutie=nr(u);
    solutie-=nr(l-1);
    printf("%lld\n", solutie);

    fclose(stdin);
    fclose(stdout);
    return 0;
}