Cod sursa(job #1204353)

Utilizator IonMosnoiIon Mosnoi IonMosnoi Data 2 iulie 2014 18:35:55
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
#include<fstream>
#include<stdlib.h>
#include <time.h> 
using namespace std;
ifstream f("hashuri.in");
ofstream o("hashuri.out");

struct cell{
	int v;
	cell *l,*n;
	cell(){
		v=-1;
		l=n=0;
	}
};
cell *head,*dr[100];
int n,x,y,k,s;
int hight(){	

int h=0,coin=rand()%2;
    while (coin) {
          ++h;
          coin=rand()%2;
          }
    return(h==0?1:h);
	
//	return  rand()%51 + 1;
	
}
void cauta(int x){
	 s=0;
	 cell *a = head;
	 while(a){
	 	while(a->n && a->n->v<=x)a=a->n;
	 	s++;
	 	dr[s]=a;
	 	a=a->l;
	 }
}

void add(int x){
	cauta(x);

	if(dr[s]->v!=x){
		k = hight();
		cell *pre = 0;
		for(int i=1;i<=k;i++)
		 if(s>0){
		 	cell *a = new cell; 
			a->v=x; a->l=pre; 
			a->n=dr[s]->n; dr[s]->n = a;
		 	pre = a; s--;
		 }else{
		 	cell *a = new cell; 
		 	a->l = head;
		 	head = a;
		 	cell *b = new cell; 
		 	b->v = x;
		 	a->n = b;
			b->l = pre;
			pre = b;		 	
		 }
	}
}
void sterge(int x){
	cauta(x-1);
	while(s && dr[s]->n && dr[s]->n->v==x)
	dr[s]->n = dr[s--]->n->n;
}

int find(int x){
	cauta(x);
	if(dr[s]->v==x)return 1;
	return 0;
}

main(){
    head = new cell;
    dr[0]=head;
   f>>n;
   srand(time(NULL));
   for(int i=1;i<=n;i++){
   	f>>x>>y;
   	if(x==1)add(y);
   	else if(x==2) sterge(y); 
	     else o<<find(y)<<"\n";
   }
	
}