Cod sursa(job #1783612)

Utilizator AlexNiuclaeNiculae Alexandru Vlad AlexNiuclae Data 19 octombrie 2016 10:21:20
Problema Secventa 5 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <bits/stdc++.h>

using namespace std;

const int nmax = (1 << 20) + 10;

int n, l, r;
int ap[nmax];

unsigned int a[nmax];
long long ans;

void read_input()
{
    freopen("secv5.in","r",stdin);

    scanf("%d %d %d", &n, &l, &r);
    for (int i = 1; i <= n; ++i)
        scanf("%u", &a[i]);
}

void run_norm()
{
    vector < unsigned int > norm;

    for (int i = 1; i <= n; ++i)
        norm.push_back(a[i]);

    sort(norm.begin(), norm.end());
    auto it = unique(norm.begin(), norm.end());
    norm.resize(distance(norm.begin(), it));

    for (int i = 1; i <= n; ++i)
        a[i] = lower_bound(norm.begin(), norm.end(), a[i]) - norm.begin() + 1;
}

long long answer(int x)
{
    int left = 1, cnt = 0;
    long long ret = 0;

    for (int i = 1; i <= n; ++i)
        ap[a[i]] = 0;

    for (int i = 1; i <= n; ++i)
    {
        ap[a[i]]++;
        if (ap[a[i]] == 1)
            cnt++;

        while (cnt > x)
        {
            ap[a[left]]--;
            if (ap[a[left]] == 0) cnt--;
            left++;
        }

        ret += (i - left + 1);
    }

    return ret;
}

void solve()
{
    run_norm();
    ans = answer(r) - answer(l - 1);
}

void print_answer()
{
    freopen("secv5.out","w",stdout);
    printf("%lld\n", ans);
}

int main()
{
    read_input();
    solve();
    print_answer();

    return 0;
}