Cod sursa(job #2872154)

Utilizator StanCatalinStanCatalin StanCatalin Data 16 martie 2022 13:27:13
Problema Hashuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.81 kb
#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

ifstream in("hashuri.in");
ofstream out("hashuri.out");

const int p = 1500041;
const int m = 250007;
const int MAX_LOOP = 23;

long long int a1, b1;

inline int calculate_hash_function_1(long long int x) {
    return ((long long)(a1 * x + b1) % p) % m;
}

long long int a2, b2;

inline int calculate_hash_function_2(long long int x) {
    return ((long long )(a2 * x + b2) % p) % m;
}

long long int a3, b3;

inline int calculate_hash_function_3(long long int x) {
    return ((long long)(a3 * x + b3) % p) % m;
}

long long int a4, b4;

inline int calculate_hash_function_4(long long int x) {
    return ((long long )(a4 * x + b4) % p) % m;
}


long long int T1[m], T2[m], T3[m], T4[m];

inline bool Search(long long int x) {
    return (T1[calculate_hash_function_1(x)] == x || T2[calculate_hash_function_2(x)] == x || T3[calculate_hash_function_3(x)] == x || T4[calculate_hash_function_4(x)] == x);
}

//void Rehash() {
//    a1 = rand()%p;
//    b1 = rand()%p;
//    a2 = rand()%p;
//    b2 = rand()%p;
//    for (int i = 0; i < m; ++i) {
//
//    }
//}


void Insert(long long int x, int it) {
//    if (it == MAX_LOOP) {
//        Rehash();
//        it = 0;
//    }
    long long int y, z, w, l;
    int h1 = calculate_hash_function_1(x);
    if (T1[h1] == 0 || T1[h1] == x) {
        T1[h1] = x;
        return;
    } else {
        y = T1[h1];
        T1[h1] = x;
    }
    int h2 = calculate_hash_function_2(y);
    if (T2[h2] == 0 || T2[h2] == y) {
        T2[h2] = y;
        return;
    } else {
        z = T2[h2];
        T2[h2] = y;
    }
    int h3 = calculate_hash_function_3(z);
    if (T3[h3] == 0 || T3[h3] == z) {
        T3[h3] = z;
        return;
    } else {
        w = T3[h3];
        T3[h3] = z;
    }
    int h4 = calculate_hash_function_4(w);
    if (T4[h4] == 0 || T4[h4] == w) {
        T4[h4] = w;
        return;
    } else {
        l = T4[h4];
        T4[h4] = w;
    }
    Insert(l, it++);
}

void Delete(long long int x) {
    int h1 = calculate_hash_function_1(x);
    int h2 = calculate_hash_function_2(x);
    int h3 = calculate_hash_function_3(x);
    int h4 = calculate_hash_function_4(x);
    if (T1[h1] == x) {
        T1[h1] = 0;
    }
    if (T2[h1] == x) {
        T2[h2] = 0;
    }
    if (T4[h4] == x) {
        T4[h4] = 0;
    }
    if (T4[h4] == x) {
        T4[h4] = 0;
    }
}

int main() {

    srand(time(nullptr));
    a1 = rand()%p;
    b1 = rand()%p;
    a2 = rand()%p;
    b2 = rand()%p;
    a3 = rand()%p;
    b3 = rand()%p;
    a4 = rand()%p;
    b4 = rand()%p;

    long long int n, op, x;
    in >> n;
    while (n--) {
        in >> op >> x;
        if (op == 1) {
            Insert(x, 0);
        } else if (op == 2) {
            Delete(x);
        } else {
            out << Search(x) << "\n";
        }
    }


    return 0;
}