Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct treap {
- struct node {
- node* left;
- node* right;
- int x;
- int y;
- node(int _x, int _y) {
- left = nullptr;
- right = nullptr;
- x = _x;
- y = _y;
- }
- };
- using node_ptr = node*;
- node_ptr root;
- treap() {
- root = nullptr;
- }
- node_ptr merge(node_ptr t1, node_ptr t2) {
- if(t1 == nullptr) return t2;
- if(t2 == nullptr) return t1;
- if(t1->y < t2->y) {
- t1->right = merge(t1->right, t2);
- return t1;
- } else {
- t2->left = merge(t1, t2->left);
- return t2;
- }
- }
- void split(node_ptr t, int x0, node_ptr & t1, node_ptr & t2) {
- if(t == nullptr) {
- t1 = nullptr;
- t2 = nullptr;
- return;
- }
- if(x0 > t->x) {
- split(t->right, x0, t->right, t2);
- t1 = t;
- } else {
- split(t->left, x0, t1, t->left);
- t2 = t;
- }
- }
- void insert(int _x, int _y) {
- node_ptr l = nullptr;
- node_ptr r = nullptr;
- node_ptr m = new node(_x, _y);
- split(root, _x, l, r);
- root = merge(merge(l, m), r);
- }
- void print() {
- print(root, 0);
- }
- void print(node_ptr t, int h) {
- if(t == nullptr) return;
- print(t->left, h + 1);
- for(int i = 0; i < h; i++) cout << "\t\t";
- cout << "(" << t->x << ", " << t->y << ")" << endl;
- print(t->right, h + 1);
- }
- };
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(0);
- treap t;
- int q;
- cin >> q;
- while(q--) {
- int x, y;
- cin >> x >> y;
- t.insert(x, y);
- }
- t.print();
- cout << "End of program..." << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement