Language: C++

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define DEBUGGING

template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
template <typename T, typename U> using P = pair<T, U>;
using ll = int64_t;
using PLL = P<ll, ll>;

template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename Head, typename... Tail> const Head& var_min(const Head &head, const Tail&... tail) { return min(head, var_min(tail...)); }
template <typename Head, typename... Tail> const Head& var_max(const Head &head, const Tail&... tail) { return max(head, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }

namespace __init {

struct InitIO {
    InitIO() {
        cin.tie(0);
        ios_base::sync_with_stdio(false);
        cout << fixed << setprecision(30);
    }
} init_io;

}

#ifdef DEBUGGING
#include "../debug.cpp"
#else
#define DEBUG(...) 0
#endif

template <typename T>
class SegmentTree {
public:
    ll size;
    vector<T> node;
    function<T(T, T)> comp;
    T identity_ele;  //comp(x, identity_ele) == x

    SegmentTree(const vector<T> &v, function<T(T, T)> comp, T identity_ele) 
        : comp(comp),
          identity_ele(identity_ele)
    {
        ll tmp = 1;
        while(tmp < v.size()) tmp = (tmp << 1);
        this->size = tmp;
        this->identity_ele = identity_ele;
        this->comp = comp;
        node.resize(2 * size - 1);
        for(ll i = 0; i < v.size(); i++) {
            node[i + size - 1] = v[i];
        }
        for(ll i = size - 2; 0 <= i; i--) {
            node[i] = comp(node[i * 2 + 1], node[i * 2 + 2]);
        }
    }

    void update(ll pos, T value) {
        pos += size - 1;
        node[pos] = value;
        while(pos > 0) {
            pos = (pos - 1) / 2;
            node[pos] = comp(node[2 * pos + 1], node[2 * pos + 2]);
        }
    }

    T query(ll a, ll b) {
        return sub_query(a, b, 0, 0, size);
    }

    T sub_query(ll a, ll b, ll k, ll l, ll r) {
        if(r <= a || b <= l) {
            return identity_ele;
        } else if(a <= l && r <= b) {
            return node[k];
        } else {
            T left = sub_query(a, b, k * 2 + 1, l, (l + r) / 2);
            T right = sub_query(a, b, k * 2 + 2, (l + r) / 2, r);
            return comp(left, right);
        }
    }
};

const ll INF = 5e15;
PLL initv = PLL(INF, INF);
using STP = SegmentTree<PLL>;

struct Node {
    PLL val;
    V<ll> children_idx;
};

V<Node> nodes;

ll calc(ll l, ll r, STP &ost, STP &est) {
    auto &fst = (l & 1 ? ost : est);
    auto &bst = (l & 1 ? est : ost);
    ll valf, idxf;
    ll valb, idxb;
    tie(valf, idxf) = fst.query(l, r);
    tie(valb, idxb) = bst.query(idxf, r);
    fst.update(idxf, initv);
    est.update(idxb, initv);
    V<ll> recs;
    if(idxf - l > 1)          recs.push_back(calc(l, idxf, ost, est)); 
    if(idxb - (idxf + 1) > 1) recs.push_back(calc(idxf + 1, idxb, ost, est));
    if(r - (idxb + 1) > 1)    recs.push_back(calc(idxb + 1, r, ost, est));
    sort(ALL(recs), [](const ll a, ll b) { return nodes[a].val.first < nodes[b].val.first; });
    Node n{ PLL(valf, valb), recs };
    nodes.push_back(n);
    return nodes.size() - 1;
}

void bfs(ll idx, V<ll> &ans) {
    queue<ll> q;
    q.push(idx);
    while(q.size()) {
        ll idx = q.front();
        q.pop();
        const Node &n = nodes[idx];
        ll a, b;
        tie(a, b) = n.val;
        ans.push_back(a);
        ans.push_back(b);
        for(ll nidx : n.children_idx) q.push(nidx);
    }
}

int main() {
    ll N;
    cin >> N;
    V<PLL> ov, ev;
    for(ll i = 0; i < N; i++) {
        ll p;
        cin >> p;
        ov.emplace_back((i & 1 ? p : INF), i);
        ev.emplace_back((i & 1 ? INF : p), i);
    }
    STP ost(ov, [](PLL a, PLL b) { return min(a, b); }, initv);
    STP est(ev, [](PLL a, PLL b) { return min(a, b); }, initv);
    ll idx = calc(0, N, ost, est);
    V<ll> ans;
    bfs(idx, ans);
    for(ll e : ans) cout << e << ' ';
    cout << endl;
    return 0;
}


Compiler output:
a.cc:34:10: fatal error: ../debug.cpp: No such file or directory
   34 | #include "../debug.cpp"
      |          ^~~~~~~~~~~~~~
compilation terminated.