Cod sursa(job #1243857)

Utilizator ionutpop118Pop Ioan Cristian ionutpop118 Data 16 octombrie 2014 15:25:25
Problema Secventa 5 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.78 kb
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int n,count1;
const int MOD = 666013;
const int NMAX = (1<<20)+5;
unsigned int v[NMAX];
vector < pair <unsigned int , int> > h[MOD];
vector < pair <unsigned int , int> >::iterator it;
void insert(unsigned int key)
{
    int index=key%MOD;
    bool found=0;
    for (it=h[index].begin();it!=h[index].end();++it)
    {
        if (it->first==key)
        {
            ++it->second;
            if (it->second==1)
                ++count1;
            found=1;
        }
    }
    if (!found)
    {
        h[index].push_back(make_pair(key,1));
        
        count1++;
    }
}
void erase(unsigned int key)
{
    int index=key%MOD;
    for (it=h[index].begin();it!=h[index].end();++it)
        if (it->first==key)
        {
            --it->second;
            if (it->second==0)
            {
                --count1;
                swap(*it,h[index].back());
                h[index].pop_back();
            }
            return ;
        }
}
long long number_secv(int a)
{
    if (a==0)
        return 0;
    int L,R;
    long long ans=0;
    for (register int i=0;i<MOD;++i)
        h[i].clear();
    count1=0;
    L=R=1;
    while (R<=n)
    {
        insert(v[R]);
        if (count1>a)
        {
            while(count1>a)
            {
                erase(v[L]);
                ++L;
            }
        }
        ans+=R-L+1;
        ++R;
    }
    return ans;
}
int main()
{
    freopen("secv5.in","r",stdin);
    freopen("secv5.out","w",stdout);
    int lmin,lmax;
    scanf("%d%d%d",&n,&lmin,&lmax);
    for (register int i=1;i<=n;++i)
        scanf("%u",&v[i]);
    printf("%lld",number_secv(lmax)-number_secv(lmin-1));
    return 0;
}