Cod sursa(job #2552136)

Utilizator mariaghinescu22Ghinescu Maria mariaghinescu22 Data 20 februarie 2020 16:46:56
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int N = 1000001;
const int M = 666013;
int no_operations, type, nr, lst[N], urm[N], val[N];

void add(int x) {
	int r = x % M;
	val[++nr] = x;
	urm[nr] = lst[r];
	lst[r] = nr;
}

int exists(int x) {
	int r = x % M;
	int y;
	for (int p = lst[r]; p != 0; p = urm[p]) {
		y = val[p];
		if (x == y) return 1;
	}
	return 0;
}

void del(int x) {
	int r = x % M;
	int p = lst[r];
	if (x == val[p]) {
		lst[r] = urm[p];
		return;
	}
	while (urm[p] != 0) {
		if (val[urm[p]] == x) {
			urm[p] = urm[urm[p]];
			return;
		}
		p = urm[p];
	}
}

int main() {
	in >> no_operations;
	for (int i = 1; i <= no_operations; i++) {
		in >> type;
		if (type == 1) {
			int x;
			in >> x;
			add(x);
		}
		if (type == 2) {
			int y;
			in >> y;
			if (exists(y)) del(y);
		}
		if (type == 3) {
			int z;
			in >> z;
			out << exists(z) << '\n';
		}
	}
	return 0;
}