Cod sursa(job #868519)

Utilizator TeOOOVoina Teodora TeOOO Data 31 ianuarie 2013 10:31:31
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.4 kb
//Include
#include <stdio.h>
#include <vector>
using namespace std;

//Definitii
#define pb push_back

//Constante
const int mod = 666013;

//Functii
void insertHash(int x);
void deleteHash(int x);
bool checkHash(int x);

//Variabile
FILE *in, *out;

int num;
int type, value;
vector <int> hash[mod];

int main()
{
    in=fopen("hashuri.in","rt");
    out=fopen("hashuri.out","wt");
    fscanf(in,"%d",&num);
    while(num--)
    {
        fscanf(in,"%d%d",&type,&value);
        if(type==1)
            insertHash(value);
        else if(type==2)
            deleteHash(value);
        else fprintf(out,"%d\n", checkHash(value) ? 1:0);
    }

    fclose(in);
    fclose(out);
}

void insertHash(int x)
{
    int pos = x % mod;
    vector <int>::iterator it, end=hash[pos].end();
    for(it=hash[pos].begin() ; it!=end ; ++it)
    {
        if(*it==x)
            return;
    }
    hash[pos].pb(x);
}

void deleteHash(int x)
{
    int pos = x % mod;
    vector <int>::iterator it, end=hash[pos].end();
    for(it=hash[pos].begin() ; it!=end ; ++it)
        if(*it==x)
        {
            hash[pos].erase(it);
            return;
        }
}

bool checkHash(int x)
{
    int pos = x % mod;
    vector <int>::iterator it, end=hash[pos].end();
    for(it=hash[pos].begin() ; it!=end ; ++it)
        if(*it==x)
            return true;
    return false;
}