Cod sursa(job #1246241)

Utilizator stefan.friptuPetru Stefan Friptu stefan.friptu Data 20 octombrie 2014 20:06:09
Problema Secventa 5 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <cstdio>
#include <algorithm>
#include <vector>

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

using namespace std;

unsigned long v[NMAX];
long count1;
unsigned long l,u,n;
vector <pair <unsigned long, long> > h[MOD];
vector <pair <unsigned long, long> > :: iterator it;

void insert (unsigned long key)
{
    long index=key%MOD,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 long key)
{
    long 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 nrsec (long a)
{
    long L=1,R=1,i;
    long long ans=0;
    for(i=0;i<MOD;i++)
        h[i].clear();
    count1=0;
    while(R<=n)
    {
        insert(v[R]);
        while(count1>a){
            erase(v[L]);
            L++;
        }
        R++;
        ans+=1LL*(R-L+1);
    }
    return ans;
}

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

    scanf("%lu%lu%lu",&n,&l,&u);

    for(long i=1;i<=n;i++)
        scanf("%lu",&v[i]);
    printf("%lld\n",nrsec(u)-nrsec(l-1));
    return 0;
}