Pagini recente » Cod sursa (job #2234028) | Cod sursa (job #765120) | Cod sursa (job #500923) | Cod sursa (job #2599251) | Cod sursa (job #1518605)
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int key;
int priority;
Node *st, *dr;
Node(const int k, const int p, Node *l, Node *r)
{
key = k;
priority = p;
st = l;
dr = r;
}
};
Node *Root, *NIL;
void initTreap()
{
NIL = new Node(0, 0, nullptr, nullptr);
Root = NIL;
}
void rotateToLeft(Node *&T)
{
Node *aux = T->dr;
T->dr = aux->st;
aux->st = T;
T = aux;
}
void rotateToRight(Node *&T)
{
Node *aux = T->st;
T->st = aux->dr;
aux->dr = T;
T = aux;
}
void balance(Node *&T)
{
if (T->st->priority > T->priority)
rotateToRight(T);
else if (T->dr->priority > T->priority)
rotateToLeft(T);
}
void insert(Node *&T, const int key, const int priority)
{
if (T == NIL)
{
T = new Node(key, priority, NIL, NIL);
}
else
{
if (key < T->key)
insert(T->st, key, priority);
else if (key > T->key)
insert(T->dr, key, priority);
balance(T);
}
}
bool find(Node *&T, const int key)
{
if (T == NIL)
return false;
if (T->key == key)
return true;
if (key < T->key)
return find(T->st, key);
else
return find(T->dr, key);
}
void erase(Node *&T, const int key)
{
if (T == NIL)
return;
if (key < T->key)
erase(T->st, key);
else if (key > T->key)
erase(T->dr, key);
else
{
if (T->st == NIL && T->dr == NIL)
{
delete T;
T = NIL;
}
else
{
if (T->st->priority > T->dr->priority)
{
rotateToRight(T);
erase(T->dr, key);
}
else
{
rotateToLeft(T);
erase(T->st, key);
}
}
}
}
int main()
{
ifstream in("hashuri.in");
ofstream out("hashuri.out");
int N;
in >> N;#include <bits/stdc++.h>
using namespace std;
const int MAX_SIZE = 1 << 21;
const int EMPTY = -1;
const int ERASED = -2;
const int MASK = MAX_SIZE - 1;
int table[MAX_SIZE];
void insert(const int key)
{
int p = key & MASK;
while (table[p] != ERASED && table[p] != EMPTY)
p = (p + 1) & MASK;
table[p] = key;
}
void erase(const int key)
{
int p = key & MASK;
while (table[p] != EMPTY && table[p] != key)
p = (p + 1) & MASK;
if (table[p] == key)
table[p] = ERASED;
}
bool find(const int key)
{
int p = key & MASK;
while (table[p] != EMPTY && table[p] != key)
p = (p + 1) & MASK;
return (table[p] == key);
}
int main()
{
ifstream in("hashuri.in");
ofstream out("hashuri.out");
for (int i = 0; i < MAX_SIZE; ++i)
table[i] = EMPTY;
int N;
in >> N;
while (N--)
{
int t, x;
in >> t >> x;
if (t == 1)
insert(x);
else if (t == 2)
erase(x);
else
out << find(x) << "\n";
}
return 0;
}
initTreap();
while (N--)
{
int t, x;
in >> t >> x;
if (t == 1)
insert(Root, x, rand() % 100000000);
else if (t == 2)
erase(Root, x);
else
out << find(Root, x) << "\n";
}
return 0;
}