Language: C++

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <unordered_map>
using namespace std;

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }





typedef int VAL;
const VAL INF = 1<<29;
const int MAX_R = 510000;

int SIZE_R;
struct RMQ {
    pair<VAL,int> dat[4*MAX_R];
    
    void init(int n = MAX_R, VAL v = INF) {
        SIZE_R = 1;
        while (SIZE_R < n) SIZE_R *= 2;
        for (int i = 0; i < 2*MAX_R-1; ++i) dat[i] = make_pair(v, -1);
    }
    
    inline void init_set(int a, VAL x) {
        int k = a + SIZE_R-1;
        dat[k] = make_pair(x, a);
    }
    
    inline void init_tree(int k = 0, int l = 0, int r = SIZE_R) {
        if (r - l == 1) {}                  // leaf
        else {
            init_tree(k*2+1, l, (l+r)/2);
            init_tree(k*2+2, (l+r)/2, r);
            dat[k] = min(dat[k*2+1], dat[k*2+2]);
        }
    }
    
    inline void set(int a, VAL x) {
        int k = a + SIZE_R-1;
        dat[k] = make_pair(x, a);
        while (k > 0) {
            k = (k-1)/2;
            dat[k] = min(dat[k*2+1], dat[k*2+2]);
        }
    }
    
    inline pair<VAL,int> get(int a, int b, int k = 0, int l = 0, int r = SIZE_R) {
        if (r <= a || b <= l) return make_pair(INF, -1);
        if (a <= l && r <= b) return dat[k];
        else {
            pair<VAL,int> vl = get(a, b, k*2+1, l, (l+r)/2);
            pair<VAL,int> vr = get(a, b, k*2+2, (l+r)/2, r);
            return min(vl, vr);
        }
    }
    
    inline pair<VAL,int> operator [] (int a) {
        return get(a, a+1);
    }
    
    void print(int r = SIZE_R) {
        for (int i = 0; i < SIZE_R; ++i) {cout << get(i, i+1) << ",";}
        cout << endl;
    }
};

RMQ rmq[2];



int N;
int p[210000];

typedef pair<int,int> pint;

map<pint, vector<pint> > ma;


void solve(int left, int right, pint par) {
    if (left >= right) return;
    
    pair<VAL,int> pa = rmq[left%2].get(left, right - 1);
    pair<VAL,int> pb = rmq[(left+1)%2].get(left + 1, right);
    
//    cout << "----------------" << endl;
//    COUT(make_pair(left, right));
//    COUT(pa);
//    COUT(pb);

    pair<int,int> add = make_pair(pa.first, pb.first);
    ma[par].push_back(add);
    
    solve(pa.second + 1, pb.second, add);
    solve(left, pa.second, add);
    solve(pb.second + 1, right, add);
}


int main() {
    while (cin >> N) {
        for (int i = 0; i < N; ++i) cin >> p[i];
        
        rmq[0].init(N+1);
        rmq[1].init(N+1);
        
        for (int i = 0; i < N; ++i) {
            rmq[i%2].init_set(i, p[i]);
        }
        rmq[0].init_tree();
        rmq[1].init_tree();
        
        ma.clear();
        solve(0, N, pint(-1,-1));

        priority_queue<pint, vector<pint>, greater<pint> > que;
        que.push( pint(-1,-1) );
        
        vector<pint> res;
        while (!que.empty()) {
            pint cur = que.top();
            if (cur != pint(-1,-1)) res.push_back(cur);
            que.pop();
            for (int i = 0; i < ma[cur].size(); ++i) {
                que.push(ma[cur][i]);
            }
        }
        
        for (int i = 0; i < res.size(); ++i) {
            cout << res[i].first << " " << res[i].second;
            if (i != res.size() - 1) cout << " ";
        }
        cout << endl;
    }
}

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <unordered_map>
using namespace std;

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }





typedef int VAL;
const VAL INF = 1<<29;
const int MAX_R = 510000;

int SIZE_R;
struct RMQ {
    pair<VAL,int> dat[4*MAX_R];
    
    void init(int n = MAX_R, VAL v = INF) {
        SIZE_R = 1;
        while (SIZE_R < n) SIZE_R *= 2;
        for (int i = 0; i < 2*MAX_R-1; ++i) dat[i] = make_pair(v, -1);
    }
    
    inline void init_set(int a, VAL x) {
        int k = a + SIZE_R-1;
        dat[k] = make_pair(x, a);
    }
    
    inline void init_tree(int k = 0, int l = 0, int r = SIZE_R) {
        if (r - l == 1) {}                  // leaf
        else {
            init_tree(k*2+1, l, (l+r)/2);
            init_tree(k*2+2, (l+r)/2, r);
            dat[k] = min(dat[k*2+1], dat[k*2+2]);
        }
    }
    
    inline void set(int a, VAL x) {
        int k = a + SIZE_R-1;
        dat[k] = make_pair(x, a);
        while (k > 0) {
            k = (k-1)/2;
            dat[k] = min(dat[k*2+1], dat[k*2+2]);
        }
    }
    
    inline pair<VAL,int> get(int a, int b, int k = 0, int l = 0, int r = SIZE_R) {
        if (r <= a || b <= l) return make_pair(INF, -1);
        if (a <= l && r <= b) return dat[k];
        else {
            pair<VAL,int> vl = get(a, b, k*2+1, l, (l+r)/2);
            pair<VAL,int> vr = get(a, b, k*2+2, (l+r)/2, r);
            return min(vl, vr);
        }
    }
    
    inline pair<VAL,int> operator [] (int a) {
        return get(a, a+1);
    }
    
    void print(int r = SIZE_R) {
        for (int i = 0; i < SIZE_R; ++i) {cout << get(i, i+1) << ",";}
        cout << endl;
    }
};

RMQ rmq[2];



int N;
int p[210000];

typedef pair<int,int> pint;

map<pint, vector<pint> > ma;


void solve(int left, int right, pint par) {
    if (left >= right) return;
    
    pair<VAL,int> pa = rmq[left%2].get(left, right - 1);
    pair<VAL,int> pb = rmq[(left+1)%2].get(left + 1, right);
    
//    cout << "----------------" << endl;
//    COUT(make_pair(left, right));
//    COUT(pa);
//    COUT(pb);

    pair<int,int> add = make_pair(pa.first, pb.first);
    ma[par].push_back(add);
    
    solve(pa.second + 1, pb.second, add);
    solve(left, pa.second, add);
    solve(pb.second + 1, right, add);
}


int main() {
    while (cin >> N) {
        for (int i = 0; i < N; ++i) cin >> p[i];
        
        rmq[0].init(N+1);
        rmq[1].init(N+1);
        
        for (int i = 0; i < N; ++i) {
            rmq[i%2].init_set(i, p[i]);
        }
        rmq[0].init_tree();
        rmq[1].init_tree();
        
        ma.clear();
        solve(0, N, pint(-1,-1));

        priority_queue<pint, vector<pint>, greater<pint> > que;
        que.push( pint(-1,-1) );
        
        vector<pint> res;
        while (!que.empty()) {
            pint cur = que.top();
            if (cur != pint(-1,-1)) res.push_back(cur);
            que.pop();
            for (int i = 0; i < ma[cur].size(); ++i) {
                que.push(ma[cur][i]);
            }
        }
        
        for (int i = 0; i < res.size(); ++i) {
            cout << res[i].first << " " << res[i].second;
            if (i != res.size() - 1) cout << " ";
        }
        cout << endl;
    }
}

























Compiler output:
a.cc:194:31: error: redefinition of 'template<class T> bool chmax(T&, T)'
  194 | template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
      |                               ^~~~~
a.cc:27:31: note: 'template<class T> bool chmax(T&, T)' previously declared here
   27 | template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
      |                               ^~~~~
a.cc:195:31: error: redefinition of 'template<class T> bool chmin(T&, T)'
  195 | template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
      |                               ^~~~~
a.cc:28:31: note: 'template<class T> bool chmin(T&, T)' previously declared here
   28 | template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
      |                               ^~~~~
a.cc:196:39: error: redefinition of 'template<class T1, class T2> std::ostream& operator<<(std::ostream&, std::pair<_T1, _T2>)'
  196 | template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
      |                                       ^~~~~~~~
a.cc:29:39: note: 'template<class T1, class T2> std::ostream& operator<<(std::ostream&, std::pair<_T1, _T2>)' previously declared here
   29 | template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
      |                                       ^~~~~~~~
a.cc:198:28: error: redefinition of 'template<class T> std::ostream& operator<<(std::ostream&, std::vector<_Tp>)'
  198 | template<class T> ostream& operator << (ostream &s, vector<T> P)
      |                            ^~~~~~~~
a.cc:31:28: note: 'template<class T> std::ostream& operator<<(std::ostream&, std::vector<_Tp>)' previously declared here
   31 | template<class T> ostream& operator << (ostream &s, vector<T> P)
      |                            ^~~~~~~~
a.cc:200:28: error: redefinition of 'template<class T> std::ostream& operator<<(std::ostream&, std::vector<std::vector<_Tp> >)'
  200 | template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
      |                            ^~~~~~~~
a.cc:33:28: note: 'template<class T> std::ostream& operator<<(std::ostream&, std::vector<std::vector<_Tp> >)' previously declared here
   33 | template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
      |                            ^~~~~~~~
a.cc:202:39: error: redefinition of 'template<class T1, class T2> std::ostream& operator<<(std::ostream&, std::map<T1, T2>)'
  202 | template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
      |                                       ^~~~~~~~
a.cc:35:39: note: 'template<class T1, class T2> std::ostream& operator<<(std::ostream&, std::map<T1, T2>)' previously declared here
   35 | template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
      |                                       ^~~~~~~~
a.cc:210:11: error: redefinition of 'const VAL INF'
  210 | const VAL INF = 1<<29;
      |           ^~~
a.cc:43:11: note: 'const VAL INF' previously defined here
   43 | const VAL INF = 1<<29;
      |           ^~~
a.cc:211:11: error: redefinition of 'const int MAX_R'
  211 | const int MAX_R = 510000;
      |           ^~~~~
a.cc:44:11: note: 'const int MAX_R' previously defined here
   44 | const int MAX_R = 510000;
      |           ^~~~~
a.cc:213:5: error: redefinition of 'int SIZE_R'
  213 | int SIZE_R;
      |     ^~~~~~
a.cc:46:5: note: 'int SIZE_R' previously declared here
   46 | int SIZE_R;
      |     ^~~~~~
a.cc:214:8: error: redefinition of 'struct RMQ'
  214 | struct RMQ {
      |        ^~~
a.cc:47:8: note: previous definition of 'struct RMQ'
   47 | struct RMQ {
      |        ^~~
a.cc:266:5: error: redefinition of 'RMQ rmq [2]'
  266 | RMQ rmq[2];
      |     ^~~
a.cc:99:5: note: 'RMQ rmq [2]' previously defined here
   99 | RMQ rmq[2];
      |     ^~~
a.cc:270:5: error: redefinition of 'int N'
  270 | int N;
      |     ^
a.cc:103:5: note: 'int N' previously declared here
  103 | int N;
      |     ^
a.cc:271:5: error: redefinition of 'int p [210000]'
  271 | int p[210000];
      |     ^
a.cc:104:5: note: 'int p [210000]' previously declared here
  104 | int p[210000];
      |     ^
a.cc:275:26: error: redefinition of 'std::map<std::pair<int, int>, std::vector<std::pair<int, int> > > ma'
  275 | map<pint, vector<pint> > ma;
      |                          ^~
a.cc:108:26: note: 'std::map<std::pair<int, int>, std::vector<std::pair<int, int> > > ma' previously declared here
  108 | map<pint, vector<pint> > ma;
      |                          ^~
a.cc:278:6: error: redefinition of 'void solve(int, int, pint)'
  278 | void solve(int left, int right, pint par) {
      |      ^~~~~
a.cc:111:6: note: 'void solve(int, int, pint)' previously defined here
  111 | void solve(int left, int right, pint par) {
      |      ^~~~~
a.cc:298:5: error: redefinition of 'int main()'
  298 | int main() {
      |     ^~~~
a.cc:131:5: note: 'int main()' previously defined here
  131 | int main() {
      |     ^~~~