Cod sursa(job #1862601)

Utilizator mihai.alphamihai craciun mihai.alpha Data 30 ianuarie 2017 08:47:02
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.9 kb
#include <bits/stdc++.h>

using namespace std;

FILE *fin;
ofstream fout("hashuri.out");

#define BUF 1 << 17

int pos = BUF;
char buf[BUF];

inline char next()  {
    if(pos == BUF)
        fread(buf, 1, BUF, fin), pos = 0;

    return buf[pos++];
}

inline int read()  {
    int x = 0, semn = 1;
    char ch = next();

    while(!isdigit(ch) && ch != '-')
        ch = next();

    if(ch == '-')
        ch = next(), semn = -1;

    while(isdigit(ch))
        x = x * 10 + ch - '0', ch = next();

    return x * semn;
}

#define Hsize 16383

class myHash {
    vector<int> v[Hsize + 1];

    public:
        inline bool check( int val ) {
            unsigned int i, h;

            h = val & Hsize;

            i = 0;
            while ( i < v[h].size() && v[h][i] != val )
                i ++;

            return ( i < v[h].size() );
        }

        inline bool pop( int val ) {
            unsigned int i, h;

            h = val & Hsize;

            i = 0;
            while ( i < v[h].size() && v[h][i] != val  )
                i ++;

            if ( i < v[h].size() ) {
                v[h][i] = v[h][v[h].size() - 1];
                v[h].pop_back();
            }

            return ( i < v[h].size() );
        }

        inline void push( int val ) {
            unsigned int h = val & Hsize;

            v[h].push_back( val );
        }
};

myHash hh;


int main()  {
    fin = fopen("hashuri.in", "r");
    ofstream fout("hashuri.out");

    int N = read();

    for(int i = 0;i < N;i++)  {
        int op = read(), val = read();

        if(op == 1)  {
            hh.push(val);
        }
        if(op == 2)  {
            hh.pop(val);
        }
        if(op == 3)  {
            int c = hh.check(val);

            fout << c << '\n';
        }
    }

    fclose(fin);
    fout.close();

    return 0;

}