Cod sursa(job #1198170)

Utilizator DjokValeriu Motroi Djok Data 14 iunie 2014 20:16:16
Problema Hashuri Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include<fstream>
#include<algorithm>
using namespace std;
#define MOD 666013;
 ifstream cin("hashuri.in");
 ofstream cout("hashuri.out");

typedef struct lnod {
      int info;
      lnod *next;  
}*nod;

int n,tip,x,aux;
nod a[666018];

void add(int x,nod &y) {
    nod p=new lnod;
    p->info=x;
    p->next=y;
    y=p; 
}

void del(int x) {
  int aux=x%MOD;   
  nod p=a[aux],r;
  while(p)
  if(p->info==x) if(p==a[x]) if(p->next==NULL) { a[x]=NULL;delete p; return; }
                             else { a[x]=p->next;delete p; return;}
                 else {
                        r->next=p->next;
                        r=r->next;
                        delete p;
                        return;
                      }
  else r=p,p=p->next;                                      
}

bool find(int x) {
    int aux=x%MOD; 
    nod p;
    for(p=a[aux];p;p=p->next)
    if(p->info==x) return 1;
    return 0; 
}

int main()
{  
  cin>>n;
  
  while(n--)
  {
    cin>>tip>>x;
    aux=x%MOD;
    if(tip==1 && !find(x)) add(x,a[aux]);
    if(tip==2 && find(x)) del(x);
    if(tip==3) cout<<find(x)<<'\n';
  }
    
 return 0;   
}