Pagini recente » Cod sursa (job #2231983) | Cod sursa (job #2761462) | Cod sursa (job #3256439) | Cod sursa (job #619756) | Cod sursa (job #1341607)
#include <fstream>
#include <iostream>
using namespace std;
template <typename T>
class Deque {
private:
struct Node {
T item;
Node * prev, * next;
Node(T newItem) {
prev = next = nullptr;
item = newItem;
}
};
Node * first, * last;
public:
Deque() {
first = last = nullptr;
}
void push_front(T newItem) {
Node * node = new Node(newItem);
if(first == nullptr)
first = last = node;
else {
first->prev = node;
node->next = first;
first = node;
}
}
void pop_front() {
if(first == nullptr)
return;
if(first->next == nullptr)
first = last = nullptr;
else {
first = first->next;
delete first->prev;
first->prev = nullptr;
}
}
void push_back(T newItem) {
Node * node = new Node(newItem);
if(last == nullptr)
first = last = node;
else {
last->next = node;
node->prev = last;
last = node;
}
}
void pop_back() {
if(last == nullptr)
return;
if(last->prev == nullptr)
first = last = nullptr;
else {
last = last->prev;
delete last->next;
last->next = nullptr;
}
}
Node * begin() {
return first;
}
Node * end() {
return last;
}
T front() {
return first->item;
}
T back() {
return last->item;
}
bool empty() {
return (first == nullptr);
}
};
template <typename T>
class Queue {
private:
struct Node {
T item;
Node * next;
Node(T newItem) {
next = nullptr;
item = newItem;
}
};
Node * first, * last;
public:
Queue() {
first = last = nullptr;
}
void push(T newItem) {
Node * node = new Node(newItem);
if(last == nullptr)
first = last = node;
else {
last->next = node;
last = node;
}
}
void pop() {
if(first == nullptr)
return;
Node * node = first;
if(first->next == nullptr)
first = last = nullptr;
else
first = first->next;
delete node;
}
T front() {
return first->item;
}
T end() {
return last->item;
}
bool empty() {
return (last == nullptr);
}
};
template <typename T>
class Stack {
private:
struct Node {
T item;
Node * prev;
Node(T newItem) {
prev = nullptr;
item = newItem;
}
};
Node * last;
public:
Stack() {
last = nullptr;
}
void push(T newItem) {
Node * node = new Node(newItem);
node->prev = last;
last = node;
}
void pop() {
if(last == nullptr)
return;
Node * node = last->prev;
delete last;
last = node;
}
T top() {
return last->item;
}
bool empty() {
return (last == nullptr);
}
};
class Hash {
#define mod 2000001
private:
struct Node {
int key;
short state;
Node() {
state = 0;
}
};
Node H[mod];
int findSlot(int key, int constraint1, int constraint2) {
int index = key % mod;
while(H[index].state != constraint1 && H[index].state != constraint2 && H[index].key != key) {
++index;
if(index == mod)
index = 0;
}
return index;
}
public:
Hash() {
}
void insert(int key) {
int index = findSlot(key, -1, 0);
H[index].key = key;
H[index].state = 1;
}
bool search(int key) {
return (H[findSlot(key, 0, 0)].key == key);
}
void remove(int key) {
int index = findSlot(key, 0, 0);
if(H[index].state != 0) {
H[index].key = -1;
H[index].state = -1;
}
}
} h;
template <typename T>
class binarySearchTree {
private:
struct Node {
T key;
Node * left, * right;
Node() {
left = right = nullptr;
}
};
Node * root;
Node * insert(Node * node, T & newKey) {
if(node == nullptr) {
node = new Node;
node->key = newKey;
return node;
}
if(newKey < node->key)
node->left = insert(node->left, newKey);
if(node->key < newKey)
node->right = insert(node->right, newKey);
return node;
}
bool search(Node * node, T & key) {
if(node == nullptr)
return false;
if(node->key == key)
return true;
else
if(key < node->key)
return search(node->left, key);
else
return search(node->right, key);
}
Node * erase(Node * node, T & key) {
if(node == nullptr)
return nullptr;
if(node->key == key) {
Node * p;
if(node->left == nullptr || node->right == nullptr) {
p = (node->left == nullptr ? node->right : node->left);
delete node;
return p;
}
for(p = node->left; p->right != nullptr; p = p->right);
node->key = p->key;
node->left = erase(node->left, node->key);
}
else
if(key < node->key)
node->left = erase(node->left, key);
else
node->right = erase(node->right, key);
return node;
}
public:
binarySearchTree() {
root = nullptr;
}
void insert(T newKey) {
root = insert(root, newKey);
}
bool search(T key) {
return search(root, key);
}
void erase(T key) {
root = erase(root, key);
}
};
template <typename T>
class AVL {
private:
struct Node {
T key;
int height;
Node * left, * right;
Node(Node * node, int Height) {
height = Height;
left = right = node;
}
};
Node * root, * NIL;
Node * insert(Node * node, T & newKey) {
if(node == NIL) {
node = new Node(NIL, 0);
node->key = newKey;
return node;
}
if(newKey < node->key)
node->left = insert(node->left, newKey);
if(node->key < newKey)
node->right = insert(node->right, newKey);
return balance(node);
}
bool search(Node * node, T & key) {
if(node == nullptr)
return false;
if(node->key == key)
return true;
else
if(key < node->key)
return search(node->left, key);
else
return search(node->right, key);
}
Node * erase(Node * node, T & key) {
if(node == NIL)
return NIL;
if(node->key == key) {
Node * p;
if(node->left == NIL || node->right == NIL) {
p = (node->left == NIL ? node->right : node->left);
delete node;
return p;
}
for(p = node->left; p->right != NIL; p = p->right);
node->key = p->key;
node->left = erase(node->left, node->key);
}
else
if(key < node->key)
node->left = erase(node->left, key);
else
node->right = erase(node->right, key);
return balance(node);
}
void setHeight(Node * node) {
node->height = 1 + max(node->left->height, node->right->height);
}
Node * leftRotate(Node * node) {
Node * p = node->right;
p->left = node;
node->right = p->left;
setHeight(node);
setHeight(p);
return p;
}
Node * rightRotate(Node * node) {
Node * p = node->left;
p->right = node;
node->left = p->right;
setHeight(node);
setHeight(p);
return p;
}
Node * balance(Node * node) {
setHeight(node);
if(node->left->height + 1 < node->right->height) {
if(node->right->left->height > node->right->right->height)
node->right = rightRotate(node->right);
node = leftRotate(node);
}
if(node->right->height + 1 < node->right->height) {
if(node->left->right-> height > node->left->left->height)
node->left = leftRotate(node->left);
node = rightRotate(node);
}
return node;
}
public:
AVL() {
NIL = new Node(NIL, -1);
root = NIL;
}
void insert(T newKey) {
root = insert(root, newKey);
}
bool search(T key) {
return search(root, key);
}
void erase(T key) {
root = erase(root, key);
}
};
int main() {
int type, x, queries;
binarySearchTree <int> bst;
ifstream in("hashuri.in");
ofstream out("hashuri.out");
in >> queries;
while(queries--) {
in >> type >> x;
switch(type) {
case 1: bst.insert(x); break;
case 2: bst.erase(x); break;
case 3: out << (bst.search(x) == 1 ? '1' : '0') << '\n';
}
}
in.close();
out.close();
return 0;
}