Pagini recente » Cod sursa (job #2750690) | Cod sursa (job #298190) | Cod sursa (job #377298) | Cod sursa (job #2939139) | Cod sursa (job #2636797)
#include <stdio.h>
#define SIZE 100000
using namespace std;
FILE* fin, * fout;
struct node {
int x;
node* next = nullptr, * prev = nullptr;
};
struct list {
node* begin = nullptr, * end = nullptr;
}hash[SIZE];
bool exists(int x) {
int index = x % SIZE;
node* e = hash[index].begin;
while (e != nullptr) {
if (e->x == x)
return true;
e = e->next;
}
return false;
}
void add(int x) {
if (exists(x)) return;
int index = x % SIZE;
node* e = new node;
e->x = x;
if (hash[index].begin == nullptr) {
hash[index].begin = e;
}
else {
hash[index].end->next = e;
e->prev = hash[index].end;
}
hash[index].end = e;
}
void erase(int x) {
if (!exists(x)) return;
int index = x % SIZE;
node* e = hash[index].begin;
while (e->x != x) {
e = e->next;
}
if (e == hash[index].begin) {
hash[index].begin = hash[index].begin->next;
}
else {
e->prev->next = e->next;
e->next->prev = e->prev;
}
delete e;
}
int main() {
fin = fopen("hashuri.in", "r");
fout = fopen("hashuri.out", "w");
int n;
fscanf(fin, "%i", &n);
while (n--) {
int t,x;
fscanf(fin, "%i %i", &t, &x);
if (t == 1)
add(x);
if (t == 2)
erase(x);
if (t == 3)
fprintf(fout,"%i\n", exists(x));
}
return 0;
}