Cod sursa(job #350234)
#include <algorithm>
#include <iostream>
#include <fstream>
#define MAX 1000100
#define bazaHash 832387
#define ll long long
#define ui unsigned int
using namespace std;
class elemHash
{
public:
ui val;
int ap;
elemHash *next;
} *hash[2][bazaHash + 1];
ui hashEl[2];
ui n, l, u, stLow= 1, stUp = 1;
ll sol;
ui a[MAX], loc[MAX];
inline void hashInsert(ui el, int loc, int ha)
{
for (elemHash *r = hash[ha][loc]; r; r = r->next)
if (r->val == el)
{
r->ap++;
return;
}
hashEl[ha]++;
elemHash *r = new elemHash;
r->val = el;
r->ap = 1;
r->next = hash[ha][loc];
hash[ha][loc] = r;
}
inline void hashErase(ui el, int loc, int ha)
{
for (elemHash *r = hash[ha][loc], *prec = hash[ha][loc]; r; prec = r, r = r->next)
if (r->val == el)
{
r->ap--;
if (!r->ap)
{
hashEl[ha]--;
if (r == hash[ha][loc])
hash[ha][loc] = r->next;
else prec->next = r->next;
delete r;
}
return;
}
}
int main()
{
ifstream cin("secv5.in");
ofstream cout("secv5.out");
cin >> n >> l >> u;
for (ui i = 1; i <= n; i++)
{
ui x = 0;
cin >> x;
a[i] = x;
loc[i] = x % bazaHash;
hashInsert(x, loc[i], 0);
for (; hashEl[0] >= l; hashErase(a[stLow], loc[stLow], 0), stLow++);
hashInsert(x, loc[i], 1);
for (; hashEl[1] > u; hashErase(a[stUp], loc[stUp], 1), stUp++);
if (hashEl[0] == l - 1)
sol += (ll) (stLow - stUp);
}
cout << sol;
return 0;
}