Pagini recente » Cod sursa (job #775534) | Cod sursa (job #1902924) | Cod sursa (job #993503) | Cod sursa (job #1391188) | Cod sursa (job #1695543)
#include <bits/stdc++.h>
using namespace std;
class Treap {
typedef struct _Node {
public:
int key, priority;
int m_size;
_Node *l, *r;
_Node(const int x) : key(x), priority(rand() & 1073741823), m_size(1), l(NULL), r(NULL) {
}
void update() {
this->m_size = 1;
if (l) {
this->m_size += this->l->m_size;
}
if (r) {
this->m_size += this->r->m_size;
}
}
} *Node;
Node merge(Node L, Node R) {
if (!L || !R)
return L ? L : R;
if (L->priority < R->priority) {
L->r = merge(L->r, R);
L->update();
return L;
} else {
R->l = merge(L, R->l);
R->update();
return R;
}
}
void split(Node T, const int key, Node &L, Node &R) {
L = R = NULL;
if (T) {
if (T->key < key) {
split(T->r, key, T->r, R);
L = T;
} else {
split(T->l, key, L, T->l);
R = T;
}
T->update();
}
}
bool find(Node T, const int key) {
if (!T)
return 0;
if (T->key == key)
return 1;
if (key < T->key)
return find(T->l, key);
else
return find(T->r, key);
}
Node root;
public:
Treap() : root(NULL) {
srand(time(0));
}
bool find(const int key) {
return find(root, key);
}
void insert(const int key) {
if (!find(key)) {
Node L, R;
split(root, key, L, R);
root = merge(merge(L, new _Node(key)), R);
}
}
void erase(const int key) {
if (find(key)) {
Node L, R, M;
split(root, key, L, M);
split(M, key + 1, M, R);
delete M;
root = merge(L, R);
}
}
} T;
int main() {
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
fin.tie(0);
ios_base::sync_with_stdio(false);
int n; fin >> n;
for (int i = 0; i < n; ++ i) {
int opType, key; fin >> opType >> key;
if (opType == 1)
T.insert(key);
else if (opType == 2)
T.erase(key);
else
fout << T.find(key) << '\n';
}
fin.close();
fout.close();
return 0;
}