Cod sursa(job #2636797)

Utilizator irimia_alexIrimia Alex irimia_alex Data 19 iulie 2020 23:26:08
Problema Hashuri Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
#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;
}