Cod sursa(job #265210)

Utilizator swxxIoo Andrei Rares swxx Data 23 februarie 2009 16:32:11
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.54 kb
#include <stdio.h>  
   
 struct nod{  
     int a;  
     nod *adr;  
     } *prim[500009];  //666030  
 long X=500009; //666013 ; 179533 ; 500009  
   
 void f1(int t)  
     {  
     int p=1;  
     int rest=t%X;  
     nod *aux;  
     if (prim[rest]==NULL)  
         {  
         aux=new nod;  
         aux->a=t;  
        aux->adr=NULL;  
         prim[rest]=aux;  
        }  
     else  
         {  
         //verif - go  
         nod *sf;  
         aux=new nod;  
         aux=prim[rest];  
         while (aux!=NULL&&p)  
             {  
             if (aux->a==t) p=0;  
             if (aux->adr==NULL) sf=aux;  
             aux=aux->adr;  
             }  
         if (p) {  
             aux=new nod;  
            aux->a=t;  
             aux->adr=NULL;  
             sf->adr=aux;  
             }  
         }  
     }  
   
 void f2(int t)  
 {  
     nod *aux;  
     int p=0;  
     int rest=t%X;  
     if (prim[rest]!=NULL)  
     {  
         if (prim[rest]->adr==NULL&&prim[rest]->a==t)  
         {  
             prim[rest]=NULL;  
         }  
         else  
         {  
         if (prim[rest]->a==t&&prim[rest]->adr->adr==NULL)  
         {  
             prim[rest]=prim[rest]->adr;  
         }  
         else  
         {  
         aux=new nod;  
         aux=prim[rest];  
         while (aux->adr!=NULL&&!p)  
         {  
             if (aux->adr->a==t)   
             {  
                 if (aux->adr->adr==NULL)  
                 {  
                     aux->adr=NULL;  
                 }  
                 else   
                 {  
                     aux->adr=aux->adr->adr;  
                }  
             }  
             else aux=aux->adr;  
         }  
         }  
         }  
     }  
 }  
       
 int f3(int t)  
     {  
     int rest=t%X;  
     nod *aux;  
     int p=0;  
     if (prim[rest]==NULL)  
         return 0;  
     else  
         {  
         aux=new nod;  
         aux=prim[rest];  
         while (aux!=NULL&&!p)  
             {  
             if (aux->a==t) p=1;  
             aux=aux->adr;  
             }  
         return p;  
         }  
     }  
   
 int main()  
 {  
 freopen("hashuri.in","r",stdin);  
 freopen("hashuri.out","w",stdout);  
 long n,i,x;  
 short int tip;  
 scanf("%ld",&n);  
 for (i=1;i<=n;++i)  
     {  
     scanf("%hd %ld",&tip,&x);  
     if (tip==1) f1(x);  
     if (tip==2) f2(x);  
     if (tip==3) printf("%d\n",f3(x));  
     }  
 return 0;  
 }