Pagini recente » Cod sursa (job #2174908) | Cod sursa (job #2858394) | Cod sursa (job #451136) | Cod sursa (job #2559196) | Cod sursa (job #2872183)
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <fstream>
using namespace std;
ifstream in("hashuri.in");
ofstream out("hashuri.out");
//
//const int p1 = 1500041;
//const int p2 = 1502309;
const int m = 3e6;
const long long p1=872742023,p2=489174503;
const long long a1=324892358,b1=728741242;
const long long a2=817247897,b2=682739174;
//long long int a1, b1;
int calculate_hash_function_1(long long int x) {
return ((long long)(a1 * x + b1) % p1) % m;
}
//long long int a2, b2;
int calculate_hash_function_2(long long int x) {
return ((long long )(a2 * x + b2) % p2) % m;
}
long long int T1[m], T2[m];
bool Search(long long int x) {
return (T1[calculate_hash_function_1(x)] == x || T2[calculate_hash_function_2(x)] == x);
}
void Insert(long long int x){
if(Search(x))
return;
int hash1=calculate_hash_function_1(x),hash2=calculate_hash_function_2(x);
while(true){
if(T1[hash1]==0){
T1[hash1]=x;
return;
}
swap(x,T1[hash1]);
hash1=calculate_hash_function_1(x);
hash2=calculate_hash_function_2(x);
if(T2[hash2]==0){
T2[hash2]=x;
return;
}
swap(x,T2[hash2]);
hash1=calculate_hash_function_1(x);
hash2=calculate_hash_function_2(x);
}
}
//
//void Insert(long long int x) {
// long long int y, z;
// 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;
// }
// Insert(z);
//}
void Delete(long long int x) {
int h1 = calculate_hash_function_1(x);
int h2 = calculate_hash_function_2(x);
if (T1[h1] == x) {
T1[h1] = 0;
}
if (T2[h1] == x) {
T2[h2] = 0;
}
}
int main() {
// srand(time(nullptr));
// a1 = rand()%p1;
// b1 = rand()%p1;
// a2 = rand()%p2;
// b2 = rand()%p2;
long long int n, op, x;
in >> n;
while (n--) {
in >> op >> x;
if (op == 1) {
Insert(x);
} else if (op == 2) {
Delete(x);
} else {
out << Search(x) << "\n";
}
}
return 0;
}