Cod sursa(job #1615800)

Utilizator StefanMudragMudrag Stefan StefanMudrag Data 26 februarie 2016 21:18:59
Problema Secventa 5 Scor 80
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I Marime 1.76 kb
#include<iostream>
#include<fstream>
#include<cstring>
#include<stdlib.h>
#define NMAX 1<<20
#define ctrb 6883

using namespace std;
struct hash_table
{
    unsigned int val[4];

    int index[4];
};

void solve();
unsigned int read_element(ifstream &fin) ;
int hash_function (unsigned int value , int poz , int n);
long long secvente (int *A , int n ,int limit );

hash_table H[2][NMAX];
int main()
{


    solve();



    return 0 ;
}
long long secvente ( int *A , int n ,int limit )
{
   int *Viz  = (int*)calloc(n ,sizeof(int));

    long long rez=0;
   for ( int nr=0,s=0 , i = 0 ; i<n ; ++i )
   {
       if (++Viz[A[i]]==1 ) ++nr ;

       while (nr > limit )
       {
           if (--Viz[A[s]]==0) --nr;
           ++s;
       }
      rez+= i-s+1;

   }

   free (Viz);

    return rez;

}
int hash_function(unsigned int value , int pz , int n )
{
   int poz = (value * ctrb) % n ;
  ;
   int i =0, j=0 ;

   for ( i = 0 ; i < 4 && H[0][poz].val[i] ; ++i )

      if (H[0][poz].val[i] == value ) return H[0][poz].index[i];



   for ( j = 0 ; j < 4 && H[1][poz].val[j] ; ++j )

        if (H[1][poz].val[j] == value ) return H[1][poz].index[j];

   if ( i > j ) H[1][poz].val[j] = value , H[1][poz].index[j]=pz;

   else H[0][poz].val[i] = value , H[0][poz].index[i]=pz;
   return pz ;

}

unsigned int read_element(ifstream &fin )
{
   unsigned int x ;

   fin>> x ;

   return x ;

}
void solve()
{
     ifstream fin("secv5.in");
     ofstream fout("secv5.out");

     int N , L , U ;
     long long rezultat = 0 ;
     int *A ;
     fin >> N>>L>>U ;

     A = new int[N];


     for ( int i = 0 ; i  <N ; ++i )
     {
        A[i] = hash_function(read_element(fin), i , N ) ;
     }

    fout << secvente(A,N,U)-secvente(A,N,L-1);

    delete [] A;

    fout.close();
    fin.close();


}