Pagini recente » Cod sursa (job #878292) | Cod sursa (job #2364764) | Cod sursa (job #1105720) | Cod sursa (job #2082894) | Cod sursa (job #3334915)
#include <fstream>
using namespace std;
const int P = 1000003; // prim, ~1e6
struct Nod {
long long val;
Nod* next;
};
Nod* liste[P]; // vector de liste (hash table)
bool exista(long long x) {
int h = x % P;
Nod* p = liste[h];
while (p != NULL) {
if (p->val == x) return true;
p = p->next;
}
return false;
}
void adauga(long long x) {
int h = x % P;
// verificam daca exista deja
Nod* p = liste[h];
while (p != NULL) {
if (p->val == x) return; // deja in multime
p = p->next;
}
// inserare la inceput (mai simplu)
Nod* nou = new Nod;
nou->val = x;
nou->next = liste[h];
liste[h] = nou;
}
void sterge(long long x) {
int h = x % P;
Nod* p = liste[h];
Nod* prev = NULL;
while (p != NULL) {
if (p->val == x) {
if (prev == NULL)
liste[h] = p->next;
else
prev->next = p->next;
delete p;
return;
}
prev = p;
p = p->next;
}
}
int main() {
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
int N;
long long x;
int op;
fin >> N;
for (int i = 0; i < P; i++)
liste[i] = NULL;
while (N--) {
fin >> op >> x;
if (op == 1) adauga(x);
else if (op == 2) sterge(x);
else if (op == 3) {
fout << (exista(x) ? 1 : 0) << '\n';
}
}
return 0;
}