Cod sursa(job #2422188)

Utilizator SavanderianAlexandru Balan Savanderian Data 17 mai 2019 18:00:06
Problema Hashuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.8 kb

#include<bits/stdc++.h>
#define div 10007

using namespace std;
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
struct hsh{
hsh *urm=NULL;
int val=0;
};

int main()
{
    int n,val;
    unsigned short int x;
    hsh vect[10007];

        fin>>n;
    while(n--)
    {
        fin>>x>>val;
            if(x==1)
            {

                    int hash1=val%div;
                    hsh *p;
                    p=&vect[hash1];
                    bool ok=true;
                        if(p->val==val)
                            ok=false;

                if(ok==true)
                {
                    while(p->urm!=NULL)
                        {
                            p=p->urm;
                            if(p->val==val)
                            {
                                ok=false;
                                break;
                            }
                        }

                    if(p->val==0)
                        p->val=val;
                    else
                    {
                        hsh *neu;
                        neu=new hsh;
                        p->urm=neu;
                        neu->urm=NULL;
                        neu->val=val;
                    }
                }


            }
            else if(x==2)
            {
                 int hash1=val%div;
                    hsh *p;
                    p=&vect[hash1];
                 bool ok=true;
                    if(p->val==val)
                        p->val=0,ok=false;
                    if(ok==true)
                    {
                        hsh *anterior=p;

                       while(p->urm!=NULL)
                       {
                            if(p->val==val)
                            {
                                anterior->urm=p->urm;
                                break;
                            }
                            anterior=p;
                           p=p->urm;

                       }
                    }
            }
            else
            {
                 int hash1=val%div;
                    hsh *p;
                    p=&vect[hash1];
                 bool ok=true;
                 if(p->val==val)
                    fout<<1<<'\n',ok=false;
                 if(ok==true){
                    ok=false;
                    while(p->urm!=NULL){

                            p=p->urm;
                               if(p->val==val){

                            fout<<1<<'\n';
                            ok=true;
                            break;
                            }

                    }
                    if(ok==false)
                        fout<<0<<'\n';
                 }
            }

    }



    return 0;
}