Pagini recente » Cod sursa (job #2747871) | Cod sursa (job #2658833) | Cod sursa (job #186939) | Cod sursa (job #851638) | Cod sursa (job #963036)
Cod sursa(job #963036)
#include <fstream>
using namespace std;
ifstream fin("secv5.in");
ofstream fout("secv5.out");
#define MOD 666013
int v[(1<<20)+1],freq[(1<<20)+1],freq2[(1<<20)+1];
int N,L,U,st,st2,l,nr,nr2,current,total;
struct node
{
unsigned int x;
int norm;
node *next;
}*h[MOD];
int hash_search (unsigned int x)
{
int j=x%MOD;
for (node *d=h[j]; d!=NULL; d=d->next)
{
if (d->x==x) return d->norm;
}
return 0;
}
void hash_add (unsigned int x, int norm)
{
int j=x%MOD;
node *d= new node;
d->x=x;
d->norm=norm;
d->next=h[j];
h[j]=d;
}
void remove_elem (int &st, int &nr, int freq[])
{
freq[v[st]]--;
if (freq[v[st]]==0) nr--;
st++;
}
void add (unsigned int x)
{
int val= hash_search (x);
if (val) v[l]=val;
else {++current; v[l]=current; hash_add (x,current);}
freq[v[l]]++; freq2[v[l]]++;
if (freq[v[l]]==1) nr++;
if (freq2[v[l]]==1) nr2++;
while (nr2 >= L && st2<=l) remove_elem (st2,nr2,freq2);
}
int main()
{
unsigned int x;
fin>>N>>L>>U;
for (st=1, st2=1 ,l=1; l<=N; l++)
{
fin>>x;
add (x);
while ( nr > U && st<=l) remove_elem (st,nr,freq);
if (nr >= L) total += st2-st;
}
fout<<total;
}