Cod sursa(job #2452863)

Utilizator IulianOleniucIulian Oleniuc IulianOleniuc Data 1 septembrie 2019 15:31:03
Problema Secventa 5 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.9 kb
#include <bits/stdc++.h>
using namespace std;

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

class HashTable {
  private:
    static const int MOD = 666013;
    vector<vector<pair<unsigned int, int>>> table;

    inline int hash(unsigned int val) {
        return val % MOD;
    }

  public:
    HashTable()
        : table(MOD) { }

    void insert(unsigned int val) {
        int h = hash(val);
        for (auto& it : table[h])
            if (it.first == val) {
                it.second++;
                return;
            }
        table[h].emplace_back(val, 1);
    }

    void remove(unsigned int val) {
        int h = hash(val);
        for (int i = 0; i < (int) table[h].size(); i++)
            if (table[h][i].first == val) {
                if (--table[h][i].second == 0) {
                    for (int j = i + 1; j < (int) table[h].size(); j++)
                        table[h][j - 1] = table[h][j];
                    table[h].pop_back();
                }
                return;
            }
    }

    int count(unsigned int val) {
        int h = hash(val);
        for (auto it : table[h])
            if (it.first == val)
                return it.second;
        return 0;
    }
};

int main() {
    int n, x, y;
    fin >> n >> x >> y;

    vector<unsigned int> v(n);
    for (int i = 0; i < n; i++)
        fin >> v[i];

    auto solve = [&](int max) {
        long long int sol = 0;
        HashTable table; int cnt = 0;
        for (int lo = 0, hi = 0; hi < n; hi++) {
            table.insert(v[hi]);
            if (table.count(v[hi]) == 1) {
                cnt++;
                while (cnt > max) {
                    table.remove(v[lo++]);
                    if (!table.count(v[lo - 1]))
                        cnt--;
                }
            }
            sol += hi - lo + 1;
        }
        return sol;
    };

    fout << solve(y) - solve(x - 1) << '\n';
    fout.close();
    return 0;
}