Cod sursa(job #2694448)

Utilizator cristi_macoveiMacovei Cristian cristi_macovei Data 9 ianuarie 2021 11:37:08
Problema Secventa 5 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.09 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];
}

int countSeq(int target) {
  if (target == 0)
    return 0;

  hashmap.clear();

  int left = 1;
  int cnt = 1, ans = 0;
  hashmap[a[1]] = 1;
  for (int i = 2; i <= n; ++i) {
    if (hashmap.count(a[i]) == 0) {
      if (cnt < target) {
        ++cnt;
        hashmap[a[i]] = 1;
      } else {
        while (hashmap[a[left]] != 1) {
          --hashmap[a[left]];
          ans += i - left;
          ++left;
        }
        --hashmap[a[left]];
        ans += i - left;
        ++left;
        hashmap[a[i]] = 1;
      }
    } else {
      ++hashmap[a[i]];
    }
  }

  while (left <= n) {
    ans += n - left + 1;
    ++left;
  }

  return ans;
}

int main() {
  read();

  int left = countSeq(qLeft - 1);
  int right = countSeq(qRight);

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

  return 0;
}