Cod sursa(job #1764946)

Utilizator andreicoman299Coman Andrei andreicoman299 Data 26 septembrie 2016 09:21:22
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.31 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#define MOD 666013
#define MAXN 1000000

#define BUF_SIZE 16384
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 long long nextnum(){
    long long a=0;
    char c=nextch();
    while((c<'0' || c>'9') && c!='-')
        c=nextch();
    int semn=1;
    if(c=='-'){
        semn=-1;
        c=nextch();
    }
    while('0'<=c && c<='9'){
        a=a*10+c-'0';
        c=nextch();
    }
    return a*semn;
}
class Hash{
    public:
    long long k=0, next[MAXN+1], *lista;
    long long val[MAXN+1];
    long long d[MAXN+1];
    Hash() {
        lista = new long long[MOD];
        memset(lista, 0x00, MOD*sizeof(int));
    }
    inline void insert(long long element, long long frecventa){
        k++;
        val[k]=element;
        d[k]=frecventa;
        next[k]=lista[(1LL*element+1LL*MOD*MOD)%MOD];
        lista[(1LL*element+1LL*MOD*MOD)%MOD]=k;
    }
    inline void erase(long long element){
        long long p=lista[(1LL*element+1LL*MOD*MOD)%MOD];
        if(p==0)
            return ;
        if(val[p]==element){
            lista[(1LL*element+1LL*MOD*MOD)%MOD]=next[lista[(1LL*element+1LL*MOD*MOD)%MOD]];
            return ;
        }
        while(next[p]!=0 && val[next[p]]!=element)
            p=next[p];
        if(next[p]!=0){
            next[p]=next[next[p]];
        }
    }
    inline long long find(long long element){
        long long p=lista[(1LL*element+1LL*MOD*MOD)%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();
        long long 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;
}