Cod sursa(job #1252510)

Utilizator OnimushaLordTiberiu Copaciu OnimushaLord Data 30 octombrie 2014 20:28:45
Problema Secventa 5 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.52 kb
# include <cstdio>
# include <vector>

# define LL unsigned long long
# define Uint unsigned int

# define MOD 666013
# define N (1<<20)+5

using namespace std;

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

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

inline void sterge(Uint 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(Uint x)
{
    int poz=x%MOD;
    ok=false;
    for(it=h[poz].begin(); it!=h[poz].end(); ++it)
        if(it->first==x)
        {
            ++it->second;
            if(it->second==1)
                distincte++;
            ok=true;
        }
    if(!ok)
    {
        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();
    LL 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("%u", a+i);

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

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