Pagini recente » Cod sursa (job #673528) | Cod sursa (job #1085014) | Cod sursa (job #266568) | Cod sursa (job #2155607) | Cod sursa (job #944304)
Cod sursa(job #944304)
#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 38993
using namespace std;
unsigned int vec[MAXN];
int minimumSpans[MAXN];
typedef unsigned int uint32;
struct Bucket
{
uint32 Key;
uint32 Count;
};
/*struct HashBucket
{
HashBucket() :
Size(0),
Capacity(7),
Entries((Bucket*)malloc(7 * sizeof(Bucket)))
{}
Bucket& operator [] (int index)
{
return Entries[index];
}
int size() const
{
return Size;
}
void push_back(Bucket b)
{
if (Size == Capacity)
{
Capacity += Size;
Bucket* ptr = (Bucket*)realloc(Entries, Capacity * sizeof(Bucket));
Entries = ptr;
}
Entries[Size] = b;
Size++;
}
void clear()
{
Size = 0;
}
int Size;
int Capacity;
Bucket* Entries;
};*/
typedef vector<Bucket> HashBucket;
template <const int HashSize1, const int HashSize2>
class HashTable
{
public:
uint32& operator [] (uint32 key)
{
uint32 hash1 = key % HashSize1;
uint32 hash2 = key % HashSize2;
for (int i=0; i<table_1[hash1].size(); ++i)
{
if (table_1[hash1][i].Key == key)
{
return table_1[hash1][i].Count;
}
}
for (int i=0; i<table_2[hash2].size(); ++i)
{
if (table_2[hash2][i].Key == key)
{
return table_2[hash2][i].Count;
}
}
Bucket b = {key, 0};
if (table_1[hash1].size() <= table_2[hash2].size())
{
table_1[hash1].push_back(b);
return table_1[hash1][table_1[hash1].size() - 1].Count;
}
table_2[hash2].push_back(b);
return table_2[hash2][table_2[hash2].size() - 1].Count;
}
void clear()
{
/*int vec[50] = {0};
int countZero = 0;
int countOne = 0;
int countMore = 0;
int maxFill = 0;
fstream fout("hash_log.txt", fstream::out);
for (int i=0; i<HASH_SIZE; ++i)
{
switch (table_1[i].size())
{
case 0:
countZero++;
break;
case 1:
countOne++;
break;
default:
countMore++;
}
maxFill = max(maxFill, (int)table_1[i].size());
vec[(int)table_1[i].size()]++;
fout << table_1[i].size() << " ";
table_1[i].clear();
}
fout << "\n";
fout.close();
cout << countZero << " " << countOne << " " << countMore << " " << maxFill << "\n";
for (int i=0; i<50; ++i)
{
cout << vec[i] << " ";
}
cout << endl;*/
for (int i=0; i<HashSize1; ++i)
{
table_1[i].clear();
}
for (int i=0; i<HashSize2; ++i)
{
table_2[i].clear();
}
}
private:
HashBucket table_1[HashSize1];
HashBucket table_2[HashSize2];
};
HashTable<22037, 23801> mapDistincts;
int main()
{
int n, L, U;
long long numSecv = 0;
fstream fin("secv5.in" , fstream::in);
fstream fout("secv5.out", fstream::out);
/*std::default_random_engine gen;
std::uniform_int_distribution<int> dis(0,1<<31);
fout << MAXN << " " << MAXN / 2 << " " << MAXN - 1 << "\n";
for (int i=1; i<=MAXN; ++i)
{
fout << dis(gen) << "\n";
}
fout.close();
return 0;*/
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 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();
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);
}
//mapDistincts.clear();
fout << numSecv << "\n";
return 0;
}