Pagini recente » Cod sursa (job #770360) | Cod sursa (job #1378659) | Cod sursa (job #1564828) | Cod sursa (job #735128) | Cod sursa (job #1322572)
#include <bits/stdc++.h>
using namespace std;
const int NR_NODES = 1 << 19;
const int INF = 1 << 30;
struct Treap
{
int st, dr;
int key, priority;
Treap(int k = 0, int p = 0)
{
key = k;
priority = p;
st = dr = 0;
}
};
Treap T[NR_NODES];
int root, size;
inline void rol( int &node )
{
int aux = T[node].st;
T[node].st = T[aux].dr;
T[aux].dr = node;
node = aux;
}
int merge(int L, int R)
{
if ( !L || !R )
return L ? L : R;
if ( T[L].priority > T[R].priority )
{
T[L].dr = merge( T[L].dr, R );
return L;
}
else
{
T[R].st = merge( L, T[R].st );
return R;
}
}
void split(int node, int value, int &L, int &R)
{
L = R = 0;
if ( !node ) return;
if ( T[node].key < value )
{
split( T[node].dr, value, T[node].dr, R );
L = node;
}
else
{
split( T[node].st, value, L, T[node].st );
R = node;
}
}
bool find(int node, int k)
{
if ( !node ) return false;
if ( T[node].key == k ) return true;
if ( k < T[node].key ) return find( T[node].st, k );
else return find( T[node].dr, k );
}
void insert(int key)
{
if ( find(root, key) ) return;
int L, R;
split(root, key, L, R);
int node = ++size;
T[node] = Treap(key, rand() % INF);
root = merge( merge( L, node ), R );
}
void erase(int key)
{
if ( find(root, key) == 0 ) return;
int L, M, R;
split(root, key, L, M);
split(M, key + 1, M, R);
T[M] = Treap(0, 0);
M = 0;
root = merge( L, R );
}
void print(int node)
{
if ( !node ) return;
print( T[node].st );
cout << T[node].key << " ";
print( T[node].dr );
}
int main()
{
ifstream in("hashuri.in");
ofstream out("hashuri.out");
ios_base::sync_with_stdio( false );
int N;
srand( time( 0 ) );
in >> N;
while ( N-- )
{
int tip, x;
in >> tip >> x;
switch(tip)
{
case 1: insert(x); break;
case 2: erase(x); break;
case 3: out << find(root, x) << "\n"; break;
default: cerr << "Error\n";
}
}
return 0;
}