Pagini recente » Borderou de evaluare (job #1963575) | Borderou de evaluare (job #1125894) | Borderou de evaluare (job #1113615) | Cod sursa (job #3128513) | Cod sursa (job #2871704)
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
ifstream in("hashuri.in");
ofstream out("hashuri.out");
const int p = 1000003;
const int m = 2e5;
int a1, b1;
int calculate_hash_function_1(int x) {
return ((a1 * x + b1) % p) % m;
}
int a2, b2;
int calculate_hash_function_2(int x) {
return ((a2 * x + b2) % p) % m;
}
int T1[m], T2[m];
bool Search(int x) {
return (T1[calculate_hash_function_1(x)] == x || T2[calculate_hash_function_2(x)] == x);
}
void Insert(int x) {
int y;
do {
if (T1[calculate_hash_function_1(x)] == 0) {
T1[calculate_hash_function_1(x)] = x;
} else {
y = T1[calculate_hash_function_1(x)];
T1[calculate_hash_function_1(x)] = x;
}
x = y;
} while (T1[calculate_hash_function_1(x)] != 0);
}
void Delete(int x) {
if (T1[calculate_hash_function_1(x)] == x) {
T1[calculate_hash_function_1(x)] = 0;
} else if (T2[calculate_hash_function_2(x)] == x) {
T2[calculate_hash_function_2(x)] = 0;
}
}
int main() {
srand(time(nullptr));
a1 = rand()%p;
b1 = rand()%p;
a2 = rand()%p;
b2 = rand()%p;
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;
}