Pagini recente » Cod sursa (job #3268960) | Cod sursa (job #79668) | Cod sursa (job #2557338) | Cod sursa (job #1980880) | Cod sursa (job #2821419)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
struct treap {
int key, prior;
treap *l, *r;
treap() {}
treap(int key) : key(key), prior((1ll * rand() * rand())%INT_MAX), l(nullptr), r(nullptr) {}
};
void split(treap* t, int key, treap*& l, treap*& r) {
if (t == nullptr) {
l = r = nullptr;
return;
}
if (t->key <= key) {
split(t->r, key, t->r, r);
l = t;
}
else {
split(t->l, key, l, t->l);
r = t;
}
}
void merge(treap*& t, treap* l, treap* r) {
if (l == nullptr || r == nullptr) {
t = (l == nullptr ? r : l);
return;
}
if (l->prior > r->prior) {
merge(l->r, l->r, r);
t = l;
}
else {
merge(r->l, l, r->l);
t = r;
}
}
void insert(treap*& t, treap* it) {
if (t == nullptr) {
t = it;
return;
}
if (it->prior > t->prior) {
split(t, it->key, it->l, it->r);
t = it;
}
else {
if (t->key < it->key) {
insert(t->r, it);
}
else {
insert(t->l, it);
}
}
}
void erase(treap*& t, int key) {
if (t == nullptr) {
return;
}
if (t->key == key) {
merge(t, t->l, t->r);
}
else {
if (t->key < key) {
erase(t->r, key);
}
else {
erase(t->l, key);
}
}
}
bool find(treap* t, int key) {
if (t == nullptr) {
return false;
}
if (t->key == key) {
return true;
}
else {
if (t->key < key) {
return find(t->r, key);
}
else {
return find(t->l, key);
}
}
}
int main() {
std::ios_base::sync_with_stdio(false);
cin.tie(nullptr);
srand(time(NULL));
treap* t = nullptr;
int q;
fin >> q;
while (q--) {
int op, val;
fin >> op >> val;
bool este = find(t, val);
if (op == 1) {
if (este) {
continue;
}
insert(t, new treap(val));
}
if (op == 2) {
erase(t, val);
}
if (op == 3) {
fout << este << '\n';
}
}
}