Cod sursa(job #1256946)

Utilizator fhandreiAndrei Hareza fhandrei Data 7 noiembrie 2014 00:41:25
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.48 kb
// Include
#include <fstream>
#include <vector>
using namespace std;
 
// Definitii
#define pb push_back
 
// Constante
const int mod = 666013;
 
// Functii
void hashInsert(int val);
void hashDelete(int val);
int hashQuery(int val);
 
// Variabile
ifstream in("hashuri.in");
ofstream out("hashuri.out");
 
int questions;
int type, val;
 
vector<int> HASH[mod];
 
// Main
int main()
{
    in >> questions;
    while(questions--)
    {
        in >> type >> val;
        switch(type)
        {
            case 1: {   hashInsert(val); break;         }
            case 2: {   hashDelete(val); break;         }
            case 3: {   out << hashQuery(val) << '\n';  }
        }
    }
     
    in.close();
    out.close();
    return 0;
}
 
void hashInsert(int val)
{
    int pos = val % mod;
    vector<int>::iterator it, end=HASH[pos].end();
    for(it=HASH[pos].begin() ; it!=end ; ++it)
        if(*it == val)
            return;
     
    HASH[pos].pb(val);
}
 
void hashDelete(int val)
{
    int pos = val % mod;
    vector<int>::iterator it, end=HASH[pos].end();
    for(it=HASH[pos].begin() ; it!=end ; ++it)
    {
        if(*it == val)
        {
            HASH[pos].erase(it);
            return;
        }
    }
}
 
int hashQuery(int val)
{
    int pos = val % mod;
    vector<int>::iterator it, end=HASH[pos].end();
    for(it=HASH[pos].begin() ; it!=end ; ++it)
        if(*it == val)
            return 1;
     
    return 0;
}