Cod sursa(job #2618715)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 25 mai 2020 20:06:09
Problema Secventa 5 Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.89 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 1048576, mod = 666013;
int n, a, b, v[nmax + 5], contor, st;
vector <pair <int, int> > h[mod];
unsigned long long ans;

bool Adauga(int x)
{
    int m = x % mod;
    for (int i = 0; i < h[m].size(); ++i)
    {
        if (h[m][i].first == x)
        {
            ++h[m][i].second;
            return false;
        }
    }
    h[m].push_back({x, 1});
    return true;
}

bool Sterge(int x)
{
    int m = x % mod;
    for (int i = 0; i < h[m].size(); ++i)
    {
        if (h[m][i].first == x)
        {
            if (h[m][i].second == 1)
            {
                swap(h[m][i], h[m][h[m].size() - 1]);
                h[m].pop_back();
                return true;
            }
            else
            {
                h[m][i].second--;
                return false;
            }
        }
    }
}

int main()
{
    fin >> n >> a >> b;
    st = 1;
    contor = 0;
    for (int i = 1; i <= n; ++i)
    {
        fin >> v[i];
        if (Adauga(v[i]) == true)
        {
            ++contor;
            while (contor > b)
            {
                if (Sterge(v[st]) == true)
                {
                    --contor;
                }
                ++st;
            }
        }
        ans += (i - st + 1);
    }
    while (st <= n) Sterge(v[st++]);
    st = 1;
    contor = 0;
    for (int i = 1; i <= n; ++i)
    {
        if (Adauga(v[i]) == true)
        {
            ++contor;
            while (contor > a - 1)
            {
                if (Sterge(v[st]) == true)
                {
                    --contor;
                }
                ++st;
            }
        }
        ans -= (i - st + 1);
    }
    fout << ans;
    fin.close();
    fout.close();
    return 0;
}