Cod sursa(job #2868664)

Utilizator tomaionutIDorando tomaionut Data 11 martie 2022 09:03:20
Problema Secventa 5 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("secv5.in");
ofstream fout("secv5.out");
int n, l, u, fr[1100000];
struct vrajeala
{
    int val, poz, newval;
}a[1100000];
bool cmp1(vrajeala o, vrajeala p)
{
    return o.val < p.val;
}
bool cmp2(vrajeala o, vrajeala p)
{
    return o.poz < p.poz;
}
long long Nr(int X)
{
    int cnt = 0, p = 1, i, x;
    long long sol = 0;
    for (i = 1; i <= n; i++)
    {
        x = a[i].newval;
        fr[x]++;
        if (fr[x] == 1)
            cnt++;
        while (cnt > X)
        {
            fr[a[p].newval]--;
            if (fr[a[p].newval] == 0)
                cnt--;
            p++;
        }
        sol += (i - p + 1);
    }
    return sol;
}
int main()
{
    int i;
    long long x, y;
    fin >> n >> l >> u;
    for (i = 1; i <= n; i++)
    {
        fin >> x;
        a[i].val = x;
        a[i].poz = i;
    }
    sort (a + 1, a + n + 1, cmp1);
    a[1].newval = x = 1;
    for (i = 2; i <= n; i++)
        if (a[i].val == a[i - 1].val)
        a[i].newval = x;
    else
        a[i].newval = ++x;

    sort (a + 1, a + n + 1, cmp2);

    x = Nr(u);
    for (i = 1; i <= (1 << 20); i++)
        fr[i] = 0;
    y = Nr(l - 1);
    fout << x - y << "\n";

    return 0;
}