Cod sursa(job #2924539)

Utilizator AnSeDraAndrei Sebastian Dragulescu AnSeDra Data 4 octombrie 2022 19:45:20
Problema Secventa 5 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.22 kb
#include <fstream>
#include <vector>

using namespace std;

#define Nmax 1048576
#define M 666013

int v[Nmax];
vector<pair<int, int>> Hash[M];

int pozitie(int x)
{
    int cod, i;
    cod = x % M;

    for(i = 0; i < Hash[cod].size(); i++)
    {
        if(x == Hash[cod][i].first)
        {
            return i;
        }
    }

    return -1;
}

void adauga(int x)
{
    int cod, i;
    cod = x % M;

    if(pozitie(x) != -1)
    {
        i = pozitie(x);
        Hash[cod][i].second++;
    }
    else
    {
        Hash[cod].push_back({x, 1});
    }
}

void sterge(int x)
{
    int cod, i;
    cod = x % M;

    if(pozitie(x) != -1)
    {
        i = pozitie(x);
        if(Hash[cod][i].second > 1)
        {
            Hash[cod][i].second--;
        }
        else
        {
            Hash[cod].erase(Hash[cod].begin() + i);
        }
    }
}

int cate(int x)
{
    int cod, i;
    cod = x % M;

    i = pozitie(x);
    if(i != -1)
    {
        return Hash[cod][i].second;
    }
    else
    {
        return 0;
    }
}

int nrSecv(int nr, int n)
{
    int i, j, lastj;
    long long s, cnt;

    i = 0;
    lastj = 0;
    cnt = 0;
    s = 0;

    for(j = 0; i <= j && j < n; j++)
    {
        if(pozitie(v[j]) == -1)
        {
            cnt++;
        }
        adauga(v[j]);

        if(cnt > nr)
        {
            s += (j - i + 1) * (j - i) / 2 - (lastj - i) * (lastj - i + 1) / 2;

            lastj = j;
            while(i <= j && cnt > nr)
            {
                if(cate(v[i]) == 1)
                {
                    cnt--;
                }
                sterge(v[i]);
                i++;
            }
        }
    }

    s += (j - i + 1) * (j - i) / 2 - (lastj - i) * (lastj - i + 1) / 2;

    while(i < n)
    {
        sterge(v[i]);
        i++;
    }

    return s;
}

int main()
{
    ifstream fin("secv5.in");
    ofstream fout("secv5.out");

    int n, l, u, i, j;
    long long sol1, sol2, cnt;

    fin >> n >> l >> u;

    for(i = 0; i < n; i++)
    {
        fin >> v[i];
    }

    sol1 = nrSecv(l - 1, n);
    sol2 = nrSecv(u, n);

    fout << sol2 - sol1;
    return 0;
}