Pagini recente » Cod sursa (job #2170399) | Cod sursa (job #1474185) | Cod sursa (job #2678508) | Cod sursa (job #1923131) | Cod sursa (job #945496)
Cod sursa(job #945496)
#include <fstream>
#include <iostream>
#include <map>
#include <hash_map>
#include <iterator>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdlib>
#define MAXN ((1<<20) + 1)
#define HASH_SIZE 19793
using namespace std;
unsigned int vec[MAXN];
unsigned int minimumSpans[MAXN];
typedef unsigned int uint32;
struct Bucket
{
uint32 Key;
uint32 Count;
};
typedef vector<Bucket> HashBucket;
template <const int HashSize>
class HashTable
{
public:
uint32& operator [] (uint32 key)
{
uint32 hash = key % HashSize;
for (int i=0; i<table[hash].size(); ++i)
{
if (table[hash][i].Key == key)
{
return table[hash][i].Count;
}
}
Bucket b = {key, 0};
table[hash].emplace_back(b);
return table[hash][table[hash].size() - 1].Count;
}
void erase(uint32 key)
{
uint32 hash = key % HashSize;
for (int i=0; i<table[hash].size(); ++i)
{
if (table[hash][i].Key == key)
{
table[hash].erase(table[hash].begin() + i);
return;
}
}
}
void clear()
{
for (int i=0; i<HashSize; ++i)
{
table[i].clear();
}
}
private:
HashBucket table[HashSize];
};
HashTable<HASH_SIZE> mapDistinctsL;
HashTable<HASH_SIZE> mapDistinctsU;
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);
//cout << buffer << endl;
char* pCurrent = buffer;
for (int i=1; i<=n; ++i)
{
//fin >> vec[i];
pCurrent++;
while (*pCurrent >= '0' && *pCurrent <= '9')
{
vec[i] = 10*vec[i] + (*pCurrent - 48);
pCurrent++;
}
//cout << vec[i] << " ";
}
//cout << endl;
//free(buffer);
unsigned int distinctsL = 0;
unsigned int distinctsU = 0;
int spanL = 1;
int spanU = 1;
for (int i=1; i<=n; ++i)
{
mapDistinctsL[vec[i]]++;
if (mapDistinctsL[vec[i]] == 1)
{
distinctsL++;
while (distinctsL > L-1)
{
mapDistinctsL[vec[spanL]]--;
if (mapDistinctsL[vec[spanL]] == 0)
{
mapDistinctsL.erase(vec[spanL]);
distinctsL--;
}
spanL++;
}
}
mapDistinctsU[vec[i]]++;
if (mapDistinctsU[vec[i]] == 1)
{
distinctsU++;
while (distinctsU > U)
{
mapDistinctsU[vec[spanU]]--;
if (mapDistinctsU[vec[spanU]] == 0)
{
mapDistinctsU.erase(vec[spanU]);
distinctsU--;
}
spanU++;
}
}
numSecv += (spanL - spanU);
}
fout << numSecv << "\n";
return 0;
}