Cod sursa(job #1173335)

Utilizator mihai995mihai995 mihai995 Data 19 aprilie 2014 11:34:59
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.89 kb
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;

const int N = 1 << 20, M = 1048583, nil = -1;
const double maxLoadFactor = 0.6;

class HashTable{
    int* hash;
    int size, cap, mod;

    int lookup(int x){
        int poz = x % cap, step = 1 + x % mod;

        while ( hash[poz] && hash[poz] != x )
            poz = (poz + step) % cap;

        return poz;
    }
public:
    HashTable(int n, int m){
        cap = n;
        mod = m;
        size = 0;
        hash = (int*)calloc(cap, sizeof(int));
    }

    int insert(int x){
        int poz = lookup(x);
        if ( hash[poz] != x ){
            hash[poz] = x;
            size++;
        }
        return poz;
    }

    inline int getCap(){
        return cap;
    }

    ~HashTable(){
        free(hash);
    }
};

class DistinctCounter{
    int cnt[M], nr;

public:
    DistinctCounter(){
        memset(cnt, 0, sizeof(cnt));
        nr = 0;
    }

    inline void insert(int x){
        if (cnt[x] == 0)
            ++nr;
        ++cnt[x];
    }

    inline void erase(int x){
        --cnt[x];
        if (cnt[x] == 0)
            --nr;
    }

    inline int size(){
        return nr;
    }
};

int v[N], cnt[M], n, L, U;
HashTable H(M, 666013);
DistinctCounter A, B;

int main(){
    ifstream in("secv5.in");
    int x;

    in >> n >> L >> U;
    for (int i = 1 ; i <= n ; i++){
        in >> x;
        v[i] = H.insert(x);
    }
    in.close();

    long long ans = 0;
    for (int i = 1, st = 1, dr = 1 ; i <= n ; i++){
        while ( st <= n + 1 && A.size() < L )
            A.insert( v[st++] );
        while ( dr <= n && B.size() <= U )
            B.insert( v[dr++] );

        ans += dr - st + 1;
        A.erase( v[i] );
        B.erase( v[i] );
    }

    ofstream out("secv5.out");
    out << ans << '\n';

    return 0;
}