Cod sursa(job #2659523)

Utilizator YusyBossFares Yusuf YusyBoss Data 16 octombrie 2020 22:29:22
Problema Secventa 5 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <fstream>
#include <vector>
#define MOD 666013
#define NMAX (1 << 20)

using namespace std;

ifstream cin ("secventa5.in");
ofstream cout ("secventa5.out");

unsigned int v[NMAX + 1];

struct s {
  int numar, frec;
};

vector <s> myhash[MOD];

int add(unsigned int x) {
  int i, n, nr;

  nr = (unsigned int) x % MOD;
  n = myhash[nr].size();
  i = 0;
  while (i < n && myhash[nr][i].numar != x)
    i++;
  if (i == n)
    myhash[nr].push_back({x, 1});
  else
    myhash[nr][i].frec++;
  return myhash[nr][i].frec;
}


void resethash() {
  int i;
  for (i = 0; i < MOD; i++)
    myhash[i].clear();
}

bool del(unsigned int x) {
  int nr, i, n;

  i = 0;
  nr = (unsigned int) x % MOD;
  n = myhash[nr].size();
  while (i < n && myhash[nr][i].numar != x)
    i++;
  if (i < n) {
    if (myhash[nr][i].frec == 1) {
      myhash[nr].erase(myhash[nr].begin() + i);
      return 0;
    }
    else
      myhash[nr][i].frec--;
  }
  return 1;
}

long long solve (int n, int k) {
  int i, j, nr, cnt;
  long long sol;
  i = j = cnt = sol = 0;

  resethash();

  while (j < n) {
    nr = add(v[j]);
    j++;
    cnt += (nr == 1);
    while (i < n && cnt > k) {
      nr = del(v[i]);
      cnt -= (nr == 0);
      i++;
    }
    sol = (long long)(sol + (long long)(j - i + 1));
  }
  return sol;
}

int main() {
  int i, l, u, n;
  cin >> n >> l >> u;
  for (i = 0; i < n; i++)
    cin >> v[i];
  cout << (long long)solve(n, u) - solve(n, l - 1);
  return 0;
}