Pagini recente » Cod sursa (job #1549672) | Cod sursa (job #384174) | Cod sursa (job #3193250) | Cod sursa (job #2468871) | Cod sursa (job #1769612)
#include <bits/stdc++.h>
using namespace std;
class Hash{
private:
const int Nmax = 666013;
const int magic = 666;
vector<vector<int> > chaining;
public:
Hash(int k)
{
chaining.resize(k);
}
bool search(int value);
void insert(int value);
void erase(int value);
};
#define DIM 666013
char buffer[DIM];
int poz = DIM - 1;
void scan(int &A)
{
A = 0;
while('0' > buffer[poz] || buffer[poz] > '9')
if(++poz == DIM) fread(buffer,1,DIM,stdin),poz = 0;
while('0' <= buffer[poz] && buffer[poz] <= '9')
{
A = A * 10 + buffer[poz] - 48;
if(++poz == DIM)
fread(buffer,1,DIM,stdin),poz = 0;
}
}
bool Hash::search(int value)
{
int position = 1LL*666*value % Nmax;
for(auto it : chaining[position])
if(it == value)
return true;
return false;
}
void Hash::insert(int value)
{
if(search(value))
return;
int position = 1LL*666*value % Nmax;
chaining[position].push_back(value);
}
void Hash::erase(int value)
{
if(!search(value))
return;
int position = 1LL*666*value % Nmax;
chaining[position].erase(find(chaining[position].begin(), chaining[position].end(), value));
}
int main()
{
freopen("hashuri.in","r",stdin);
freopen("hashuri.out","w",stdout);
Hash hashTable(666013);
int op,x,N;
scan(N);
for (int i = 0; i < N; ++i) {
scan(op);scan(x);
if (op == 1) {
if(hashTable.search(x))
continue;
hashTable.insert(x);
continue;
}
if (op == 2) {
if(!hashTable.search(x))
continue;
hashTable.erase(x);
continue;
}
printf("%d\n",hashTable.search(x));
}
return 0;
}