Cod sursa(job #943662)

Utilizator claudiumihailClaudiu Mihail claudiumihail Data 26 aprilie 2013 08:08:49
Problema Secventa 5 Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.54 kb
#include <fstream>
#include <iostream>
#include <map>
#include <hash_map>
#include <iterator>
#include <algorithm>
#include <cstring>
 
#define MAXN ((1<<20) + 1)
#define HASH_SIZE 3145739
 
using namespace std;
//using namespace __gnu_cxx;
 
unsigned int vec[MAXN];
int minimumSpans[MAXN];

struct HashTable
{
    HashTable(int size)
    {
        table = (unsigned int*)calloc(size, sizeof(unsigned int));
    }
    
    unsigned int& operator[] (unsigned int key)
    {
        return table[key % HASH_SIZE];
    }
    
    const unsigned int& operator[] (unsigned int key) const
    {
        return table[key % HASH_SIZE];
    }

    unsigned int* table;
};

HashTable mapDistincts(HASH_SIZE);
 
int main()
{
    int n, L, U;
    long long numSecv = 0;
    fstream fin("secv5.in", fstream::in);
    fstream fout("secv5.out", fstream::out);
     
    fin >> n >> L >> U;
    //cout << n << " " << L << " " << U << endl;
     
    for (int i=1; i<=n; ++i)
    {
        fin >> vec[i];
        //cout << vec[i] << " ";
    }
    //cout << endl;
 
    //hash_map<int, int> mapDistincts;
    unsigned int distincts = 0;
    int span = 1;
     
    for (int i=1; i<=n; ++i)
    {
        mapDistincts[vec[i]]++;
        if (mapDistincts[vec[i]] == 1)
        {
            distincts++;
        }
         
        while (distincts > L-1)
        {
            mapDistincts[vec[span]]--;
            if (mapDistincts[vec[span]] == 0)
            {
                distincts--;
            }
             
            span++;
        }
         
        minimumSpans[i] = span;
    }
     
    //ostream_iterator<int> itOut(cout, " ");
    //copy(minimumSpans, minimumSpans + n + 1, itOut);
    //cout << endl;
     
    //mapDistincts.clear();
    memset(mapDistincts.table, 0, HASH_SIZE * sizeof(unsigned int));
    distincts = 0;
    span = 1;
     
    for (int i=1; i<=n; ++i)
    {
        mapDistincts[vec[i]]++;
        if (mapDistincts[vec[i]] == 1)
        {
            distincts++;
        }
         
        while (distincts > U)
        {
            mapDistincts[vec[span]]--;
            if (mapDistincts[vec[span]] == 0)
            {
                distincts--;
            }
             
            span++;
        }
         
        //maximumSpans[i] = span;
         
        numSecv += (minimumSpans[i] - span);
    }
 
    //copy(maximumSpans, maximumSpans + n + 1, itOut);
    //cout << endl;
     
    fout << numSecv << "\n";
     
    return 0;
}