Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <algorithm>
- #include <iostream>
- #include <vector>
- class Treap {
- private:
- struct Node {
- int value = 0;
- int lefts = 0;
- int rights = 0;
- int min = 0;
- int prior = rand() % 150000;
- int size = 1;
- Node* left = nullptr;
- Node* right = nullptr;
- Node(int val) { value = val; }
- };
- typedef Node* Pnode;
- Pnode root_ = nullptr;
- void Merge(Pnode first, Pnode second);
- void MergeHelper(Pnode& current, Pnode t1, Pnode t2);
- std::pair<Pnode, Pnode> Split(size_t key);
- void SplitHelper(Pnode current, size_t key, Pnode& left, Pnode& right);
- void Update(Pnode node);
- static int Min(int val1, int val2, int val3) {
- std::vector<int> temp = {val1, val2, val3};
- std::sort(temp.begin(), temp.end());
- return temp[0];
- }
- void PrintHelper(Pnode current) {
- if (current == nullptr) {
- return;
- }
- // i++;
- // int temp = i;
- // i++;
- PrintHelper(current->left);
- std::cout << current->value << " ";
- PrintHelper(current->right);
- // std::cout << current->value << " ";
- }
- Treap(Pnode node) { root_ = node; }
- void ClearHelper(Pnode node) {
- if (node == nullptr) {
- return;
- }
- ClearHelper(node->left);
- ClearHelper(node->right);
- delete node;
- }
- public:
- void Print() {
- // int i = 0;
- PrintHelper(root_);
- std::cout << "\n";
- }
- Treap() { root_ = nullptr; };
- void Insert(size_t pos, int value);
- // size_t Size() const;
- size_t& Size();
- int GetMin(size_t left, size_t right);
- void Clear();
- };
- void Treap::Clear() { ClearHelper(root_); }
- std::pair<Treap::Pnode, Treap::Pnode> Treap::Split(size_t key) {
- Pnode left = nullptr;
- Pnode right = nullptr;
- SplitHelper(root_, key, left, right);
- return {left, right};
- }
- void Treap::SplitHelper(Pnode current, size_t key, Pnode& left, Pnode& right) {
- if (current == nullptr) {
- left = nullptr;
- right = nullptr;
- return;
- }
- size_t curr_key = current->lefts;
- if (key <= curr_key) {
- SplitHelper(current->left, key, left, current->left);
- right = current;
- // Update(current);
- } else {
- SplitHelper(current->right, key - curr_key - 1, current->right, right);
- left = current;
- }
- Update(current);
- }
- void Treap::Update(Pnode node) {
- node->lefts = (node->left != nullptr) ? node->left->size : 0;
- node->rights = (node->right != nullptr) ? node->right->size : 0;
- int min = node->value;
- if (node->left != nullptr && node->right != nullptr) {
- min = Min(node->left->min, node->right->min, node->value);
- } else if (node->left != nullptr) {
- min = std::min(node->left->min, node->value);
- } else if (node->right != nullptr) {
- min = std::min(node->right->min, node->value);
- }
- node->min = min;
- node->size = 1 + node->lefts + node->rights;
- }
- void Treap::MergeHelper(Pnode& current, Pnode t1, Pnode t2) {
- if (t1 == nullptr || t2 == nullptr) {
- current = t1 ? t1 : t2;
- } else if (t1->prior > t2->prior) {
- MergeHelper(t1->right, t1->right, t2);
- current = t1;
- } else {
- MergeHelper(t2->left, t1, t2->left);
- current = t2;
- }
- Update(current);
- }
- void Treap::Merge(Pnode first, Pnode other) {
- MergeHelper(root_, first, other);
- }
- void Treap::Insert(size_t pos, int value) {
- std::pair<Pnode, Pnode> res = Split(pos);
- Pnode m = new Node(value);
- Treap temp;
- temp.Merge(res.first, m);
- this->Merge(temp.root_, res.second);
- }
- int Treap::GetMin(size_t left, size_t right) {
- int result = 0;
- Treap l_tree;
- Treap r_tree;
- Treap l2_tree;
- Treap r2_tree;
- Pnode copy_root = root_;
- Treap copy(copy_root);
- std::pair<Pnode, Pnode> res = copy.Split(left);
- r_tree.root_ = res.second;
- l_tree.root_ = res.first;
- std::pair<Pnode, Pnode> res2 = r_tree.Split(right - left);
- result = res2.first->min;
- r_tree.Merge(res2.first, res2.second);
- this->Merge(l_tree.root_, r_tree.root_);
- return result;
- }
- /*public double MaxCostOn(int A, int B)
- {
- ImplicitTreap l, m, r;
- this.Split(A, out l, out r);
- r.Split(B - A, out m, out r);
- return CostOf(m);
- }*/
- void Requests() {
- size_t n = 0;
- std::cin >> n;
- Treap treap;
- char request = 0;
- for (size_t i = 0; i < n; ++i) {
- std::cin >> request;
- if (request == '+') {
- int value = 0, pos = 0;
- std::cin >> pos;
- std::cin >> value;
- treap.Insert(pos, value);
- // treap.Print();
- } else {
- int left = 0, right = 0;
- std::cin >> left >> right;
- std::cout << treap.GetMin(left - 1, right) << "\n";
- }
- }
- treap.Clear();
- // std::cout << "---\n";
- // treap.Print();
- }
- int main() {
- Requests();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement