Cod sursa(job #2473483)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 13 octombrie 2019 18:07:08
Problema Secventa 5 Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.54 kb
#include <bits/stdc++.h>
#define NMAX 2097152
using namespace std;
ifstream fin("secv5.in");
ofstream fout("secv5.out");
const unsigned int p = 666013;
unsigned int n, l, u, v[NMAX + 5];
vector <pair <int, int> > h[p + 5];
void Adauga(unsigned int x, unsigned int &k)
{
    int ha = x % p;
    for (int i = 0; i < h[ha].size(); ++i)
    {
        if (h[ha][i].first == x)
        {
            h[ha][i].second++;
            return;
        }
    }
    h[ha].push_back({x, 1});
    ++k;
}
void Sterge(unsigned int x, unsigned int &k)
{
    int ha = x % p;
    for (int i = 0; i < h[ha].size(); ++i)
    {
        if (h[ha][i].first == x)
        {
            h[ha][i].second--;
            if (h[ha][i].second == 0)
            {
                --k;
                swap(h[ha][i], h[ha][h[ha].size()]);
                h[ha].pop_back();
                return;
            }
        }
    }
}

long long f(unsigned int x)
{
    for (int i = 0; i < p; ++i)
        h[i].clear();
    if (x == 0)
        return 0;
    unsigned int contor = 0;
    int st = 1;
    long long ans = 0;
    for (int i = 1; i <= n; ++i)
    {
        Adauga(v[i], contor);
        while (contor > x)
        {
            Sterge(v[st], contor);
            ++st;
        }
        ans = ans + 1LL * i - st + 1;
    }
    return ans;
}

int main()
{
    fin >> n >> l >> u;
    for (int i = 1; i <= n; ++i)
    {
        fin >> v[i];
    }
    fout << f(u) - f(l - 1) << "\n";
    fin.close();
    fout.close();
    return 0;
}