Cod sursa(job #1139025)

Utilizator UAIC_Balan_Negrus_HreapcaUAIC Balan Negrus Hreapca UAIC_Balan_Negrus_Hreapca Data 10 martie 2014 20:04:18
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.47 kb
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iterator>
#include <random>
#include <assert.h>
using namespace std;

const string file = "hashuri";

const string infile = file + ".in";
const string outfile = file + ".out";

const int INF = 0x3f3f3f3f; 

const int MODN = 66697;

vector<int> H[MODN];
int cHash(int x)
{
    return x % MODN;
}

void insert(int x)
{
    int h = cHash(x);
    bool found = false;
    for(vector<int>::iterator itr = H[h].begin();
            itr != H[h].end();
            itr++)
    {
        if(*itr == x)
        {
            found = true;
            break;
        }
    }
    if(found == false)
        H[h].push_back(x);
}

void remove(int x)
{
    int h = cHash(x);
    bool found = false;
    vector<int>::iterator jtr;
    for(vector<int>::iterator itr = H[h].begin();
            itr != H[h].end();
            itr++)
    {
        if(*itr == x)
        {
            found = true;
            jtr = itr;
        }
    }
    if(found)
    {
        iter_swap(H[h].end() - 1, jtr);
        H[h].pop_back();
    }
}

bool exists(int x)
{
    int h = cHash(x);
    bool found = false;
    for(vector<int>::iterator itr = H[h].begin();
            itr != H[h].end();
            itr++)
    {
        if(*itr == x)
        {
            found = true;
            break;
        }
    }
    return found;
}
//#define ONLINE_JUDGE

int main()
{
#ifdef ONLINE_JUDGE
	ostream &fout = cout;
	istream &fin = cin;
#else
	fstream fin(infile.c_str(), ios::in);
	fstream fout(outfile.c_str(), ios::out);
#endif	

    int N;
    fin >> N;
    for(int i = 0; i < N; i++)
    {
        int op, x;
        fin >> op >> x;
        if(op == 1)
        {
            insert(x);
        }
        else if(op == 2)
        {
            remove(x);
        }
        else if(op == 3)
        {
            fout << (exists(x) == true ? 1 : 0 ) << "\n";
        }
    }

#ifdef ONLINE_JUDGE
#else
    fout.close();
	fin.close();
#endif
}