Cod sursa(job #1893447)

Utilizator mihai.alphamihai craciun mihai.alpha Data 25 februarie 2017 18:09:08
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.35 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

#define ui unsigned int

ifstream f("hashuri.in");
ofstream g("hashuri.out");

struct HASH  {
    private:
    static const int SZ = 666013;
    vector <int> h[SZ];
    public:
    inline int push(int el)  {
        ui key = el % SZ;
        h[key].push_back(el);
    }
    inline bool pop(int el)  {
        ui key = el % SZ;
        for(ui i = 0;i < h[key].size();i++)  {
            if(h[key][i] == el)  {
                swap(h[key][i], h[key][h[key].size() - 1]);
                h[key].pop_back();
                return true;
            }
        }
        return false;
    }
    inline bool find(int el)  {
        ui key = el % SZ;
        for(ui i = 0;i < h[key].size();i++)  {
            if(h[key][i] == el)  {
                return true;
            }
        }
        return false;
    }
};

HASH hh;

int main()  {
    int N;
    f >> N;
    for(int i = 0;i < N;i++)  {
        int op, x;
        f >> op >> x;
        switch(op)  {
            case 1:
            hh.push(x);
            break;
            case 2:
            hh.pop(x);
            break;
            case 3:
            g << hh.find(x) << "\n";
            break;
        }
    }
    f.close();
    g.close();
    return 0;
}