Cod sursa(job #10799)

Utilizator ZeusCatalin Tiseanu Zeus Data 29 ianuarie 2007 15:33:51
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.02 kb

using namespace std;

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>

#define magic 306013

#define ui unsigned int

int N, L, U, cnt[ 1<<20 + 1 ], nr, CURR;
ui A[1<<20+2];

ui hash[magic][21]; 
int val[magic][21];

inline int hash_it( ui x )
{
     int key = x % magic; 
     
     for( int step = 2; step; step++ )
     {
         for( int j = 1; j <= hash[key][0]; j++ )
              if( hash[key][j] == x )
                  return val[key][j];
         
         if( hash[key][0] == 20 )
         {
              key = ( key + 3456*step ) % magic;
              continue;    
         }
         
         hash[key][ ++hash[key][0] ] = x;
         val[key][ hash[key][0] ] = ++CURR;
         
         break;
     }
     
     return CURR;          
}

long long doit( int LEN )
{
     long long ret = 0;
     
     int r = 0, dist = 0;
     
     bool can;
     
     memset( cnt, 0, sizeof( cnt ) );

     for( int i = 1; i <= N; i++ )
     {
         while( r + 1 <= N  )
         {
                if( !(cnt[ A[r+1] ] || dist + 1 <= LEN ) )
                   break;
                
                dist += !cnt[ A[r+1] ];
                    
                cnt[ A[r+1] ]++;  
                
                ++r;    
         }       
         
         cnt[ A[i] ]--;
         
         if( !cnt[A[i]] )
             dist--;
         
         ret += (long long)( r ) - i + 1;
     }
     
     return ret;
}

int main()
{
    freopen("secv5.in", "r", stdin);
    freopen("secv5.out", "w", stdout);
    
    char buf[12], *p;
    
    int key;
    
    scanf("%d %d %d \n", &N, &L, &U);
    for( int i = 1; i <= N; i++ )
    {
         gets( buf );
         for( p = buf; *p != '\0'; p++ )
              A[i] = A[i] * 10 + (*p) - '0';        
    }
    
    for( int i = 1; i <= N; i++ )
         A[i] = hash_it( A[i] );     
    
    cout << doit( U ) - doit( L - 1 ) << endl;
    
    return 0;
}