Cod sursa(job #2246500)

Utilizator alex.cojocaruAlex Cojocaru alex.cojocaru Data 27 septembrie 2018 10:02:59
Problema Secventa 5 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.43 kb
#include <iostream>
#include <stdio.h>
#include <vector>

#define MOD 666013
#define NMAX 1100000

using namespace std;

struct elem {
  long long val ;
  long long frecv ;
};

vector <elem> has [ MOD + 1 ] ;

long long v [ NMAX + 2 ] ;

long long H (unsigned int x ) { return x % MOD ; }

void add (long long x ) {
  elem x2 ;
  x2.val = x ;
  x2.frecv = 1 ;
  has[H(x)].push_back(x2) ;
}

void update (long long x) {
  long long i = H(x) ;
  vector <elem>::iterator j ;
  for (j = has[i].begin() ; j != has[i].end() && (*j).val != x ; j++ ) ;
  if (j != has[i].end() )
  (*j) = {(*j).val, (*j).frecv+1 } ;
}

void delet (long long x ) {
  long long i = H (x) ;
  vector <elem>::iterator j ;
  for (j = has[i].begin() ; j != has[i].end() && (*j).val != x ; j++ ) ;
  if (j != has[i].end()) {
    if ((*j).frecv-1 == 0)
      has[i].erase(j) ;
    else
      (*j) = {(*j).val, (*j).frecv-1 } ;
  }
}

bool exist (long long x) { /// daca exista x cu restul i
  long long i = H (x) ;
  vector <elem>::iterator j ;
  for (j = has[i].begin() ; j != has[i].end() && (*j).val != x ; j++ );
  if (j != has[i].end())
    return true ;
  return false ;
}

int main() {

  FILE *fin, *fout ;
  fin = fopen ("secv5.in", "r" ) ;
  fout = fopen ("secv5.out", "w" ) ;
  long long i, j, n, u, l, count, sol1, sol2 ;
  fscanf (fin, "%lld%lld%lld", &n, &l, &u ) ;
  for (i = 0 ; i < n ; i++ )
    fscanf (fin, "%lld", &v[i] ) ;
  i = 0 ;
  j = 0 ;
  count = 0 ;
  sol1 = 0 ;
  while (j < n && i <= j ) {
    if (exist(v[j]) == false ) {
      if (count+1 <= u ) {
        add (v[j]) ;
        sol1 += (j - i + 1) ;
        j++;
        count++;
      }
      else {
        delet(v[i]) ;
        if (exist(v[i]) == false )
          count--;
        i++;
      }
    }
    else {
      update(v[j]) ;
      sol1 += (j - i + 1) ;
      j++;
    }
  }
  while (i < j ) {
    delet(v[i]) ;
    i++;
  }
  i = 0 ;
  j = 0 ;
  count = 0 ;
  sol2 = 0 ;
  while (j < n && i <= j ) {
    if (exist(v[j]) == false ) {
      if (count+1 < l ) {
        add (v[j]) ;
        sol2 += (j - i + 1 ) ;
        j++;
        count++;
      }
      else {
        delet(v[i]) ;
        if (exist(v[i]) == false && count > 0 )
          count--;
        i++;
      }
    }
    else {
      update(v[j]) ;
      sol2 += (j - i + 1) ;
      j++;
    }
  }
  fprintf (fout, "%lld", sol1-sol2 ) ;
  return 0;
}