Pagini recente » Cod sursa (job #855786) | Cod sursa (job #331028) | Cod sursa (job #279875) | Monitorul de evaluare | Cod sursa (job #797551)
Cod sursa(job #797551)
#include <fstream>
#include <vector>
using namespace std;
const int K = 1046527, inf = 0x3f3f3f3f;
struct HashTable{
int hash[K];
inline int h(int k, int i){
return (k + i * (i + 1)) % K;
}
inline bool can_write(int x, int i){
int q = h(x, i);
return !hash[q] || hash[q] == inf || hash[q] == x;
}
inline bool move_on(int x, int i){
int q = h(x, i);
return hash[q] && hash[q] != x;
}
bool find(int x){
int i = 0;
while (move_on(x, i))
i++;
return hash[h(x, i)] == x;
}
void insert(int x){
int i = 0;
while (!can_write(x, i))
i++;
hash[h(x, i)] = x;
}
void erase(int x){
int i = 0;
while (move_on(x, i))
i++;
if (hash[h(x, i)] == x)
hash[h(x, i)] = inf;
}
};
HashTable H;
ifstream in("hashuri.in");
ofstream out("hashuri.out");
int main(){
int t, q, x;
in >> t;
while (t--){
in >> q >> x;
if (q == 1)
H.insert(x);
if (q == 2)
H.erase(x);
if (q == 3)
out << H.find(x) << "\n";
}
return 0;
}