Cod sursa(job #2439961)

Utilizator DavidLDavid Lauran DavidL Data 17 iulie 2019 11:56:21
Problema Secventa 5 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.83 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];
int k;
int dp[NMAX];
list < pair<ll, int> > f[MOD];

inline int getMod(ll a, int M)
{
    return a - a / M * M;
}

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

void creste(ll nr)
{
    int loc = getMod(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(ll nr)
{
    int loc = getMod(nr, MOD);
    for (auto da: f[loc])
        if (da.first == nr)
            return da.second;
    return 0;
}

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;
}