Cod sursa(job #1588737)

Utilizator DrumeaVDrumea Vasile DrumeaV Data 3 februarie 2016 16:09:21
Problema Secventa 5 Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.67 kb
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>

#define st first
#define nd second

using namespace std;

typedef long long ll;

const int Mn = 1 << 20;
const int mod = 66013;

ll n, l, u, pos, nr;
int cnt[Mn];
ll ar[Mn];
vector< pair< ll,int > > h[mod];
vector< pair< ll,int > >::iterator it;
char buff[Mn];

void read(ll &num)
{
    num = 0;
    char sign = '+';
    while (!isdigit(buff[pos]))
    {
        sign = buff[pos];

        if (++pos == Mn)
           fread(buff,1,Mn,stdin),pos = 0;
    }

    while (isdigit(buff[pos]))
    {
        num = num * 10 + buff[pos] - '0';

        if (++pos == Mn)
           fread(buff,1,Mn,stdin),pos = 0;
    }

    if (sign == '-')
       num *= -1;
}

int add(int x)
{
    int aux = x % mod;
    for (it = h[aux].begin(); it != h[aux].end(); it++)
        if (it->st == x)
           return it->nd;

    h[aux].push_back(make_pair(x,++nr));
    return nr;
}

ll get(int x)
{
    memset(cnt, 0, sizeof(cnt));
    int a = 1,b = 0;
    ll sol = 0;

    for (int i = 1;i <= n;i++)
    {
        while (a <= n && b + int(cnt[ar[a]] == 0) <= x)
        {
            b += int(cnt[ar[a]] == 0);
            cnt[ar[a]]++;
            a++;
        }

        sol += (a - i + 1);
        cnt[ar[i]]--;

        if (cnt[ar[i]] == 0)
           b--;
    }

    return sol;
}

int main()
{
    freopen("secv5.in","r",stdin);
    freopen("secv5.out","w",stdout);

    read(n); read(l); read(u);
    for (int i = 1; i <= n; i++)
    {
        read(ar[i]);
        ar[i] = add(ar[i]);
    }

    printf("%lld\n",get(u) - get(l - 1));

  return 0;
}