Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <fstream>
- #include <string>
- #include <queue>
- using namespace std;
- typedef long long ll;
- struct info {
- int l, r, p;
- };
- struct Node {
- ll x, y, number;
- Node *left, *right;
- Node(ll x, ll y, ll number)
- {
- this->x = x;
- this->number = number;
- this->y = y;
- left = right = NULL;
- }
- };
- typedef Node* pNode;
- pNode merge(pNode Left, pNode Right)
- {
- if (Left == NULL)
- return Right;
- if (Right == NULL)
- return Left;
- if (Left->y > Right->y)
- {
- Left->right = merge(Left->right, Right);
- return Left;
- }
- else
- {
- Right->left = merge(Left, Right->left);
- return Right;
- }
- }
- pair<pNode, pNode> split(pNode Tree, ll x)
- {
- if (Tree == NULL)
- return {NULL, NULL};
- if (Tree->x > x)
- {
- auto pair_tree = split(Tree->left, x);
- Tree->left = pair_tree.second;
- return {pair_tree.first, Tree};
- }
- else
- {
- auto pair_tree = split(Tree->right, x);
- Tree->right = pair_tree.first;
- return {Tree, pair_tree.second};
- }
- }
- pNode insert(pNode Tree, pNode vertex)
- {
- if (Tree == NULL)
- {
- return vertex;
- }
- auto pair_tree = split(Tree, vertex->x);
- Tree = merge(pair_tree.first, vertex);
- Tree = merge(Tree, pair_tree.second);
- return Tree;
- }
- pNode remove(pNode Tree, ll x)
- {
- auto del1 = split(Tree, x - 1);
- auto del2 = split(del1.second, x);
- return merge(del1.first, del2.second);
- }
- bool check(pNode Tree, ll x)
- {
- if (Tree == NULL)
- return false;
- if (Tree->x == x)
- return true;
- if (x < Tree->x)
- return check(Tree->left, x);
- if (x > Tree->x)
- return check(Tree->right, x);
- }
- void print(pNode Tree, int parent, info *infos) {
- info local;
- if (!Tree)
- return;
- local.p = parent;
- if (Tree->left) {
- local.l = Tree->left->number;
- }
- else {
- local.l = 0;
- }
- if (Tree->right) {
- local.r = Tree->right->number;
- }
- else {
- local.r = 0;
- }
- infos[Tree->number - 1] = local;
- if (Tree->left)
- print(Tree->left, Tree->number, infos);
- if (Tree->right)
- print(Tree->right, Tree->number, infos);
- }
- bool compare(pair<pair<ll,ll>, ll> first, pair<pair<ll,ll>, ll> second) {
- if (first.first.first <= second.first.first) {
- return true;
- } else {
- return false;
- }
- }
- int main()
- {
- string question;
- ll a, b;
- int cnt_el = 0;
- int n;
- cin >> n;
- info* infos = new info[n];
- vector<pair<pair<ll, ll>, ll>> in;
- for (int i = 0; i < n; i++)
- {
- cin >> a >> b;
- in.push_back(make_pair(make_pair(a, -b), i+1));
- }
- sort(in.begin(), in.end(), compare);
- queue<pNode> tree;
- for (auto e : in) {
- cout << e.first.first << ' ' << e.first.second << endl;
- pNode vertex = new Node(e.first.first, e.first.second, e.second);
- tree.push(vertex);
- }
- while (tree.size() > 1) {
- pNode vertex1 = tree.front();
- tree.pop();
- pNode vertex2 = tree.front();
- tree.pop();
- tree.push(merge(vertex1, vertex2));
- }
- print(tree.front(), 0, infos);
- cout << "YES" << endl;
- for (int i = 0; i < n; i++) {
- printf("%d %d %d\n", infos[i].p, infos[i].l, infos[i].r);
- }
- //system("pause");
- }
- /*bool in_tree = check(tree, a);
- if (in_tree) {
- cout << "NO";
- return 0;
- }*/
- //pNode vertex = new Node(a, -b, i + 1);
- //tree = insert(tree, vertex);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement