Cod sursa(job #1128017)

Utilizator cernat.catallinFMI Cernat Catalin Stefan cernat.catallin Data 27 februarie 2014 14:50:22
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <stdio.h>
#include <vector>
using namespace std;

#define MOD 666013

vector <int> hash_t[MOD];

inline int getHash(int x){
	return x % MOD;
}

inline vector <int>::iterator searchHash(int x){
	int ind = getHash(x);
	vector <int>::iterator it;

	for (it = hash_t[ind].begin(); it != hash_t[ind].end(); ++it)
		if (*it == x)
			return it;
	return hash_t[ind].end();
}

inline void addHash(int x){
	int ind = getHash(x);

	if ( searchHash(x) == hash_t[ind].end() )
		hash_t[ind].push_back(x);
}

inline void deleteHash(int x){
	int ind = getHash(x);
	vector <int>::iterator it = searchHash(x);

	if (it != hash_t[ind].end())
		hash_t[ind].erase(it);
}

int main(){
    freopen("hashuri.in", "r", stdin);
    freopen("hashuri.out", "w", stdout);
	int n, op, x;

    scanf("%d", &n);
    for (int i = 1; i <= n; ++i){
		scanf("%d %d", &op, &x);
		if (op == 1)
			addHash(x);
		else if (op == 2)
			deleteHash(x);
		else
			printf("%d\n", searchHash(x) != hash_t[ getHash(x) ].end());
    }

    fclose(stdin);
    fclose(stdout);

    return 0;
}