Cod sursa(job #911151)

Utilizator ArmandNMArmand Nicolicioiu ArmandNM Data 11 martie 2013 12:59:46
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
#define MOD 60000
int n,i,k,poz,x;
vector < vector <long long> > h;

int hashh(long long x);

int verificare(long long x)
{
    int i;
    if (h[hashh(x)].size() >0 )
    {
        for (i=0;i<=h[hashh(x)].size()-1;i++)
        {
            if (h[hashh(x)][i]==x)
            {
                poz=i;
                return 1;
            }
        }
    }
    return 0;
}

void stergere(long long x)
{
    long long aux;
    if (verificare(x)==1)
    {
        aux=h[hashh(x)][poz];
        h[hashh(x)][poz]=h[hashh(x)][h[hashh(x)].size()-1];
        h[hashh(x)][h[hashh(x)].size()-1]=aux;
        h[hashh(x)].pop_back();
    }
}

void adaugare(long long x)
{
    h[hashh(x)].push_back(x);
}

int hashh(long long x)
{
    long y;
    y=x>>1;
    y*=5;
    y=y<<2;
    y*=3;
    y=y%MOD;
    return int(y);
}

int main()
{
    f>>n;
    h.resize(MOD);
    for (i=1;i<=n;i++)
    {
        f>>k;
        f>>x;
        if (k==1) adaugare(x);
        if (k==2) stergere(x);
        if (k==3) g<<verificare(x)<<'\n';
    }
    f.close();
    g.close();
    return 0;
}