Pagini recente » Cod sursa (job #1717022) | Cod sursa (job #157254) | Cod sursa (job #1249397) | Cod sursa (job #2200616) | Cod sursa (job #1171197)
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <cassert>
#include <deque>
using namespace std;
const char infile[] = "hashuri.in";
const char outfile[] = "hashuri.out";
ofstream fout(outfile);
const int MAXN = 100005;
const int oo = (1 << 30) - 1;
typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;
const inline int min(const int &a, const int &b) { if( a > b ) return b; return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b; return a; }
const inline void Get_min(int &a, const int b) { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b) { if( a < b ) a = b; }
const int lim = (1 << 20);
char buff[lim];
int pos;
inline void getInt(int &x) {
x = 0;
while(!('0' <= buff[pos] && buff[pos] <= '9'))
if(++ pos == lim) {
pos =0 ;
fread(buff, 1, lim, stdin);
}
while('0' <= buff[pos] && buff[pos] <= '9') {
x = x * 10 + buff[pos] - '0';
if(++ pos == lim) {
pos = 0;
fread(buff, 1, lim, stdin);
}
}
}
struct Treap {
Treap *left, *right;
int key, priority;
Treap(Treap *_left, Treap *_right, int _key, int _priority) {
left = _left;
right = _right;
key = _key;
priority = _priority;
}
} *Root, *NIL;
int M;
inline void rotateLeft(Treap *&Node) {
Treap *aux = Node->left;
Node->left = aux->right;
aux->right = Node;
Node = aux;
}
inline void rotateRight(Treap *&Node) {
Treap *aux = Node->right;
Node->right = aux->left;
aux->left = Node;
Node = aux;
}
inline void Balance(Treap *&Node) {
if(Node->left->priority > Node->priority)
rotateLeft(Node);
if(Node->right->priority > Node->priority)
rotateRight(Node);
}
inline void Insert(Treap *&Node, int key, int priority) {
if(Node == NIL) {
Node = new Treap(NIL, NIL, key, priority);
return;
}
if(Node->key == key)
return;
if(key < Node->key)
Insert(Node->left, key, priority);
else Insert(Node->right, key, priority);
Balance(Node);
}
inline bool Find(Treap *&Node, int key) {
if(Node == NIL)
return 0;
if(Node->key == key)
return 1;
if(key < Node->key)
return Find(Node->left, key);
else return Find(Node->right, key);
}
inline void Erase(Treap *&Node, int key) {
if(key < Node->key)
Erase(Node->left, key);
else if(Node->key < key)
Erase(Node->right, key);
else {
if(Node->left == NIL && Node->right == NIL) {
delete Node;
Node = NIL;
}
else {
if(Node->left->priority > Node->right->priority) {
rotateLeft(Node);
Erase(Node->right, key);
}
else {
rotateRight(Node);
Erase(Node->left, key);
}
}
}
}
int main() {
freopen(infile, "r", stdin);
NIL = new Treap(0, 0, 0, 0);
Root = NIL;
getInt(M);
for(int i = 1 ; i <= M ; ++ i) {
int op, x;
getInt(op);
getInt(x);
switch(op) {
case 1:
Insert(Root, x, rand() % oo + 1);
break;
case 2:
if(Find(Root, x))
Erase(Root, x);
break;
case 3:
fout << Find(Root, x) << '\n';
break;
}
}
fout.close();
return 0;
}