Cod sursa(job #2694686)

Utilizator cristi_macoveiMacovei Cristian cristi_macovei Data 10 ianuarie 2021 14:02:46
Problema Secventa 5 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <iostream>
#include <fstream>
#include <unordered_map>

const int NMAX = (1 << 20);

int n, qLeft, qRight;

int a[1 + NMAX];

std::unordered_map<int, int> 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 += 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;

  return 0;
}