Cod sursa(job #1611621)

Utilizator h_istvanHevele Istvan h_istvan Data 24 februarie 2016 12:00:35
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <algorithm>
#define MOD 2000001

using namespace std;

struct Elem {
	int value;
	Elem* next;
};

Elem* hashmap[MOD];

Elem* find(int value) {
	Elem* curr = hashmap[value % MOD];
	while (curr) {
		if (curr->value == value) break;
		curr = curr->next;
	}
	return curr;
}

void remove(int value) {
	Elem* curr = hashmap[value % MOD];
	Elem* prev = 0;
	while (curr) {
		if (curr->value == value) break;
		prev = curr;
		curr = curr->next;
	}
	if (curr) {
		if (prev) prev->next = curr->next;
		else hashmap[value % MOD] = curr->next;
		delete curr;
	}
}

void insert(int value) {
	Elem* curr = hashmap[value % MOD];
	Elem* newValue = new Elem;
	newValue->value = value;
	hashmap[value % MOD] = newValue;
	if (curr) newValue->next = curr;
	else newValue->next = 0;
}

int main() {
	freopen("hashuri.in", "r", stdin);
	freopen("hashuri.out", "w", stdout);
	int n;
	scanf("%d", &n);
	for(int i=0; i < n; ++i) {
		int op, x;
		scanf("%d %d", &op, &x);
		if (op == 1) {
			insert(x);
		} else if (op == 2) {
			remove(x);
		} else {
			Elem* el = find(x);
			if (el) printf("1\n");
			else printf("0\n");
		}
	}
}