Cod sursa(job #2093915)

Utilizator andreicoman299Coman Andrei andreicoman299 Data 24 decembrie 2017 16:49:42
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.91 kb
#include <bits/stdc++.h>
#define MOD 1000003
#define MAXN 1000000

#define BUF_SIZE 1 << 14
char buf[BUF_SIZE];
int pbuf=BUF_SIZE;
FILE*fi,*fo;
inline char nextch(){
    if(pbuf==BUF_SIZE){
        fread(buf, BUF_SIZE, 1, fi);
        pbuf=0;
    }
    return buf[pbuf++];
}
inline int nextnum(){
    int a = 0;
    char c = nextch();
    while(!isdigit(c))
        c = nextch();
    while(isdigit(c)){
        a = a * 10 + c - '0';
        c = nextch();
    }
    return a;
}
class Hash{
    public:
    int k = 0, next[MAXN + 1], lista[MOD];
    int val[MAXN + 1];
    //Hash(){
    //    memset(lista, 0x00, MOD * sizeof(int));
    //}
    inline void insert(int element){
        k++;
        val[k] = element;
        next[k] = lista[element % MOD];
        lista[element % MOD] = k;
    }
    inline void erase(int element){
        int ind = element % MOD;
        int p = lista[ind];
        if(p == 0)
            return;
        if(val[p] == element){
            lista[ind] = next[lista[ind]];
            return;
        }
        while(next[p] != 0 && val[next[p]] != element)
            p = next[p];
        if(next[p] != 0)
            next[p] = next[next[p]];
    }
    inline int find(int element){
        int p = lista[element % MOD];
        while(p != 0 && val[p] != element)
            p = next[p];
        return p;
    }
} H;

int main(){
    fi=fopen("hashuri.in","r");
    fo=fopen("hashuri.out","w");
    int n = nextnum();
    for(int i = 0; i < n; i++){
        int c = nextnum();
        int val = nextnum();
        if(c == 1){
            if(!H.find(val))
                H.insert(val);
        }
        else if(c == 2)
            H.erase(val);
        else{
            if(H.find(val))
                fprintf(fo,"1\n");
            else
                fprintf(fo,"0\n");
        }
    }
    fclose(fi);
    fclose(fo);
    return 0;
}