Cod sursa(job #1306971)

Utilizator obidanDan Ganea obidan Data 31 decembrie 2014 18:59:08
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <vector>
using namespace std;
const int M = 666013;

vector<int> H[M];

int hash_func(int value)
{
    return value % M;
}

void Add(int value)
{
   int where = hash_func(value);
   vector<int>::iterator it;
   for(it = H[where].begin(); it != H[where].end(); ++it)
   {
       if(*it == value) return;
   }
   H[where].push_back(value);
}

void Delete(int value)
{
   int  where=hash_func(value);
    vector<int>::iterator it;
    for(it=H[where].begin(); it != H[where].end(); ++it)
    {
        if(*it == value)
           {
               H[where].erase(it);
               return;
           }

    }

}
int Query(int value)
{
    int where=hash_func(value);
    vector<int>::iterator it;
    for(it=H[where].begin(); it!= H[where].end();++it)
    {
        if(*it == value)
            return 1;
    }
    return 0;
}

int main()
{
    freopen("hashuri.in","r",stdin);
    freopen("hashuri.out","w",stdout);
    int i,j,n,x,y;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d %d",&x,&y);
        if(x==1)
        {
            Add(y);
        }
        else if(x==2)
        {
            Delete(y);
        }
        else
        {
            printf("%d\n",Query(y));
        }
    }

}