Pagini recente » Cod sursa (job #595358) | Cod sursa (job #458272) | Cod sursa (job #2678002) | Cod sursa (job #811458) | Cod sursa (job #2378666)
#include <cstdio>
#include <vector>
#include <set>
#define MOD 500009
using namespace std;
int n;
multiset <int> v[MOD + 1];
int getPos(int x)
{
//Return hash value of x as the pos in the hash table
unsigned int hs = 5381;
while(x > 0){
hs = ((hs << 5) + hs) + ((x % 10) + 33); /* hs * 33 + (x % 10) */
x /= 10;
}
return hs % MOD;
}
bool searchElement(const int x)
{
int pos = getPos(x);
//Binary search in multiset (returns position where it finds x)
multiset<int>::iterator const it = v[pos].lower_bound(x);
//If x was found, return true
return ((*it) == x);
}
void insertElement(const int x)
{
int pos = getPos(x);
if(!searchElement(x))
v[pos].insert(x);
}
void deleteElement(int x)
{
int pos = getPos(x);
//Multiset search + deletion
v[pos].erase(x);
}
int main()
{
int i, op, x;
FILE *f, *g;
f = fopen("hashuri.in", "r");
g = fopen("hashuri.out", "w");
fscanf(f, "%d", &n);
for(i=1; i<=n; ++i){
fscanf(f, "%d%d", &op, &x);
switch(op){
case 1:
insertElement(x);
break;
case 2:
deleteElement(x);
break;
case 3:
fprintf(g, "%d\n", searchElement(x));
break;
}
}
return 0;
}