Cod sursa(job #1294470)

Utilizator mihail.jianuJianu Mihail mihail.jianu Data 17 decembrie 2014 17:08:51
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.19 kb
#include<cstdio>
const int N=1000000;
class Hash{
    public:
        int v[N+1];
        void add(int x){
            int m1=x%p1;
            int m2=x%p2;
            int pos=m1;
            while(v[pos]>0){
                pos+=m2;
                if(pos>=N)
                    pos-=N;
            }
            v[pos]=x;
        }
        void del(int x){
            int p=has(x);
            if(p!=-1)
                v[p]=-1;
        }
        int has(int x){
            int pos=x%p1,m2=x%p2;
            while(v[pos]!=0){
                if(v[pos]==x)
                    return pos;
                pos+=m2;
                if(pos>=N)
                    pos-=N;
            }
            return -1;
        }
    private:
        const int p1=666013;
        const int p2=300007;
};
Hash h;
int n;
int main(){
    freopen("hashuri.in","r",stdin);
    freopen("hashuri.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        if(x==1)
            h.add(y);
        else if(x==2)
            h.del(y);
        else
            if(h.has(y)!=-1)
                printf("1\n");
            else
                printf("0\n");
    }
    return 0;
}