Pagini recente » Cod sursa (job #290683) | Cod sursa (job #144692) | Cod sursa (job #1485389) | Cod sursa (job #2908857) | Cod sursa (job #1491154)
#include <stdio.h>
#include <stdlib.h>
#define MOD 9973
typedef struct nd {
int value;
struct nd *next;
} node;
node * hash[MOD];
void push_hash (int val) {
int hashed_value = val % MOD;
if (hash[hashed_value] == NULL) {
hash[hashed_value] = (node *) malloc (sizeof (node));
hash[hashed_value]->value = val;
hash[hashed_value]->next = NULL;
} else {
node *new = (node *) malloc (sizeof (node));
new->value = val;
new->next = hash[hashed_value];
hash[hashed_value] = new;
}
}
int search_hash (int val) {
int hashed_value = val % MOD;
node *crt;
for (crt = hash[hashed_value]; crt != NULL; crt = crt->next)
if (crt->value == val)
return 1;
return 0;
}
void remove_hash (int val) {
int hashed_value = val % MOD;
node *killed;
if (hash[hashed_value] != NULL && hash[hashed_value]->value == val) {
killed = hash[hashed_value];
hash[hashed_value] = hash[hashed_value]->next;
free (killed);
return;
}
node *crt, *prev;
for (crt = hash[hashed_value]; crt != NULL; crt = crt->next) {
if (crt->value == val) {
prev->next = crt->next;
free (crt);
return;
}
prev = crt;
}
}
int heap[500000];
int length;
void swap (int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
void sift_up (int idx) {
if (idx == 0)
return;
int father = (idx - 1) / 2;
if (heap[idx] < heap[father]) {
swap (&heap[idx], &heap[father]);
sift_up (father);
}
}
void push_heap (int val) {
heap[length++] = val;
sift_up (length - 1);
}
void sift_down (int idx) {
int fst_son = 2 * idx + 1;
int snd_son = 2 * idx + 2;
if (fst_son >= length)
return;
else if (snd_son >= length) {
if (heap[idx] >= heap[fst_son])
swap (&heap[idx], &heap[fst_son]);
return;
}
if (heap[idx] > heap[fst_son] && heap[idx] > heap[snd_son]) {
if (heap[fst_son] < heap[snd_son]) {
swap (&heap[idx], &heap[fst_son]);
sift_down (fst_son);
}
else {
swap (&heap[idx], &heap[snd_son]);
sift_down (snd_son);
}
} else {
if (heap[idx] > heap[fst_son]) {
swap (&heap[idx], &heap[fst_son]);
sift_down (fst_son);
} else if (heap[idx] >= heap[snd_son]) {
swap (&heap[idx], &heap[snd_son]);
sift_down (snd_son);
}
}
}
int pop_heap () {
int result = heap[0];
heap[0] = heap[--length];
sift_down(0);
return result;
}
int main(int argc, char *argv[]) {
/*
freopen ("algsort.in", "r", stdin);
freopen ("algsort.out", "w", stdout);
int n;
scanf ("%d", &n);
int i;
for (i = 0; i < n; ++i) {
int x;
scanf ("%d", &x);
push_heap (x);
}
while (length != 0)
printf ("%d ", pop_heap());
printf ("\n");
*/
freopen ("hashuri.in", "r", stdin);
freopen ("hashuri.out", "w", stdout);
int n;
scanf ("%d", &n);
int i;
for (i = 0; i < n; ++i) {
int op, val;
scanf ("%d%d", &op, &val);
if (op == 1)
push_hash (val);
else if (op == 2)
remove_hash (val);
else if (op == 3)
printf ("%d\n", search_hash (val));
}
return 0;
}