Cod sursa(job #2849105)

Utilizator mateitudordmDumitru Matei mateitudordm Data 14 februarie 2022 15:59:11
Problema Secventa 5 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <bits/stdc++.h>
#define nmax 1005000
#define int long long
#pragma GCC optimize("Ofast")

using namespace std;

int v[nmax + 1], c[nmax + 1], cpart[nmax + 1], n, f[nmax + 1];
map<int, int> orient;

int rez(int x)
{
    if (x == 0)
        return 0;
    int i, cnt = 0, dr = 0, fsize = 0;
    fsize = 0;
    for (i = 1; i <= n; i++)
        f[i] = 0;
    for (i = 1; i <= n; i++)
    {
        while (dr < n && fsize < x)
            f[v[++dr]]++, fsize += (f[v[dr]] == 1);
        while (f[v[dr + 1]] != 0)
            f[v[++dr]]++, fsize += (f[v[dr]] == 1);
        cnt += dr - i + 1;
        f[v[i]]--;
        if (f[v[i]] == 0)
            fsize--;
    }
    return cnt;
}

signed main()
{
    ifstream cin("secv5.in");
    ofstream cout("secv5.out");
    int l, u, p = 0, a, i, st, dr, cnt = 0, nr = 0;
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n >> l >> u;
    for (i = 1; i <= n; i++)
    {
        cin >> v[i];
        if (orient[v[i]] == 0)
            orient[v[i]] = v[i] = ++nr;
        else
            v[i] = orient[v[i]];
    }
    cout << rez(u) - rez(l - 1);
    return 0;
}