p155.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream &operator>>(istream &in, vector<T> &a) {
    for(auto &x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream &operator<<(ostream &out, const vector<T> &a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n;
vector<pair<int, int>> a;

void read() {
    cin >> n;
    a.resize(n);
    cin >> a;
}

void solve() {
    vector<int> order(n);
    iota(order.begin(), order.end(), 0);
    sort(order.begin(), order.end(), [&](int i, int j) {
        return a[i].first < a[j].first;
    });

    vector<int> par(n, -1);
    vector<int> l(n, -1), r(n, -1);
    vector<int> st;

    function<int(int)> collapse = [&](int last) {
        int prev_top = st.back();
        st.pop_back();

        if(last != -1) {
            par[last] = prev_top;
            r[prev_top] = last;
        }

        return prev_top;
    };

    for(int i: order) {
        int last = -1;
        while(!st.empty() && a[st.back()].second > a[i].second) {
            last = collapse(last);
        }

        if(last != -1) {
            par[last] = i;
            l[i] = last;
        }

        st.push_back(i);
    }

    cout << "YES\n";

    int last = -1;
    while(!st.empty()) {
        last = collapse(last);
    }

    for(int i = 0; i < n; i++) {
        cout << par[i] + 1 << ' ' << l[i] + 1 << ' ' << r[i] + 1 << '\n';
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
statement.txt
======================
155. Cartesian Tree
time limit per test: 0.25 sec.
memory limit per test: 65536 KB
input: standard input
output: standard output



Let us consider a special type of binary search trees, called cartesian trees. Recall that a binary searchtree is a rooted ordered binary tree, such that for its every node x the following condition is satisfied: each node in its left subtree has the key less than the key of x, and each node in its right subtree has the key greater than the key of x.
That is, if we denote the left subtree of the node x by L(x), its right subtree by R(x) and its key by kx, for each node x we will have
  * if y in L(x) then ky < kx
  * if z in R(x) then kz > kx
The binary search tree is called cartesian if its every node x in addition to the main key kx also has an auxiliary key that we will denote by ax, and for these keys the heap condition is satisfied, that is
  * if y is the parent of x then ay < ax
Thus a cartesian tree is a binary rooted ordered tree, such that each of its nodes has a pair of two keys (k, a) and three conditions described are satisfied.
Given a set of pairs, construct a cartesian tree out of them, or detect that it is not possible.

Input
The first line of the input file contains an integer number N - the number of pairs you should build cartesian tree out of (1 <= N <= 50000). The following N lines contain two integer numbers each - given pairs (ki, ai). For each pair |ki|, |ai| <= 30000. All main keys and all auxiliary keys are different, i.e. ki <> kj and ai <> aj for each i <> j.

Output
On the first line of the output file print YES if it is possible to build a cartesian tree out of given pairs or NO if it is not. If the answer is positive, output the tree itself in the following N lines. Let the nodes be numbered from 1 to N corresponding to pairs they contain as these pairs are given in the input file. For each node output three numbers - its parent, its left child and its right child. If the node has no parent or no corresponding child, output 0 instead.
If there are several possible trees, output any one.

Sample test(s)

Input
7
5 4
2 2
3 9
0 5
1 3
6 6
4 11

Output
YES
2 3 6
0 5 1
1 0 7
5 0 0
2 4 0
1 0 0
3 0 0

Note
Author:	Andrew Stankevich
Resource:	ACM ICPC 2002-2003 NEERC, Northern Subregion
Date:	November, 2002






=================
