Pagini recente » Cod sursa (job #1633571) | Cod sursa (job #1535539) | Cod sursa (job #458628) | Cod sursa (job #760813) | Cod sursa (job #944244)
Cod sursa(job #944244)
#include <fstream>
#include <iostream>
#include <map>
#include <hash_map>
#include <iterator>
#include <algorithm>
#include <vector>
#include <cstring>
#define MAXN ((1<<20) + 1)
#define HASH_SIZE 38993
using namespace std;
using namespace __gnu_cxx;
unsigned int vec[MAXN];
int minimumSpans[MAXN];
typedef unsigned int uint32;
typedef vector<pair<uint32,uint32> > HashBucket;
class HashTable
{
public:
uint32& operator [] (uint32 key)
{
uint32 hash = key % HASH_SIZE;
if (table[hash] != NULL)
{
HashBucket::iterator it = find_if(table[hash]->begin(), table[hash]->end(), FindKey(key));
if (it != table[hash]->end())
{
return it->second;
}
}
else
{
table[hash] = new HashBucket();
table[hash]->reserve(28);
}
table[hash]->push_back(make_pair(key, 0));
return (*table[hash])[table[hash]->size() - 1].second;
}
void clear()
{
for (int i=0; i<HASH_SIZE; ++i)
{
if (table[i] != NULL)
{
table[i]->clear();
}
//fill(table[i].begin(), table[i].end(), pz);
}
}
private:
struct FindKey
{
FindKey(uint32 key) :
keyToFind(key)
{}
bool operator () (const pair<uint32,uint32>& kvp) const
{
return kvp.first == keyToFind;
}
uint32 keyToFind;
};
HashBucket* table[HASH_SIZE];
};
HashTable mapDistincts;
//hash_map<uint32, int> mapDistincts;
int main()
{
int n, L, U;
long long numSecv = 0;
fstream fin ("secv5.in" , fstream::in);
fstream fout("secv5.out", fstream::out);
fin.seekg(0, ios::end);
int end = fin.tellg();
char* buffer = static_cast<char*>(malloc(end));
fin.seekg (0, ios::beg);
fin >> n >> L >> U;
//cout << n << " " << L << " " << U << endl;
fin.read(buffer, end);
char* pCurrent = buffer;
for (int i=1; i<=n; ++i)
{
pCurrent++;
while (*pCurrent >= '0' && *pCurrent <= '9')
{
vec[i] = 10*vec[i] + (*pCurrent - 48);
pCurrent++;
}
}
//cout << endl;
//free(buffer);
//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;
}
mapDistincts.clear();
//ostream_iterator<int> itOut(cout, " ");
//copy(minimumSpans, minimumSpans + n + 1, itOut);
//cout << endl;
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++;
}
numSecv += (minimumSpans[i] - span);
}
//copy(maximumSpans, maximumSpans + n + 1, itOut);
//cout << endl;
fout << numSecv << "\n";
return 0;
}