Cod sursa(job #2196388)

Utilizator mihai.alphamihai craciun mihai.alpha Data 19 aprilie 2018 10:15:56
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.31 kb
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define VI vector<int>
#define PII pair<int,int>
#define PDD pair<double,double>
#define ll long long
#define lf double
#define pb push_back
#define For(i, j, n)for(int i = j;i <= n;i++)
#define iFor(i, j, n)for(int i = n;i >= j;i--)
#define R scanf
#define S scanf
#define P printf
#define fin stdin
#define fout stdout

#define DBG 1

#define BUF 1 << 17
char buf[BUF];
int pos = BUF;

inline char next()  {
    if(pos == BUF)
        fread(buf, 1, BUF, fin), pos = 0;
    return buf[pos++];
}

inline int read()  {
    int x = 0;
    char ch = next();
    while(!isdigit(ch))
        ch = next();
    while(isdigit(ch))  {
        x = x * 10 + ch - '0', ch = next();
    }
    return x;
}

int n;

const int maxn = 1e6 + 3;
const int MOD = 666013;

struct myhash  {
    int k, next[maxn], lista[MOD], val[maxn];
    inline void insert(int nr)  {
        int mod = nr % MOD;
        k++;
        val[k] = nr;
        next[k] = lista[mod];
        lista[mod] = k;
    }
    inline void erase(int nr)  {
        int mod = nr % MOD;
        int poz = lista[mod];
        if(val[poz] == nr)  {
            lista[mod] = next[lista[mod]];
            return;
        }
        int prev;
        while(poz)  {
            int el = val[poz];
            if(el == nr)  {
                break;
            }
            prev = poz;
            poz = next[poz];
        }
        if(poz)  {
            next[prev] = next[poz];
        }
    }
    inline bool check(int nr)  {
        int mod = nr % MOD;
        int poz = lista[mod];
        while(poz)  {
            int el = val[poz];
            if(el == nr)  {
                return 1;
            }
            poz = next[poz];
        }
        return 0;
    }
} hh;

int main()  {
	if(DBG)  {
		freopen("hashuri.in", "r", stdin);
		freopen("hashuri.out", "w", stdout);
	}
    n = read();
    while(n)  {
        n--;
        int op, x;
        op = read(), x = read();
        if(op == 1)  {
            if(hh.check(x) == 0)
                hh.insert(x);
        }
        else if(op == 2)  {
            hh.erase(x);
        }
        else if(op == 3)  {
            P("%d\n", hh.check(x));
        }
    }
	return 0;
}