Cod sursa(job #2881467)

Utilizator tudorcomanTudor Coman tudorcoman Data 30 martie 2022 15:24:32
Problema Secventa 5 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.45 kb

#include <fstream>
#include <unordered_set>
#include <iostream>
#include <vector>

using namespace std;

const int maxN = 1 << 20;
int v[maxN + 1];

class Hash {
private:
  const static int mod = 100007;
  vector<int> table[mod];
  int dim; 
public:
  Hash() { }
  int search(int value) const {
    int rem = value % mod;
    for(int i = 0; i < (int)table[rem].size(); ++ i)
      if(table[rem][i] == value)
        return i;
    return -1;
  }
 
  inline void insert(int value) {
    if(search(value) == -1) dim ++;
    table[value % mod].push_back(value);
  }
 
  inline void erase(int value) {
    int rem = value % mod;
    int s = table[rem].size() - 1;
    int pos = this->search(value);
    if(pos != -1) {
      swap(table[rem][pos], table[rem][s]);
      table[rem].pop_back();
    }
    if(search(value) == -1) dim --;
  }

  inline int size() {
      return dim;
  }
} hashuri[2];

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

    int N, l, u;
    in >> N >> l >> u;
    
    int a, b; a = b = 0;
    int st = 0;

    for (int i = 0; i < N; i ++) {
        in >> v[i];
        hashuri[0].insert(v[i]);
        while(hashuri[0].size() > u)
            hashuri[0].erase(v[st ++]);
        a += (i - st + 1);
    }

    st = 0;
    for (int i = 0; i < N; i ++) {
        hashuri[1].insert(v[i]);
        while (hashuri[1].size() >= l)
            hashuri[1].erase(v[st ++]);
        b += (i - st + 1);
    }


    out << a - b << '\n';
    return 0;
}