Pagini recente » Cod sursa (job #205587) | Istoria paginii summer-challenge-2009/solutii/runda-2 | Cod sursa (job #152052) | Cod sursa (job #830045) | Cod sursa (job #943664)
Cod sursa(job #943664)
#include <fstream>
#include <iostream>
#include <map>
#include <hash_map>
#include <iterator>
#include <algorithm>
#include <cstring>
#define MAXN ((1<<20) + 1)
#define HASH_SIZE 6291469
using namespace std;
//using namespace __gnu_cxx;
unsigned int vec[MAXN];
int minimumSpans[MAXN];
unsigned fnv_hash (unsigned int key)
{
unsigned char *p = reinterpret_cast<unsigned char*>(&key);
unsigned int h = 2166136261;
for (int i = 0; i < sizeof(unsigned int); i++ )
{
h = ( h * 16777619 ) ^ p[i];
}
return h;
}
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[fnv_hash(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;
}