Cod sursa(job #1566354)

Utilizator ericptsStavarache Petru Eric ericpts Data 11 ianuarie 2016 23:36:32
Problema Secventa 5 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <fstream>
#include <iostream>
#include <unordered_map>

using namespace std;

typedef unsigned int uint;

unordered_map<uint, uint> H, L;

const uint MAX_N = (1 << 20) + 10;

uint lo[MAX_N],
    hi[MAX_N];

bool okl[MAX_N],
     okh[MAX_N];

uint v[MAX_N];

uint n;

uint l, h;

void dec(unordered_map<uint, uint> & H, const uint val) {
  H[val]--;
  if(H[val] == 0)
    H.erase(val);
}

int main() {
  ifstream in("secv5.in");
  in >> n >> l >> h;
  for(uint i = 1; i <= n; ++i)
    in >> v[i];
  in.close();

  L.reserve(n);
  H.reserve(n);
  
  uint low = 1;
  uint high = 1;

  for(uint i = 1; i <= n; ++i) {
    H[v[i]]++;
    L[v[i]]++;

    while((uint)H.size() > h) {
      dec(H, v[low++]);
    }
    hi[i] = low;


    while((uint)L.size() > l) {
      dec(L, v[high++]);
    }

    while(L[v[high]] > 1) {
      dec(L, v[high++]);
    }

    if((uint)L.size() == l)
      lo[i] = high;
    else
      lo[i] = -1;

  }

  long long sol = 0;

  for(uint i = 1; i <= n; ++i) {
    if(lo[i] != -1)
      sol += lo[i] - hi[i] + 1;
  }
  
  ofstream out("secv5.out");
  out << sol << "\n";
  out.close();
  return 0;
}