Cod sursa(job #240599)

Utilizator marcelcodreaCodrea Marcel marcelcodrea Data 8 ianuarie 2009 00:19:55
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include<stdio.h>
#define R 666013

struct Nod {
    int x;
    Nod *next;
};

int n;
Nod *a[666015];
int op;
int x;
void insert(Nod *&u, int val)
{
    Nod *f = new Nod;
    f -> x = val;
    f -> next = u;
    u = f;
}

void ins(int q)
{
    insert(a[q % R], q);
}
void del(int q)
{
    if (a[q % R] == NULL) return;
    if (a[q % R] -> x == q)
     a[q % R] = a[q % R] -> next;

    for(Nod *it = a[q % R]; it; it = it -> next)
     {
         if (it -> next == NULL) break;
         if (it -> next -> x == q) it -> next = it -> next -> next;
     }

}
int query(int q)
{
    for(Nod *it = a[q % R]; it; it = it -> next)
     if (it -> x == q) return 1;
    return 0;
}


int main()
{
    freopen("hashuri.in","r",stdin);
    freopen("hashuri.out","w",stdout);

    scanf("%d",&n);
    for(int i = 1; i <= n; i++)
     {
      scanf("%d %d", &op, &x);
      if (op == 1) ins(x);
      if (op == 2) del(x);
      if (op == 3) printf("%d\n",query(x));
     }
    return 0;
}