Cod sursa(job #2637927)

Utilizator irimia_alexIrimia Alex irimia_alex Data 25 iulie 2020 18:23:24
Problema Hashuri Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.23 kb
// HashTable.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <fstream>
#include <time.h>

using namespace std;

ifstream fin("hashuri.in");
ofstream fout("hashuri.out");

class Hash {
public:
	static constexpr int SIZE = 1000000;
	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->prev != nullptr)
			e->prev->next = e->next;
		if (e->next != nullptr)
			e->next->prev = e->prev;

		if (e == hash[index].begin)
			hash[index].begin = e->next;
		else if (e == hash[index].end)
			hash[index].end = e->prev;

		delete e;
	}
};

int main()
{
	Hash h;
	int t;
	float max = 0;
	fin >> t;
	while (t--) {
		clock_t T = clock();
		int v, x;
		fin >> v >> x;
		if (v == 1) {
			h.add(x);
		}
		else if (v == 2) {
			h.erase(x);
		}
		else {
			fout << h.exists(x) << '\n';
		}
	}
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file