Pagini recente » Rating Matei Hermina (herminamatei) | Cod sursa (job #1208973) | Cod sursa (job #1846208) | Istoria paginii runda/miercuri_ora_10.00/clasament | Cod sursa (job #1183813)
#include <iostream>
#include <fstream>
#include <set>
#include <cstdlib>
#include <ctime>
using namespace std;
const int Nodes_Max = 1000000;
struct Treap
{
int key;
int priority;
int left, right;
Treap()
{
key = 0;
priority = -1;
left = 0;
right = 0;
}
void Tclear( int a, int b )
{
key = a;
priority = b;
left = right = 0;
}
} T[Nodes_Max + 1];
int max_dim, root;
inline void rol( int &node )
{
int aux = T[node].left;
T[node].left = T[aux].right;
T[aux].right = node;
node = aux;
}
inline void ror( int &node )
{
int aux = T[node].right;
T[node].right = T[aux].left;
T[aux].left = node;
node = aux;
}
inline void balance( int &node )
{
if ( T[ T[node].left ].priority > T[node].priority )
rol( node );
if ( T[ T[node].right ].priority > T[node].priority )
ror( node );
}
void insert( int &node, int key, int prior = rand() + 1 )
{
if ( node == 0 )
{
node = ++max_dim;
T[node].Tclear( key, prior );
}
else
{
if ( key < T[node].key )
insert( T[node].left, key, prior );
else
if ( key > T[node].key )
insert( T[node].right, key, prior );
balance( node );
}
}
void erase( int &node, int key )
{
if ( node == 0 ) return;
if ( key < T[node].key )
erase( T[node].left, key );
else
if ( key > T[node].key )
erase( T[node].right, key );
else
{
if ( T[node].left == 0 && T[node].right == 0 )
{
node = 0;
}
else
{
if ( T[node].left && T[node].right )
{
if ( T[ T[node].left ].priority > T[ T[node].right ].priority )
rol( T[node].left );
else
ror( T[node].right );
}
else
{
if ( T[node].left )
rol( T[node].left );
else
ror( T[node].right );
}
erase( node, key );
}
}
if ( node ) balance( node );
}
int find( int ind, int value )
{
if ( ind == 0 ) return 0;
if ( T[ind].key == value ) return 1;
if ( T[ind].key > value ) return find( T[ind].left, value );
else return find( T[ind].right, value );
}
int N, type, key;
int main()
{
freopen("hashuri.in", "r", stdin);
freopen("hashuri.out", "w", stdout);
scanf("%d", &N);
while ( N-- )
{
scanf("%d %d", &type, &key);
if ( type == 1 )
{
insert( root, key );
}
if ( type == 2 )
{
///erase( root, key );
}
if ( type == 3 )
{
printf("%d\n", find( root, key ));
}
}
return 0;
}