Pagini recente » Cod sursa (job #382984) | Cod sursa (job #3208675) | Teoria jocurilor | Cod sursa (job #1492099) | Cod sursa (job #1200460)
#include <cstdio>
#include <cstdlib>
struct nod {
int info;
nod *next;
};
const int MOD = 666013;
nod *hash[MOD];
int query(int key, int value) {
for (nod *i = hash[key]; i; i = i -> next)
if (i -> info == value)
return 1;
return 0;
}
void insert(int key, int value) {
if (query(key, value))
return ;
nod *tmp = new nod;
tmp -> info = value;
tmp -> next = hash[key];
hash[key] = tmp;
}
void erase(int key, int value) {
if (!query(key, value))
return ;
if (hash[key] -> info == value) {
nod *tmp = hash[key];
hash[key] = hash[key] -> next;
delete tmp;
}
else {
for (nod *i = hash[key], *j = hash[key] -> next; j; i = i -> next, j = j -> next)
if (j -> info == value) {
i -> next = j -> next;
delete j;
break;
}
}
}
int main () {
FILE *fin, *fout;
fin = fopen("hashuri.in", "r");
fout = fopen("hashuri.out", "w");
int N; fscanf(fin, "%d", &N);
for (int i = 0; i < N; ++i) {
int tip, x; fscanf (fin, "%d%d", &tip, &x);
if (tip == 1)
insert(abs(x) % MOD, x);
else if (tip == 2)
erase(abs(x) % MOD, x);
else
fprintf(fout, "%d\n", query(abs(x) % MOD, x));
}
for (int i = 0; i < MOD; ++i)
while (hash[i]) {
nod *tmp = hash[i];
hash[i] = hash[i] -> next;
delete tmp;
}
return 0;
}