Pagini recente » Cod sursa (job #1174816) | Cod sursa (job #1207727) | Cod sursa (job #1149089) | Cod sursa (job #890521) | Cod sursa (job #1208794)
#include <cstdio>
#include <cstring>
#include <time.h>
#include <cstdlib>
#define Hmax 34
#define INF 0x3f3f3f3f
using namespace std;
class SkipList{
public:
int value,nivel;
SkipList *next[Hmax],*prev;
SkipList(int value,int nivel){
this->value = value;
this->nivel = nivel;
memset(next,0,sizeof(next));
prev = NULL;
}
}*root,*path[Hmax];
int H;
void init(){
srand(unsigned(time(0)));
root = new SkipList(-2*INF,Hmax-2);
for(int i = 0; i < Hmax; ++i)
path[i] = root;
}
int Get_lvl(){
int lvl = 0, x = rand();
while(x & 1) x >>= 1, ++lvl;
if(lvl > H) lvl = ++H;
return lvl;
}
int Search(int value)
{
SkipList *nodc = root;
for(int i = H; i < Hmax; ++i)
path[i] = nodc;
for(int i = nodc->nivel; i >= 0; --i)
{
for(nodc; nodc->next[i] && nodc->next[i]->value < value; nodc = nodc->next[i]);
path[i] = nodc;
}
if(nodc->next[0] && nodc->next[0]->value == value) return 1;
return 0;
}
void Insert(int value)
{
if(Search(value)) return;
SkipList *nodc = new SkipList(value,Get_lvl());
if(path[0]->next[0])
path[0]->next[0]->prev = nodc;
nodc->prev = path[0];
for(int i = 0; i <= nodc->nivel; ++i)
{
nodc->next[i] = path[i]->next[i];
path[i]->next[i] = nodc;
}
}
void Delete(int value)
{
if(!Search(value)) return;
SkipList *nodc = path[0]->next[0];
for(int i = 0; i <= nodc->nivel; ++i)
if(path[i]->next[i] == nodc)
path[i]->next[i] = nodc->next[i];
delete nodc;
}
#define DIM 66613
char buffer[DIM];
int poz = DIM - 1;
void scan(int &A)
{
A = 0;
while('0' > buffer[poz] || buffer[poz] > '9')
if(++poz == DIM) fread(buffer,1,DIM,stdin),poz = 0;
while('0' <= buffer[poz] && buffer[poz] <= '9')
{
A = A * 10 + buffer[poz] - 48;
if(++poz == DIM)
fread(buffer,1,DIM,stdin),poz = 0;
}
}
int main()
{
freopen("hashuri.in","r",stdin);
freopen("hashuri.out","w",stdout);
init();
int N,op,x;
scan(N);
for(int i = 1; i <= N; ++i){
scan(op),scan(x);
if(op == 1)
Insert(x);
if(op == 2)
Delete(x);
if(op == 3)
printf("%d\n",Search(x));
}
return 0;
}