Cod sursa(job #2655854)

Utilizator Razvan48Capatina Razvan Nicolae Razvan48 Data 5 octombrie 2020 19:25:31
Problema Secventa 5 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.12 kb
#include <fstream>
#include <vector>

using namespace std;

const int NMAX = 1 << 20;
const int MODULO = 666013;

template <typename K, typename V>
class HashMap
{

public:
  HashMap() = default;

  void add(K key, V value)
  {
    auto pos = m_find(key);

    if (pos.second == -1)
    {
      hash_map[pos.first].push_back(make_pair(key, value));
    }
    else
    {
      hash_map[pos.first][pos.second].second = value;
    }
  }

  void remove(K key)
  {
    auto pos = m_find(key);

    if (pos.second == -1) return;

    hash_map[pos.first][pos.second] = hash_map[pos.first][hash_map[pos.first].size() - 1];
    hash_map[pos.first].pop_back();
  }

  V get(K key)
  {
    auto pos = m_find(key);

    if (pos.second == -1)
    {
      return -1;
    }
    else
    {
      return hash_map[pos.first][pos.second].second;
    }
  }

private:
  vector<pair<K, V>> hash_map[MODULO];

  pair<int, int> m_find(K key)
  {
    K hashed_key = key % MODULO;

    for (int i = 0; i < hash_map[hashed_key].size(); i++)
    {
      if (hash_map[hashed_key][i].first == key)
      {
        return make_pair(hashed_key, i);
      }
    }

    return make_pair(hashed_key, -1);
  }
};

HashMap<unsigned int, int> hm;

int n;

unsigned int v[1 + NMAX];

long long int calcSecv(int maxim)
{
  int count = 0;
  long long int nrSecv = 0;

  for (int st = 1, dr = 1; dr <= n; dr++)
  {
    hm.add(v[dr], hm.get(v[dr]) + 1);

    if (hm.get(v[dr]) == 1)
    {
      count++;
    }

    while (count > maxim)
    {
      hm.add(v[st], hm.get(v[st]) - 1);
      if (hm.get(v[st]) == 0)
      {
        count--;
      }
      st++;
    }

    nrSecv += dr - st + 1;
  }

  return nrSecv;

}

int main()
{
  ifstream in("secv5.in");
  ofstream out("secv5.out");
  int l, u;

  in >> n >> l >> u;
  for (int i = 1; i <= n; i++)
  {
    in >> v[i];
    hm.add(v[i], 0);
  }

  long long calcU, calcL;

  calcU = calcSecv(u);

  for (int i = 1; i <= n; i++)
  {
    hm.add(v[i], 0);
  }

  calcL = calcSecv(l - 1);

  out << calcU - calcL;

  return 0;
}