Cod sursa(job #1705554)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 20 mai 2016 19:27:32
Problema Secventa 5 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.59 kb
#include <bits/stdc++.h>

const int DIM = 1 << 20;
using namespace std;

unsigned int v[DIM]; int n, l, u;

class nrx_reader {
    private:
        FILE *input_file;
        static const int SIZE = 1 << 12;
        char buffer[SIZE]; int cursor, sign;
        long long fractional_part;

        inline void advance( void ) {
            if( ++cursor == SIZE ) {
                cursor = 0;
                fread( buffer, SIZE, 1, input_file );
            } return;
        }

        inline char current( void ) {
            return buffer[cursor];
        }
    public:
        ~nrx_reader() { fclose( input_file ); }
         nrx_reader( const char *file_name, const char *file_type ) {
            input_file = fopen( file_name, file_type ); cursor = 0;
            fread( buffer, SIZE, 1, input_file );
        }

        template <class type>
        nrx_reader &operator >>( type &value ) {
            value = 0; sign = 1; fractional_part = 10;

            while( current() < '0' || current() > '9' ) {
                if( current() == '-' )
                    sign = -1;
                advance();
            }

            while( current() >= '0' && current() <= '9' ) {
                value = value * 10 + ( current() - '0' ) * sign;
                advance();
            }

            if( current() == '.' ) {
                advance();

                while( current() >= '0' && current() <= '9' ) {
                    value += ( current() - '0' ) * 1.0 * sign / fractional_part;
                    advance();
                }
            }

            return *this;
        }

        nrx_reader &operator >>( char *str ) {
            while( current() == ' ' || current() == '\n' )
                advance();

            while( current() != ' ' && current() != '\n' ) {
                *str = current() - '0'; str ++;
                advance();
            }

            return *this;
        }

        void get_line( char *str ) {
            while( current() == '\n' )
                advance();

            while( current() != '\n' ) {
                *str = current() - '0'; str ++;
                advance();
            }

            return;
        }

        void reset_file( void ) {
            fseek( input_file, 0, SEEK_SET );
            return;
        }
} input_file( "secv5.in", "r" );
ofstream output_file( "secv5.out" );

template <class type>
class nrx_hash {
    private:
        static const int MOD = 90907; int hash_size = 0;
        struct hash_str{ type value; int cnt_value; };
        vector <hash_str> my_hash[MOD];
    public:
        int    get_size( void ) { return  hash_size; }
        bool empty_hash( void ) { return !hash_size; }

        void insert_hash( type hash_value ) {
            int position = hash_value % MOD;

            for( int it = 0; it < my_hash[position].size(); it ++ ) {
                hash_str current_hash = my_hash[position][it];

                if( current_hash.value == hash_value ) {
                    my_hash[position][it].cnt_value ++;
                    return;
                }
            }

            my_hash[position].push_back( {hash_value, 1} ); hash_size ++;
            return;
        }

        void erase_hash( type hash_value ) {
            int position = hash_value % MOD;

            for( int it = 0; it < my_hash[position].size(); it ++ ) {
                hash_str current_hash = my_hash[position][it];

                if( current_hash.value == hash_value ) {
                    my_hash[position][it].cnt_value --;

                    if( my_hash[position][it].cnt_value == 0 ) {
                        hash_size --;
                        my_hash[position].erase( my_hash[position].begin() + it );
                    }
                }
            }

            return;
        }

        void clear_hash( void ) {
            for( int i = 0; i < MOD; i ++ )
                my_hash[i].resize(0);
            hash_size = 0;
            return;
        }
}; nrx_hash <unsigned int> my_hash;

long long solve( int cnt ) {
    my_hash.clear_hash(); long long answer = 0;

    for( int left = 0, right = 0; right < n; right ++ ) {
        my_hash.insert_hash( v[right] );

        while( my_hash.get_size() > cnt )
            my_hash.erase_hash( v[left++] );

        answer += right - left + 1;
    }

    return answer;
}

int main() {

    input_file >> n >> l >> u;

    for( int i = 0; i < n; i ++ )
        input_file >> v[i];

    output_file << solve(u) - solve(l - 1) << "\n";

    return 0;
}