Cod sursa(job #1247773)

Utilizator raduchirilaChirila Radu Razvan raduchirila Data 23 octombrie 2014 19:08:13
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.7 kb
#include<cstdio>
#include<vector>
#include<algorithm>
#define mod 666013
#define NMAX (1<<20)+5
using namespace std;
unsigned int v[NMAX];
vector <pair<unsigned int,int> > h[mod];
vector <pair<unsigned int,int> >::iterator it;
int n,count1;
void insert(unsigned int key)
{
    int index=key%mod;
    int gasit=0;
    for(it=h[index].begin();it!=h[index].end();++it)
    {
        if(it->first==key)
        {
            ++(it->second);
            if(it->second==1)
                count1++;
            gasit=1;
        }
    }
    if(!gasit)
    {
        h[index].push_back(make_pair(key,1));
        count1++;
    }
}

void erasee(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;
        }
    }
    return;
}

long long nrsec(int a)//de la 1 la a nr distincte
{
    int left,ri;
    long long res=0;
    for(int i=0;i<mod;++i)
    {
        h[i].clear();
    }
    count1=0;
    left=1;ri=1;
    while(ri<=n)
    {
        insert(v[ri]);
        while(count1>a)
        {
            erasee(v[left]);
            left++;
        }
        res=res+(long long)(ri-left+1);
        ri++;
    }
    return res;
}

int main()
{
    freopen("secv5.in","r",stdin);
    freopen("secv5.out","w",stdout);
    int l,u;
    scanf("%d%d%d",&n,&l,&u);
    for(int i=1;i<=n;++i)
        scanf("%u",&v[i]);
    printf("%I64d",nrsec(u)-nrsec(l-1));
    return 0;
}