Cod sursa(job #1243852)

Utilizator HotSteelBeteag Ion Andrei HotSteel Data 16 octombrie 2014 15:17:44
Problema Secventa 5 Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.87 kb
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

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

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

void insert2(unsigned int key)
{
    int index = key % MOD;
    bool found = false;
    for(it = h[index].begin() ; it != h[index].end() ; ++it)
    {
        if(it->first == key)
        {
            ++it->second;
            if(it->second == 1)
                count1++;
            found = true;

            return;
        }
    }

    if(!found)
    {

        h[index].push_back(make_pair(key,1));
        count1++;
    }
}

void erase2(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)
            {
                //h[index].erase(it);
                swap(*it,h[index].back());
                h[index].pop_back();
                --count1;
            }

            return;
        }
    }
}

long long numb_secv(int a)
{
    long long ans = 0;
    int i;
    for(i = 0 ; i < MOD ; ++i)
        h[i].clear();

    count1 = 0;

    int L,R;
    L = 1;
    R = 1;
    while(R <= n)
    {
        insert2(v[R]);

        while(count1 > a)
        {

            erase2(v[L]);
            L++;
        }

        ans+= 1LL * (R-L+1);

        R++;
    }

    return ans;
}

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

    int u,l;
    scanf("%d%d%d",&n,&l,&u);

    int i;
    int k;
    for(i = 1 ; i <= n ; ++i)
    {
        scanf("%d",&k);
        v[i] = k;

    }

    printf("%lld",numb_secv(u) - numb_secv(l-1));

    return 0;
}