#include <cstdio>
#include <algorithm>
using namespace std;
class AVL{
public:
int value,height;
AVL *st,*dr;
AVL(int value,int height,AVL *st,AVL *dr){
this->value = value;
this->height = height;
this->st = st;
this->dr = dr;
}
}*root,*nil;
void init(){
root = nil = new AVL(0,0,NULL,NULL);
}
void Rotate_left(AVL *&n)
{
AVL *aux = n->st;
n->st = aux->dr;
n -> height = max(n->st->height,n->dr->height)+1;
aux->dr = n;
aux->height = max(aux->st->height,aux->dr->height) + 1;
n = aux;
}
void Rotate_right(AVL *&n)
{
AVL *aux = n->dr;
n->dr = aux->st;
n->height = max(n->st->height,n->dr->height)+1;
aux->st = n;
aux->height = max(aux->st->height,aux->dr->height) + 1;
n = aux;
}
void Balance(AVL *&n)
{
if(n->st->height > n->dr->height + 1)
Rotate_left(n);
else
if(n->st->height + 1 < n->dr->height )
Rotate_right(n);
n->height = max(n->st->height,n->dr->height) + 1;
}
void Delete(int value,AVL *&n)
{
if(n == nil) return;
if(n->value > value)
Delete(value,n->st);
else
if(n->value < value)
Delete(value,n->dr);
else
{
if(n->dr != nil)
{
Rotate_right(n);
Delete(value,n);
}
else
if(n->st != nil)
{
Rotate_left(n);
Delete(value,n);
}
else
{
delete n; n = nil;
return;
}
}
Balance(n);
}
void Insert(int value,AVL *&n)
{
if(n == nil){
n = new AVL(value,1,nil,nil);
return;
}
if(n->value > value)
Insert(value,n->st);
else if(n->value < value)
Insert(value,n->dr);
else return; /// exista deja
Balance(n);
}
void srd(AVL *nod){
if(nod == nil) return;
srd(nod->st);
printf("%d ",nod->value);
srd(nod->dr);
}
int Search(int value,AVL *nod){
if(nod == nil) return 0;
if(nod->value == value) return 1;
if(nod->value > value)
return Search(value,nod->st);
return Search(value,nod->dr);
}
int main()
{
freopen("hashuri.in","r",stdin);
freopen("hashuri.out","w",stdout);
init();
int N,op,x;
scanf("%d",&N);
for(int i = 1; i <= N; ++i)
{
scanf("%d%d",&op,&x);
if(op == 1)
Insert(x,root);
if(op == 2)
Delete(x,root);
if(op == 3)
printf("%d\n",Search(x,root));
}
return 0;
}