Cod sursa(job #906064)

Utilizator tudgal1001Profir Tudor tudgal1001 Data 6 martie 2013 14:14:48
Problema Secventa 5 Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.6 kb
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#define M 666013
#define NMax (1<<20)+5
using namespace std;

unsigned v[NMax];
int n;
vector<pair<unsigned,int> > a[M];

vector<pair<unsigned,int> >::iterator find (unsigned x)
{
    vector<pair<unsigned,int> >::iterator it;
    for (it=a[x%M].begin(); it!=a[x%M].end(); ++it)
        if ((it->first)==x)
            return it;
    return a[x%M].end();
}

void hash ()
{
    int i;
    for (i=1; i<=n; i++)
        if (find(v[i])==a[v[i]%M].end())
            a[v[i]%M].push_back(make_pair(v[i],0));
}

void reset ()
{
    int i;
    vector<pair<unsigned,int> >::iterator it;
    for (i=1; i<=n; i++)
    {
        it=find(v[i]);
        it->second=0;
    }
}

long long sol (int x)
{
    if (!x) return 0;
    int nr=1,st=1,i;
    long long sum=1,aux;
    vector<pair<unsigned,int> >::iterator it;
    it=find(v[1]);
    it->second=1;
    for (i=2; i<=n; i++)
    {
        it=find(v[i]);
        if (it->second==0)
            nr++;
        it->second++;
        while (nr>x)
        {
            it=find(v[st]);
            if (it->second==1)
                nr--;
            it->second--;
            st++;
        }
        aux=(long long)(i-st+1);
        sum+=aux;
    }
    return sum;
}

int main ()
{
    char s[10];
    int l,u,i,j,lg;
    long long r1,r2;
    freopen("secv5.in","r",stdin);
    freopen("secv5.out","w",stdout);
    scanf("%d%d%d",&n,&l,&u);
    for (i=1; i<=n; i++)
    {
        scanf("%s",s);
        v[i]=strtoul(s,NULL,10);
    }
    hash();
    r1=sol(u);
    reset();
    r2=sol(l-1);
    printf("%lld\n",r1-r2);
    return 0;
}