Pagini recente » Cod sursa (job #2022989) | Cod sursa (job #173321) | Cod sursa (job #1892238) | Cod sursa (job #291940) | Cod sursa (job #1971385)
#include <fstream>
#include <cstdio>
#include <utility>
#include <chrono>
#include <random>
//12:36
using namespace std;
using namespace chrono;
class InputReader {
public:
InputReader(const char *fileName) {
in = fopen(fileName, "r");
fread(buffer, 1, SIZE, in);
cursor = 0;
}
template <typename T>
InputReader& operator>>(T &nr) {
while (!isdigit(buffer[cursor]))
advance();
nr = 0;
while (isdigit(buffer[cursor])) {
nr *= 10;
nr += buffer[cursor] - '0';
advance();
}
return (*this);
}
private:
FILE *in;
static const int SIZE = (1 << 17);
int cursor;
char buffer[SIZE];
void advance() {
++ cursor;
if (cursor == SIZE) {
fread(buffer, 1, SIZE, in);
cursor = 0;
}
}
};
class OutputWriter {
public:
OutputWriter(const char *fileName) {
out = fopen(fileName, "w");
cursor = 0;
}
~OutputWriter() {
flush();
}
OutputWriter& operator<<(const int &ch) {
char digits[4];
int cnt = 0;
int _ch = ch;
do {
digits[cnt ++] = _ch % 10 + '0';
_ch /= 10;
} while (_ch);
for (int i = cnt - 1; i >= 0; -- i)
(*this) << digits[i];
return (*this);
}
OutputWriter& operator<<(const char &ch) {
if (cursor == SIZE)
flush();
buffer[cursor ++] = ch;
return (*this);
}
void flush() {
fwrite(buffer, 1, cursor, out);
cursor = 0;
}
private:
static const int SIZE = (1 << 17);
FILE *out;
char buffer[SIZE];
int cursor;
};
struct Treap {
int key, pr;
Treap *left, *right;
} *root, *nil;
pair <Treap*, Treap*> split(Treap *t, int key) {
if (t == nil)
return make_pair(nil, nil);
pair <Treap*, Treap*> aux;
if (key < t -> key) {
aux = split(t -> left, key);
t -> left = aux.second;
aux.second = t;
}
else {
aux = split(t -> right, key);
t -> right = aux.first;
aux.first = t;
}
return aux;
}
Treap* join(Treap *l, Treap *r) {
if (l == nil)
return r;
if (r == nil)
return l;
if (l -> pr > r -> pr) {
l -> right = join(l -> right, r);
return l;
}
else {
r -> left = join(l, r -> left);
return r;
}
}
void insert(Treap* &t, int key, int pr) {
if (pr > t -> pr) {
auto aux = split(t, key);
t = new Treap;
t -> key = key;
t -> pr = pr;
t -> left = aux.first;
t -> right = aux.second;
return ;
}
else if (key <= t -> key)
insert(t -> left, key, pr);
else
insert(t -> right, key, pr);
}
void erase(Treap* &t, int key) {
if (t -> key == key) {
Treap *aux = t;
t = join(t -> left, t -> right);
delete aux;
return ;
}
else if (key < t -> key)
erase(t -> left, key);
else
erase(t -> right, key);
}
bool find(Treap *t, int key) {
if (t == nil)
return false;
else if (t -> key == key)
return true;
else if (key < t -> key)
return find(t -> left, key);
else
return find(t -> right, key);
}
int main()
{
InputReader cin("hashuri.in");
OutputWriter cout("hashuri.out");
auto now = system_clock :: now();
auto d = now.time_since_epoch();
mt19937 gen(duration_cast <milliseconds>(d).count());
uniform_int_distribution <int> dist(1, (1 << 29));
nil = new Treap;
nil -> key = nil -> pr = -1;
nil -> left = nil -> right = nil;
root = nil;
int M = 0;
cin >> M;
for (int i = 1; i <= M; ++ i) {
int type, x;
cin >> type >> x;
if (type == 1)
insert(root, x, dist(gen));
else if (type == 2) {
if (find(root, x))
erase(root, x);
}
else
cout << find(root, x) << '\n';
}
return 0;
}