Cod sursa(job #2618719)

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

using namespace std;

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

const int nmax = 1048576, mod = 100003;
int n, l, u, v[nmax + 5];
vector <pair <int, int> > h[mod + 5];

int Find(int x)
{
    int m = x % mod;
    for (int i = 0; i < h[m].size(); ++i)
    {
        if (h[m][i].first == x) return i;
    }
    return -1;
}

void Add(int x)
{
    int pos = Find(x);
    int m = x % mod;
    if (pos == -1)
    {
        h[m].push_back({x, 1});
    }
    else
    {
        h[m][pos].second++;
    }
}

void Remove(int x)
{
    int pos = Find(x);
    int m = x % mod;
    --h[m][pos].second;
    if (h[m][pos].second == 0)
    {
        swap(h[m][pos], h[m][h[m].size() - 1]);
        h[m].pop_back();
    }
}


inline long long f(int x)
{
    int st = 1;
    int contor = 0;
    long long ans = 0;
    for (int dr = 1; dr <= n; ++dr)
    {
        if (Find(v[dr]) == -1) ++contor;
        Add(v[dr]);
        while (contor > x && st <= dr)
        {
            Remove(v[st]);
            if (Find(v[st]) == -1) --contor;
            ++st;
        }
        ans = 1LL * ans + 1LL * (dr - st + 1);
    }
    while (st <= n)
    {
        Remove(v[st]);
        ++st;
    }
    return ans;
}

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