Cod sursa(job #1250172)

Utilizator armandpredaPreda Armand armandpreda Data 27 octombrie 2014 20:59:50
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

const int MOD=666013,MAX=(1<<20)+5;
int n,count1;
unsigned int v[MAX];
vector <pair <unsigned int,int> > h[MOD];
vector <pair <unsigned int,int> > :: iterator it;
void insert(unsigned int key)
{
    int 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 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 numb_secv(int a)
{
    int L=1,R=1;
    long long ans=0;
    for(int i=0;i<MOD;++i)
        h[i].clear();
    count1=0;
    for(int i=0;i<MOD;++i)
        h[i].clear();
    while(R<=n)
    {
        insert(v[R]);
        while(count1>a)
        {
            erase(v[L]);
            L++;
        }
        ans=ans+R-L+1;
        R++;
    }
    return ans;
}
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",numb_secv(u)-numb_secv(l-1));
    return 0;
}