Cod sursa(job #1043260)

Utilizator SeekHunt1334Septimiu Bodica SeekHunt1334 Data 28 noiembrie 2013 10:48:35
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

ifstream fin("hashuri.in");
ofstream fout("hashuri.out");

int n;
const int mod = 666013;
vector<int> G[mod];

void Erase(const int nr)
{
    int poz = nr % mod;
    int lpoz = G[poz].size()-1;

    for (unsigned int i = 0; i < G[poz].size(); ++i)
        if ( G[poz][i] == nr )
        {
            swap(G[poz][i], G[poz][lpoz]); // swap found element with last element and
            G[poz].pop_back(); // pop
            return;
        }
}

bool Find(const int nr)
{
    int poz = nr % mod;
    vector<int>::iterator it;
    for (it = G[poz].begin(); it != G[poz].end(); ++it)
        if ( *it == nr ) return true;
    return false;
}

void Insert(const int nr)
{
    if ( !Find(nr) ) return;
    int poz = nr % mod;
    G[poz].push_back(nr);
}

int main()
{
    fin >> n;

    int op, nr;
    for(int i = 0; i < n; ++i)
    {
        fin >> op >> nr;

        if ( op == 1 ) Insert(nr);
        if ( op == 2 ) Erase(nr);
        if ( op == 3 ) cout << Find(nr) << '\n';
    }

    cout << '\n';

    fin.close();
    fout.close();
    return 0;
}