Cod sursa(job #622901)

Utilizator cristianalex81Cristian Alexandru cristianalex81 Data 18 octombrie 2011 18:07:29
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <stdio.h>
#include <vector>
#define m 666013

using namespace std;

int n;
vector <int> ht[m];

inline vector<int>::iterator poz(int x, int i)
{
    vector <int>::iterator index;
    for (index = ht[i].begin();index!=ht[i].end();index++)
    {
        if (*index == x)
            return index;
    }
    return ht[i].end();
}

inline void add(int x)
{
    int i=x%m;
    vector <int>::iterator index = poz(x,i);
    if (index == ht[i].end())
        ht[i].push_back(x);
}

inline void del(int x)
{
    int i=x%m;
    vector <int>::iterator index = poz(x,i);
    if (index != ht[i].end())
        ht[i].erase(index);
}

inline int search(int x)
{
    int i=x%m;
    vector <int>::iterator index = poz(x,i);
    if (index != ht[i].end())
        return 1;
    return 0;
}

int main()
{
    int i,c,x;
    freopen("hashuri.in","r",stdin);
    freopen("hashuri.out","w",stdout);
    scanf("%d",&n);
    for (i=0;i<n;i++)
    {
        scanf("%d %d",&c,&x);
        if (c==1) //add x to table
        {
            add(x);
        }
        if (c==2) //delete x from table
        {
            del(x);
        }
        if (c==3) //is x in table ?
        {
            printf("%d\n",search(x));
        }
    }
    return 0;
}