Cod sursa(job #1218673)

Utilizator popoiu.georgeGeorge Popoiu popoiu.george Data 12 august 2014 11:47:12
Problema Zeap Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Semestrul 2 Marime 3 kb
#include <algorithm>
using namespace std;

#define INF 0x3f3f3f3f
#define DIM 10

struct treap
{
	int key,priority,min,max,dif_min;
	treap *left,*right;

	treap (int key,int priority,int min,int max,int dif_min,treap *left,treap *right)
	{
		this->key=key;
		this->priority=priority;
		this->min=min;
		this->max=max;
		this->dif_min=dif_min;
		this->left=left;
		this->right=right;
	}
} *rad,*nil;
char buff[DIM];
int n;

void update (treap *&nod)
{
    nod->min=min (nod->key,nod->left->min);
	nod->max=max (nod->key,nod->right->max);
	nod->dif_min=min (min (nod->left->dif_min,nod->right->dif_min),min (abs (nod->key-nod->left->max),abs (nod->key-nod->right->min)));
}

void rot_left (treap *&nod)
{
	treap *fiu=nod->left;

	nod->left=fiu->right;
	fiu->right=nod;
	nod=fiu;
	if (nod!=nil && nod->right!=nil)
        update (nod->right);
}

void rot_right (treap *&nod)
{
	treap* fiu=nod->right;

	nod->right=fiu->left;
	fiu->left=nod;
	nod=fiu;
	if (nod!=nil && nod->left!=nil)
		update (nod->left);
}

void balance (treap *&nod)
{
	if (nod->left->priority>nod->priority)
		rot_left (nod);
	if (nod->right->priority>nod->priority)
		rot_right (nod);
	update (nod);
}

void insert (treap *&nod,int key)
{
	if (nod==nil)
	{
		nod=new treap (key,rand (),key,key,INF,nil,nil);
		++n;
		return ;
	}
	if (key<nod->key)
		insert (nod->left,key);
	else if (key>nod->key)
		insert (nod->right,key);
	balance (nod);
}

int find (treap *&nod,int key)
{
	if (nod==nil)
		return 0;
	if (key==nod->key)
        return 1;
	if (key<nod->key)
		return find (nod->left,key);
	else
		return find (nod->right,key);
}

void erase (treap *&nod,int key)
{
	if (nod==nil)
		return ;
	if (key<nod->key)
		erase (nod->left,key);
	else if (key>nod->key)
        erase (nod->right,key);
    else
    {
        if (nod->left==nil && nod->right==nil)
        {
            delete nod;
            nod=nil;
        }
        else
        {
            if (nod->left->priority>nod->right->priority)
                rot_left (nod);
            else
                rot_right (nod);
            erase (nod,key);
        }
    }
	if (nod!=nil)
		update (nod);
}

int main ()
{
	srand (time (0));
	freopen ("zeap.in","r",stdin);
	freopen ("zeap.out","w",stdout);
    int x;

	rad=nil=new treap(0,0,INF,-INF,INF,NULL,NULL);
	for ( ; scanf ("%s",&buff)!=EOF; )
	{
		if (buff[0]=='I')
		{
			scanf ("%d",&x);
			insert (rad,x);
		}
		else if (buff[0]=='C')
		{
			scanf ("%d",&x);
			printf ("%d\n",find (rad,x));
		}
        else if (buff[0]=='S')
        {
			scanf ("%d",&x);
			if (!find (rad,x))
				printf ("-1\n");
			else
			{
				erase (rad,x);
				--n;
			}
		}
		else if (buff[0]=='M' && buff[1]=='A')
		{
			if (n<2)
                printf ("-1\n");
			else
				printf ("%d\n",rad->max-rad->min);
		}
        else
        {
			if (n<2)
                printf ("-1\n");
			else
				printf ("%d\n", rad->dif_min);
		}
	}

	return 0;
}