Cod sursa(job #1772539)

Utilizator adu18sptAndrei Mircea adu18spt Data 6 octombrie 2016 20:30:57
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include<fstream>
using namespace std;
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");

const int mod=1<<22,prim=101;

int h[mod];

void inc(int &poz)
{
	if(++poz==mod)
	poz=0;
}

int find(int x)
{
	int poz=(1LL*x*prim)%mod;
	for(;h[poz]!=x && h[poz]!=0;inc(poz));
	if(h[poz]==x) return poz;
	else return -1;
}

void add(int x)
{
	if(find(x)!=-1) return;
	int poz=(1LL*x*prim)%mod;
	for(;h[poz]!=x && h[poz]>0;inc(poz));
	h[poz]=x;
}

void erase(int x)
{
	int poz=find(x);
	if(poz!=-1) 
	h[poz]=-1;
}

int main()
{
	int n;
	
	fin>>n;
	for(int i=1;i<=n;i++)
	{
		int tip,x;
		fin>>tip>>x;
		if(tip==1) add(x);
		else if(tip==2) erase(x);
		else if(find(x)==-1) fout<<0<<"\n";
		else fout<<1<<"\n";
	}
	
	return 0;
}