Cod sursa(job #1846320)

Utilizator ionutpop118Pop Ioan Cristian ionutpop118 Data 12 ianuarie 2017 15:58:51
Problema Secventa 5 Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.73 kb
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int NMAX = 1100000;

struct elem
{
    int x, poz;
};
elem aux[NMAX + 5];
int nou[NMAX + 5];
int v[NMAX + 5], f[NMAX + 5];

bool cmp(elem a, elem b)
{
    return a.x < b.x;
}
bool cmp2(elem a, elem b)
{
    return a.poz < b.poz;
}
int main()
{
    freopen("secv5.in", "r", stdin);
    freopen("secv5.out", "w", stdout);
    long long ans;
    int n, a, b, x, st, nr;
    scanf("%d %d %d", &n, &a, &b);
    /// Normalizare ///
    for (int i = 1; i <= n; ++i)
    {
        scanf("%d", &x);
        aux[i].x = x;
        aux[i].poz = i;
    }
    sort (aux + 1, aux + n + 1, cmp);
    nou[1] = 1;
    for (int i = 2; i <= n; ++i)
        if (aux[i].x == aux[i - 1].x)
            nou[i] = nou[i - 1];
        else
            nou[i] = nou[i - 1] + 1;
    for (int i = 1; i <= n; ++i)
        aux[i].x = nou[i];
    sort (aux + 1, aux + n + 1, cmp2);
    for (int i = 1; i <= n; ++i)
        v[i] = aux[i].x;

    ans = nr = 0; st = 1;
    for (int i = 1; i <= n; ++i)
    {
        if (f[v[i]] == 0)
            ++nr;
        ++f[v[i]];

        while (nr > b)
        {
            --f[v[st]];
            if (f[v[st]] == 0)
                --nr;
            ++st;
        }

        ans += (1LL) * (i - st + 1);
    }

    memset(f, 0, sizeof(f));
    st = 1; nr = 0;
    for (int i = 1; i <= n; ++i)
    {
        if (f[v[i]] == 0)
            ++nr;
        ++f[v[i]];

        while (nr >= a)
        {
            --f[v[st]];
            if (f[v[st]] == 0)
                --nr;
            ++st;
        }

        ans -= (1LL) * (i - st + 1);
    }
    printf("%lld\n", ans);
    return 0;
}