Cod sursa(job #2439951)

Utilizator DavidLDavid Lauran DavidL Data 17 iulie 2019 11:45:54
Problema Secventa 5 Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.05 kb
#include <bits/stdc++.h>
#define ll unsigned int
using namespace std;
#define cin fi
#define cout fo
ifstream fi("secv5.in");
ofstream fo("secv5.out");

const int NMAX = 1100000;
const int MOD = 1000033;

int n, l, u;
ll x[NMAX], y[NMAX];
unordered_map <ll, int> M;
int k;
int dp[NMAX];
list < pair<int, int> > f[MOD];

void scade(int nr)
{
    int loc = nr % MOD;
    for (auto it = f[loc].begin(); it != f[loc].end(); it++)
        if ((*it).first == nr)
            (*it).second--;
}

void creste(int nr)
{
    int loc = nr % MOD;
    bool gasit = 0;
    for (auto it = f[loc].begin(); it != f[loc].end(); it++)
        if ((*it).first == nr)
            (*it).second++, gasit = 1;
    
    if (!gasit)
        f[loc].push_front(make_pair(nr, 1));
}

int val(int nr)
{
    int loc = nr % MOD;
    for (auto da: f[loc])
        if (da.first == nr)
            return da.second;
    return 0;
}
/*
void normalizare()
{
    for (int i = 1; i <= n; i++)
        y[i] = x[i];

    sort(y + 1, y + n + 1);

    for (int i = 1; i <= n; i++)
        if (y[i] != y[i - 1])
            M[y[i]] = ++k;

    for (int i = 1; i <= n; i++)
        x[i] = M[x[i]];
}*/

long long nrSubsecventeMaximXDistincte(int nr)
{
    if (nr == 0)
        return 0;

    for (int i = 0; i < MOD; i++)
        f[i].clear();

    int total = 0;
    dp[1] = 1;
    creste(x[1]);
    total++;

    for (int i = 2; i <= n; i++)
    {
        if (val(x[i]) == 0)
            total++;
        creste(x[i]);

        dp[i] = dp[i - 1];
        while (total > nr)
        {
            scade(x[dp[i]]);
            if (val(x[dp[i]]) == 0)
                total--;

            dp[i]++;
        }
    }

    long long ret = 0;
    for (int i = 1; i <= n; i++)
        ret += i - dp[i] + 1;

    return ret;
}

signed main()
{
    cin >> n >> l >> u;
    for (int i = 1; i <= n; i++)
        cin >> x[i];

    //normalizare();

    cout << nrSubsecventeMaximXDistincte(u) - nrSubsecventeMaximXDistincte(l - 1);

    return 0;
}