Cod sursa(job #3120941)

Utilizator daristyleBejan Darius-Ramon daristyle Data 9 aprilie 2023 17:26:29
Problema Hashuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.67 kb
#include <fstream>

using namespace std;

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

const int MOD_MAX = 666013;
const int N_MAX = 1e6;

class HashMap {
private:
		const int NIL = -1;
		int head[MOD_MAX];
		int key[N_MAX];
		int nxt[N_MAX];
		int mod;
		int curr = 0;

public:
		HashMap(int x) {
			mod = x;

			for(int i = 0; i < mod; ++i)
				head[i] = NIL;
			for(int i = 0; i < N_MAX; ++i)
				nxt[i] = NIL;
		}

		int get_key(int x) {
			return x % mod;
		}

		void add(int x) {
			int list = get_key(x);
			key[curr] = x;
			nxt[curr] = head[list];
			head[list] = curr;
			++curr;
		}

		bool find(int x) {
			int list = get_key(x);
			int it = head[list];
			while(it != NIL && key[it] != x)
				it = nxt[it];

			return it != NIL;
		}

		void erase(int x) {
			int list = get_key(x);
			int ant = head[list], it = nxt[ant];
			if(key[ant] == x){
				head[list] = nxt[ant];
			}else{
				while(it != NIL && key[it] != x){
					ant = it;
					it = nxt[it];
				}

				if(it != NIL){
					nxt[ant] = nxt[it];
				}
			}
		}

		void eraseall(int x) {
			int list = get_key(x);
			int ant = head[list], it;

			while(key[ant] == x)
				ant = nxt[ant];
			head[list] = ant;

			it = nxt[ant];
			while(it != NIL){
				if(key[it] == x)
					nxt[ant] = nxt[it];

				ant = it;
				it = nxt[it];
			}
		}
};

HashMap hm(MOD_MAX);

int main() {
	int n, type, x;

	fin >> n;

	for(int i = 0; i < n; ++i){
		fin >> type >> x;

		switch(type){
			case 1:
				if(!hn.find(x))
					hm.add(x);
				break;
			case 2:
				hm.erase(x);
				break;
			case 3:
				fout << hm.find(x) << '\n';
				break;
			default:
				fout << "TIPUL OPERATIEI ESTE GRESIT\n";
		}
	}

	fin.close();
	fout.close();
	return 0;
}