Pagini recente » Cod sursa (job #1141111) | Cod sursa (job #2187523) | Cod sursa (job #156607) | Istoria paginii runda/oji_23022025 | Cod sursa (job #2434127)
#include <fstream>
#include <list>
#include <functional>
template <typename Tkey, typename Tvalue> struct info {
Tkey key;
Tvalue value;
};
inline int next_int() {
int n = 0;
char c = getchar_unlocked();
while (!('0' <= c && c <= '9')) {
c = getchar_unlocked();
}
while ('0' <= c && c <= '9') {
n = n * 10 + c - '0';
c = getchar_unlocked();
}
return n;
}
template <typename Tkey, typename Tvalue> class Hashtable {
private:
std::list<struct info<Tkey, Tvalue>> *H;
int size;
int capacity;
public:
Hashtable(int capacity) {
this->capacity = capacity;
size = 0;
H = new std::list<struct info<Tkey, Tvalue>> [capacity];
}
~Hashtable() {
H->clear();
delete [] H;
}
inline void put(Tkey key, Tvalue value) {
typename std::list<struct info<Tkey, Tvalue>>::iterator it;
int sw = 0;
std::hash<Tkey> hash;
int index = hash(key) % capacity;
for (it = H[index].begin() ; it != H[index].end() ; ++it) {
if (it->key == key) {
it->value = value;
sw = 1;
break;
}
}
if (sw == 0) {
info<Tkey, Tvalue> *node = new info<Tkey, Tvalue>;
node->key = key;
node->value = value;
H[index].push_back(*node);
delete node;
++size;
}
}
inline void remove(Tkey key) {
typename std::list<struct info<Tkey, Tvalue>>::iterator it;
std::hash<Tkey> hash;
int index = hash(key) % capacity;
for (it = H[index].begin() ; it != H[index].end() ; ++it) {
if (it->key == key) {
H[index].erase(it);
break;
}
}
}
inline bool has_key(Tkey key) {
typename std::list<struct info<Tkey, Tvalue>>::iterator it;
std::hash<Tkey> hash;
int index = hash(key) % capacity;
for (it = H[index].begin() ; it != H[index].end() ; ++it) {
if (it->key == key) {
return true;
}
}
return false;
}
};
int main() {
// std::ifstream cin("hashuri.in");
// std::ofstream cout("hashuri.out");
// std::ios::sync_with_stdio(false);
int n, op_type, x;
freopen("hashuri.in", "r", stdin);
freopen("hashuri.out", "w", stdout);
n = next_int();
Hashtable <int, int> hash = Hashtable<int, int>(n);
for(int i = 0 ; i < n ; ++i) {
op_type = next_int();
x = next_int();
switch(op_type) {
case 1: hash.put(x, i);
break;
case 2: hash.remove(x);
break;
default: printf("%d\n", hash.has_key(x));
break;
}
}
return 0;
}