Cod sursa(job #1769612)

Utilizator gabrielinelusGabriel-Robert Inelus gabrielinelus Data 2 octombrie 2016 20:14:11
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.84 kb
#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;
}