Cod sursa(job #3295253)

Utilizator CosminaneBoac Mihai Cosmin Cosminane Data 3 mai 2025 19:45:01
Problema Zeap Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.94 kb
#include <fstream>
#include <iostream>
using namespace std;
struct treap{
	int key, priority, min1, max1, mind;
	treap *st, *dr;
	treap(int key, int priority, treap* st, treap* dr){
		this->key = this->min1 = this->max1 = this->mind = key;
		this->priority = priority;
		this->st = st;
		this->dr = dr;
		this->mind = 1e9;
	}
};
treap *root, *nil;
void rot_st( treap *&nod){
	treap *t = nod->st;
	nod->st = t->dr;
	t->dr = nod;
	nod = t;
}
void rot_dr( treap *&nod ){
	treap *t = nod->dr;
	nod->dr = t->st;
	t->st = nod;
	nod = t;
}
void vf( treap *&nod ){
	if( nod == nil ){
		return;
	}
	nod->min1 = nod->max1 = nod->key;
	nod->mind = 1e9;
	if( nod->st != nil ){
		nod->min1 = nod->st->min1;
		nod->mind = min( nod->st->mind, nod->mind );
		nod->mind = min( nod->key - nod->st->max1, nod->mind );
	}
	if( nod->dr != nil ){
		nod->max1 = nod->dr->max1;
		nod->mind = min( nod->dr->mind, nod->mind );
		nod->mind = min( nod->dr->min1 - nod->key, nod->mind );
	}
}
void balance(treap *&nod){
	if( nod == nil ){
		return;
	}
	if( nod->st->priority > nod->priority ){
		rot_st( nod );
	}
	else if( nod->dr->priority > nod->priority ){
		rot_dr( nod );
	}
	vf( nod->st );
	vf( nod->dr );
	vf( nod );
}
void insert(treap *&nod, int key, int priority){
	if( nod == nil ){
		nod = new treap( key, priority, nil, nil );
		return;
	}
	if( key <= nod->key ){
		insert( nod->st, key, priority );
	}
	else if( key > nod->key ){
		insert( nod->dr, key, priority );
	}
	balance( nod );
}
void erase(treap *&nod, int key){
	//cout << nod->key << ' ' << key << '\n';
	if( key < nod->key ){
		erase( nod->st, key );
	}
	else if( key > nod->key ){
		erase( nod->dr, key );
	}
	else{
		if( nod->st == nil && nod->dr == nil ){
			delete nod;
			nod = nil;
			return;
		}
		if( nod->st->priority >= nod->dr->priority ){
			rot_st( nod );
		}
		else{
			rot_dr( nod );
		}
		erase( nod, key );
	}
	balance( nod );
}
bool search(treap *&nod, int key){
	if( nod == nil ){
		return false;
	}
	if( nod->key == key ){
		return true;
	}
	if( key < nod->key ){
		return search( nod->st, key );
	}
	return search( nod->dr, key );
}
int main(){
	int x;
	string s;
	ifstream fin( "zeap.in" );
	ofstream fout( "zeap.out" );
	root = nil = new treap( 0, 0, NULL, NULL );
	while( fin >> s ){
		if( s == "I" ){
			fin >> x;
			if( search(root, x) == false ){
				insert(root, x, rand() + 1);
			}
		}
		else if( s == "S" ){
			fin >> x;
			if( search( root, x ) == true ){
				erase( root, x );
			}
			else{
				fout << -1 << '\n';
			}
		}
		else if( s == "C" ){
			fin >> x;
			fout << search( root, x ) << '\n';
		}
		else if( s == "MIN" ){
			if( root->mind == 1e9 ){
				fout << -1 << '\n';
			}
			else{
				fout << root->mind << '\n';
			}
		}
		else{
			if( root->mind == 1e9 ){
				fout << -1 << '\n';
			}
			else{
				fout << root->max1 - root->min1 << '\n';
			}
		}
	}
	return 0;
}