Pagini recente » Cod sursa (job #1219892) | Cod sursa (job #1492817) | Cod sursa (job #2204075) | Cod sursa (job #555805) | Cod sursa (job #943662)
Cod sursa(job #943662)
#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;
}