Cod sursa(job #2695206)

Utilizator cristi_macoveiMacovei Cristian cristi_macovei Data 12 ianuarie 2021 08:43:02
Problema Secventa 5 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <vector>

const int NMAX = (1 << 20);

template <size_t MOD>
class HashMap {
private:
  std::vector<std::pair<unsigned int, int>> data[1 + MOD];

  int hash(int a) {
    return a % MOD;
  }

public:
  int* operator [] (unsigned int key) {
    int hashedKey = hash(key);

    for (auto& pair: data[hashedKey]) {
      if (pair.first == key)
        return &pair.second;
    }

    data[hashedKey].emplace_back(key, 0);
    return &data[hashedKey].back().second;
  }
};

int n, qLeft, qRight;

unsigned int a[1 + NMAX];

HashMap<666013> hashmap;

void read() {
  std::ifstream in("secv5.in");

  in >> n >> qLeft >> qRight;

  for (int i = 1; i <= n; ++i)
    in >> a[i];
}

int64_t countSeq(int target) {
  int left = 1, cnt = 0;
  int64_t ans = 0;

  for (int i = 1; i <= n; ++i) {
    ++(*hashmap[a[i]]);
    if (*hashmap[a[i]] == 1)
      ++cnt;

    while (cnt > target) {
      --(*hashmap[a[left]]);
      if (*hashmap[a[left]] == 0)
        --cnt;
      ++left;
    }

    ans += (int64_t)(i - left + 1);
  }

  return ans;
}

int main() {
  read();

  int64_t left = countSeq(qLeft - 1);

  for (int i = 1; i <= n; ++i)
    *hashmap[a[i]] = 0;

  int64_t right = countSeq(qRight);

  std::ofstream out("secv5.out");
  out << right - left << '\n';

  return 0;
}