Cod sursa(job #495809)

Utilizator theodora_maneaManea Theodora Maria theodora_manea Data 26 octombrie 2010 21:31:50
Problema Hashuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <stdio.h>
#include <stdlib.h>

struct point {
	int inf;
	point *leg;
};
point *h[676014];
long i,n,op,nr,n1,x;

/* void creare(long &n1) {
	long m,x;
	m=(n*n*n)/3;
    x=1;
    while(x<=m) x*=2;
    m=(x/2+x)/2;
	n1=m;
} */

void insert(int x, int nr) {
	point *p;
	p=new point;
	p->inf=nr;
	p->leg=h[x];
	h[x]=p;
}

void cauta(point *p,int nr,int ok) {
	point *q,*t;
	int ok1;
	ok1=0;
	if (p==NULL) {
		if (ok==1) printf("0\n");
		return;
	}
	if (p->inf==nr) {
		t=p;
		if (p->leg==NULL) ok1=1;
		p=p->leg;
		delete t;
		if (ok1) h[x]=NULL;
		if (ok==1) printf("1\n");
		return;
	}
	while (p!=NULL && p->inf!=nr) {
		q=p;
		p=p->leg;
	}
	if (ok==1) {
		if (p==NULL) printf("0\n");
		else printf("1\n");
	}
	else 
		if (p!=NULL) {
			t=p;
			q->leg=p->leg;
			delete t;
		}
}

int main () {
	freopen("hashuri.in","r",stdin);
	freopen("hashuri.out","w",stdout);
	scanf("%ld",&n);
	n1=676013;
	//creare(n1);
	for (i=1; i<=n; i++) {
		scanf("%ld%ld",&op,&nr);
		if (op==1) {
			x=nr%n1;
			insert(x,nr);
		}
		else
			if (op==2) {
				x=nr%n1;
				cauta(h[x],nr,0);
			}
			else 
				if (op==3) {
					x=nr%n1;
					cauta(h[x],nr,1);
				}
	}
    return 0;
}