Cod sursa(job #1729966)

Utilizator Kln1000Ciobanu Bogdan Kln1000 Data 15 iulie 2016 22:36:24
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int v[10000010];
const int key=43;

inline int hashcode(int x){
return (1LL*x*key+x/key)%10000000;}

inline void increment(int &a){
if (++a==10000000) a=0;
}

inline void hash_add(int x){int a=hashcode(x),i;
for(i=a;v[i]!=0 and v[i]!=x;increment(i));
if(v[i]==x) return;
for(i=a;v[i]>0;increment(i));
v[i]=x;
}

inline void hash_erase(int x){int i;
for (i=hashcode(x);v[i]!=0 and v[i]!=x;increment(i));
if (v[i]==x) v[i]=-1;}

inline bool hash_verify(int x){int i;
for (i=hashcode(x);v[i]!=0 and v[i]!=x;increment(i));
return (v[i]==x);
}

int main()
{int n,q,x;
f>>n;
for (int i=0;i<n;++i){
f>>q>>x;
if (q==1)
hash_add(x);
else if(q==2)
hash_erase(x);
else if(q==3)
t<<hash_verify(x)<<'\n';}
return 0;
}