Cod sursa(job #2083035)

Utilizator andreicoman299Coman Andrei andreicoman299 Data 6 decembrie 2017 23:36:24
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.01 kb
#include <bits/stdc++.h>
#define MOD 666013
#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;
    int val[MAXN+1];
    int d[MAXN+1];
    Hash() {
        lista = new int[MOD];
        memset(lista, 0x00, MOD*sizeof(int));
    }
    inline void insert(int element, int frecventa){
        k++;
        val[k] = element;
        d[k] = frecventa;
        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];
        if(p != 0)
            return p;
        return 0;
    }
} 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, 1);
        }
        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;
}